Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7300341rwr; Tue, 2 May 2023 12:27:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5koCJJTIOhLw2fXA2XdPJVZRvmivpW7TfdPiaWgQijSkBP/Z7INWvvB7EGSV3OfVufy8iM X-Received: by 2002:a05:6a20:3d2a:b0:eb:6e43:8e1b with SMTP id y42-20020a056a203d2a00b000eb6e438e1bmr24717783pzi.30.1683055655603; Tue, 02 May 2023 12:27:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683055655; cv=none; d=google.com; s=arc-20160816; b=X8RN+t+YpN64jrDgphybO+b4Ir5gb+PacKtZlNr1m/rou3uQjvQKZx8nLldeMRHxWl +O0ZP3eIWxM06ud38kNz6D0yC8aGKdDHNUWZOFEZvmL+bij4f9ylBm7YriiuaiqVu03i SR7R6YuTIDrB0VoaHOmO0uneciK980hYMUyT1ORVLNJRRZAF0mOr0z7qEf7ttcNTaKRY rc0ODMpbjxlraaS/021C5gHEl4WpiRbUVBUI6ZwzIurrMbfOU39gXp8AKPzS78QqSgjD Ljn5zXF7DSl/FIy73dmqcVXCT+UoswslESjF+ML0C1hVBEwoyw6B0qa9Y4An8U3CyC/Y DV8g== 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=/AUSUmrI+P7/+99Qu6phENx8nWverUMMhzCM0iScMBs=; b=Rd1TPqPInxJt/HZaoQwpUMP9qaG8pEpMgasOXfmUOjM0khkrZCRDKYA79HM9pApKL8 mZ7h0MDYQ08chnTivMgzQLB/KFfDIg8KNoZ7h6ByTa4FlXm7TNcK4krwNSgSpcFvx5hc GRfSCI4E7MQzl6h/PPjVk/c1/QpD0gaSVlq99jAvakmCa7rnC9SIa58HzOukDEkOPnK5 TO84DgSpwBWrwDDs/HHK/hBuJwOA5eqOlj2zWcu+42N5lUKMgbEMvogh+XCc7cOIt0qu ghdZ0kQ2tgPeo0VW5Kd6eWetUsiH3ieyjX/U84vBGLVV+htcxChWpOl58XIct4RbwTZH 4YCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=A8IPl7XS; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 185-20020a6301c2000000b00523eac95883si31941069pgb.320.2023.05.02.12.27.22; Tue, 02 May 2023 12:27:35 -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=@gmail.com header.s=20221208 header.b=A8IPl7XS; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229619AbjEBT0G (ORCPT + 99 others); Tue, 2 May 2023 15:26:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229515AbjEBT0F (ORCPT ); Tue, 2 May 2023 15:26:05 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 514EC1BD4; Tue, 2 May 2023 12:26:01 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3f19b9d5358so42459315e9.1; Tue, 02 May 2023 12:26:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683055560; x=1685647560; 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=/AUSUmrI+P7/+99Qu6phENx8nWverUMMhzCM0iScMBs=; b=A8IPl7XSjnSBORcWZdF+wrp4liindlsj/EFJuQ667TQjLSd0VNOtM03C+cX2d0Xkx0 yT3w4a7ZWvvaIHGji/U1kDoPlYYXfz5B0jaVp3duDNcIwp9WOjYLqVhGESANFpy51BnH NzB2+W9lOFUoH1Jl07+CYDfTfERa7FYirtn+Bxe+yjV8hV44R+k7O1bN76p641pr2Ymb QO15obXh5w8ppB56iyY8Xy3AoJb5X7330ozrt5xVKG1CBvjsUzwkYCvE0rfKnizwDYzu sh2VD2syoE3U0pJf/7E8YqKqaj/o3il5kaXtv5UnY051GaeGafOzq6vhQ9KdZVoDfXuw YpgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683055560; x=1685647560; 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=/AUSUmrI+P7/+99Qu6phENx8nWverUMMhzCM0iScMBs=; b=MYevraMQh94BBesEJWkzm+EpI7Sx8jYuujJk0i8xFZkpsrGKQW/lXiMON9KJRh4Swf L5h0KXeRcRHAw+SaVSPWRfjxJPNOc2PMVoG2L0gXbLltUU6sPeyERKwxuw7ema5bzLkS AFA87/Gr8uR/DEhnJvohtEVQnTREeWgG9UCDFYjyHtOgkhktsPf+8omw96XrZ+HYLRDm NAEzX24Bwc6IqmrmL+FF2/JZO4nXXKzCwRSsHcIDyI59qONebq4H31zqX0VOHKJQheCj cnAnuZQ+Qu5BAp3/H2gHa0bUfaKM9loBWRwZC/RNF1iRn1sARQg0z7ezFIrQquPQ9gJn 8jzA== X-Gm-Message-State: AC+VfDxPxNIkJyAcm9APqMr98aqc2LDqu/7AvhywChFH2Kja679NLr7L E8mf1qaA/cbycGcMYsI2lNDNeHNEZuFOBQ== X-Received: by 2002:a05:600c:2212:b0:3f1:6ebe:d598 with SMTP id z18-20020a05600c221200b003f16ebed598mr12463201wml.7.1683055559416; Tue, 02 May 2023 12:25:59 -0700 (PDT) Received: from localhost (host86-156-84-164.range86-156.btcentralplus.com. [86.156.84.164]) by smtp.gmail.com with ESMTPSA id 13-20020a05600c230d00b003f31da39b62sm15329343wmo.18.2023.05.02.12.25.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 12:25:58 -0700 (PDT) Date: Tue, 2 May 2023 20:25:57 +0100 From: Lorenzo Stoakes To: Jason Gunthorpe Cc: David Hildenbrand , Peter Zijlstra , 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 , 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 , "Kirill A . Shutemov" , Pavel Begunkov , Mika Penttila , Dave Chinner , Theodore Ts'o , Peter Xu , Matthew Rosato , "Paul E . McKenney" , Christian Borntraeger , Mike Rapoport Subject: Re: [PATCH v7 3/3] mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings Message-ID: <968fa174-6720-4adf-9107-c777ee0d8da4@lucifer.local> References: <1691115d-dba4-636b-d736-6a20359a67c3@redhat.com> <20230502172231.GH1597538@hirez.programming.kicks-ass.net> <406fd43a-a051-5fbe-6f66-a43f5e7e7573@redhat.com> <3a8c672d-4e6c-4705-9d6c-509d3733eb05@lucifer.local> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Tue, May 02, 2023 at 04:07:50PM -0300, Jason Gunthorpe wrote: > On Tue, May 02, 2023 at 07:17:14PM +0100, Lorenzo Stoakes wrote: > > > On a specific point - if mapping turns out to be NULL after we confirm > > stable PTE, I'd be inclined to reject and let the slow path take care of > > it, would you agree that that's the correct approach? > > I think in general if GUP fast detects any kind of race it should bail > to the slow path. > > The races it tries to resolve itself should have really safe and > obvious solutions. > > I think this comment is misleading: > > > + /* > > + * GUP-fast disables IRQs - this prevents IPIs from causing page tables > > + * to disappear from under us, as well as preventing RCU grace periods > > + * from making progress (i.e. implying rcu_read_lock()). > > True, but that is not important here since we are not reading page > tables > > > + * This means we can rely on the folio remaining stable for all > > + * architectures, both those that set CONFIG_MMU_GATHER_RCU_TABLE_FREE > > + * and those that do not. > > Not really clear. We have a valid folio refcount here, that is all. Some of this is a product of mixed signals from different commenters and my being perhaps a little _too_ willing to just go with the flow. With interrupts disabled and IPI blocked, plus the assurances that interrupts being disabled implied the RCU version of page table manipulation is also blocked, my understanding was that remapping in this process to another page could not occur. Of course the folio is 'stable' in the sense we have a refcount on it, but it is unlocked so things can change. I'm guessing the RCU guarantees in the TLB logic are not as solid as IPI, because in the IPI case it seems to me you couldn't even clear the PTE entry before getting to the page table case. Otherwise, I'm a bit uncertain actually as to how we can get to the point where the folio->mapping is being manipulated. Is this why? > > > + * We get the added benefit that given inodes, and thus address_space, > > + * objects are RCU freed, we can rely on the mapping remaining stable > > + * here with no risk of a truncation or similar race. > > Which is the real point: > > 1) GUP-fast disables IRQs which means this is the same context as rcu_read_lock() > 2) We have a valid ref on the folio due to normal GUP fast operation > Thus derefing struct folio is OK > 3) folio->mapping can be deref'd safely under RCU since mapping is RCU free'd > It may be zero if we are racing a page free path > Can it be zero for other reasons? Zero? You mean NULL? In any case, I will try to clarify these, I do agree the _key_ point is that we can rely on safely derefing the mapping, at least READ_ONCE()'d, as use-after-free or dereffing garbage is the fear here. > > If it can't be zero for any other reason then go to GUP slow and let > it sort it out > > Otherwise you have to treat NULL as a success. > Well that was literally the question :) and I've got somewhat contradictory feedback. My instinct aligns with yours in that, just fallback to slow path, so that's what I'll do. But just wanted to confirm. > Really what you are trying to do here is remove the folio lock which > would normally protect folio->mapping. Ie this test really boils down > to just 'folio_get_mapping_a_ops_rcu() == shmem_aops' > > The hugetlb test is done on a page flag which should be stable under > the pageref. > > So.. Your function really ought to be doing this logic: > > // Should be impossible for a slab page to be in a VMA > if (unlikely(folio_test_slab(folio))) > return do gup slow; > > // Can a present PTE even be a swap cache? > if (unlikely(folio_test_swapcache(folio))) > return do gup slow; > > if (folio_test_hugetlb(folio)) > return safe for fast > > // Safe without the folio lock ? > struct address_space *mapping = READ_ONCE(folio->mapping) > if ((mapping & PAGE_MAPPING_FLAGS) == PAGE_MAPPING_ANON) > return safe for fast > if ((mapping & PAGE_MAPPING_FLAGS) == 0 && mapping) > return mapping->a_ops == shmem_aops; > > // Depends on what mapping = NULL means > return do gup slow > Yeah this is how I was planning to implement it, or something along these lines. The only question was whether my own view aligned with others to avoid more spam :) The READ_ONCE() approach is precisely how I wanted to do it in thet first instance, but feared feedback about duplication and wondered if it made much difference, but now it's clear this is ther ight way. > Jason