From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 938D5C4363D for ; Tue, 22 Sep 2020 15:56:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EC6F02084C for ; Tue, 22 Sep 2020 15:56:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="l7hoQh1e" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EC6F02084C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6A5F0900043; Tue, 22 Sep 2020 11:56:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6551890000A; Tue, 22 Sep 2020 11:56:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 54409900043; Tue, 22 Sep 2020 11:56:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0161.hostedemail.com [216.40.44.161]) by kanga.kvack.org (Postfix) with ESMTP id 3AA3F90000A for ; Tue, 22 Sep 2020 11:56:14 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id E291C1EF1 for ; Tue, 22 Sep 2020 15:56:13 +0000 (UTC) X-FDA: 77291149026.24.mist09_4c0676a2714f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin24.hostedemail.com (Postfix) with ESMTP id B7CA81A4A0 for ; Tue, 22 Sep 2020 15:56:13 +0000 (UTC) X-HE-Tag: mist09_4c0676a2714f X-Filterd-Recvd-Size: 7177 Received: from mail-qk1-f195.google.com (mail-qk1-f195.google.com [209.85.222.195]) by imf34.hostedemail.com (Postfix) with ESMTP for ; Tue, 22 Sep 2020 15:56:13 +0000 (UTC) Received: by mail-qk1-f195.google.com with SMTP id t138so19542208qka.0 for ; Tue, 22 Sep 2020 08:56:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=IYRTnlo90GNwIVVFhycLDwsrTtG0682e9ZAFQMaySN4=; b=l7hoQh1ejgd7aceitowuvAcgWnfb6V3IVadFJbkyLd0RsGpVIZrRAmC4qrxxiLU9mR 4VjBXw1UoI/QGOkYjE5oY+xdYlhNSs2jybizOpABLiKM6Wy7cuILDhq6W3lVg1HhneDh cNQksP8240Cm61ESg3I4jkLAXUjzRpb7wmx05Fk1rofGFheST7e2Fio35+trXpK6RdOZ fFPFTXnEYuzAqgdPAKmx+eMDGc0Or6jRk9J+c0FIe4CIRucI+VceaTitnmv58UBTHOrO yoAgQnha7oLMAKNQ7vtfmn6xsIejFx6kw4yTRqISvj1iOjfuBLwgtS31BiM74C4BP5Rw Ni8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=IYRTnlo90GNwIVVFhycLDwsrTtG0682e9ZAFQMaySN4=; b=dYo0w2mX2PVBxIBzrQyERx8XBLlYSaD/5Tb/UGCfP8za+98RYHq5HaVHXoYxB91rct FfstQalhQjX+p9Y20OemMQhri3dcqs/vPX/9ZPRtMKhGTfWBYB71+M3Grn2N2pr49/mX yNf4s9l1U252ohHDcmM67wl0094SEdJ7ANjnXyOPJ7LM1Olxw3mEC6YJBIL1IzV/zSWC /5/xiZ4fG1E1UIJ4iQRwhRxPeJD+zMNdEhoinwD94to+G41qXIzeIRyBYpg+b2P34Fuh 3dT9nL4Ie3KZ4261kq3sPL0J5jXUHIBhU8c3k/8rJqvWH20FjrLVTCQrMZMM8bbR/O0W /rLw== X-Gm-Message-State: AOAM532eXY0kklCrKnTbOX5jlxhoHqbUkoeDilZaOVXVCsrPqsv7vGo1 D8W40ONU6rm07LbAN6gZZ1EyBg== X-Google-Smtp-Source: ABdhPJxiNGLGozHFwhcJgJgUPSubcn3CK125SBHmwyhXN/SmG+epVQJjoF+wjAJP3DRk6zkVVubwOw== X-Received: by 2002:a37:a414:: with SMTP id n20mr5531835qke.332.1600790166390; Tue, 22 Sep 2020 08:56:06 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-156-34-48-30.dhcp-dynamic.fibreop.ns.bellaliant.net. [156.34.48.30]) by smtp.gmail.com with ESMTPSA id 7sm11768068qkc.73.2020.09.22.08.56.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Sep 2020 08:56:05 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1kKkeC-0034WT-Nw; Tue, 22 Sep 2020 12:56:04 -0300 Date: Tue, 22 Sep 2020 12:56:04 -0300 From: Jason Gunthorpe To: Peter Xu Cc: Jann Horn , Linux-MM , kernel list , Andrew Morton , Jan Kara , Michal Hocko , Kirill Tkhai , Kirill Shutemov , Hugh Dickins , Christoph Hellwig , Andrea Arcangeli , John Hubbard , Oleg Nesterov , Leon Romanovsky , Linus Torvalds Subject: Re: [PATCH 1/5] mm: Introduce mm_struct.has_pinned Message-ID: <20200922155604.GA731578@ziepe.ca> References: <20200921211744.24758-1-peterx@redhat.com> <20200921211744.24758-2-peterx@redhat.com> <20200921223004.GB19098@xz-x1> <20200922115436.GG8409@ziepe.ca> <20200922142802.GC19098@xz-x1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200922142802.GC19098@xz-x1> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Sep 22, 2020 at 10:28:02AM -0400, Peter Xu wrote: > On Tue, Sep 22, 2020 at 08:54:36AM -0300, Jason Gunthorpe wrote: > > On Tue, Sep 22, 2020 at 12:47:11AM +0200, Jann Horn wrote: > > > On Tue, Sep 22, 2020 at 12:30 AM Peter Xu wrote: > > > > On Mon, Sep 21, 2020 at 11:43:38PM +0200, Jann Horn wrote: > > > > > On Mon, Sep 21, 2020 at 11:17 PM Peter Xu wrote: > > > > > > (Commit message collected from Jason Gunthorpe) > > > > > > > > > > > > Reduce the chance of false positive from page_maybe_dma_pinned() by keeping > > > > > > track if the mm_struct has ever been used with pin_user_pages(). mm_structs > > > > > > that have never been passed to pin_user_pages() cannot have a positive > > > > > > page_maybe_dma_pinned() by definition. > > > > > > > > > > There are some caveats here, right? E.g. this isn't necessarily true > > > > > for pagecache pages, I think? > > > > > > > > Sorry I didn't follow here. Could you help explain with some details? > > > > > > The commit message says "mm_structs that have never been passed to > > > pin_user_pages() cannot have a positive page_maybe_dma_pinned() by > > > definition"; but that is not true for pages which may also be mapped > > > in a second mm and may have been passed to pin_user_pages() through > > > that second mm (meaning they must be writable over there and not > > > shared with us via CoW). > > > > The message does need a few more words to explain this trick can only > > be used with COW'able pages. > > > > > Process A: > > > > > > fd_a = open("/foo/bar", O_RDWR); > > > mapping_a = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, fd_a, 0); > > > pin_user_pages(mapping_a, 1, ...); > > > > > > Process B: > > > > > > fd_b = open("/foo/bar", O_RDONLY); > > > mapping_b = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd_b, 0); > > > *(volatile char *)mapping_b; > > > > > > At this point, process B has never called pin_user_pages(), but > > > page_maybe_dma_pinned() on the page at mapping_b would return true. > > > > My expectation is the pin_user_pages() should have already broken the > > COW for the MAP_PRIVATE, so process B should not have a > > page_maybe_dma_pinned() > > When process B maps with PROT_READ only (w/o PROT_WRITE) then it seems the same > page will be mapped. I thought MAP_PRIVATE without PROT_WRITE was nonsensical, it only has meaning for writes initiated by the mapping. MAP_SHARED/PROT_READ is the same behavior on Linux, IIRC. But, yes, you certainly can end up with B having page_maybe_dma_pinned() pages in shared VMA, just not in COW'able mappings. > I think I get the point from Jann now. Maybe it's easier I just remove the > whole "mm_structs that have never been passed to pin_user_pages() cannot have a > positive page_maybe_dma_pinned() by definition" sentence if that's misleading, > because the rest seem to be clear enough on what this new field is used for. "for COW" I think is still the important detail here, see for instance my remark on the PUD/PMD splitting where it is necessary to test for cow before using this. Perhaps we should call it "has_pinned_for_cow" to place emphasis on this detail? Due to the shared pages issue It really doesn't have any broader utility, eg for file back pages or otherwise. Jason