From patchwork Fri Oct 13 16:03:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13421152 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 CA845CDB47E for ; Fri, 13 Oct 2023 16:05:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 627C96B012B; Fri, 13 Oct 2023 12:05:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5D7846B012E; Fri, 13 Oct 2023 12:05:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4787E6B0139; Fri, 13 Oct 2023 12:05:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 33D346B012B for ; Fri, 13 Oct 2023 12:05:17 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D12FA1403D3 for ; Fri, 13 Oct 2023 16:05:16 +0000 (UTC) X-FDA: 81340912632.05.C00BDAF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id C616E20014 for ; Fri, 13 Oct 2023 16:05:13 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="JTnz/fFn"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.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=1697213113; 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=obp1C/bTKivreVyZbMeOEbpW8BryZ03pHgo31ZL3OJs=; b=GiKvP7mJjoCpkM4X3q/QGAJnOO8e5ikMYhaBUQjHzI47uh2i6yyr0zwyNp9d7zqEgwNv7H /9ouSnosc/pXJoyHmj5nJSJQ89zWr3cJjdx7gFlUH9foRqIYkHqeha3d3us/1oyw1zddiX TTjCC3NC57KR5BfqIzfyieuS0y+e3M0= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="JTnz/fFn"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697213113; a=rsa-sha256; cv=none; b=VbGEJrzNFCgR0wN6TQzTYOYQ1Zg6ldiTEMg/0eVTzqTcIffHuH1WnD4BG56NU9aFT0A/GH c9oJYpC3VGxn2TfKt9W3Mbibar+sQFw3zYn+el1jQHq0aLBXYs4mLjZJnXx6XtRdS7nVE5 yPj01jAwEAEacMYJlTtzLvC0nWy3m5M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697213112; 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=obp1C/bTKivreVyZbMeOEbpW8BryZ03pHgo31ZL3OJs=; b=JTnz/fFnz1xSNBSnkASA6++sWaNRMgynQ+0eXO2KARxb4EIOlIJbaNSlga5j4JIccfyNig Y13v8Zp8NZCyM1AISLJtbdfDf8pqHPfKz+RQOVh2WSxrVDI9saCPKBgwFQEdhy4dsFoc5E sMEmD4AOAz+DPhViwE1rVhCD1vSB9ps= 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-173-MyygaxCRPVa_0k68CjzVSg-1; Fri, 13 Oct 2023 12:05:08 -0400 X-MC-Unique: MyygaxCRPVa_0k68CjzVSg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B13E9889067; Fri, 13 Oct 2023 16:05:07 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.226]) by smtp.corp.redhat.com (Postfix) with ESMTP id 33CF025C2; Fri, 13 Oct 2023 16:05:05 +0000 (UTC) From: David Howells To: Jeff Layton , Steve French Cc: David Howells , Matthew Wilcox , Marc Dionne , Paulo Alcantara , Shyam Prasad N , Tom Talpey , Dominique Martinet , Ilya Dryomov , Christian Brauner , linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-cachefs@redhat.com Subject: [RFC PATCH 12/53] netfs: Provide tools to create a buffer in an xarray Date: Fri, 13 Oct 2023 17:03:41 +0100 Message-ID: <20231013160423.2218093-13-dhowells@redhat.com> In-Reply-To: <20231013160423.2218093-1-dhowells@redhat.com> References: <20231013160423.2218093-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Rspam-User: X-Stat-Signature: nbiyjj5fgdmjoq6nizc4ku54grwz9hia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l6p6cnIe9t9dgpfg28GyPo2mW5W4dF/aSD+Y+e32jJ4RcNF5KLqD9WSl13ebuq75Ee8HWqeMWMfxnX1MFCsIYJt2kyuuJvLSPkqJ3rwZZLlfjKAdH3aed3c2GOtjTuY3G+s+c9Bzf6M5NIihysFdDMHrrdPX5evQByjWRhuLY+8OjcCRUDHrceVqTHXx4jPkrfMHw5NdxU/rGz2+KI8M/WczyBTUB4GbZKstwx4+46BeqrGwIsVA5LTw1N7rm9lncr8ONACxqWmjg4e+NmiY/Qek8svSSKb9bvK/T2SjfBBWHfPX9WXVDYMwdKctzBVjYx0SBj6uVSnvhG/4s/0= 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: Provide tools to create a buffer in an xarray, with a function to add new folios with a mark. This will be used to create bounce buffer and can be used more easily to create a list of folios the span of which would require more than a page's worth of bio_vec structs. Signed-off-by: David Howells cc: Jeff Layton cc: linux-cachefs@redhat.com cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org --- fs/netfs/internal.h | 16 +++++ fs/netfs/misc.c | 140 ++++++++++++++++++++++++++++++++++++++++++ include/linux/netfs.h | 4 ++ 3 files changed, 160 insertions(+) diff --git a/fs/netfs/internal.h b/fs/netfs/internal.h index 1f067aa96c50..00e01278316f 100644 --- a/fs/netfs/internal.h +++ b/fs/netfs/internal.h @@ -52,6 +52,22 @@ static inline void netfs_proc_add_rreq(struct netfs_io_request *rreq) {} static inline void netfs_proc_del_rreq(struct netfs_io_request *rreq) {} #endif +/* + * misc.c + */ +int netfs_xa_store_and_mark(struct xarray *xa, unsigned long index, + struct folio *folio, bool put_mark, + bool pagecache_mark, gfp_t gfp_mask); +int netfs_add_folios_to_buffer(struct xarray *buffer, + struct address_space *mapping, + pgoff_t index, pgoff_t to, gfp_t gfp_mask); +int netfs_set_up_buffer(struct xarray *buffer, + struct address_space *mapping, + struct readahead_control *ractl, + struct folio *keep, + pgoff_t have_index, unsigned int have_folios); +void netfs_clear_buffer(struct xarray *buffer); + /* * objects.c */ diff --git a/fs/netfs/misc.c b/fs/netfs/misc.c index c3baf2b247d9..c70f856f3129 100644 --- a/fs/netfs/misc.c +++ b/fs/netfs/misc.c @@ -8,6 +8,146 @@ #include #include "internal.h" +/* + * Attach a folio to the buffer and maybe set marks on it to say that we need + * to put the folio later and twiddle the pagecache flags. + */ +int netfs_xa_store_and_mark(struct xarray *xa, unsigned long index, + struct folio *folio, bool put_mark, + bool pagecache_mark, gfp_t gfp_mask) +{ + XA_STATE_ORDER(xas, xa, index, folio_order(folio)); + +retry: + xas_lock(&xas); + for (;;) { + xas_store(&xas, folio); + if (!xas_error(&xas)) + break; + xas_unlock(&xas); + if (!xas_nomem(&xas, gfp_mask)) + return xas_error(&xas); + goto retry; + } + + if (put_mark) + xas_set_mark(&xas, NETFS_BUF_PUT_MARK); + if (pagecache_mark) + xas_set_mark(&xas, NETFS_BUF_PAGECACHE_MARK); + xas_unlock(&xas); + return xas_error(&xas); +} + +/* + * Create the specified range of folios in the buffer attached to the read + * request. The folios are marked with NETFS_BUF_PUT_MARK so that we know that + * these need freeing later. + */ +int netfs_add_folios_to_buffer(struct xarray *buffer, + struct address_space *mapping, + pgoff_t index, pgoff_t to, gfp_t gfp_mask) +{ + struct folio *folio; + int ret; + + if (to + 1 == index) /* Page range is inclusive */ + return 0; + + do { + /* TODO: Figure out what order folio can be allocated here */ + folio = filemap_alloc_folio(readahead_gfp_mask(mapping), 0); + if (!folio) + return -ENOMEM; + folio->index = index; + ret = netfs_xa_store_and_mark(buffer, index, folio, + true, false, gfp_mask); + if (ret < 0) { + folio_put(folio); + return ret; + } + + index += folio_nr_pages(folio); + } while (index <= to && index != 0); + + return 0; +} + +/* + * Set up a buffer into which to data will be read or decrypted/decompressed. + * The folios to be read into are attached to this buffer and the gaps filled + * in to form a continuous region. + */ +int netfs_set_up_buffer(struct xarray *buffer, + struct address_space *mapping, + struct readahead_control *ractl, + struct folio *keep, + pgoff_t have_index, unsigned int have_folios) +{ + struct folio *folio; + gfp_t gfp_mask = readahead_gfp_mask(mapping); + unsigned int want_folios = have_folios; + pgoff_t want_index = have_index; + int ret; + + ret = netfs_add_folios_to_buffer(buffer, mapping, want_index, + have_index - 1, gfp_mask); + if (ret < 0) + return ret; + have_folios += have_index - want_index; + + ret = netfs_add_folios_to_buffer(buffer, mapping, + have_index + have_folios, + want_index + want_folios - 1, + gfp_mask); + if (ret < 0) + return ret; + + /* Transfer the folios proposed by the VM into the buffer and take refs + * on them. The locks will be dropped in netfs_rreq_unlock(). + */ + if (ractl) { + while ((folio = readahead_folio(ractl))) { + folio_get(folio); + if (folio == keep) + folio_get(folio); + ret = netfs_xa_store_and_mark(buffer, folio->index, folio, + true, true, gfp_mask); + if (ret < 0) { + if (folio != keep) + folio_unlock(folio); + folio_put(folio); + return ret; + } + } + } else { + folio_get(keep); + ret = netfs_xa_store_and_mark(buffer, keep->index, keep, + true, true, gfp_mask); + if (ret < 0) { + folio_put(keep); + return ret; + } + } + return 0; +} + +/* + * Clear an xarray buffer, putting a ref on the folios that have + * NETFS_BUF_PUT_MARK set. + */ +void netfs_clear_buffer(struct xarray *buffer) +{ + struct folio *folio; + XA_STATE(xas, buffer, 0); + + rcu_read_lock(); + xas_for_each_marked(&xas, folio, ULONG_MAX, NETFS_BUF_PUT_MARK) { + folio_put(folio); + } + rcu_read_unlock(); + xa_destroy(buffer); +} + /** * netfs_invalidate_folio - Invalidate or partially invalidate a folio * @folio: Folio proposed for release diff --git a/include/linux/netfs.h b/include/linux/netfs.h index 66479a61ad00..e8d702ac6968 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -109,6 +109,10 @@ static inline int wait_on_page_fscache_killable(struct page *page) return folio_wait_private_2_killable(page_folio(page)); } +/* Marks used on xarray-based buffers */ +#define NETFS_BUF_PUT_MARK XA_MARK_0 /* - Page needs putting */ +#define NETFS_BUF_PAGECACHE_MARK XA_MARK_1 /* - Page needs wb/dirty flag wrangling */ + enum netfs_io_source { NETFS_FILL_WITH_ZEROES, NETFS_DOWNLOAD_FROM_SERVER,