From patchwork Fri Nov 8 17:32:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13868578 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 03B5DD64070 for ; Fri, 8 Nov 2024 17:35:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 783B96B00A7; Fri, 8 Nov 2024 12:35:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7348A6B00A9; Fri, 8 Nov 2024 12:35:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5AE2A6B00BF; Fri, 8 Nov 2024 12:35:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 3C1006B00A7 for ; Fri, 8 Nov 2024 12:35:20 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DD17CC0945 for ; Fri, 8 Nov 2024 17:35:19 +0000 (UTC) X-FDA: 82763627982.21.DBF4220 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf07.hostedemail.com (Postfix) with ESMTP id 1D7D440022 for ; Fri, 8 Nov 2024 17:34:25 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RHcn6Jdm; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf07.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=1731087233; 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=NCjiDuYPLh8EX/SFjmoUAd9DPkvd6EWcQ/jqW9O8MdE=; b=7ltdgxm0PvC4Kf6SP6P+vvIxogWkSOXO9H17Yi8yW8RQ29UHZEAGj6KjAj4n3L68OPP+Cu OWEmQ5goL8FBxeiMBgeVenpR7apZ22U6tvNZsTeskvw67MSrPifafuFs0hai45XnAyAuzu fqDTlZiliW8SSlJgHtkcLK/j0clnQCo= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RHcn6Jdm; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf07.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=1731087233; a=rsa-sha256; cv=none; b=S05jebkMFcECeX9mHresuD5Otr+4TJATQffrQUQDnj/gQx3fr9zV/2+Mc1J9SpZAmNmGL+ GbNTeMjvc4gabxZyatEZ0bAtlvno/Q+9u0c19JQuyhK9OQvbg2kudAhAemHkNoWjtcNGxE DjO5ED80P12srud+ShtaQchcC4wMW+c= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731087317; 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=NCjiDuYPLh8EX/SFjmoUAd9DPkvd6EWcQ/jqW9O8MdE=; b=RHcn6JdmpNYLVUj/kRFAe0SznavZHav+OhUxM4NvYbPRR4cEd0NXBsWS+A0zofVuk8Ceyx gaxbSN96cr6qrqE0TsA4+ED7ckBIDPVW84cACZE134MD/HI7qWDxuk+6L8Q7xsz5gV70mO 90F6vpKDP2bgm/2oj838dNk1lWHo1RY= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-269-qDKTEzG6PWWaHc7ckoxNVQ-1; Fri, 08 Nov 2024 12:35:12 -0500 X-MC-Unique: qDKTEzG6PWWaHc7ckoxNVQ-1 X-Mimecast-MFC-AGG-ID: qDKTEzG6PWWaHc7ckoxNVQ Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 78DC219560B0; Fri, 8 Nov 2024 17:35:09 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.231]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 09DB9300019E; Fri, 8 Nov 2024 17:35:03 +0000 (UTC) From: David Howells To: Christian Brauner , Steve French , Matthew Wilcox Cc: David Howells , Jeff Layton , Gao Xiang , Dominique Martinet , Marc Dionne , Paulo Alcantara , Shyam Prasad N , Tom Talpey , Eric Van Hensbergen , Ilya Dryomov , netfs@lists.linux.dev, 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-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 20/33] netfs: Add functions to build/clean a buffer in a folio_queue Date: Fri, 8 Nov 2024 17:32:21 +0000 Message-ID: <20241108173236.1382366-21-dhowells@redhat.com> In-Reply-To: <20241108173236.1382366-1-dhowells@redhat.com> References: <20241108173236.1382366-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 1D7D440022 X-Stat-Signature: pfdd349snmhfhhcm95ijh1e9q3djoo4t X-HE-Tag: 1731087265-928105 X-HE-Meta: U2FsdGVkX1+OsXXMG+hu32KMSLm7syDFTn+8ymEyXVSa2fyVvOM/VPwH3UbbkVK0sIoHs6rlbC/oCw27V8PCKVRX4Ik82Tt85BghGlsIW6hoIflLcb2UyJgE/pSlSSOUJeAAzoLMur4FHTjFnlkZoPN0J3AjYtLQw6E5X4yx5thIPZUDhS72utE2oue2KBfiBKCKAZGK+cjuZhh7IlGI78Ys6KgEsai1jx5EagWOtffKKNWWgsadBEDLZeImda4K4xjBVHiqlCwhEYX8YeLXlCEfUsHYPyrll+LyseGlfhNWoO7yp+fRjh2vM/27dtvQoyy03hrd9rIzmiiXg53ONe13vA0sp0BnXj6L5ZhYSpgHrWOSFq+W3EhwtUAAykGdXBbf3Sqk7K4JrDnsRuJlSXJh8qy8WuDCzrOF19+LTDl/UNqgyuTert6q85WOX4UZx6j6dxwecj+018f0dEOqjWX34zImhwLGuRZ56SE0W+VdQUGtVQaGOtalPFKYGwRKs0V3tL4e50ClgTQOg6e2NpQ9avK9bqmMXf6dojR5U2gROYNmJ4f5C6UmfEMUvE/h/G9epq3nCEYSQlKQQK2AOCBXJPi0p4UOwq1ilHOm8Z/y0hclF2vzl6jgx3nc1obuIGRD/gVODRetTW1xbWzMP+rKwZ1v3AQ9VeQiBTQu/AwrXSusX+fsPOibFiOdZBe02DZVxxrT3/eSd2//rMxPCY2lu4dpOcAAyo14PB8I3GXZ04Z9EjbV9lj6lTwZGj0olfmrte/s9heQtE1D98c982PNntCwmWR254YC6IDRHydGP0etqEkACjdmJQa9QhkeCYiZfN4aAnnwtikQtB41u+MrkIqUPfXbiing45GSlZklQdU2+YxMixAqz/TQi0679DZ2nPlpPKRU6tS0ov1lM2oT/vLXl8ISnrYHKPVRxjGLFY7ebwzmAeBGM9zClg9l/mx67MnErBoJCUViyGa aN8r6QY6 lXXZBqCab14ZQedILmVtJ6rWAWVSI3F2oDxSDZ+/eyOCY/KdJTBhKPtOo8bkDBFZsk0sypycVAlmX4Bl/yNqpWcGkYvEeuMKT7sMVtilE6UKdMrDaBAX7AtCbGqYLFsoSZrDsSGz+kYBTVTA736ZuTX1JW1eEsYiGZ0J91odmcjLA9EJ5NPgJ5CC3z0ZBChyemod27/lu7n5s3/1sAxmV1c7R+Y0Lu0d7o/Swy39QZFTsqdZ8BuLwux0hu2ppGKUpPp1e71Vr2bvoWE82ciseSs4tgkXVNS32TfydKc3GNt0HjRfTf34ZS6KMgBKhzhkRbWD3XJhRpC5Miyxmr4nMsz/9gQStQJfccejXYo2njKw1m/Vc9p+ZRKHCJ4+Sj6rkvHH73wAx33ZBxYS0OxvGjhDJRXSJq/MB/QSm/ZngycvWnTokcbMwU7/3CP3neXC2OGZXZkVaUtTMfGqGch87fmaZLffMoXf0+j1pP5F1nUymex3IgAfI8xVyZcqajausSSFvl8VsSstg/ZMs9H2+3WLDaeoJcs2Owxzjb3GIObMCcEAHc9DKP89lPVyrpRn73Ajv/o5UVkQxNeg4opywMSuCvA== 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: List-Subscribe: List-Unsubscribe: Add two netfslib functions to build up or clean up a buffer in a folio_queue. The first, netfs_alloc_folioq_buffer() will add folios to a buffer, extending up at least to the given size. If it can, it will add multipage folios. The folios are optionally have the mapping set and will have the index set according to the distance from the front of the folio queue. The second function will free up a folio queue and put any folios in the queue that have the first mark set. The netfs_folio tracepoint is also altered to cope with folios that have a NULL mapping, and the folios being added/put will have trace lines emitted and will be accounted in the stats. Signed-off-by: David Howells cc: Jeff Layton cc: Marc Dionne cc: netfs@lists.linux.dev cc: linux-afs@lists.infradead.org cc: linux-fsdevel@vger.kernel.org --- fs/netfs/misc.c | 95 ++++++++++++++++++++++++++++++++++++ include/linux/netfs.h | 6 +++ include/trace/events/netfs.h | 6 +-- 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/fs/netfs/misc.c b/fs/netfs/misc.c index 4249715f4171..01a6ba0e2f82 100644 --- a/fs/netfs/misc.c +++ b/fs/netfs/misc.c @@ -8,6 +8,101 @@ #include #include "internal.h" +/** + * netfs_alloc_folioq_buffer - Allocate buffer space into a folio queue + * @mapping: Address space to set on the folio (or NULL). + * @_buffer: Pointer to the folio queue to add to (may point to a NULL; updated). + * @_cur_size: Current size of the buffer (updated). + * @size: Target size of the buffer. + * @gfp: The allocation constraints. + */ +int netfs_alloc_folioq_buffer(struct address_space *mapping, + struct folio_queue **_buffer, + size_t *_cur_size, ssize_t size, gfp_t gfp) +{ + struct folio_queue *tail = *_buffer, *p; + + size = round_up(size, PAGE_SIZE); + if (*_cur_size >= size) + return 0; + + if (tail) + while (tail->next) + tail = tail->next; + + do { + struct folio *folio; + int order = 0, slot; + + if (!tail || folioq_full(tail)) { + p = netfs_folioq_alloc(0, GFP_NOFS, netfs_trace_folioq_alloc_buffer); + if (!p) + return -ENOMEM; + if (tail) { + tail->next = p; + p->prev = tail; + } else { + *_buffer = p; + } + tail = p; + } + + if (size - *_cur_size > PAGE_SIZE) + order = umin(ilog2(size - *_cur_size) - PAGE_SHIFT, + MAX_PAGECACHE_ORDER); + + folio = folio_alloc(gfp, order); + if (!folio && order > 0) + folio = folio_alloc(gfp, 0); + if (!folio) + return -ENOMEM; + + folio->mapping = mapping; + folio->index = *_cur_size / PAGE_SIZE; + trace_netfs_folio(folio, netfs_folio_trace_alloc_buffer); + slot = folioq_append_mark(tail, folio); + *_cur_size += folioq_folio_size(tail, slot); + } while (*_cur_size < size); + + return 0; +} +EXPORT_SYMBOL(netfs_alloc_folioq_buffer); + +/** + * netfs_free_folioq_buffer - Free a folio queue. + * @fq: The start of the folio queue to free + * + * Free up a chain of folio_queues and, if marked, the marked folios they point + * to. + */ +void netfs_free_folioq_buffer(struct folio_queue *fq) +{ + struct folio_queue *next; + struct folio_batch fbatch; + + folio_batch_init(&fbatch); + + for (; fq; fq = next) { + for (int slot = 0; slot < folioq_count(fq); slot++) { + struct folio *folio = folioq_folio(fq, slot); + if (!folio || + !folioq_is_marked(fq, slot)) + continue; + + trace_netfs_folio(folio, netfs_folio_trace_put); + if (folio_batch_add(&fbatch, folio)) + folio_batch_release(&fbatch); + } + + netfs_stat_d(&netfs_n_folioq); + next = fq->next; + kfree(fq); + } + + folio_batch_release(&fbatch); +} +EXPORT_SYMBOL(netfs_free_folioq_buffer); + /* * Reset the subrequest iterator to refer just to the region remaining to be * read. The iterator may or may not have been advanced by socket ops or diff --git a/include/linux/netfs.h b/include/linux/netfs.h index 738c9c8763f0..921cfcfc62f1 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -458,6 +458,12 @@ struct folio_queue *netfs_folioq_alloc(unsigned int rreq_id, gfp_t gfp, void netfs_folioq_free(struct folio_queue *folioq, unsigned int /*enum netfs_trace_folioq*/ trace); +/* Buffer wrangling helpers API. */ +int netfs_alloc_folioq_buffer(struct address_space *mapping, + struct folio_queue **_buffer, + size_t *_cur_size, ssize_t size, gfp_t gfp); +void netfs_free_folioq_buffer(struct folio_queue *fq); + /** * netfs_inode - Get the netfs inode context from the inode * @inode: The inode to query diff --git a/include/trace/events/netfs.h b/include/trace/events/netfs.h index 7c3c866ae183..167c89bc62e0 100644 --- a/include/trace/events/netfs.h +++ b/include/trace/events/netfs.h @@ -155,6 +155,7 @@ EM(netfs_streaming_filled_page, "mod-streamw-f") \ EM(netfs_streaming_cont_filled_page, "mod-streamw-f+") \ EM(netfs_folio_trace_abandon, "abandon") \ + EM(netfs_folio_trace_alloc_buffer, "alloc-buf") \ EM(netfs_folio_trace_cancel_copy, "cancel-copy") \ EM(netfs_folio_trace_cancel_store, "cancel-store") \ EM(netfs_folio_trace_clear, "clear") \ @@ -195,10 +196,7 @@ E_(netfs_trace_donate_to_deferred_next, "defer-next") #define netfs_folioq_traces \ - EM(netfs_trace_folioq_alloc_append_folio, "alloc-apf") \ - EM(netfs_trace_folioq_alloc_read_prep, "alloc-r-prep") \ - EM(netfs_trace_folioq_alloc_read_prime, "alloc-r-prime") \ - EM(netfs_trace_folioq_alloc_read_sing, "alloc-r-sing") \ + EM(netfs_trace_folioq_alloc_buffer, "alloc-buf") \ EM(netfs_trace_folioq_clear, "clear") \ EM(netfs_trace_folioq_delete, "delete") \ EM(netfs_trace_folioq_make_space, "make-space") \