From patchwork Mon Dec 16 20:41:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13910343 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 11993E7717F for ; Mon, 16 Dec 2024 20:44:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 965546B00DA; Mon, 16 Dec 2024 15:44:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 913D66B00DB; Mon, 16 Dec 2024 15:44:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78EDC6B00DC; Mon, 16 Dec 2024 15:44:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5802B6B00DA for ; Mon, 16 Dec 2024 15:44:00 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 02265140200 for ; Mon, 16 Dec 2024 20:43:59 +0000 (UTC) X-FDA: 82901997864.25.12F23D4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 7D12F40014 for ; Mon, 16 Dec 2024 20:43:35 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RFnXnPKE; spf=pass (imf01.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734381815; 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=BzGBXBlASg1kiYXlTSf6RvaOO95d2WWCGxBaSiOw7jc=; b=3Udu2VNg6dw5dmtfEyBRhMOzbHeLiveND8NpTdGCxOvfcsZrevAW1z6pdW1E24vdQZNd/4 kbQS7sY0G/wLtTHdCCsWUQP7JNmU96kpFhLM50zQfnw/IenahBdP6kEAzgUeZgr0WMcb18 EL3y5oIJNf9zJXun2NVZ0BKMUlN4JNM= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RFnXnPKE; spf=pass (imf01.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734381815; a=rsa-sha256; cv=none; b=NZn/LbL1Iwkutey65XUsU96lR50BP2rw3gvk4Qn8p127/eIIZmz0nGK9lh18epML56/K9i uQ5pdBr2W7Mmx3vM8hLeZjidjCoFwtxMbXyky9EyKQ8VWZosY2IxkcH3nz0/saF0kEk7d2 n0tb1PzYOuihOGF+gbZOv2XfyK8i1xM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734381837; 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=BzGBXBlASg1kiYXlTSf6RvaOO95d2WWCGxBaSiOw7jc=; b=RFnXnPKE3ze0jTWf/Tm7zccARXMmSrzbk8oNiMJykB9HawkOyqSVIU4q8UZ8BaaekHBTHg Vx2GvM6C7y7Tgh8YSVqpnF5CQEHsOzgQfV/nfVJjCQnnjPr7KGD9LoaTaS5lcmxFEcVgCP raQ15OpL4nS5k6LGuNDf6uKAWgW80Do= Received: from mx-prod-mc-03.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-678-jA5F1TEfPtuLa31mFpolAQ-1; Mon, 16 Dec 2024 15:43:52 -0500 X-MC-Unique: jA5F1TEfPtuLa31mFpolAQ-1 X-Mimecast-MFC-AGG-ID: jA5F1TEfPtuLa31mFpolAQ 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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0E56B19560B4; Mon, 16 Dec 2024 20:43:49 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.48]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 68BE4300F9B5; Mon, 16 Dec 2024 20:43:42 +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 v5 18/32] netfs: Add functions to build/clean a buffer in a folio_queue Date: Mon, 16 Dec 2024 20:41:08 +0000 Message-ID: <20241216204124.3752367-19-dhowells@redhat.com> In-Reply-To: <20241216204124.3752367-1-dhowells@redhat.com> References: <20241216204124.3752367-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Rspamd-Queue-Id: 7D12F40014 X-Rspamd-Server: rspam12 X-Stat-Signature: d7fkto1oxidtpa1xuz8b6g9ch6yafhtp X-Rspam-User: X-HE-Tag: 1734381815-943060 X-HE-Meta: U2FsdGVkX195QtzCG0FHZbPlfd7JqOrXdagoTunE+ylTBnkS3fzVRM+MMYfWxhkaPrt53egXiJIir/t3WzXAXGASerRPN5XOmJIwVQa5q6zfk5fpBe5Xk8laLnNHjLGdnR30e+qiB0HnBfigtEiX7hZQxCUGZDhEuiHh2ltgMWau2/TK65eT1RYF490iRcplmRVnpw1URqO3HbgRgmvhJMwwWN1tUgYoqgRRtwGpDqe1eP7LFK/87npQNdGhUh23xwuiumk6IPSzJzEP43PQ2RE+plpm2ZFswEMLpLI69aNYxiDQcC5v33bLHNapHsESSCd/CppcIaK6q54p3I+M2QSgXIsq3p6vvjpQ6YmAwh44GS0AMEe13z49NwpqCZFnuZzp4ygl7qEt3BhFyYnAefGW4amDYucjez8WoFmWTFlTE/1Bye2Y9yMD+mLSNiKgedjY+AkzBLN8w6fEHtcQioclpa/ECwnMxSqT9ewwluLJFXdGzG5jEWg4Vir1vSDrucY/8c03Y0/CVmjiMeKDO03K4V2+FB9/eHd5WWqLddA0a9tvoyMt7YWjNiYZHcGGLlBg/CluAOpGBaXHOyh/M63NUqDURn4np5j54rJdDb6+kvs9rmY0b92xP9zbfgIufV6eaBo3C+05Lz5sAYVorGMFMi/lkxurCYExE603CPh9KxTaF/4uQmiyhtitOyOAeTe5m7S0N+34xUWIhJDGvJcv8rKDb5SQGrG+hqit7WnIusJc3AN9ZYWL4HIMaVTw4QXvEGY5Hr/HGiI4z8NMPMLYXuTzXHjsp06NSdK22w2K3xsCWHAVq+/4Kam4f/j4KZquyCQe0flzwyBZvDpqv0IYOLqu2sGpThDfyOe6Ki5v4cu9h9MG1pNVA/mw3ESzivB1/h1yJ8YvsKdGqUG4aTHrLsUSNOmcQd5mMkNt3B5EGvhPv+Lt1z2teJiOc5TsPEypSLEQwZW8igmiKi5 zK5ZhAX6 IZAW830myLnTbk1jdT+GJ98W+D+VnWNTOGlpMMTnOlfl1J4gVscQqweOxIZbDn9irED1LLDTpKkLa/A/SHn8HmhO8gjzktT7IlRlBFqgk/UF238jIYOA9MzMtHRA5XkUDhB8PEESBBlC2+C7YOvD+M+L+2z3LP954JdmioumhzVQS6Kfd6hzbf7MdYJbfa252M9JAWjYtqAuhE+Oa/NBJ3CkfSSx3Gb4HVzg7zFzxZg1NDHXU0oCPvdhYSCahAxJbnNLqM7NbovBZhzevO6NEGELtAh3gRUYMnus8wg6QsEoZhLr/FRttFpcMJ+krywzWmEcfz5jV4kwxxWNSoY81hMHvoatOsPucKMIG/pPw/9Rme0zq2wCwiooNZzVFvdsmrSoFSJE1X70NKIyGZKZ7AZKLxeh+ziwLbN+Nl0V2nDv+Dhkf9K6XymTA2PBKczGfNNy/qNPmbR9DUnvSeVJhvlavq6+XC1Wow4TmFLvf8HUSLdmZI6Jup7OjB147ycSx+AFGqw5lWPQVnpDgWC5PFypd1luD3KHTlbGZ 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 | 96 ++++++++++++++++++++++++++++++++++++ include/linux/netfs.h | 6 +++ include/trace/events/netfs.h | 6 +-- 3 files changed, 104 insertions(+), 4 deletions(-) diff --git a/fs/netfs/misc.c b/fs/netfs/misc.c index 4249715f4171..7099aa07737a 100644 --- a/fs/netfs/misc.c +++ b/fs/netfs/misc.c @@ -8,6 +8,102 @@ #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 374e54beacbe..dd737344cff3 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -457,6 +457,12 @@ struct folio_queue *netfs_folioq_alloc(unsigned int rreq_id, gfp_t gfp, void netfs_folioq_free(struct folio_queue *folioq, unsigned int trace /*enum netfs_trace_folioq*/); +/* 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 02f6e179b7bc..fc237ff23a33 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") \