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 13381C7EE22 for ; Fri, 28 Apr 2023 16:40:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8ED2C6B007D; Fri, 28 Apr 2023 12:40:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A5AD6B0080; Fri, 28 Apr 2023 12:40:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 73D686B0083; Fri, 28 Apr 2023 12:40:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 627946B007D for ; Fri, 28 Apr 2023 12:40:01 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 356C5160229 for ; Fri, 28 Apr 2023 16:40:01 +0000 (UTC) X-FDA: 80731361802.22.9B70BEE Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf22.hostedemail.com (Postfix) with ESMTP id 9B019C0024 for ; Fri, 28 Apr 2023 16:39:58 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cF876kMR; spf=pass (imf22.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682699999; a=rsa-sha256; cv=none; b=iLORIyk+E46ji5DOu7t7DGfC1FMY1ST/jslC7SZheI4aptpfoFUn7+2mb2MxaJQaS7Cx80 fpjDxRlpDOSppkSWk1dLCMeOuri6Kft/7dThb1sbLuXa+wi7FUA6yNlDxg7Afzwb7AuO8j S5spqw8Igr4JgCl1lWISTmPZtLp1M4c= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cF876kMR; spf=pass (imf22.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682699999; 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=rWkVvKB+beFGKaX1u6z+cXwjtof0A4OJbagxKAP5YOc=; b=TR5lSW41gsLZhG0z5sNC4lR1uAwljWIr2MVnU/ILTri0goE6ZAOrMPeUvyJmDNt2I+mAMc 9hNgYe0lxVV585mQ56p1H5JoYKPRfFtyyaNo1pR1+mdH5wBDfhg5SsSBuamsSX1ftpcJ6n UQnqPOMsYsYGPtGQOuzIgS+hTkg9F98= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682699997; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=rWkVvKB+beFGKaX1u6z+cXwjtof0A4OJbagxKAP5YOc=; b=cF876kMRNBy1bv43KLhO764QHdjyUalp5pxkEl7SR5QjngVwdZHnxcsg0+yBWwI2KnegWk YoYu+eRL7alFyxReyaUoo0CzMGhPuOLIi08q9sJdJQO9/zNnbaWc7LSDUByNbvjGHui8CG qGfQ72t6n8lCfMydpNTCn8jSymcLO+g= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-437-XqhHA1lPO1iTekg6A5xxwg-1; Fri, 28 Apr 2023 12:39:56 -0400 X-MC-Unique: XqhHA1lPO1iTekg6A5xxwg-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-74e0dd4e504so135385a.0 for ; Fri, 28 Apr 2023 09:39:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682699995; x=1685291995; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=rWkVvKB+beFGKaX1u6z+cXwjtof0A4OJbagxKAP5YOc=; b=kOoYrUUZar247j/T7+7n+/yFAU16xyHruck83n7b7V9rfVdPqY4LgFgqCArq81yiCY bONAM+6f4ssf/5530i1VyAoU1fW0X0f0lodZJ7ZpC5mPayKN94YvSMz/TPUmC0zY3DKp HPLC3pgAg5GTGuGo3LaxoK3dAfr8gnRBRri/dv+mX0qaiPkWfbenc8NdYegvjHFMM/WI UIYt3mzTQVBVxy3Ta0mQ2QSejOxRGCUhBsoi5uTSgZc3VONoWR3GKpTNv/gEVWD/MrXR fhQvmZiNSsBqlHfRPNIeMmwxKpmVQY0BhaSDya44VSDKcSgylvAkOSr3DOTjR43Wx0UF +3ig== X-Gm-Message-State: AC+VfDyHapssKQL0X90Ntfe6zTeZfwop5mBzEtu3O9PQtBRAmRhJTFI9 e1F1+PnnDyANXGqIEL9R2St6biTTN/77FHiKd/ELisiWFaRkfMElWP4IPAT/dZD6Usz83ShF6zd KsLLU6q4KHOY= X-Received: by 2002:a05:6214:3002:b0:603:fa46:d368 with SMTP id ke2-20020a056214300200b00603fa46d368mr8548772qvb.1.1682699995617; Fri, 28 Apr 2023 09:39:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5JPS7VyAu8Sw9pswc/4Y0KvJbwIs0sXe5VJVf724QrbDRJaDnc8X56jBH6cDgE4mKlziivDA== X-Received: by 2002:a05:6214:3002:b0:603:fa46:d368 with SMTP id ke2-20020a056214300200b00603fa46d368mr8548725qvb.1.1682699995096; Fri, 28 Apr 2023 09:39:55 -0700 (PDT) Received: from x1n (bras-base-aurron9127w-grc-40-70-52-229-124.dsl.bell.ca. [70.52.229.124]) by smtp.gmail.com with ESMTPSA id a26-20020a0c8bda000000b005dd8b9345dbsm440012qvc.115.2023.04.28.09.39.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 09:39:54 -0700 (PDT) Date: Fri, 28 Apr 2023 12:39:51 -0400 From: Peter Xu To: "Kirill A . Shutemov" Cc: David Hildenbrand , 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: 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> MIME-Version: 1.0 In-Reply-To: <20230428162207.o3ejmcz7rzezpt6n@box.shutemov.name> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspam-User: X-Rspamd-Queue-Id: 9B019C0024 X-Rspamd-Server: rspam01 X-Stat-Signature: 81zxsoxbzngxf3u6wkjiuw7q1jzfpaz8 X-HE-Tag: 1682699998-648375 X-HE-Meta: U2FsdGVkX1/p0YhbT670D0qJLhi4nmbAtdeTBHttHDfLaxyo2/OwAa7yu4A0M1XCggLAkQs916u3jqyAjda38FsBJl2Yr4zcZ/YgbOxOhybyHifgbLJFMkMTLHPlLbr4TQXsAY4sz9rEPUbrXS1oZFmtltya/G9Qj6Du2OzkSqorG7sp/Y4at2NIbDDFmKG51aYhPzZLz3n2bAs0HMYZlGrFnNiXWbB9Ut/OEr1Oa6jmSmnJ5q2KZzVg/8FCn8NmfLKMduV3Q0az2Fuef594IWMgAGa3sdCu1NX1W5WePK9CRHP4Hkfb07o1Fj+hc5RhR5ZJghjOqjDCAEelrAYOjAznZSSVlVNh72rJzVPysAHrABdbLt6OcBvYN+P7cgcbv57Wcv4RFP38bz7wh0+bhgtEvvduMeniHAl3m8rqv9ApIKyzitYiJozaL2JhjqnB+5pDWTt36dT3EFQkiNQJFviY5H/AmKYA+GMHxtNwKqxr7CCAtcSUcMnbEhjxOlv+3PD2UyBxGKz9+cb7PuMjflYsZeO1ksexMjMKUNI1yAz4c8d3Y+Hp9sByqkU3nMypkDwV12rj73roLzEYetFpqTJS0dCjb3bgRctHyvtApcPtGHZPotV9N52qZUxgB5UUOIz46W/DkY3stPo+iyvqQGAxyaewcloHN7AZ4tTgKqR9nylQBn499Ob3AG/+72YsMzVYJj8TpQEwYmNTkWCh7lVM5lga40xxDqFmN+7qQoUT/z6eipUR2rZX0U+trr2MiQJQwTPzwZoTHE36K+pR4PX2lqouUew4J8Qam7ptTabrGi60N2TjUa3kh8AY/+7G0pF7Fkq97OMg7YYdb/PacN8WANUPs8J4QyPs8x1P4tyoD1vUdoocO7FRBesVsFNgQVoIFWA1hl2eanp0UvruRLZdyvaCcfQm3q1Kc3Z6XTIXg54x+fbBUCHMxf9jLbWNBTxBrpbpGRdrh2z0wj1 l29EkX3o AvaW2kyZZGdXsU8yhd5jNkZwyP/KZBHyE3EdFhWlPz8CtRRBJMSYwEAY/ywtzkBmtn4A0RE9jf/VSdQUoAvFQrFywdblGqFZI2CKkNbnx42E7JFYQGZBANGDMvkgoKkgVwuAWIOV8fZzoHTtFrRmbVTH9EbgdZGu50CL2YqJ7WjRYz410IYZdVk1HxuOu7e2Zdbr0B1iYpS0xXq8VOnCFwqybJeL68ryeZw6RxTRcDV28g8XRg589b+cplhZ/ymEKFJvWAApGEmBM8OH7zGF21oZiWHFA1N7cGPFFka+CPA2FPDChsndG61HEy8vMGO/++NXjBYUM4rggXztVywbWwG8icEHdfNY39QWrzkSeKYtDtMy78AsbI5HFRmHtknGlrk8H/0XYUx/CIwAXJf733P/BwS9xrpjED8RVYnRk5T1Wyi+FYcziqR5AmA== 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 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? > > Hm? > > (I didn't look close at GUP for a while and my reasoning might be off.) Thanks, -- Peter Xu