From patchwork Thu Mar 2 23:16:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13158026 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 298B6C678D4 for ; Thu, 2 Mar 2023 23:16:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4858D6B0072; Thu, 2 Mar 2023 18:16:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 436186B0073; Thu, 2 Mar 2023 18:16:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FD976B0074; Thu, 2 Mar 2023 18:16:56 -0500 (EST) 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 20DE76B0072 for ; Thu, 2 Mar 2023 18:16:56 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E3D4014105C for ; Thu, 2 Mar 2023 23:16:55 +0000 (UTC) X-FDA: 80525520390.23.2DDCE99 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 14F68140011 for ; Thu, 2 Mar 2023 23:16:52 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=e5UibSOr; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf09.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677799013; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1eE9Pafa530L5JjpUU74EsDdw6S9K1gsLvm9PBz92W8=; b=nd2mE/eQS06ceRIw89+/ATDN0lsgbkuhKp/0Lmf8p4ihkVqKb3Yv+TThYKsTBNpLdwO+sn FrfxujxZFUF7CpJokYvpBdTlwqlDKi++jAyVmSb5mHd1YJHcreDTpxxJnFaGiIkB48A0/B Elx6W878BBygDXJqBdfEsEbAXFKHaLY= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=e5UibSOr; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf09.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677799013; a=rsa-sha256; cv=none; b=I1nePhIAc72/mFz4IlGwc7AOJl/pv3Di8zNa8y6JydxGBqW5E0kLlnoXjsSBzUS58YNBhw ZcSy/kAJ2+GBBGajnDnIwnsEht/HvDvPnIQTx7/fEe9vdxdBTjfXD8/k2K+1hsORhUVi8e /fo3aYIJ8JrNWhtDByprz1peoPtlNow= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677799012; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1eE9Pafa530L5JjpUU74EsDdw6S9K1gsLvm9PBz92W8=; b=e5UibSOrWGxcxGg2l4E3lQMiSC1vlGR9bBNe4k4Xs0e4w/tEBHZWvaouNgCKh5KXj4bFMG 8Ys+EikQqRfQrcspmFb2sOoqBxSsAyqf0Poi0BA0SIjFAOdw7ZtJGPv/6kveQcpYEK8z1O vUeSrDSMlaDIn9EU6KQSSsgLkSZVI1E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-622-zwFLchJEPOeBqPxHgNImPw-1; Thu, 02 Mar 2023 18:16:47 -0500 X-MC-Unique: zwFLchJEPOeBqPxHgNImPw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 630DB800B23; Thu, 2 Mar 2023 23:16:46 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54038440D9; Thu, 2 Mar 2023 23:16:44 +0000 (UTC) From: David Howells To: Linus Torvalds , Steve French Cc: David Howells , Vishal Moola , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Paulo Alcantara , Jeff Layton , Matthew Wilcox , Marc Dionne , linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French , Andrew Morton , linux-mm@kvack.org Subject: [PATCH 1/3] mm: Add a function to get a single tagged folio from a file Date: Thu, 2 Mar 2023 23:16:36 +0000 Message-Id: <20230302231638.521280-2-dhowells@redhat.com> In-Reply-To: <20230302231638.521280-1-dhowells@redhat.com> References: <20230302231638.521280-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 14F68140011 X-Stat-Signature: s9p7d1nzpfyxzu31brreuw6joojbm77f X-HE-Tag: 1677799012-807018 X-HE-Meta: U2FsdGVkX1/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3NoSYC0WzjM1zCsnz76nA88vjPKSfwfqKWh0btlY6r7YobxnGVSqTgP3tKEOB+ci6apORVtCoj46QvNIrGl3ZgnP5/ZgToTKKErkHSNscvqYmRR8+NSaylH5a0tEmI9nAkBdDnkIadWWS5trMFzjJ5oTyteariY20P/49ZGvSu7IJWof7o6xmyiYHjuWz+sC944keZWHPyULNCtF5CumiwDhQyOuD7d9UuYicwzBqKE3Xsd7XUbb58eUonI8plevF4TT1XewFolWV7y4ROHMnYq8Ev8flWzvYBcw+MUfFLcYqvxZNSCehoSdDej7doyYn6j0Xoc/d+QPcKfGBJSbdJ267d1fvpy3LGYik+q+U9eaFj7E2B5vUzXeugtkA9yJ6lu6bh4tmwCjuSFPouE+lBjfxr8Wriw88AxkcpdsQIw/cY8+RJjW0VowWug162DMaIPfa9jyWwkWgJ+mQgtatqacKNaLodxGkPvL4JNS4+D4H+7KIrAk8nJsA1MbbRm49xqMrhrsiP6dQj/05mkul7abji+rcpDelwUnU8+jYbypK8UIGsMp9BG3wOzBqeAkjczSgXjo8wiVqeJ6hXSttszPcX1cpXeW8GysHDwvcHohOe5NRIho1f6nlbjE4jP96M= 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: Add a function to get a single tagged folio from a file rather than a batch for use in afs and cifs where, in the common case, the batch is likely to be rendered irrelevant by the {afs,cifs}_extend_writeback() function. For filemap_get_folios_tag() to be of use, the batch has to be passed down, and if it contains scattered, non-contiguous folios, these are likely to end up being pinned by the batch for significant periods of time whilst I/O is undertaken on earlier pages. Further, for write_cache_pages() to be useful, it would need to wait for PG_fscache which is used to indicate that I/O is in progress from a folio to the cache - but it can't do this unconditionally as some filesystems, such as btrfs, use PG_private_2 for other purposes. Signed-off-by: David Howells cc: Steve French cc: Linus Torvalds cc: "Vishal Moola (Oracle)" cc: "Matthew Wilcox (Oracle)" cc: Andrew Morton cc: linux-afs@lists.infradead.org cc: linux-cifs@vger.kernel.org cc: linux-mm@kvack.org Link: https://lore.kernel.org/r/2214157.1677250083@warthog.procyon.org.uk/ --- include/linux/pagemap.h | 2 ++ mm/filemap.c | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 0acb8e1fb7af..577535633006 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -741,6 +741,8 @@ unsigned filemap_get_folios_contig(struct address_space *mapping, pgoff_t *start, pgoff_t end, struct folio_batch *fbatch); unsigned filemap_get_folios_tag(struct address_space *mapping, pgoff_t *start, pgoff_t end, xa_mark_t tag, struct folio_batch *fbatch); +struct folio *filemap_get_folio_tag(struct address_space *mapping, pgoff_t *start, + pgoff_t end, xa_mark_t tag); struct page *grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index); diff --git a/mm/filemap.c b/mm/filemap.c index 2723104cc06a..1b1e9c661018 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2339,6 +2339,64 @@ unsigned filemap_get_folios_tag(struct address_space *mapping, pgoff_t *start, } EXPORT_SYMBOL(filemap_get_folios_tag); +/** + * filemap_get_folio_tag - Get the first folio matching @tag + * @mapping: The address_space to search + * @start: The starting page index + * @end: The final page index (inclusive) + * @tag: The tag index + * + * Search for and return the first folios in the mapping starting at index + * @start and up to index @end (inclusive). The folio is returned with an + * elevated reference count. + * + * If a folio is returned, it may start before @start; if it does, it will + * contain @start. The folio may also extend beyond @end; if it does, it will + * contain @end. If folios are added to or removed from the page cache while + * this is running, they may or may not be found by this call. + * + * Return: The folio that was found or NULL. @start is also updated to index + * the next folio for the traversal or will be left pointing after @end. + */ +struct folio *filemap_get_folio_tag(struct address_space *mapping, pgoff_t *start, + pgoff_t end, xa_mark_t tag) +{ + XA_STATE(xas, &mapping->i_pages, *start); + struct folio *folio; + + rcu_read_lock(); + while ((folio = find_get_entry(&xas, end, tag)) != NULL) { + /* + * Shadow entries should never be tagged, but this iteration + * is lockless so there is a window for page reclaim to evict + * a page we saw tagged. Skip over it. + */ + if (xa_is_value(folio)) + continue; + + if (folio_test_hugetlb(folio)) + *start = folio->index + 1; + else + *start = folio_next_index(folio); + goto out; + } + + /* + * We come here when there is no page beyond @end. We take care to not + * overflow the index @start as it confuses some of the callers. This + * breaks the iteration when there is a page at index -1 but that is + * already broke anyway. + */ + if (end == (pgoff_t)-1) + *start = (pgoff_t)-1; + else + *start = end + 1; +out: + rcu_read_unlock(); + return folio; +} +EXPORT_SYMBOL(filemap_get_folio_tag); + /* * CD/DVDs are error prone. When a medium error occurs, the driver may fail * a _large_ part of the i/o request. Imagine the worst scenario: