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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E85E8C77B7E for ; Fri, 28 Apr 2023 16:56:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75F946B0071; Fri, 28 Apr 2023 12:56:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 70FEC6B0072; Fri, 28 Apr 2023 12:56:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 588E06B0074; Fri, 28 Apr 2023 12:56:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 473B26B0071 for ; Fri, 28 Apr 2023 12:56:36 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B01281402B5 for ; Fri, 28 Apr 2023 16:56:34 +0000 (UTC) X-FDA: 80731403508.20.106690F Received: from wnew2-smtp.messagingengine.com (wnew2-smtp.messagingengine.com [64.147.123.27]) by imf04.hostedemail.com (Postfix) with ESMTP id 04F304000F for ; Fri, 28 Apr 2023 16:56:31 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=shutemov.name header.s=fm1 header.b="D vfwva+"; dkim=pass header.d=messagingengine.com header.s=fm3 header.b=OFPz5iuR; spf=pass (imf04.hostedemail.com: domain of kirill@shutemov.name designates 64.147.123.27 as permitted sender) smtp.mailfrom=kirill@shutemov.name; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682700992; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=s7h15564+ZYYAViBl9dWz2ps3GNKFkd0Y8UCd24XhUo=; b=DWKqVxdbzCayBsMT0SKMvlLFnBvq5mdhpa5t0ynH8hIwAXP112tP66gzjtHEtv02QpQ72g PctqqN5/iDszg//e2NSYZPAIAAoiZxhvqhDFcStUwg3ezxVPykim02AiRHqci1LS4UCD7H qie70FFeH9LNK9ClWWwxdo54qooUNQ0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682700992; a=rsa-sha256; cv=none; b=q4xjYVs12SMHArYO7fRm12Y+mGvOZ6ft/a+16LsV3KRWiX5uq8DyHLHbW04TfXjY3aBdnp ycrOBseYfO6QwIkWw0nMceiImgWTFG1foOjokY4wlMCZsECjgSnFgfbarUmg+mf0Xi3LSy joPWmltkoOwk8nqwd5wO5itrzOhIBk8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=shutemov.name header.s=fm1 header.b="D vfwva+"; dkim=pass header.d=messagingengine.com header.s=fm3 header.b=OFPz5iuR; spf=pass (imf04.hostedemail.com: domain of kirill@shutemov.name designates 64.147.123.27 as permitted sender) smtp.mailfrom=kirill@shutemov.name; dmarc=none Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailnew.west.internal (Postfix) with ESMTP id 2DD4F2B067C8; Fri, 28 Apr 2023 12:56:27 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Fri, 28 Apr 2023 12:56:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov.name; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1682700986; x= 1682708186; bh=s7h15564+ZYYAViBl9dWz2ps3GNKFkd0Y8UCd24XhUo=; b=D vfwva+PpE2bkqM9nBbsEDMmcm+Ci/oC3+gLb+eoYPBK7VnOUX9/Kmy9k+wUZe1XW G3dcNldrmryO9QVCThFES79uJfO9C+bIpbYrZXaAYdkIM2PmfIE0Mf0mqQAa9lpw rZN2DjLXpP5PN7qI10sAHMTOI3EoqPyeU7S4ZbEXz2PSpkyE1NU3JDXPjjkQ8jbh jx+sJYt0PZsoFcmaWNWeLNJ0u/F/y6GBYzgsgyW1x3MjejhKuhud1t9nvJNY0V06 qIOs9srS4F7Q+AZdC31fpiFPXoyaa0oELF2br+ZenbFfz9UE50sdHNguFuhggzOW X7EdbzakUawiLD0a1NefA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1682700986; x=1682708186; bh=s7h15564+ZYYA ViBl9dWz2ps3GNKFkd0Y8UCd24XhUo=; b=OFPz5iuRb4UIme0+aYtT1+xphj4bH 9dU1iad7/gmHmWNR9NXxQuLBaCtOBZpIeYnmYm+MiUiXoT3otJ9ksrfZIetJQWEs hDia1HDdBOCewAHn55PaJ6UTCgHCUonCb/eUt7p9cziRJnKoZJylGuiF8VNRSHbN DDkl7jd+oS6sSONWun3EzscX0BSISEvNwkTvQIajlQKi8ZBQWZL8pNfGEstnB362 EXgbqaPV6VgjU/JonDgfZLh87CLOOT54JOxXerhJDFjUHfTR/PB7AkQf4u0Wer5N 6X3OxhlzHxCy7iez7H2rPKNsGjXJ42TJHrK12+RBMpyTOciUkxh4TqmJg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfedukedguddtiecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttddttddttddvnecuhfhrohhmpedfmfhi rhhilhhlucetucdrucfuhhhuthgvmhhovhdfuceokhhirhhilhhlsehshhhuthgvmhhovh drnhgrmhgvqeenucggtffrrghtthgvrhhnpefgtdevueegtefhffffkeelgfeklefgkeef tdehieelteeuhfekfeeluddvtdekveenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehkihhrihhllhesshhhuhhtvghmohhvrdhnrghmvg X-ME-Proxy: Feedback-ID: ie3994620:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 28 Apr 2023 12:56:25 -0400 (EDT) Received: by box.shutemov.name (Postfix, from userid 1000) id 8DFEC1041AE; Fri, 28 Apr 2023 19:56:23 +0300 (+03) Date: Fri, 28 Apr 2023 19:56:23 +0300 From: "Kirill A . Shutemov" To: David Hildenbrand Cc: Peter Xu , Lorenzo Stoakes , Jason Gunthorpe , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Jens Axboe , Matthew Wilcox , Dennis Dalessandro , Leon Romanovsky , Christian Benvenuti , Nelson Escobar , Bernard Metzler , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Bjorn Topel , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Christian Brauner , Richard Cochran , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , linux-fsdevel@vger.kernel.org, linux-perf-users@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Oleg Nesterov , John Hubbard , Jan Kara , Pavel Begunkov , Mika Penttila , David Howells , Christoph Hellwig Subject: Re: [PATCH v5] mm/gup: disallow GUP writing to file-backed mappings by default Message-ID: <20230428165623.pqchgi5gtfhxd5b5@box.shutemov.name> References: <094d2074-5b69-5d61-07f7-9f962014fa68@redhat.com> <400da248-a14e-46a4-420a-a3e075291085@redhat.com> <077c4b21-8806-455f-be98-d7052a584259@lucifer.local> <62ec50da-5f73-559c-c4b3-bde4eb215e08@redhat.com> <6ddc7ac4-4091-632a-7b2c-df2005438ec4@redhat.com> <20230428160925.5medjfxkyvmzfyhq@box.shutemov.name> <39cc0f26-8fc2-79dd-2e84-62238d27fd98@redhat.com> <20230428162207.o3ejmcz7rzezpt6n@box.shutemov.name> <173337c0-14f4-3246-15ff-7fbf03861c94@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <173337c0-14f4-3246-15ff-7fbf03861c94@redhat.com> X-Rspam-User: X-Rspamd-Queue-Id: 04F304000F X-Rspamd-Server: rspam09 X-Stat-Signature: 556cdjz4uoz6z5856kqa8b13u7tfkm9q X-HE-Tag: 1682700991-362384 X-HE-Meta: U2FsdGVkX18ln9YGRPK55Lmzu4UmMkLKwIwqgScPVHTlsGs+ZFl68AUr6EcdO3xTVfiUjQRk76n0snArdh8U4JlzC0XDsYUSH+C3GbmRT7TOwE2sfZpzqckP4F8lrUT603OrC6WT4KBW6tZVpr+i/6HuZwO7myagbVvLXIAXBytc2nUZlwBhwxczhG/06dCcmxYhnUvN/2Tg2Sng0U+/A734RMNxuqUV+CxhqGrqwsg/cMw0AazrdFTMnqXw0OM1Q2p1or6Fr5VZcirKrRHQeNMERVMXNXtxljgBi7h3VCPLJtCqKf36rjgmDCqq2yVmjjqFQ7KOZ7iJVu9AlVa7N9Eh5BGIUOKpM8qyNcsLvWNv0ugmRo0ZtwQ/LuK5OylC80Nt8az44ytYBi5mJhMrfSeySnfLTSW4YTqYbTR/M8v0eeeTD4G6M0VfhS7ALcc+L+KUwz4z/zVdQAegVHa6i4q2rECRQCSmyAlqL3Tn5St/HXv9sxUmPDdIcjULBZIMUZ745CvNcnquf1ba4CZeUtp66xQwZANRPIkzSc0qNnaDL2CwgCTwDluY9EzahG3BMGUdpyM8/cPCrvzE4tM2GMT8ipDDugXTULN3J+s2bDYUjsCRrbFB2EcWPNQiTPDbstnxFVHaHIIK8EFaNUtWhIB4SVd8dlurHwBe1fI/BLhjVdxuy5I6NvjR5QaykXza9dJ4VlWEy7eqJRAsyGJfxWetfpx1RVftTHyONQkGGSzUPtybohfRMbeRo2v7hkOPf3xvX3ro6Pps0LVg/DtiRkeBf/HKUvc7/sC83UATCqj9C9jdBVKg82R+3CyH97SL7ubBjBW+pxHoMr8utrfnr8lqzWsq4k9L+ZWXSTh/tqS7hkKKP2o8UvApFOWMAZivQ4DGVnM2GY5V7QkoopQRX1Gjr2PdTaZRAK1GMNEU6eH/lJ1eXvxR/IsxntHRCB/xjkOJ20xCPg6exMg5l6d vONtOFqu meNS/cBUKucYCl1Hgb54mN8PNq4nPE0yK9EL1N9xDSTonhZatXRGu4lMRNALWQgEUxd+jFwS2zrWU+OB+9T13SB4A11mUQ/Q7xxbm0r8h/q9FoHbvoxhoTS5uddyHIJNeTORZDxQ/9IiHRBBFXkut400hjYHfnrd8fxfFaO5QdRivX5bGSGxIMJZwED5iYzVqbp8lnyWVeodAPeRuOM6rfE4YwBzMEH7w3P8yv5TTT/S0ApJxAPzoHsLgZSLUnaMlhvMQaEn1SrgCr8aooQgcikulte/ogsFKGDnb+1ldJ8rfS7vXUH5ulDmU6tk3Iz8JbyY1a7nKrZHtXejqGrxgpGIfBWsHsVm7p1dGfhRO6UCoRUzXeYzszNG+og== 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 Fri, Apr 28, 2023 at 06:51:46PM +0200, David Hildenbrand wrote: > On 28.04.23 18:39, Peter Xu wrote: > > On Fri, Apr 28, 2023 at 07:22:07PM +0300, Kirill A . Shutemov wrote: > > > On Fri, Apr 28, 2023 at 06:13:03PM +0200, David Hildenbrand wrote: > > > > On 28.04.23 18:09, Kirill A . Shutemov wrote: > > > > > On Fri, Apr 28, 2023 at 05:43:52PM +0200, David Hildenbrand wrote: > > > > > > On 28.04.23 17:34, David Hildenbrand wrote: > > > > > > > On 28.04.23 17:33, Lorenzo Stoakes wrote: > > > > > > > > On Fri, Apr 28, 2023 at 05:23:29PM +0200, David Hildenbrand wrote: > > > > > > > > > > > > > > > > > > > > > > Security is the primary case where we have historically closed uAPI > > > > > > > > > > > items. > > > > > > > > > > > > > > > > > > > > As this patch > > > > > > > > > > > > > > > > > > > > 1) Does not tackle GUP-fast > > > > > > > > > > 2) Does not take care of !FOLL_LONGTERM > > > > > > > > > > > > > > > > > > > > I am not convinced by the security argument in regard to this patch. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > If we want to sells this as a security thing, we have to block it > > > > > > > > > > *completely* and then CC stable. > > > > > > > > > > > > > > > > > > Regarding GUP-fast, to fix the issue there as well, I guess we could do > > > > > > > > > something similar as I did in gup_must_unshare(): > > > > > > > > > > > > > > > > > > If we're in GUP-fast (no VMA), and want to pin a !anon page writable, > > > > > > > > > fallback to ordinary GUP. IOW, if we don't know, better be safe. > > > > > > > > > > > > > > > > How do we determine it's non-anon in the first place? The check is on the > > > > > > > > VMA. We could do it by following page tables down to folio and checking > > > > > > > > folio->mapping for PAGE_MAPPING_ANON I suppose? > > > > > > > > > > > > > > PageAnon(page) can be called from GUP-fast after grabbing a reference. > > > > > > > See gup_must_unshare(). > > > > > > > > > > > > IIRC, PageHuge() can also be called from GUP-fast and could special-case > > > > > > hugetlb eventually, as it's table while we hold a (temporary) reference. > > > > > > Shmem might be not so easy ... > > > > > > > > > > page->mapping->a_ops should be enough to whitelist whatever fs you want. > > > > > > > > > > > > > The issue is how to stabilize that from GUP-fast, such that we can safely > > > > dereference the mapping. Any idea? > > > > > > > > At least for anon page I know that page->mapping only gets cleared when > > > > freeing the page, and we don't dereference the mapping but only check a > > > > single flag stored alongside the mapping. Therefore, PageAnon() is fine in > > > > GUP-fast context. > > > > > > What codepath you are worry about that clears ->mapping on pages with > > > non-zero refcount? > > > > > > I can only think of truncate (and punch hole). READ_ONCE(page->mapping) > > > and fail GUP_fast if it is NULL should be fine, no? > > > > > > I guess we should consider if the inode can be freed from under us and the > > > mapping pointer becomes dangling. But I think we should be fine here too: > > > VMA pins inode and VMA cannot go away from under GUP. > > > > Can vma still go away if during a fast-gup? > > > > So, after we grabbed the page and made sure the the PTE didn't change (IOW, > the PTE was stable while we processed it), the page can get unmapped (but > not freed, because we hold a reference) and the VMA can theoretically go > away (and as far as I understand, nothing stops the file from getting > deleted, truncated etc). > > So we might be looking at folio->mapping and the VMA is no longer there. > Maybe even the file is no longer there. No. VMA cannot get away before PTEs are unmapped and TLB is flushed. And TLB flushing is serialized against GUP_fast(). -- Kiryl Shutsemau / Kirill A. Shutemov