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 96C7DC77B61 for ; Fri, 28 Apr 2023 17:01:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2CA496B0072; Fri, 28 Apr 2023 13:01:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 279C36B0074; Fri, 28 Apr 2023 13:01:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1422E6B0075; Fri, 28 Apr 2023 13:01:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 05D5D6B0072 for ; Fri, 28 Apr 2023 13:01:20 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AB75E802E9 for ; Fri, 28 Apr 2023 17:01:19 +0000 (UTC) X-FDA: 80731415478.26.DE73169 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) by imf29.hostedemail.com (Postfix) with ESMTP id 3B696120039 for ; Fri, 28 Apr 2023 17:01:16 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=CAPtf6LH; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.48 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682701277; 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=XGdkvcQJaM4eh+AI3pIM+CO6RBCKjPhApJrUbhg6iVk=; b=Vi7klrs8EhMbqKZP8M+lmRQkDOfDX1TA5lVBcgYVA9tM83wWT5F771v4FnOXNKsO3lLjEP UEqegLX5HlOhzj0BLLuk3YQqhiVBxFSgL4Rj9F2VrUeEdsOe9CLgESAEhWSQg/6ZAdVRVX cSgkpMjsCbn+dJ7eFpf8l2cEzBR2Zr4= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=CAPtf6LH; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.48 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682701277; a=rsa-sha256; cv=none; b=duppZeR9n6Fr0ok1a9Q92cXBJYw3LRBvjS9IBfR/q4tYQ2kpH4SOQJFjOHqDC0PVZOUIox zr7y5Vr6somU+DT+aTm4KZApEf+85gPJdDKkIklYGVYvzUguZKZhqbzJVIYZFi6twmkq69 BAbPugFkXCbwKicmEWDpIHrUZrVMGRk= Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-2f833bda191so6298327f8f.1 for ; Fri, 28 Apr 2023 10:01:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682701275; x=1685293275; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=XGdkvcQJaM4eh+AI3pIM+CO6RBCKjPhApJrUbhg6iVk=; b=CAPtf6LHOXt+40fb4GOSz0wc3X90q+WiiA8ULiPwdqvjfW/Gta4omW/QicTfD6b/NO /ZlEFOmnUX67JGQiq09W0vk5NZBLsy+hzRwJDnRpTiy2vbx6JFYZgW5ukoCdHMLyyrcq xtkrZDsocxLaYoTevCr7KXy0C5cr460r4jnOnaN/AYjQSyQvyRVN74TaV1ixYPb9Uw/d rp0LRHZXHgGuPnxhSvbUnTxjmU/u4ZVfnM+V0OLgnW+48mXyuEhU0/OTLtNvqd5M0/va zH3/A5pfLXNcIpZJckkYL9u5784g/f06cRaU3foOOszbVgGrCrlNVOQh/GYJxBYtJgjH kzTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682701275; x=1685293275; 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=XGdkvcQJaM4eh+AI3pIM+CO6RBCKjPhApJrUbhg6iVk=; b=kAfkMkCOBoax0/aprNHPBr7BizgyKY/e6tkRh9g2dQlp/b+AuZTEAKHmdwiqgUl0+A feEfmQlJQa0qfMBKTpVybpgTOU9rAlHg6sYeeRviMFeLcN7QNdTPXoJ7xsCrLK0JW+Qy 3abFbXLv5w+iSr/GW5Tgk+cicI1hAIcLkY259+QN+A/rwESkSDue6fP7kPBvqr5v+bMg 1yXmyIIiW1FlrYNZ2heyl+3gGKJl8P8PqGVrOA457cVgOImE8TfjnSk6hMdvaz0f4OKc hTqyB58PDc9DGOj8lC1+u6saevbYLTchXLQR2TJvqkASNbLJNxhVSddchnlEVfHrGWog 1ZNw== X-Gm-Message-State: AC+VfDyhiN8J+Jt7EMWQlayuFkmRuBdC685A7e7ZpzonVGEPRHZOHXwK rYsU5kKzhL2aMXRsLdPRlxY= X-Google-Smtp-Source: ACHHUZ7JnZGVdgPbr9Z1PadO5uuvgmx6M5wm4lBPA/SHMCBCs+RVa6CkMcN5HzSAxHb+bm8Y23G0LA== X-Received: by 2002:a5d:4f90:0:b0:2f8:f144:a22c with SMTP id d16-20020a5d4f90000000b002f8f144a22cmr4591651wru.62.1682701275448; Fri, 28 Apr 2023 10:01:15 -0700 (PDT) Received: from localhost (host86-156-84-164.range86-156.btcentralplus.com. [86.156.84.164]) by smtp.gmail.com with ESMTPSA id e18-20020adffc52000000b002efacde3fc7sm21459866wrs.35.2023.04.28.10.01.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 10:01:14 -0700 (PDT) Date: Fri, 28 Apr 2023 18:01:13 +0100 From: Lorenzo Stoakes To: David Hildenbrand Cc: Peter Xu , "Kirill A . Shutemov" , 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: <40fc128f-1978-42db-b9c1-77ac3c2cebfe@lucifer.local> 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-Server: rspam02 X-Rspamd-Queue-Id: 3B696120039 X-Stat-Signature: rzj13fqhpjsozxp1kmzf14whc476esdm X-HE-Tag: 1682701276-380013 X-HE-Meta: U2FsdGVkX1+9bkuUOD/LJysVmzKCMsHEnUvM1LkhZLa/EOuZ0K5IZT2oaFsUtO15TamMSwUD1KWe5939Ycxbkr/yesD9zNT3HUDQOzCnEBBj5eXtc0cpGhNZgREsaWi0bdRrw4D3PmPXAobN6ryLuELZoM5ef6j42/Cluvocf1YW2GDjO3AdC0YcXShXIb9nwk8BEbslVn7VMPS0ewv1eopGI9xAWxL04XYJM7oSVWVGHqPefY2Kbpr7kzl24OEm1R9A63Mg6zBFYyUfKTFPnZ97nb3XbEphgQrDK7hNOayTAy8E2EcG64SfR6tiFJ5AWW3VZbXi2mLhxEZO07t0/X5vw7x0pQuuD59WdVm3UBOAldSB4fS4aky/ReDgZNFp+2FdBY80J7NHlpvf2WPBF8Yhyelt2B05wHivYz+gm9ctto13yKOmgua5dH8HcjDJ8EKtqiZKM+Cz5tPNEClO5R7fXe+fC9uapdK/ZFX37583XGrC0oMkHk7RuuOPLAGUzWtsWUe8xeakrIbrMvap2UUPBxqQLdog5yquqc5xVkiOUXGEVg01FpMLqYXR05rOWKysXWM4Jj5W4CkGypvVcx5UwuSzvaq7NeI/G1gGBHRs9O9e8bxMTxPYklYHbqbngd000dNgvi+n8fPcaEyEhnq05/W8tfPny8/gTeFCOFt3y7n+Tk8yMtvr7s/+Olw/80wHd/uZm6MXu+bTuzVUZ3/A4O72hjvK2T5fLy1y05I1JGzpomKiXhhxievWLDsc6ED/q+7BzQuji6KtRJBJNvuEl/ekoXqIiGbejV6qzlzjCs3xHPdYCXczvWQZBqnyOMZja1F3jBHfNgcCwBBvXVVF8wry8b6hY1iEUhJnZCcqNsL/Rrx7ocEDLFz1zajK0+MECV/8MfPOvk35H6RUu0kcf3NuDKuh3aTHIpuJ3CMWn9925sq7Sfm+2NZbrK6YRQFSxlwx4JGXrewDGPo WuIlIqXa 1FPYVA1ChAKHxfwpAIeNjzaXzBp62JVbXpsEqPYcDK7he6KvOVVxzZR5QvqpGnYAEPt0QFTKqwH6HsSb4DQdV2b8lgI/GxnfHqKzHpKB5q2vT9HcecCFuizZQ5unJGrfs6hHTewcW5pg9mr1WvesXlF/J8F2OkTUfRufoiVdwjNuP83VWrixYr2F75Yk1cEOLrVMQjz/nAk+FFA8yTAngCHUIns6LibunKWlVAJMVJXpgYy/nOONYQKNZgbQZjum7iskkbRJZYNl+SKesptTwlKyqU3Acua+BD7aQxm4VTg1Hyb4fyHM5mL2Px/77sXtGblJJi0jQwyeo/z0NwQs3WtUd8peWpvj4W5Gn0MdDb8iR5f0456+O6avdYlD1Wc55TFjPp7vSf51PW/KU0LKsvbh8GMjP1ei2z3acOJHZXFbYNJXMqKQDBzK0zFFetL9FbS6OFfaG8FASQP8nlGv0NEQBiQ== 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. > This shouldn't be an issue though right? Because after a pup call unlocks the mmap_lock we're in the same situation anyway. GUP doesn't generally guarantee the mapping remains valid, only pinning the underlying folio. I'm thinking of respinning with a gup_fast component then, if a_ops is sufficient to identify file systems. We'll just revert to slow path for non-FOLL_FAST_ONLY cases. This would at least cover both FOLL_LONGTERM angles and could provoke some further interesting discussion :) > -- > Thanks, > > David / dhildenb >