From patchwork Fri Oct 25 20:39:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13851572 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 EA522D149F3 for ; Fri, 25 Oct 2024 20:42:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C0C96B00B5; Fri, 25 Oct 2024 16:42:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 74A826B00B7; Fri, 25 Oct 2024 16:42:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C42F6B00B8; Fri, 25 Oct 2024 16:42:35 -0400 (EDT) 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 37C856B00B5 for ; Fri, 25 Oct 2024 16:42:35 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B2CCEA0463 for ; Fri, 25 Oct 2024 20:41:58 +0000 (UTC) X-FDA: 82713297408.16.4114673 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 33E33140017 for ; Fri, 25 Oct 2024 20:42:20 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="DHyI/396"; spf=pass (imf23.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.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=1729888900; 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=0eZURDLnq+MZTbZ3wWrkZ7fnKiq2JsO7keHUbxuEUPm0M7JzyzuefX6xHZ82AGlWzXbfzv 1L8Bljlm1NH3DmCdDlH37Xz1928ZGPQUcdgRFjZuH+KUn0redVnA/3OS0PHQWM1zb84hS4 fWxBk+mOK+rwcf1EZZZnQdIR9wXyqHw= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="DHyI/396"; spf=pass (imf23.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.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=1729888900; a=rsa-sha256; cv=none; b=HRsODVuzAG9r9fwYpHP3h0dbn6AglQ711qT9DWaYRSDkgeK9D2vlcRc+wLyuzAoBmnA5MS +eemKV04dkG3LX93mGb0KuH8+5osqZZSv/q+Z5FCZngyTbXcTIKN8QRkzVEXpjw41LQTnb S5ycWaU/lNUu44XYW/qw6xEh9A4A5eo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729888952; 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=DHyI/396MhTePAEvaJYDZ3JPo1FIgNYAAMiyOh530KP/oDZe10KVGfmhhKNBTt8E/b9mEN dmYFsJKdxVgpPo8slfRhsOHmnYmwMzpZHvroS/ct9hYbWjWxF05yYRE51Gb3k871ZFrGVB rWGrFasnkFBtn0WsWIZz0y3zbPLPxLQ= Received: from mx-prod-mc-02.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-383-4854QUjvPuqip3OEMXye1Q-1; Fri, 25 Oct 2024 16:42:29 -0400 X-MC-Unique: 4854QUjvPuqip3OEMXye1Q-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 733CB19560A7; Fri, 25 Oct 2024 20:42:26 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.231]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A50F6196BB7D; Fri, 25 Oct 2024 20:42:21 +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 v2 19/31] netfs: Add functions to build/clean a buffer in a folio_queue Date: Fri, 25 Oct 2024 21:39:46 +0100 Message-ID: <20241025204008.4076565-20-dhowells@redhat.com> In-Reply-To: <20241025204008.4076565-1-dhowells@redhat.com> References: <20241025204008.4076565-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Rspam-User: X-Stat-Signature: mdy4gajprb1pranrt4ndzq9j5gthw1rf X-Rspamd-Queue-Id: 33E33140017 X-Rspamd-Server: rspam11 X-HE-Tag: 1729888940-217876 X-HE-Meta: U2FsdGVkX18iVV7AXjtDDvAcWgJpqN/+FILipx81V3SsAkI2LBv7g4r++11R2I4YFQQWEkegemjc86I2/HavhV5pee3w3plQ9QlMxJD6t7lZba3HrhHhiL5mCkpfR2BquccGqur709zGaBt5N5pzT+sdAYnXhgbaKiWqjX2BtBOLUR9SR6rqLjxKmQZH/VtWyYFVw8AX2W/oCFn6hjdhG81E0jyrJWfiFGhgBwlwIsTlekx9uBK8vAiOpeaXgju5NwrGJiXFpyfv0Bm6ekOKHREHmdh8tO/qRugbXNQuAEQMeSAILoS5P9ff9DXarENsejDgPPKYEb6BO163GgJQzq6qiyC4VhabcmGknM0aNoH58e89zDkG5SDEtyZWBBoiUGtlgQlBlA9pE6GUutm8gh5VgCmgAPG70oQz4vT6gdj1p5/B6kBsCZ0PMHrmwkORblvKRoC9f5IeqmLXwkslRGjVPx/mb1C56xBFCbJ2HrSDtlyGbxUZIR2bDXmfZzSk7+MPGIodk8hCVqgAojJA2+25B+KUvVgeC01MIqFhu05aWRH7k1JyxC2Rn1045SU7B0XmC7lc825llcrZruELi4zm4OKmS5GOYVaAsd6TM67eUT2NiBPzdt53LJvXnLEj9VTObaAW4kfI8tY/lSZMxYbBikvWvdW4BKXlNFRNEXSlVDZiro5zqYjM66hw44RWGEui60uwHrNMh2kziFmisqw/F9VoTUw/ENcuCATHNdyiATGOYwvsm+cDhhJr9H6eyXQHJ3XitA5oVfFWZvxh1I2Eju2ILisnBw7rQLDmEEopVQy94S22xpEvcdwrbqV+yZQhVfy04keoVCXW9pDI59CyjdeCfAtMgpAN+q2DEMkHMoq/H/Os+t1gifUEdn9H8xTv3blP559ikBe6vDsWBNJ4M0Rtlh4J5gKOmb23AI5JmDeixYp6GX2ARtApbv47ufzaKlgwyeq3gpgvoIY 3RfpCe3k buNQPA4i8HCBVZDlb87QBgHe2HDFnWq02w+dhz5j1aTFOGC6c0gt7Ur22PjkizOftsFCN9AD54H7Y3eXfjuN3QUliHn/pxiVwCLHLzeFO5nN/ZmK00kzKCwyo8OQCIf4zI2cvEOaqgXAKXLJrcbAg2uRSPkumJyD1T7pdA0Xmht/uJwxJ/sTcOKEQnC9MvLhBB0TDvde16SFeuzLa7kGQxGzfxtx0/lAtLGAKrU4VMLhgjX2c2FweBinlKrxOBciqgjFmuusld2facZmZ2NOl0dW8k/s/ONYcs24BL7/SlWLm2y2LO5USuw+aY+DNYJl1ghOkY7rsF6TflEsEC4TdVeokSYfDBksAz+WCjnvlwrr1W/wQYVKVilm5FPT/F6FmDhZshvZWBiY6UEnd1Ys1HQA1ccrBBHj7byTmVx0X12ySwgqOMSZUvb1B50fF4yGF7vztgFZNII5l4iLa/ypzx3yi2wVMinzqdLQFg38Afv6DWC7NzsJVcmqmwRkfivY30pOc 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") \