From patchwork Thu Dec 21 13:23:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13501931 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 CF227C35274 for ; Thu, 21 Dec 2023 13:25:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB9246B0087; Thu, 21 Dec 2023 08:25:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A3DFF6B0089; Thu, 21 Dec 2023 08:25:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B7F16B008C; Thu, 21 Dec 2023 08:25:18 -0500 (EST) 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 7415C6B0087 for ; Thu, 21 Dec 2023 08:25:18 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4EEF01A0D42 for ; Thu, 21 Dec 2023 13:25:18 +0000 (UTC) X-FDA: 81590896716.27.5A6B07D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 8EC2220002 for ; Thu, 21 Dec 2023 13:25:16 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RsEkhyzu; spf=pass (imf03.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=1703165116; 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=/UF7ncNYwO6Zmwpre4ch58LPX02xE0uGtdq+3Y5EMaI=; b=Nexy+Ma/GGBfTcCYGTSoInd4zdske8352iQFbvfqq0TtYSSVQqiLaJuRYXDw5uyClz/tFv 9WMsozxylPJOD/1KOoMyO5Sv8hc4HvKB8jweoKoCR1hwRVRAxAYq702CxKc7+zaTSJWDlW JaKuDwll6X+oD9/OK24T5geeaLIIfY4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703165116; a=rsa-sha256; cv=none; b=X7m3ZSwmXdpYFJA6emhT2QtQz1woLyBJDf38xVtkij0R9PrTDXmd8EKM9SIv24y4TCSY/P 7BSWG6Kd6aX4x+nKnXOB1qrDWh2u0bF9W9+WlAtIpRsB/Y+WMTbc6ER+UBO7t0NWe0FSRA RHAu3Q3Xoebh1aIVlIS4ek2ejRuI4Ww= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RsEkhyzu; spf=pass (imf03.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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703165115; 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=/UF7ncNYwO6Zmwpre4ch58LPX02xE0uGtdq+3Y5EMaI=; b=RsEkhyzu6+HKo1SON4WhgELvY1vzed/Ntn8b+pwJaFNJ+wcjoi+cIQzEyTG8mtbD3rh7ZQ fdrp1Ixx+jJHNLHnCvZ4mg+GUZr4MA/V/Da3cC8Ard3O9obaYcQ0BtgUvYFUjFfh9tIFNT 2E4TPc8bi3wDHiiprwajKK9h/jV3wZY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-281-JG3VDTKFNKCTtjQw31ac7Q-1; Thu, 21 Dec 2023 08:25:11 -0500 X-MC-Unique: JG3VDTKFNKCTtjQw31ac7Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9C487280D469; Thu, 21 Dec 2023 13:25:10 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.39.195.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id 735EE40C6EB9; Thu, 21 Dec 2023 13:25:07 +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 , Eric Van Hensbergen , Ilya Dryomov , Christian Brauner , linux-cachefs@redhat.com, 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 Subject: [PATCH v5 17/40] netfs: Add func to calculate pagecount/size-limited span of an iterator Date: Thu, 21 Dec 2023 13:23:12 +0000 Message-ID: <20231221132400.1601991-18-dhowells@redhat.com> In-Reply-To: <20231221132400.1601991-1-dhowells@redhat.com> References: <20231221132400.1601991-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Rspamd-Queue-Id: 8EC2220002 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: ef96556h9ahs58fc4sfkrud9p7ax1uim X-HE-Tag: 1703165116-241283 X-HE-Meta: U2FsdGVkX18OJT1QjeHryu2nD2YbktvYIA0xoy4dZQg+Pq7+395XXsOavboW+lqfh0/cFnQj4wevXvLt2t29qeab4Fmihj1EroHX2+JATUehKgfX51LWeD6OBR4Awe6eUNK9/FoYoeKBiFufXflzn50QAREvmXl51nMNknAhzn8rZx6+kyeGF7kq1/wvBzK01PfDPVq2WvkJwLXrBZsUMNMpRzFezlNAwQ5Y7ib6ZDsOfldgmwpO0LTtLJCD0axGHsB2yDq1QgH6bnQUF+YFZKnnXocMswO0KfUTHskaLNtWuFWpcesJ1IFxLPIt9Lgi/xTkXVBjGexw+mtGoRk4LmDSoQJDCfKCRI3rKHGHSzfOH/+z21KA0QLz6gysmovLR/BkIG9ILWMIxSuLQ90u0844Yrh6FKKZMmiAzHlVEX+vy1SIc9qjOqtHjN7ZdAQatNis2VaDP9GTuuxM5iuz61ZqSDe4MlXyvMSmABFQf627PYnGjbM4zBItQdPH03jFfO+leNPXvFAg86JI8NO7NEywCFsILWFOhN6OUYIEfM+LURUAJf5gY3ngOWthP47XIi3WFK81LtaGDlOA0r6T2/ojws1PIUqadb1rlCTFunzWJGTkozAanN4zgX43ZzaoDczC8iiVCYmylA3rXkX5VnWh4PL2C+wRsRpsJwjVSAdteFEheE3Hhim1vQg3BSTQi8XEDE8uUh09CVsbLKkt9BV18aix3TTGBHbafW5kOvybS8B+zWGX9ghg+HujC5Qv/vhcN7OPFWf3bdBapzFT6ulWoOF2yuvcOXPMDHfRUj+1yj3gWhzZ/vq4YyWG4RrMaQlskuElV89ye/u79KPP7stRanVnjzV10I1CM/sTtgzd6H/8XejVVOfO3GMj+q4VR4OMyg1A2YVc7NtlCgC/SdbTLZh+EibTqcyv7QdHfRwdzyD/+R0WHnuzgWRbUDML9FYtxOKkomveWmo3RP/ VPfE4K1U cUUcvWttcHGsuEuUA+OTUkZdHfGrJc56AaErcV4KM+zweG4de4TVvGPNq9mMrVca+ZyfSVsmAJKk4T8lzvnAjz96eSP3kCUkiVQUm4KsoUtLriqQTek9+6ngL+xKEtghaWaJoAxDbpnJTGLLr2ndEdbaiHOCtQYQd1fq/5I3e2MwO/NQ0DSkH+CqGGbjlX8ElxKNiYN6gLGJ9TsgmwTRC5RKp5//egIvvlXZY+LCyTmIfZVljrk+DmXSs9twxt8hJudWGEeAHIDjonHQ8RLXZNnXzb6FZyNOz7g/u08ScRsOQ6q4= 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 a function to work out how much of an ITER_BVEC or ITER_XARRAY iterator we can use in a pagecount-limited and size-limited span. This will be used, for example, to limit the number of segments in a subrequest to the maximum number of elements that an RDMA transfer can handle. Signed-off-by: David Howells Reviewed-by: Jeff Layton cc: linux-cachefs@redhat.com cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org --- fs/netfs/iterator.c | 97 +++++++++++++++++++++++++++++++++++++++++++ include/linux/netfs.h | 2 + 2 files changed, 99 insertions(+) diff --git a/fs/netfs/iterator.c b/fs/netfs/iterator.c index 2ff07ba655a0..b781bbbf1d8d 100644 --- a/fs/netfs/iterator.c +++ b/fs/netfs/iterator.c @@ -101,3 +101,100 @@ ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len, return npages; } EXPORT_SYMBOL_GPL(netfs_extract_user_iter); + +/* + * Select the span of a bvec iterator we're going to use. Limit it by both maximum + * size and maximum number of segments. Returns the size of the span in bytes. + */ +static size_t netfs_limit_bvec(const struct iov_iter *iter, size_t start_offset, + size_t max_size, size_t max_segs) +{ + const struct bio_vec *bvecs = iter->bvec; + unsigned int nbv = iter->nr_segs, ix = 0, nsegs = 0; + size_t len, span = 0, n = iter->count; + size_t skip = iter->iov_offset + start_offset; + + if (WARN_ON(!iov_iter_is_bvec(iter)) || + WARN_ON(start_offset > n) || + n == 0) + return 0; + + while (n && ix < nbv && skip) { + len = bvecs[ix].bv_len; + if (skip < len) + break; + skip -= len; + n -= len; + ix++; + } + + while (n && ix < nbv) { + len = min3(n, bvecs[ix].bv_len - skip, max_size); + span += len; + nsegs++; + ix++; + if (span >= max_size || nsegs >= max_segs) + break; + skip = 0; + n -= len; + } + + return min(span, max_size); +} + +/* + * Select the span of an xarray iterator we're going to use. Limit it by both + * maximum size and maximum number of segments. It is assumed that segments + * can be larger than a page in size, provided they're physically contiguous. + * Returns the size of the span in bytes. + */ +static size_t netfs_limit_xarray(const struct iov_iter *iter, size_t start_offset, + size_t max_size, size_t max_segs) +{ + struct folio *folio; + unsigned int nsegs = 0; + loff_t pos = iter->xarray_start + iter->iov_offset; + pgoff_t index = pos / PAGE_SIZE; + size_t span = 0, n = iter->count; + + XA_STATE(xas, iter->xarray, index); + + if (WARN_ON(!iov_iter_is_xarray(iter)) || + WARN_ON(start_offset > n) || + n == 0) + return 0; + max_size = min(max_size, n - start_offset); + + rcu_read_lock(); + xas_for_each(&xas, folio, ULONG_MAX) { + size_t offset, flen, len; + if (xas_retry(&xas, folio)) + continue; + if (WARN_ON(xa_is_value(folio))) + break; + if (WARN_ON(folio_test_hugetlb(folio))) + break; + + flen = folio_size(folio); + offset = offset_in_folio(folio, pos); + len = min(max_size, flen - offset); + span += len; + nsegs++; + if (span >= max_size || nsegs >= max_segs) + break; + } + + rcu_read_unlock(); + return min(span, max_size); +} + +size_t netfs_limit_iter(const struct iov_iter *iter, size_t start_offset, + size_t max_size, size_t max_segs) +{ + if (iov_iter_is_bvec(iter)) + return netfs_limit_bvec(iter, start_offset, max_size, max_segs); + if (iov_iter_is_xarray(iter)) + return netfs_limit_xarray(iter, start_offset, max_size, max_segs); + BUG(); +} +EXPORT_SYMBOL(netfs_limit_iter); diff --git a/include/linux/netfs.h b/include/linux/netfs.h index c05150f51beb..41d438e5d6db 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -322,6 +322,8 @@ void netfs_put_subrequest(struct netfs_io_subrequest *subreq, ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len, struct iov_iter *new, iov_iter_extraction_t extraction_flags); +size_t netfs_limit_iter(const struct iov_iter *iter, size_t start_offset, + size_t max_size, size_t max_segs); int netfs_start_io_read(struct inode *inode); void netfs_end_io_read(struct inode *inode);