Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7187145rwr; Tue, 2 May 2023 10:39:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5H7193bvq6U1bIUDkdi0nwoRkRzJnt8OgU3ZjENVW55FDhFwzPIYjo686ZidCUfFb6PyxX X-Received: by 2002:a17:903:41c6:b0:1a6:74f6:fa92 with SMTP id u6-20020a17090341c600b001a674f6fa92mr21737299ple.19.1683049140297; Tue, 02 May 2023 10:39:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683049140; cv=none; d=google.com; s=arc-20160816; b=vejdQ7uhQC437j82LfvSnEWCYZ9mR18CJ6zYNJYbXfqeWGs/9jmUdMJtXJJzd3/u+b BxJATVXuPp/5rf3sqftsQUiJZO/Wu3fTOODWpujkVNGWZBLoH5G2g6UXf6z4aJDWgJob rDXGaSr+kLCee7vEuo8/vZQ8edby7f6R17wTr5lyQhYzOZXdaqSsmNVZtizbjvGvm7w3 m+5D8ZwehYA7fHE81lyPQgAvEaVvcDFOdTo//7II2FBxRYGdOlLvA3UAg3H42z53obn4 eYW+TmG/WDOq4cJui7pA02ZhOR3bwvBLmE5hsT0Y0dHwdMfebnoGst06FfUftf4F759P bJ/A== 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=kCAPbuVKXeBY3NxRBpe261J3m7++OAgwEdQm754iF8g=; b=Uq0bA7UhJGfbOM12LyfLNm/WBlVud30PJ+0+C10Hk76JEwD8BJb5MaTXdKBqCRPXYK v5ITOJ3hPzuOIZmTG2fiBsMJ0s1sEILb1/XekBqjSyg6mZnckgn+3KixI8CQVAjYHRpO 09d2RUoR+kms+CNzDwMOejVacaMSChh66JKn5ALCuZXPONlvUSAbDGIPBOk6eFLGHSU1 CjRiDGdNhpg3Z72Rcq8vvfoKJUKPe2Iw4Gw8PjuB4OuWSEENBgUkKAjopdY7IOdlLBnH +tX7+KAnf8R0SEe9n42o5AIPsze43s1UVHVMVH9dlzg1vUEu1IiSX2VPBD9s8ge5P5Eh u1hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=mCxBV5ZY; 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 b18-20020a170903229200b001a1bf377c3csi34012462plh.598.2023.05.02.10.38.45; Tue, 02 May 2023 10:39:00 -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=mCxBV5ZY; 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 S234138AbjEBRbd (ORCPT + 99 others); Tue, 2 May 2023 13:31:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231728AbjEBRbb (ORCPT ); Tue, 2 May 2023 13:31:31 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 066DBE79; Tue, 2 May 2023 10:31:30 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-3f09b4a1527so42699625e9.0; Tue, 02 May 2023 10:31:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683048688; x=1685640688; 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=kCAPbuVKXeBY3NxRBpe261J3m7++OAgwEdQm754iF8g=; b=mCxBV5ZYJzLUbZctDfi9hFDmZOYoLKZqzIPerPYG9FZXARz1KDgVBA/PTMd3eDi7DE SVC/AmdybF/V2kI18El9skoD0YhpSa4Vh7rAfPUgot2Y/QF05gp5/XQuubkN0gOx5cuA MfL3bbFuS/lNP+btkBZrM7gnM4mkxnia5X41hWyJkY3m5mxs3LEuh5J2+CAiCyDluIsg cB2yx6uoZL5S/ASk23YG8rrX7+pl8SE9FS0hukdVhC59Ou64m+CiYOQDUQ07yJIwTJoL HtK2OkkeSJodUoN86B/pwW6pCP2wxeFBD5vdgg00mgivQg1SuS97+byIOLkmEVTp7cjj X7oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683048688; x=1685640688; 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=kCAPbuVKXeBY3NxRBpe261J3m7++OAgwEdQm754iF8g=; b=Gc0jmqmeQG3pxZL1d9ex7q62ctDxD4v6LgjVi5EnxJE04jGzT5cjWa9AKlFNU2SDZh 1NyFjbSZgfGM9mMti9njgHT+UH8F8MmXLobPLXsaL5cW65yKgFVU4q4NY0uOHjzNgwMz Oqf1qxjClOToQPD0/FypHOs7TREub6pNGB5v4WcnW0hJW2DmuYLBaCc9uko7M6FkBEhY cgnXaniiIUr3MV6QVNKvVEbnUlmzaaaHLUHYRmJfTlzpkc2ATG8QsdLKQ3eKbebm8qe1 Nm4j5Kdri8b+/HYd+CvDR2+OD0aOqrZDrSI7/RRlM70DQdPa3sYoVGrnga0TtXfEp7Xo hgXA== X-Gm-Message-State: AC+VfDyjn0t9zgFEzQ9aXrlh+T0FapwgJVXWoVSxeCKDZhrBPDHJ//MS Ep4M929wC4KZbWR7gJbU7xd7Tfqp4jdNXw== X-Received: by 2002:adf:db86:0:b0:2f4:a3ea:65d2 with SMTP id u6-20020adfdb86000000b002f4a3ea65d2mr13996931wri.57.1683048688267; Tue, 02 May 2023 10:31:28 -0700 (PDT) Received: from localhost (host86-156-84-164.range86-156.btcentralplus.com. [86.156.84.164]) by smtp.gmail.com with ESMTPSA id t15-20020adfe44f000000b002f00793bd7asm31385628wrm.27.2023.05.02.10.31.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 10:31:27 -0700 (PDT) Date: Tue, 2 May 2023 18:31:26 +0100 From: Lorenzo Stoakes To: David Hildenbrand Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Jason Gunthorpe , 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 , Jason Gunthorpe , 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: <392debc7-2de8-440e-8b26-20f2d42cdf8d@lucifer.local> References: <1691115d-dba4-636b-d736-6a20359a67c3@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1691115d-dba4-636b-d736-6a20359a67c3@redhat.com> 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 07:13:49PM +0200, David Hildenbrand wrote: > [...] > > > +{ > > + struct address_space *mapping; > > + > > + /* > > + * 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()). > > + * > > + * 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. > > + * > > + * 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. > > + */ > > + lockdep_assert_irqs_disabled(); > > + > > + /* > > + * If no mapping can be found, this implies an anonymous or otherwise > > + * non-file backed folio so in this instance we permit the pin. > > + * > > + * shmem and hugetlb mappings do not require dirty-tracking so we > > + * explicitly whitelist these. > > + * > > + * Other non dirty-tracked folios will be picked up on the slow path. > > + */ > > + mapping = folio_mapping(folio); > > + return !mapping || shmem_mapping(mapping) || folio_test_hugetlb(folio); > > "Folios in the swap cache return the swap mapping" -- you might disallow > pinning anonymous pages that are in the swap cache. > > I recall that there are corner cases where we can end up with an anon page > that's mapped writable but still in the swap cache ... so you'd fallback to > the GUP slow path (acceptable for these corner cases, I guess), however > especially the comment is a bit misleading then. How could that happen? > > So I'd suggest not dropping the folio_test_anon() check, or open-coding it > ... which will make this piece of code most certainly easier to get when > staring at folio_mapping(). Or to spell it out in the comment (usually I > prefer code over comments). I literally made this change based on your suggestion :) but perhaps I misinterpreted what you meant. I do spell it out in the comment that the page can be anonymous, But perhaps explicitly checking the mapping flags is the way to go. > > > +} > > + > > /** > > * try_grab_folio() - Attempt to get or pin a folio. > > * @page: pointer to page to be grabbed > > @@ -123,6 +170,8 @@ static inline struct folio *try_get_folio(struct page *page, int refs) > > */ > > struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) > > { > > + bool is_longterm = flags & FOLL_LONGTERM; > > + > > if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page))) > > return NULL; > > @@ -136,8 +185,7 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) > > * right zone, so fail and let the caller fall back to the slow > > * path. > > */ > > - if (unlikely((flags & FOLL_LONGTERM) && > > - !is_longterm_pinnable_page(page))) > > + if (unlikely(is_longterm && !is_longterm_pinnable_page(page))) > > return NULL; > > /* > > @@ -148,6 +196,16 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) > > if (!folio) > > return NULL; > > + /* > > + * Can this folio be safely pinned? We need to perform this > > + * check after the folio is stabilised. > > + */ > > + if ((flags & FOLL_WRITE) && is_longterm && > > + !folio_longterm_write_pin_allowed(folio)) { > > + folio_put_refs(folio, refs); > > + return NULL; > > + } > > So we perform this change before validating whether the PTE changed. > > Hmm, naturally, I would have done it afterwards. > > IIRC, without IPI syncs during TLB flush (i.e., > CONFIG_MMU_GATHER_RCU_TABLE_FREE), there is the possibility that > (1) We lookup the pte > (2) The page was unmapped and free > (3) The page gets reallocated and used > (4) We pin the page > (5) We dereference page->mapping But we have an implied RCU lock from disabled IRQs right? Unless that CONFIG option does something odd (I've not really dug into its brehaviour). It feels like that would break GUP-fast as a whole. > > If we then de-reference page->mapping that gets used by whoever allocated it > for something completely different (not a pointer to something reasonable), > I wonder if we might be in trouble. > > Checking first, whether the PTE changed makes sure that what we pinned and > what we're looking at is what we expected. > > ... I can spot that the page_is_secretmem() check is also done before that. > But it at least makes sure that it's still an LRU page before staring at the > mapping (making it a little safer?). As do we :) We also via try_get_folio() check to ensure that we aren't subject to a split. > > BUT, I keep messing up this part of the story. Maybe it all works as > expected because we will be synchronizing RCU somehow before actually > freeing the page in the !IPI case. ... but I think that's only true for page > tables with CONFIG_MMU_GATHER_RCU_TABLE_FREE. My understanding based on what Peter said is that the IRQs being disabled should prevent anything bad from happening here. > > -- > Thanks, > > David / dhildenb >