Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2404000rwr; Fri, 28 Apr 2023 09:51:59 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6LIPNtKgMK1AOBFQ4KISVL2vLsXOskzXCSowSnm4GYKPu7Y7/l3rpcPXwZCDoKVXty7GMH X-Received: by 2002:a17:902:e88c:b0:1a6:bc34:2ee with SMTP id w12-20020a170902e88c00b001a6bc3402eemr7459432plg.21.1682700719616; Fri, 28 Apr 2023 09:51:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682700719; cv=none; d=google.com; s=arc-20160816; b=rb2iYxLFPiB+b8f1HxEGKLg4sqUIV5LKEB3hnIt81VwJKCz3vvD1WYuht729GfXkWW 7nWwGTm7wKMk0xWHzkCALYUyoQfbW4E4hJloEIyhDVuO01QhE/ORBsaJIeBk/sKdEz4u EHcN7pijJYjZfOt8wmKCxMyYy5vNJP64bzmpltCfWrHL8Z3tE7a0ZfCRjbyTUV87wacp 43XGwfI6qzFKlLX/CCixtzWgLfmoywDVKmdgKyqbPNtX0XVdFPj/CJROdXXTgwbODB9h HGAcjQQvYgIJCuZ9upKCQAwt6F3x32I2tzql6vyanDLbh3Ne6INAyUbWAUKh0qg1dnkD i6CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=rWkVvKB+beFGKaX1u6z+cXwjtof0A4OJbagxKAP5YOc=; b=YPELFPr73GqiL24PsMMT+t3DAQxf8AxZqO1AXIox7rN+3UMuSlA0M0oOfb6GLWxig0 qJ4+BxaauvzR2NtHSkb3ADuwIZ5+AsMsE2UfgeOCvME2txMHLZSWmaRZL8J9ZBYsJMaV So7CRODG7UfrkjgPpk9YpUp9yfcjdw5O+86EwWGYQ6eCqQXpm8p/OcByn2IsG3t1umSS v0sTv0GmHdsb1TDYypoLEKY0jG7uVKzyCWd8DFJIWptsXmca6Hz/nJw3IlstLchMcf6y +AW6uw0WecdB4Yz5H2AFkiq9Xf6++97OsFXgSlSIRgsHfRIZTbaWG1Hq4N0TvKmfYCAM pn3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cF876kMR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p1-20020a1709027ec100b001a95c413f7csi15442882plb.550.2023.04.28.09.51.47; Fri, 28 Apr 2023 09:51:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cF876kMR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346315AbjD1Qkz (ORCPT + 99 others); Fri, 28 Apr 2023 12:40:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346010AbjD1Qkx (ORCPT ); Fri, 28 Apr 2023 12:40:53 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8ACAD524F for ; Fri, 28 Apr 2023 09:39:58 -0700 (PDT) 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-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-547-J8xEexk-MHmFGPl9zLDBgQ-1; Fri, 28 Apr 2023 12:39:56 -0400 X-MC-Unique: J8xEexk-MHmFGPl9zLDBgQ-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-5ef57d3198aso274776d6.1 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=1682699996; x=1685291996; 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=hac4GXlQHNXy06EBD2Kp3AkLSMTmCP3Y4lSPxx5WwPS1IxxiZpg8OJ3IK2K/v+sLum uzoJ3PyJFkTbCUf+gsVKyWSkLPyB6GWA6vedcmr2/21bdKHX1VXPVPkksPfJtsNLZjXy wzIBTvN04HJ0Bnr1yHkFVBUHxHXO7+zjz3UwgzMSITEL1IRnYuDlo7aXlNwWNcBUU11E OgSPpXPv2fZVm1+yBBeZ9EixEsiPS4drJ2aJ+KKik0/oYWXDY470nbj8EeEbWPRQOs3e 2oXCyp9Fq98ZjGq501LMHOrggWTpgIWRxMTmw6IkVUkJ7e3828VlaW82+6sAXbPXo6Xm /Z1Q== X-Gm-Message-State: AC+VfDwoC4jYLNtNNwQuetcMqzzLGokHgw+rYF3TwlOrZX1ZBAVoTb+o ZCVvZscZimFjRaICiyzd1zq6wXFjNC+t9GwP5Y3UyJweNCU9QpiGogjybG4HVKvTncrMSIz1Vm6 OuJ7FksXwkOc/JeyrnYWVqmqi X-Received: by 2002:a05:6214:3002:b0:603:fa46:d368 with SMTP id ke2-20020a056214300200b00603fa46d368mr8548775qvb.1.1682699995621; Fri, 28 Apr 2023 09:39:55 -0700 (PDT) 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 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20230428162207.o3ejmcz7rzezpt6n@box.shutemov.name> X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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