Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp6689892rwr; Tue, 2 May 2023 04:17:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7VRPR90/Jm9CJqnd3qCLKc6bQtIyuk1C4xdsWZvDAOuPqjwRRUdU7dytnS/wsmd/h4/2O9 X-Received: by 2002:a17:90b:4c0e:b0:24e:3b85:53c0 with SMTP id na14-20020a17090b4c0e00b0024e3b8553c0mr55647pjb.19.1683026250762; Tue, 02 May 2023 04:17:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683026250; cv=none; d=google.com; s=arc-20160816; b=EhAWLuud6bGJ97cJh9mQFdABCTg5kfenp/luObZCZvE/NStJw4QivKZia53rkjDcUc ABnXp9AcLtqVfD4FES9fXi1vavu8nys7YdFchAa5tQ2Ir4WjyAS4VDU6D7US3WJ7BG/i l9b1zO4k31YXvT3jODB2cjY9tm1unShCv3/s/sDL34byJ5BllVSkzdseLv229SiYi3yU y0akKuzYnkeDcGu1qfIYnQibTPMKMrEIVmXN/uQrKE/VIdNgjETW9JEvIZZGli+2+fkp /rrZDGzja3F59Jz9A1Acb9PdyxMVR4U445md3doaP3gMHF8uHAsSYJ4GZqZIqmZV6sQC uGVw== 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=qSL9OkS9lUf+/Y0rKTYhB5gamqdgUqvemLYpwvoIblc=; b=eL9VsbDEzsMMzCbe9jUSTypk37tMKsx8VEiUJVUbYzySucHB66S+4rwsaDeAtK2z7B B9Z8MKbDQWQChxIPJ9EqKWe7vHAXcKqJcMaiPeYAxwV4QYosdydr3Kuxkk+zLg3mpYYW 4rL3Lqos/qZJvo6Ecae1xfDqjDiUuiaOr5nzhjz6de8ElDDbWkG8u/RWGS1hMqSf8KIp EhRfNvf/6O8fmxQ6t50r7563RqxRzj8mTWlCIUvNun+5FFtuR1L2Ux/bk395uqwGiABY Uk2XPSafD3BOjXj8R0THzNo1Ymccoi4W+rrEso/h7yQNABv1ND21NtFlCu7pxa7N3s/a Emyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=nyzYxVwk; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z7-20020a17090abd8700b00246869a3320si10565979pjr.178.2023.05.02.04.17.18; Tue, 02 May 2023 04:17:30 -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=@infradead.org header.s=desiato.20200630 header.b=nyzYxVwk; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233817AbjEBLOz (ORCPT + 99 others); Tue, 2 May 2023 07:14:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229457AbjEBLOy (ORCPT ); Tue, 2 May 2023 07:14:54 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A5FAA4; Tue, 2 May 2023 04:14:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=qSL9OkS9lUf+/Y0rKTYhB5gamqdgUqvemLYpwvoIblc=; b=nyzYxVwkDYSGs+p8GZKdRUmbVj Guk04quwpkt4KIxKlgvRq3hDoGXk8vII7ZwOwysVQJCDxD43IcwITOv8Ukdr45u9zmDX6QMz4+ZLF LqkGMCkKLaN//YYSjQyG5rR4N3Zm3NnDNwEoajETpYIv0qstEhNJpklrqzN+7T7GAfsy1KwEUWIp3 2ZzcofszrGMZajnGYhtwC+KoakXv3sPRbf2mmipIl0Okn41UYHFcjQqBRK0YkzxsIQRNfunJH19jn gmExS18rx3m3bAnr1WnkjVxjqHvFlbaQ4e4wrs7y3THRwDT87WlmVeGXRICl7RWDw8PvbZ2LrvrCF 9u27uBeQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1ptnwv-00GI5F-13; Tue, 02 May 2023 11:13:37 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id BEDB2300348; Tue, 2 May 2023 13:13:34 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 79C6A23C5C34E; Tue, 2 May 2023 13:13:34 +0200 (CEST) Date: Tue, 2 May 2023 13:13:34 +0200 From: Peter Zijlstra To: Lorenzo Stoakes 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 , 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 , David Hildenbrand , Dave Chinner , Theodore Ts'o , Peter Xu Subject: Re: [PATCH v6 3/3] mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings Message-ID: <20230502111334.GP1597476@hirez.programming.kicks-ass.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,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 12:11:49AM +0100, Lorenzo Stoakes wrote: > @@ -95,6 +96,77 @@ static inline struct folio *try_get_folio(struct page *page, int refs) > return folio; > } > > +#ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE > +static bool stabilise_mapping_rcu(struct folio *folio) > +{ > + struct address_space *mapping = READ_ONCE(folio->mapping); > + > + rcu_read_lock(); > + > + return mapping == READ_ONCE(folio->mapping); This doesn't make sense; why bother reading the same thing twice? Who cares if the thing changes from before; what you care about is that the value you see has stable storage, this doesn't help with that. > +} > + > +static void unlock_rcu(void) > +{ > + rcu_read_unlock(); > +} > +#else > +static bool stabilise_mapping_rcu(struct folio *) > +{ > + return true; > +} > + > +static void unlock_rcu(void) > +{ > +} > +#endif Anyway, this all can go away. RCU can't progress while you have interrupts disabled anyway. > +/* > + * Used in the GUP-fast path to determine whether a FOLL_PIN | FOLL_LONGTERM | > + * FOLL_WRITE pin is permitted for a specific folio. > + * > + * This assumes the folio is stable and pinned. > + * > + * Writing to pinned file-backed dirty tracked folios is inherently problematic > + * (see comment describing the writeable_file_mapping_allowed() function). We > + * therefore try to avoid the most egregious case of a long-term mapping doing > + * so. > + * > + * This function cannot be as thorough as that one as the VMA is not available > + * in the fast path, so instead we whitelist known good cases. > + * > + * The folio is stable, but the mapping might not be. When truncating for > + * instance, a zap is performed which triggers TLB shootdown. IRQs are disabled > + * so we are safe from an IPI, but some architectures use an RCU lock for this > + * operation, so we acquire an RCU lock to ensure the mapping is stable. > + */ > +static bool folio_longterm_write_pin_allowed(struct folio *folio) > +{ > + bool ret; > + > + /* hugetlb mappings do not require dirty tracking. */ > + if (folio_test_hugetlb(folio)) > + return true; > + This: > + if (stabilise_mapping_rcu(folio)) { > + struct address_space *mapping = folio_mapping(folio); And this is 3rd read of folio->mapping, just for giggles? > + > + /* > + * Neither anonymous nor shmem-backed folios require > + * dirty tracking. > + */ > + ret = folio_test_anon(folio) || > + (mapping && shmem_mapping(mapping)); > + } else { > + /* If the mapping is unstable, fallback to the slow path. */ > + ret = false; > + } > + > + unlock_rcu(); > + > + return ret; then becomes: if (folio_test_anon(folio)) return true; /* * Having IRQs disabled (as per GUP-fast) also inhibits RCU * grace periods from making progress, IOW. they imply * rcu_read_lock(). */ lockdep_assert_irqs_disabled(); /* * Inodes and thus address_space are RCU freed and thus safe to * access at this point. */ mapping = folio_mapping(folio); if (mapping && shmem_mapping(mapping)) return true; return false; > +}