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 6C899C77B60 for ; Fri, 28 Apr 2023 17:13:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07BC86B0078; Fri, 28 Apr 2023 13:13:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0056A6B007B; Fri, 28 Apr 2023 13:13:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC1446B007D; Fri, 28 Apr 2023 13:13:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CA7CC6B0078 for ; Fri, 28 Apr 2023 13:13:40 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8B5D9A02C3 for ; Fri, 28 Apr 2023 17:13:40 +0000 (UTC) X-FDA: 80731446600.29.4849168 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by imf11.hostedemail.com (Postfix) with ESMTP id A2C324001A for ; Fri, 28 Apr 2023 17:13:38 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=MgFkGaN0; spf=pass (imf11.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.41 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682702018; 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=MbLAx2LnX50Pa8IRbgZc/5rCjh9gLR+XLmRDEwxwfQQ=; b=2Me6DxQW1d9WS9DEBb9cd/xsqh43k1YJ/kdy3B7OpzSvdb3Ijq1+cZU+apRA2AXzwDxXWw aV8aS62J6FwVBQ7jmZ3rwOmUSzuC8KFuF0Z5gCSsgpJREItYhKVwgBQfuiHv3db0/7WVdv BC2QR04Vol5OeSjToIs0a44fybdJcus= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=MgFkGaN0; spf=pass (imf11.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.41 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682702018; a=rsa-sha256; cv=none; b=ildwR2wtTHt6Laix4dNU/vtMSG/BLLaus0GXNTFsvLeiCLfcaNTVwtl98w5a5Nar/MsZPK bfPgtDf1YO7Ha8rcBlItP35vyysnAZG0YIFm3p2iVTLThsoSNdvihpx6XIIi2yKIGie2El ZYyPBVQ8r3HTGuedVWfgxzdrRyqXYEs= Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-3f19ab994ccso79528075e9.2 for ; Fri, 28 Apr 2023 10:13:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682702017; x=1685294017; 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=MbLAx2LnX50Pa8IRbgZc/5rCjh9gLR+XLmRDEwxwfQQ=; b=MgFkGaN0cnH0b8haF3jyGA/gT6J28ROImE4Hm1er1/GoWP+W8NS0vTcqtkG6nnlsDH LkIxopt/o9wa/x1WfqdTlwtelQ3MKUItziArUt/IFtkXc3xbC5soOyy3PPvfjgQP8NtL dahxELxKsd2OOb4gWyRjQ7K40/KdbXCM1Ps2Cgmg2ZU7l9k6KQQCis8+VfvfQMxg9+HR HIzpLMpW4lbSz7puqLP/iRlmOpjtRyV/tym86g8HQoa9OmmSRZMx64diBzIEQMorNTdu iSD9r+u8gpiH3tk/bbRcm0zPeap7VWRoCKOGY6nWuMt45Kv6KZHPK+ZUhwVhIJIBspKd M4kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682702017; x=1685294017; 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=MbLAx2LnX50Pa8IRbgZc/5rCjh9gLR+XLmRDEwxwfQQ=; b=CseLBSnGURza2I2jvX8Rb92jRZj5Cbb0XlEq36wpBnnJTTof+L0MtyynbYDBX7V16i GLFeDO+c4676X2AS0ruwiMwGOxTyAvkSSzz7rJdsQNm3KxuEwr08n5uF79avFWTZNVaF smKddM0cpRwnMaxexlVZXnjzCPsGS5bjOrEZi21JP/ypSMk8i2VGMEZnN3BjckLutBRv f+fuWv+dFbNN6SwSQwLyIYnti/btwF5sfqjkv5NZRFHJirRWAOmTvVvyn3y9YoHKZRtk 8/ANKRO78QdRzR6LZeMKveCUsMMmWNHDt8v1vxkSqqHTdrXGyfBtZWEdbvAoVhOkvhYm r9sw== X-Gm-Message-State: AC+VfDwqhlp8e9DCdJWAhU6fxNsMTTSOdfWrdd/TScA8nwZzfscyPggX wqonsI67HaqNsI3K2tcxUu4= X-Google-Smtp-Source: ACHHUZ4F0XmEmjF4UX1pBtKJCfhATbaw6EI/uToH7e476Qzq9nncg1VGRyPbJRFuphlobHXfV9JibQ== X-Received: by 2002:a7b:cc94:0:b0:3f1:69cc:475b with SMTP id p20-20020a7bcc94000000b003f169cc475bmr4839212wma.36.1682702016993; Fri, 28 Apr 2023 10:13:36 -0700 (PDT) Received: from localhost (host86-156-84-164.range86-156.btcentralplus.com. [86.156.84.164]) by smtp.gmail.com with ESMTPSA id x2-20020a5d60c2000000b002de9a0b0a20sm21439126wrt.113.2023.04.28.10.13.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 10:13:36 -0700 (PDT) Date: Fri, 28 Apr 2023 18:13:35 +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: References: <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> <40fc128f-1978-42db-b9c1-77ac3c2cebfe@lucifer.local> <3d7fcfab-e445-1dc7-f000-9fbe7bea04c0@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3d7fcfab-e445-1dc7-f000-9fbe7bea04c0@redhat.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A2C324001A X-Stat-Signature: pyhc8hw1ew6prjfw5owozxtkic9qk1hq X-Rspam-User: X-HE-Tag: 1682702018-602667 X-HE-Meta: U2FsdGVkX18wpmKYWH4y3PK0kyVm2IbcTQNa2KplxdMdFzTXzv9a7oVWkBCt++R2on2dwcaFhjj6nPdtoMs6a69B+clTQAmaV5rL7G6PUjkr838IXXC1UQeHs2eUDCioB+6ivMCFCECnO+ANz+P+B5UPTfwfw3Mr8kRIK/c4o3stOFk0eiLLglq5v8evaWlWdKA9p/ClJnb1dymJT4Psww/D6ZG3B0hEI4n3iUAVSVxybXe/sMG4HquVJBo8Vnhn1O/AR7Zus9BiPCGSToWhysEgQUpyJhmcuSZ0EzyE6wJIvL4rkTQtTYsdocDaDVDEcP2SQaY5+BBAi02ubRjyfssdnaBuzzrG7oWVi66a+L9u1RDHrXqLD1qesXOO223vmN1hG3F1Eh2c0d1Mz5O51HQK8PeVO+i/L4bsq6Zq310fMXwHMetPTKTOYMoBxBUGCvQcwzdWFdLNSb3Czrv2nT1Ph8UGz3orM3dKLOZpFtwq2tSGLApl0iUuJHRInKwSFrxf4zaWJ5nrxAr+cUu/IOz8LDJF4uKNdI4KLk8iopEOFcxzWVoAl3Ql61xJmRauPBILhkJKOXUnGgoyumowd5/QKJePMy4pRZs1RvjEr+h/kHBU3vzJGyjfgulHD3vhoB6FIhCW2mWJT08Hjt2tDfR38KC39nKEu/swPIoFg55Jw34xFAaoYs/A8WKcHF+vwpoRcYiCnIgojPpz0GEevloCVgbb+oCRFfExQvGRWllCTecTL+2Llm5iHoJrnRE27FO8ZAYDVin4kaTEMhHFiz4oeRsrSmqzym4/Gr/CJK677fFZyB4PIGbhoHuWeVuYKmamqdBKH6/N2bgpHqmqDK3mbW0gySWTUvQqlZN+P6fbcxtHIu+y0Ac2GIxBSzmPDcKfdomplimoKzFQNw3AaCP0H8qdwDiC0uEQU5Q/X9l/OMk80QN5fJQz/NhQ8+r5YA1LP22YL4BKs6RodEJ vgkmlURU xsPK7WdAHDB6eIf0j18CG/txXF1837KNTDmleWa3ln4qVSskxduCRSSQFyU1pfa8Bw45nAl2tePWZS+zwUnBUITOg/QuGosLXQJEvs7vtXzeh3PGp2Y+mjZldv12N3noO6jHxWnRotj2uW5Cm/tBWLo9PN3AeDLSSGenr53YOwEyseaoknvlvA+TmAdbegEKnLKM6uhapL3PS1iGxTyGCXjaYA2GinksQwGEm0kotr2Y+yfLWmkFv7oY1VLjKMwgtPMHN+TH4mZOFqzgKuMIM40w3eOAKqIMOejvLyxfrjtFslKw8P130aKo5/nT8MgwygOciCNT4QQzZ2HlVyo3RPESWI3kkEzzyurnRuViLh/5F2fEJIVs5gsT4/bQdofIGl3eftY+dyABAirM1yzNM1drL+Icj08v8nhk6UCo7wu7NTtKoIS/1wj8ifwakMdJwqHFcYC0QrfbMimV9VeUkrSn+wg== 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:05:38PM +0200, David Hildenbrand wrote: > On 28.04.23 19:01, Lorenzo Stoakes wrote: > > 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. > > Yes. But the issue here is rather dereferencing something that has already > been freed, eventually leading to undefined behavior. > Is that an issue with interrupts disabled though? Will block page tables being removed and as Kirill says (sorry I maybe misinterpreted you) we should be ok. > Maybe de-referencing folio->mapping is fine ... but yes, we could handle > that optimization in a separate patch. > > -- > Thanks, > > David / dhildenb >