From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B641EC6379F for ; Fri, 10 Feb 2023 23:33:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229779AbjBJXdD (ORCPT ); Fri, 10 Feb 2023 18:33:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229768AbjBJXdB (ORCPT ); Fri, 10 Feb 2023 18:33:01 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF63573944 for ; Fri, 10 Feb 2023 15:32:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071935; 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=3Y4j0lnR/W/3vRUqeGPawzcVqSHyHS0pEsYo/MQFDo8=; b=KcxpVSrFvVZKvNLSphov8MktzSUCgHuv7OdvFeibATYUZyD2+N12N0FXtZLUYcASXw5az3 /rTHtVuebkmq5WyB0NDCDOy9MSJZo1a+d35ZE3OFwqwmHXHreOuC1YA76Xe7MnxLclxSWE RyzkwY9jusYFhTnJp1H0VOfDB1W/PSA= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-390-DaL9rAFoNj-iA1lSkOSrcg-1; Fri, 10 Feb 2023 18:32:12 -0500 X-MC-Unique: DaL9rAFoNj-iA1lSkOSrcg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 37C9C281722E; Fri, 10 Feb 2023 23:32:12 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7FA93400DB1E; Fri, 10 Feb 2023 23:32:10 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French , linux-cachefs@redhat.com Subject: [PATCH 01/11] netfs: Add a function to extract a UBUF or IOVEC into a BVEC iterator Date: Fri, 10 Feb 2023 23:31:55 +0000 Message-Id: <20230210233205.1517459-2-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a function to extract the pages from a user-space supplied iterator (UBUF- or IOVEC-type) into a BVEC-type iterator, retaining the pages by getting a pin on them (as FOLL_PIN) as we go. This is useful in three situations: (1) A userspace thread may have a sibling that unmaps or remaps the process's VM during the operation, changing the assignment of the pages and potentially causing an error. Retaining the pages keeps some pages around, even if this occurs; futher, we find out at the point of extraction if EFAULT is going to be incurred. (2) Pages might get swapped out/discarded if not retained, so we want to retain them to avoid the reload causing a deadlock due to a DIO from/to an mmapped region on the same file. (3) The iterator may get passed to sendmsg() by the filesystem. If a fault occurs, we may get a short write to a TCP stream that's then tricky to recover from. We don't deal with other types of iterator here, leaving it to other mechanisms to retain the pages (eg. PG_locked, PG_writeback and the pipe lock). Signed-off-by: David Howells cc: Jeff Layton cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: linux-cachefs@redhat.com cc: linux-cifs@vger.kernel.org cc: linux-fsdevel@vger.kernel.org --- fs/netfs/Makefile | 1 + fs/netfs/iterator.c | 103 ++++++++++++++++++++++++++++++++++++++++++ include/linux/netfs.h | 4 ++ 3 files changed, 108 insertions(+) create mode 100644 fs/netfs/iterator.c diff --git a/fs/netfs/Makefile b/fs/netfs/Makefile index f684c0cd1ec5..386d6fb92793 100644 --- a/fs/netfs/Makefile +++ b/fs/netfs/Makefile @@ -3,6 +3,7 @@ netfs-y :=3D \ buffered_read.o \ io.o \ + iterator.o \ main.o \ objects.o =20 diff --git a/fs/netfs/iterator.c b/fs/netfs/iterator.c new file mode 100644 index 000000000000..6f0d79080abc --- /dev/null +++ b/fs/netfs/iterator.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Iterator helpers. + * + * Copyright (C) 2022 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#include +#include +#include +#include +#include "internal.h" + +/** + * netfs_extract_user_iter - Extract the pages from a user iterator into a= bvec + * @orig: The original iterator + * @orig_len: The amount of iterator to copy + * @new: The iterator to be set up + * @extraction_flags: Flags to qualify the request + * + * Extract the page fragments from the given amount of the source iterator= and + * build up a second iterator that refers to all of those bits. This allo= ws + * the original iterator to disposed of. + * + * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-pee= r DMA be + * allowed on the pages extracted. + * + * On success, the number of elements in the bvec is returned, the original + * iterator will have been advanced by the amount extracted. + * + * The iov_iter_extract_mode() function should be used to query how cleanup + * should be performed. + */ +ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len, + struct iov_iter *new, + iov_iter_extraction_t extraction_flags) +{ + struct bio_vec *bv =3D NULL; + struct page **pages; + unsigned int cur_npages; + unsigned int max_pages; + unsigned int npages =3D 0; + unsigned int i; + ssize_t ret; + size_t count =3D orig_len, offset, len; + size_t bv_size, pg_size; + + if (WARN_ON_ONCE(!iter_is_ubuf(orig) && !iter_is_iovec(orig))) + return -EIO; + + max_pages =3D iov_iter_npages(orig, INT_MAX); + bv_size =3D array_size(max_pages, sizeof(*bv)); + bv =3D kvmalloc(bv_size, GFP_KERNEL); + if (!bv) + return -ENOMEM; + + /* Put the page list at the end of the bvec list storage. bvec + * elements are larger than page pointers, so as long as we work + * 0->last, we should be fine. + */ + pg_size =3D array_size(max_pages, sizeof(*pages)); + pages =3D (void *)bv + bv_size - pg_size; + + while (count && npages < max_pages) { + ret =3D iov_iter_extract_pages(orig, &pages, count, + max_pages - npages, extraction_flags, + &offset); + if (ret < 0) { + pr_err("Couldn't get user pages (rc=3D%zd)\n", ret); + break; + } + + if (ret > count) { + pr_err("get_pages rc=3D%zd more than %zu\n", ret, count); + break; + } + + count -=3D ret; + ret +=3D offset; + cur_npages =3D DIV_ROUND_UP(ret, PAGE_SIZE); + + if (npages + cur_npages > max_pages) { + pr_err("Out of bvec array capacity (%u vs %u)\n", + npages + cur_npages, max_pages); + break; + } + + for (i =3D 0; i < cur_npages; i++) { + len =3D ret > PAGE_SIZE ? PAGE_SIZE : ret; + bv[npages + i].bv_page =3D *pages++; + bv[npages + i].bv_offset =3D offset; + bv[npages + i].bv_len =3D len - offset; + ret -=3D len; + offset =3D 0; + } + + npages +=3D cur_npages; + } + + iov_iter_bvec(new, orig->data_source, bv, npages, orig_len - count); + return npages; +} +EXPORT_SYMBOL_GPL(netfs_extract_user_iter); diff --git a/include/linux/netfs.h b/include/linux/netfs.h index 4c76ddfb6a67..b11a84f6c32b 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -17,6 +17,7 @@ #include #include #include +#include =20 enum netfs_sreq_ref_trace; =20 @@ -296,6 +297,9 @@ void netfs_get_subrequest(struct netfs_io_subrequest *s= ubreq, void netfs_put_subrequest(struct netfs_io_subrequest *subreq, bool was_async, enum netfs_sreq_ref_trace what); void netfs_stats_show(struct seq_file *); +ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len, + struct iov_iter *new, + iov_iter_extraction_t extraction_flags); =20 /** * netfs_inode - Get the netfs inode context from the inode From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A97CC05027 for ; Fri, 10 Feb 2023 23:33:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229798AbjBJXds (ORCPT ); Fri, 10 Feb 2023 18:33:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229786AbjBJXdq (ORCPT ); Fri, 10 Feb 2023 18:33:46 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 976FD74061 for ; Fri, 10 Feb 2023 15:32:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071940; 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=RRYgxBpJE8iQKg7kW8huRpIoVRfk9pDx8+CAW946aQ8=; b=Gv0hGd70ofJom8OeRMUU7dtKORBFLMcmO4l7ja70Zfj66gMLhCb2ZrLVt0PG5oD+N2Q2ab bXh9W87jXCIt3aDa2C4PCOH1AaOZyx15gN9/5z9aM0W3qj//3+iH1or1PcQvwQ/tb+T37T jMP/WcCbREU0ZgFaOLpMSQ7Mdw3c9x4= 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-471-OxZAJiSgNG2F1tY3fuQpFQ-1; Fri, 10 Feb 2023 18:32:15 -0500 X-MC-Unique: OxZAJiSgNG2F1tY3fuQpFQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ABD6718A63EB; Fri, 10 Feb 2023 23:32:14 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECCAB2026D4B; Fri, 10 Feb 2023 23:32:12 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French , linux-cachefs@redhat.com Subject: [PATCH 02/11] netfs: Add a function to extract an iterator into a scatterlist Date: Fri, 10 Feb 2023 23:31:56 +0000 Message-Id: <20230210233205.1517459-3-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Provide a function for filling in a scatterlist from the list of pages contained in an iterator. If the iterator is UBUF- or IOBUF-type, the pages have a pin taken on them (as FOLL_PIN). If the iterator is BVEC-, KVEC- or XARRAY-type, no pin is taken on the pages and it is left to the caller to manage their lifetime. It cannot be assumed that a ref can be validly taken, particularly in the case of a KVEC iterator. Signed-off-by: David Howells cc: Jeff Layton cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: linux-cachefs@redhat.com cc: linux-cifs@vger.kernel.org cc: linux-fsdevel@vger.kernel.org --- fs/netfs/iterator.c | 269 ++++++++++++++++++++++++++++++++++++++++++ include/linux/netfs.h | 4 + mm/vmalloc.c | 1 + 3 files changed, 274 insertions(+) diff --git a/fs/netfs/iterator.c b/fs/netfs/iterator.c index 6f0d79080abc..49e674846b78 100644 --- a/fs/netfs/iterator.c +++ b/fs/netfs/iterator.c @@ -7,7 +7,9 @@ =20 #include #include +#include #include +#include #include #include "internal.h" =20 @@ -101,3 +103,270 @@ ssize_t netfs_extract_user_iter(struct iov_iter *orig= , size_t orig_len, return npages; } EXPORT_SYMBOL_GPL(netfs_extract_user_iter); + +/* + * Extract and pin a list of up to sg_max pages from UBUF- or IOVEC-class + * iterators, and add them to the scatterlist. + */ +static ssize_t netfs_extract_user_to_sg(struct iov_iter *iter, + ssize_t maxsize, + struct sg_table *sgtable, + unsigned int sg_max, + iov_iter_extraction_t extraction_flags) +{ + struct scatterlist *sg =3D sgtable->sgl + sgtable->nents; + struct page **pages; + unsigned int npages; + ssize_t ret =3D 0, res; + size_t len, off; + + /* We decant the page list into the tail of the scatterlist */ + pages =3D (void *)sgtable->sgl + array_size(sg_max, sizeof(struct scatter= list)); + pages -=3D sg_max; + + do { + res =3D iov_iter_extract_pages(iter, &pages, maxsize, sg_max, + extraction_flags, &off); + if (res < 0) + goto failed; + + len =3D res; + maxsize -=3D len; + ret +=3D len; + npages =3D DIV_ROUND_UP(off + len, PAGE_SIZE); + sg_max -=3D npages; + + for (; npages < 0; npages--) { + struct page *page =3D *pages; + size_t seg =3D min_t(size_t, PAGE_SIZE - off, len); + + *pages++ =3D NULL; + sg_set_page(sg, page, len, off); + sgtable->nents++; + sg++; + len -=3D seg; + off =3D 0; + } + } while (maxsize > 0 && sg_max > 0); + + return ret; + +failed: + while (sgtable->nents > sgtable->orig_nents) + put_page(sg_page(&sgtable->sgl[--sgtable->nents])); + return res; +} + +/* + * Extract up to sg_max pages from a BVEC-type iterator and add them to the + * scatterlist. The pages are not pinned. + */ +static ssize_t netfs_extract_bvec_to_sg(struct iov_iter *iter, + ssize_t maxsize, + struct sg_table *sgtable, + unsigned int sg_max, + iov_iter_extraction_t extraction_flags) +{ + const struct bio_vec *bv =3D iter->bvec; + struct scatterlist *sg =3D sgtable->sgl + sgtable->nents; + unsigned long start =3D iter->iov_offset; + unsigned int i; + ssize_t ret =3D 0; + + for (i =3D 0; i < iter->nr_segs; i++) { + size_t off, len; + + len =3D bv[i].bv_len; + if (start >=3D len) { + start -=3D len; + continue; + } + + len =3D min_t(size_t, maxsize, len - start); + off =3D bv[i].bv_offset + start; + + sg_set_page(sg, bv[i].bv_page, len, off); + sgtable->nents++; + sg++; + sg_max--; + + ret +=3D len; + maxsize -=3D len; + if (maxsize <=3D 0 || sg_max =3D=3D 0) + break; + start =3D 0; + } + + if (ret > 0) + iov_iter_advance(iter, ret); + return ret; +} + +/* + * Extract up to sg_max pages from a KVEC-type iterator and add them to the + * scatterlist. This can deal with vmalloc'd buffers as well as kmalloc'd= or + * static buffers. The pages are not pinned. + */ +static ssize_t netfs_extract_kvec_to_sg(struct iov_iter *iter, + ssize_t maxsize, + struct sg_table *sgtable, + unsigned int sg_max, + iov_iter_extraction_t extraction_flags) +{ + const struct kvec *kv =3D iter->kvec; + struct scatterlist *sg =3D sgtable->sgl + sgtable->nents; + unsigned long start =3D iter->iov_offset; + unsigned int i; + ssize_t ret =3D 0; + + for (i =3D 0; i < iter->nr_segs; i++) { + struct page *page; + unsigned long kaddr; + size_t off, len, seg; + + len =3D kv[i].iov_len; + if (start >=3D len) { + start -=3D len; + continue; + } + + kaddr =3D (unsigned long)kv[i].iov_base + start; + off =3D kaddr & ~PAGE_MASK; + len =3D min_t(size_t, maxsize, len - start); + kaddr &=3D PAGE_MASK; + + maxsize -=3D len; + ret +=3D len; + do { + seg =3D min_t(size_t, len, PAGE_SIZE - off); + if (is_vmalloc_or_module_addr((void *)kaddr)) + page =3D vmalloc_to_page((void *)kaddr); + else + page =3D virt_to_page(kaddr); + + sg_set_page(sg, page, len, off); + sgtable->nents++; + sg++; + sg_max--; + + len -=3D seg; + kaddr +=3D PAGE_SIZE; + off =3D 0; + } while (len > 0 && sg_max > 0); + + if (maxsize <=3D 0 || sg_max =3D=3D 0) + break; + start =3D 0; + } + + if (ret > 0) + iov_iter_advance(iter, ret); + return ret; +} + +/* + * Extract up to sg_max folios from an XARRAY-type iterator and add them to + * the scatterlist. The pages are not pinned. + */ +static ssize_t netfs_extract_xarray_to_sg(struct iov_iter *iter, + ssize_t maxsize, + struct sg_table *sgtable, + unsigned int sg_max, + iov_iter_extraction_t extraction_flags) +{ + struct scatterlist *sg =3D sgtable->sgl + sgtable->nents; + struct xarray *xa =3D iter->xarray; + struct folio *folio; + loff_t start =3D iter->xarray_start + iter->iov_offset; + pgoff_t index =3D start / PAGE_SIZE; + ssize_t ret =3D 0; + size_t offset, len; + XA_STATE(xas, xa, index); + + rcu_read_lock(); + + xas_for_each(&xas, folio, ULONG_MAX) { + if (xas_retry(&xas, folio)) + continue; + if (WARN_ON(xa_is_value(folio))) + break; + if (WARN_ON(folio_test_hugetlb(folio))) + break; + + offset =3D offset_in_folio(folio, start); + len =3D min_t(size_t, maxsize, folio_size(folio) - offset); + + sg_set_page(sg, folio_page(folio, 0), len, offset); + sgtable->nents++; + sg++; + sg_max--; + + maxsize -=3D len; + ret +=3D len; + if (maxsize <=3D 0 || sg_max =3D=3D 0) + break; + } + + rcu_read_unlock(); + if (ret > 0) + iov_iter_advance(iter, ret); + return ret; +} + +/** + * netfs_extract_iter_to_sg - Extract pages from an iterator and add ot an= sglist + * @iter: The iterator to extract from + * @maxsize: The amount of iterator to copy + * @sgtable: The scatterlist table to fill in + * @sg_max: Maximum number of elements in @sgtable that may be filled + * @extraction_flags: Flags to qualify the request + * + * Extract the page fragments from the given amount of the source iterator= and + * add them to a scatterlist that refers to all of those bits, to a maximum + * addition of @sg_max elements. + * + * The pages referred to by UBUF- and IOVEC-type iterators are extracted a= nd + * pinned; BVEC-, KVEC- and XARRAY-type are extracted but aren't pinned; P= IPE- + * and DISCARD-type are not supported. + * + * No end mark is placed on the scatterlist; that's left to the caller. + * + * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-pee= r DMA + * be allowed on the pages extracted. + * + * If successul, @sgtable->nents is updated to include the number of eleme= nts + * added and the number of bytes added is returned. @sgtable->orig_nents = is + * left unaltered. + * + * The iov_iter_extract_mode() function should be used to query how cleanup + * should be performed. + */ +ssize_t netfs_extract_iter_to_sg(struct iov_iter *iter, size_t maxsize, + struct sg_table *sgtable, unsigned int sg_max, + iov_iter_extraction_t extraction_flags) +{ + if (maxsize =3D=3D 0) + return 0; + + switch (iov_iter_type(iter)) { + case ITER_UBUF: + case ITER_IOVEC: + return netfs_extract_user_to_sg(iter, maxsize, sgtable, sg_max, + extraction_flags); + case ITER_BVEC: + return netfs_extract_bvec_to_sg(iter, maxsize, sgtable, sg_max, + extraction_flags); + case ITER_KVEC: + return netfs_extract_kvec_to_sg(iter, maxsize, sgtable, sg_max, + extraction_flags); + case ITER_XARRAY: + return netfs_extract_xarray_to_sg(iter, maxsize, sgtable, sg_max, + extraction_flags); + default: + pr_err("netfs_extract_iter_to_sg(%u) unsupported\n", + iov_iter_type(iter)); + WARN_ON_ONCE(1); + return -EIO; + } +} +EXPORT_SYMBOL_GPL(netfs_extract_iter_to_sg); diff --git a/include/linux/netfs.h b/include/linux/netfs.h index b11a84f6c32b..a1f3522daa69 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -300,6 +300,10 @@ void netfs_stats_show(struct seq_file *); ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len, struct iov_iter *new, iov_iter_extraction_t extraction_flags); +struct sg_table; +ssize_t netfs_extract_iter_to_sg(struct iov_iter *iter, size_t len, + struct sg_table *sgtable, unsigned int sg_max, + iov_iter_extraction_t extraction_flags); =20 /** * netfs_inode - Get the netfs inode context from the inode diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ca71de7c9d77..61f5bec0f2b6 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -656,6 +656,7 @@ int is_vmalloc_or_module_addr(const void *x) #endif return is_vmalloc_addr(x); } +EXPORT_SYMBOL_GPL(is_vmalloc_or_module_addr); =20 /* * Walk a vmap address to the struct page it maps. Huge vmap mappings will From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F1F4C6379F for ; Fri, 10 Feb 2023 23:33:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229827AbjBJXdx (ORCPT ); Fri, 10 Feb 2023 18:33:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229804AbjBJXdv (ORCPT ); Fri, 10 Feb 2023 18:33:51 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A777574063 for ; Fri, 10 Feb 2023 15:32:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071940; 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=e8eQEU7H5KI+mR6DaJwt19VwGfbirTBzTPLMp2PHuNY=; b=LKG8QOOPEKzhnV5lFUCaoRkX4v3AojqACoPY5Dg6EqS0sm0txsIT8fmLCyeO+tOAl8vL9q Wrr8Ox6q6F8ejZw9G0T6R2IX47Da1BD1G+PwXkoyFfY28IydZzLJCupajt4cfJrzd5wTlr 66DYVb7RdH1YBEIhnUulM+9pV9aKaVE= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-169-7PeJLcj5NDuyFTWYoFrm6A-1; Fri, 10 Feb 2023 18:32:17 -0500 X-MC-Unique: 7PeJLcj5NDuyFTWYoFrm6A-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ED6121C05142; Fri, 10 Feb 2023 23:32:16 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 46DFD1121315; Fri, 10 Feb 2023 23:32:15 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French , linux-rdma@vger.kernel.org Subject: [PATCH 03/11] cifs: Add a function to build an RDMA SGE list from an iterator Date: Fri, 10 Feb 2023 23:31:57 +0000 Message-Id: <20230210233205.1517459-4-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a function to add elements onto an RDMA SGE list representing page fragments extracted from a BVEC-, KVEC- or XARRAY-type iterator and DMA mapped until the maximum number of elements is reached. Nothing is done to make sure the pages remain present - that must be done by the caller. Signed-off-by: David Howells cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: Tom Talpey cc: Jeff Layton cc: linux-cifs@vger.kernel.org cc: linux-fsdevel@vger.kernel.org cc: linux-rdma@vger.kernel.org Link: https://lore.kernel.org/r/166697256704.61150.17388516338310645808.stg= it@warthog.procyon.org.uk/ # rfc Link: https://lore.kernel.org/r/166732028840.3186319.8512284239779728860.st= git@warthog.procyon.org.uk/ # rfc --- fs/cifs/smbdirect.c | 214 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) diff --git a/fs/cifs/smbdirect.c b/fs/cifs/smbdirect.c index 8c816b25ce7c..3e0aacddc291 100644 --- a/fs/cifs/smbdirect.c +++ b/fs/cifs/smbdirect.c @@ -44,6 +44,17 @@ static int smbd_post_send_page(struct smbd_connection *i= nfo, static void destroy_mr_list(struct smbd_connection *info); static int allocate_mr_list(struct smbd_connection *info); =20 +struct smb_extract_to_rdma { + struct ib_sge *sge; + unsigned int nr_sge; + unsigned int max_sge; + struct ib_device *device; + u32 local_dma_lkey; + enum dma_data_direction direction; +}; +static ssize_t smb_extract_iter_to_rdma(struct iov_iter *iter, size_t len, + struct smb_extract_to_rdma *rdma); + /* SMBD version number */ #define SMBD_V1 0x0100 =20 @@ -2490,3 +2501,206 @@ int smbd_deregister_mr(struct smbd_mr *smbdirect_mr) =20 return rc; } + +static bool smb_set_sge(struct smb_extract_to_rdma *rdma, + struct page *lowest_page, size_t off, size_t len) +{ + struct ib_sge *sge =3D &rdma->sge[rdma->nr_sge]; + u64 addr; + + addr =3D ib_dma_map_page(rdma->device, lowest_page, + off, len, rdma->direction); + if (ib_dma_mapping_error(rdma->device, addr)) + return false; + + sge->addr =3D addr; + sge->length =3D len; + sge->lkey =3D rdma->local_dma_lkey; + rdma->nr_sge++; + return true; +} + +/* + * Extract page fragments from a BVEC-class iterator and add them to an RD= MA + * element list. The pages are not pinned. + */ +static ssize_t smb_extract_bvec_to_rdma(struct iov_iter *iter, + struct smb_extract_to_rdma *rdma, + ssize_t maxsize) +{ + const struct bio_vec *bv =3D iter->bvec; + unsigned long start =3D iter->iov_offset; + unsigned int i; + ssize_t ret =3D 0; + + for (i =3D 0; i < iter->nr_segs; i++) { + size_t off, len; + + len =3D bv[i].bv_len; + if (start >=3D len) { + start -=3D len; + continue; + } + + len =3D min_t(size_t, maxsize, len - start); + off =3D bv[i].bv_offset + start; + + if (!smb_set_sge(rdma, bv[i].bv_page, off, len)) + return -EIO; + + ret +=3D len; + maxsize -=3D len; + if (rdma->nr_sge >=3D rdma->max_sge || maxsize <=3D 0) + break; + start =3D 0; + } + + return ret; +} + +/* + * Extract fragments from a KVEC-class iterator and add them to an RDMA li= st. + * This can deal with vmalloc'd buffers as well as kmalloc'd or static buf= fers. + * The pages are not pinned. + */ +static ssize_t smb_extract_kvec_to_rdma(struct iov_iter *iter, + struct smb_extract_to_rdma *rdma, + ssize_t maxsize) +{ + const struct kvec *kv =3D iter->kvec; + unsigned long start =3D iter->iov_offset; + unsigned int i; + ssize_t ret =3D 0; + + for (i =3D 0; i < iter->nr_segs; i++) { + struct page *page; + unsigned long kaddr; + size_t off, len, seg; + + len =3D kv[i].iov_len; + if (start >=3D len) { + start -=3D len; + continue; + } + + kaddr =3D (unsigned long)kv[i].iov_base + start; + off =3D kaddr & ~PAGE_MASK; + len =3D min_t(size_t, maxsize, len - start); + kaddr &=3D PAGE_MASK; + + maxsize -=3D len; + do { + seg =3D min_t(size_t, len, PAGE_SIZE - off); + + if (is_vmalloc_or_module_addr((void *)kaddr)) + page =3D vmalloc_to_page((void *)kaddr); + else + page =3D virt_to_page(kaddr); + + if (!smb_set_sge(rdma, page, off, seg)) + return -EIO; + + ret +=3D seg; + len -=3D seg; + kaddr +=3D PAGE_SIZE; + off =3D 0; + } while (len > 0 && rdma->nr_sge < rdma->max_sge); + + if (rdma->nr_sge >=3D rdma->max_sge || maxsize <=3D 0) + break; + start =3D 0; + } + + return ret; +} + +/* + * Extract folio fragments from an XARRAY-class iterator and add them to an + * RDMA list. The folios are not pinned. + */ +static ssize_t smb_extract_xarray_to_rdma(struct iov_iter *iter, + struct smb_extract_to_rdma *rdma, + ssize_t maxsize) +{ + struct xarray *xa =3D iter->xarray; + struct folio *folio; + loff_t start =3D iter->xarray_start + iter->iov_offset; + pgoff_t index =3D start / PAGE_SIZE; + ssize_t ret =3D 0; + size_t off, len; + XA_STATE(xas, xa, index); + + rcu_read_lock(); + + xas_for_each(&xas, folio, ULONG_MAX) { + if (xas_retry(&xas, folio)) + continue; + if (WARN_ON(xa_is_value(folio))) + break; + if (WARN_ON(folio_test_hugetlb(folio))) + break; + + off =3D offset_in_folio(folio, start); + len =3D min_t(size_t, maxsize, folio_size(folio) - off); + + if (!smb_set_sge(rdma, folio_page(folio, 0), off, len)) { + rcu_read_lock(); + return -EIO; + } + + maxsize -=3D len; + ret +=3D len; + if (rdma->nr_sge >=3D rdma->max_sge || maxsize <=3D 0) + break; + } + + rcu_read_unlock(); + return ret; +} + +/* + * Extract page fragments from up to the given amount of the source iterat= or + * and build up an RDMA list that refers to all of those bits. The RDMA l= ist + * is appended to, up to the maximum number of elements set in the paramet= er + * block. + * + * The extracted page fragments are not pinned or ref'd in any way; if an + * IOVEC/UBUF-type iterator is to be used, it should be converted to a + * BVEC-type iterator and the pages pinned, ref'd or otherwise held in some + * way. + */ +static ssize_t smb_extract_iter_to_rdma(struct iov_iter *iter, size_t len, + struct smb_extract_to_rdma *rdma) +{ + ssize_t ret; + int before =3D rdma->nr_sge; + + switch (iov_iter_type(iter)) { + case ITER_BVEC: + ret =3D smb_extract_bvec_to_rdma(iter, rdma, len); + break; + case ITER_KVEC: + ret =3D smb_extract_kvec_to_rdma(iter, rdma, len); + break; + case ITER_XARRAY: + ret =3D smb_extract_xarray_to_rdma(iter, rdma, len); + break; + default: + WARN_ON_ONCE(1); + return -EIO; + } + + if (ret > 0) { + iov_iter_advance(iter, ret); + } else if (ret < 0) { + while (rdma->nr_sge > before) { + struct ib_sge *sge =3D &rdma->sge[rdma->nr_sge--]; + + ib_dma_unmap_single(rdma->device, sge->addr, sge->length, + rdma->direction); + sge->addr =3D 0; + } + } + + return ret; +} From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DAD92C05027 for ; Fri, 10 Feb 2023 23:34:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229841AbjBJXee (ORCPT ); Fri, 10 Feb 2023 18:34:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229869AbjBJXeW (ORCPT ); Fri, 10 Feb 2023 18:34:22 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71B487406B for ; Fri, 10 Feb 2023 15:32:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071943; 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=jlR4JcCg1XC8eTF3I+rHcc7NxJf4yOAtTyxChrY5i+Y=; b=LkVUUvNZVcID1nuaE6TSvsxH288eEfJR+V7f/6G1qAdU2ghkky/GK7r4r/IVjyKuevLJg1 wOqYi+gylO+SZxYotJlMrpXbivb9VqlInTWA/vYunRvGrTeak8prxSnbwtLFnPmAsXvOhX /E0a/AoKDWKDqhkXCWKT90Dm16MD/gQ= 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-91-ZvPK5vUbNbORrETn_rtiuw-1; Fri, 10 Feb 2023 18:32:20 -0500 X-MC-Unique: ZvPK5vUbNbORrETn_rtiuw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 42D3885A588; Fri, 10 Feb 2023 23:32:19 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 908CF2026D4B; Fri, 10 Feb 2023 23:32:17 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French , linux-crypto@vger.kernel.org Subject: [PATCH 04/11] cifs: Add a function to Hash the contents of an iterator Date: Fri, 10 Feb 2023 23:31:58 +0000 Message-Id: <20230210233205.1517459-5-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a function to push the contents of a BVEC-, KVEC- or XARRAY-type iterator into a symmetric hash algorithm. UBUF- and IOBUF-type iterators are not supported on the assumption that either we're doing buffered I/O, in which case we won't see them, or we're doing direct I/O, in which case the iterator will have been extracted into a BVEC-type iterator higher up. Signed-off-by: David Howells cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: Jeff Layton cc: linux-cifs@vger.kernel.org cc: linux-fsdevel@vger.kernel.org cc: linux-crypto@vger.kernel.org Link: https://lore.kernel.org/r/166697257423.61150.12070648579830206483.stg= it@warthog.procyon.org.uk/ # rfc Link: https://lore.kernel.org/r/166732029577.3186319.17162612653237909961.s= tgit@warthog.procyon.org.uk/ # rfc --- fs/cifs/cifsencrypt.c | 144 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/fs/cifs/cifsencrypt.c b/fs/cifs/cifsencrypt.c index cbc18b4a9cb2..7be589aeb520 100644 --- a/fs/cifs/cifsencrypt.c +++ b/fs/cifs/cifsencrypt.c @@ -24,6 +24,150 @@ #include "../smbfs_common/arc4.h" #include =20 +/* + * Hash data from a BVEC-type iterator. + */ +static int cifs_shash_bvec(const struct iov_iter *iter, ssize_t maxsize, + struct shash_desc *shash) +{ + const struct bio_vec *bv =3D iter->bvec; + unsigned long start =3D iter->iov_offset; + unsigned int i; + void *p; + int ret; + + for (i =3D 0; i < iter->nr_segs; i++) { + size_t off, len; + + len =3D bv[i].bv_len; + if (start >=3D len) { + start -=3D len; + continue; + } + + len =3D min_t(size_t, maxsize, len - start); + off =3D bv[i].bv_offset + start; + + p =3D kmap_local_page(bv[i].bv_page); + ret =3D crypto_shash_update(shash, p + off, len); + kunmap_local(p); + if (ret < 0) + return ret; + + maxsize -=3D len; + if (maxsize <=3D 0) + break; + start =3D 0; + } + + return 0; +} + +/* + * Hash data from a KVEC-type iterator. + */ +static int cifs_shash_kvec(const struct iov_iter *iter, ssize_t maxsize, + struct shash_desc *shash) +{ + const struct kvec *kv =3D iter->kvec; + unsigned long start =3D iter->iov_offset; + unsigned int i; + int ret; + + for (i =3D 0; i < iter->nr_segs; i++) { + size_t len; + + len =3D kv[i].iov_len; + if (start >=3D len) { + start -=3D len; + continue; + } + + len =3D min_t(size_t, maxsize, len - start); + ret =3D crypto_shash_update(shash, kv[i].iov_base + start, len); + if (ret < 0) + return ret; + maxsize -=3D len; + + if (maxsize <=3D 0) + break; + start =3D 0; + } + + return 0; +} + +/* + * Hash data from an XARRAY-type iterator. + */ +static ssize_t cifs_shash_xarray(const struct iov_iter *iter, ssize_t maxs= ize, + struct shash_desc *shash) +{ + struct folio *folios[16], *folio; + unsigned int nr, i, j, npages; + loff_t start =3D iter->xarray_start + iter->iov_offset; + pgoff_t last, index =3D start / PAGE_SIZE; + ssize_t ret =3D 0; + size_t len, offset, foffset; + void *p; + + if (maxsize =3D=3D 0) + return 0; + + last =3D (start + maxsize - 1) / PAGE_SIZE; + do { + nr =3D xa_extract(iter->xarray, (void **)folios, index, last, + ARRAY_SIZE(folios), XA_PRESENT); + if (nr =3D=3D 0) + return -EIO; + + for (i =3D 0; i < nr; i++) { + folio =3D folios[i]; + npages =3D folio_nr_pages(folio); + foffset =3D start - folio_pos(folio); + offset =3D foffset % PAGE_SIZE; + for (j =3D foffset / PAGE_SIZE; j < npages; j++) { + len =3D min_t(size_t, maxsize, PAGE_SIZE - offset); + p =3D kmap_local_page(folio_page(folio, j)); + ret =3D crypto_shash_update(shash, p, len); + kunmap_local(p); + if (ret < 0) + return ret; + maxsize -=3D len; + if (maxsize <=3D 0) + return 0; + start +=3D len; + offset =3D 0; + index++; + } + } + } while (nr =3D=3D ARRAY_SIZE(folios)); + return 0; +} + +/* + * Pass the data from an iterator into a hash. + */ +static int cifs_shash_iter(const struct iov_iter *iter, size_t maxsize, + struct shash_desc *shash) +{ + if (maxsize =3D=3D 0) + return 0; + + switch (iov_iter_type(iter)) { + case ITER_BVEC: + return cifs_shash_bvec(iter, maxsize, shash); + case ITER_KVEC: + return cifs_shash_kvec(iter, maxsize, shash); + case ITER_XARRAY: + return cifs_shash_xarray(iter, maxsize, shash); + default: + pr_err("cifs_shash_iter(%u) unsupported\n", iov_iter_type(iter)); + WARN_ON_ONCE(1); + return -EIO; + } +} + int __cifs_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server, char *signature, struct shash_desc *shash) From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75CB9C6379F for ; Fri, 10 Feb 2023 23:35:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229940AbjBJXfA (ORCPT ); Fri, 10 Feb 2023 18:35:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229914AbjBJXeg (ORCPT ); Fri, 10 Feb 2023 18:34:36 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55277749A8 for ; Fri, 10 Feb 2023 15:32:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071947; 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=cihn7w3U8zLzw9HrEZ9lSsz+pbyA21ip+Ry4w/y1Gs8=; b=R6+4ZkBeO4gLI3oQsmi8GRZcI0k7fwMJfqOCXsVw/0FStG6PFRDA55EIGSj0+dwLrrSULn zUh5JrLuD/Qz4S5YFj6Y+GKbHs12x82sUrMvBQnupznCbvpV6Q0i0qtIY3TQvCPBceJ3ce fYTETL3ycdGJ8w5drRLOLVec1im54UM= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-463-92rx6RdIP922-0ZhEO-VTg-1; Fri, 10 Feb 2023 18:32:22 -0500 X-MC-Unique: 92rx6RdIP922-0ZhEO-VTg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A3FC13806111; Fri, 10 Feb 2023 23:32:21 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 02ADD140EBF4; Fri, 10 Feb 2023 23:32:19 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French Subject: [PATCH 05/11] cifs: Add some helper functions Date: Fri, 10 Feb 2023 23:31:59 +0000 Message-Id: <20230210233205.1517459-6-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add some helper functions to manipulate the folio marks by iterating through a list of folios held in an xarray rather than using a page list. Signed-off-by: David Howells cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: Jeff Layton cc: linux-cifs@vger.kernel.org Link: https://lore.kernel.org/r/164928616583.457102.15157033997163988344.st= git@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165211418840.3154751.3090684430628501879.st= git@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165348878940.2106726.204291614267188735.stg= it@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165364825674.3334034.3356201708659748648.st= git@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/166126394799.708021.10637797063862600488.st= git@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/166697258147.61150.9940790486999562110.stgi= t@warthog.procyon.org.uk/ # rfc Link: https://lore.kernel.org/r/166732030314.3186319.9209944805565413627.st= git@warthog.procyon.org.uk/ # rfc --- fs/cifs/cifsfs.h | 3 ++ fs/cifs/file.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/fs/cifs/cifsfs.h b/fs/cifs/cifsfs.h index 63a0ac2b9355..492d6f47a751 100644 --- a/fs/cifs/cifsfs.h +++ b/fs/cifs/cifsfs.h @@ -110,6 +110,9 @@ extern int cifs_file_strict_mmap(struct file *file, str= uct vm_area_struct *vma); extern const struct file_operations cifs_dir_ops; extern int cifs_dir_open(struct inode *inode, struct file *file); extern int cifs_readdir(struct file *file, struct dir_context *ctx); +extern void cifs_pages_written_back(struct inode *inode, loff_t start, uns= igned int len); +extern void cifs_pages_write_failed(struct inode *inode, loff_t start, uns= igned int len); +extern void cifs_pages_write_redirty(struct inode *inode, loff_t start, un= signed int len); =20 /* Functions related to dir entries */ extern const struct dentry_operations cifs_dentry_ops; diff --git a/fs/cifs/file.c b/fs/cifs/file.c index 1c3b4ed1de94..5421aa5e35cc 100644 --- a/fs/cifs/file.c +++ b/fs/cifs/file.c @@ -36,6 +36,99 @@ #include "cifs_ioctl.h" #include "cached_dir.h" =20 +/* + * Completion of write to server. + */ +void cifs_pages_written_back(struct inode *inode, loff_t start, unsigned i= nt len) +{ + struct address_space *mapping =3D inode->i_mapping; + struct folio *folio; + pgoff_t end; + + XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); + + if (!len) + return; + + rcu_read_lock(); + + end =3D (start + len - 1) / PAGE_SIZE; + xas_for_each(&xas, folio, end) { + if (!folio_test_writeback(folio)) { + WARN_ONCE(1, "bad %x @%llx page %lx %lx\n", + len, start, folio_index(folio), end); + continue; + } + + folio_detach_private(folio); + folio_end_writeback(folio); + } + + rcu_read_unlock(); +} + +/* + * Failure of write to server. + */ +void cifs_pages_write_failed(struct inode *inode, loff_t start, unsigned i= nt len) +{ + struct address_space *mapping =3D inode->i_mapping; + struct folio *folio; + pgoff_t end; + + XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); + + if (!len) + return; + + rcu_read_lock(); + + end =3D (start + len - 1) / PAGE_SIZE; + xas_for_each(&xas, folio, end) { + if (!folio_test_writeback(folio)) { + WARN_ONCE(1, "bad %x @%llx page %lx %lx\n", + len, start, folio_index(folio), end); + continue; + } + + folio_set_error(folio); + folio_end_writeback(folio); + } + + rcu_read_unlock(); +} + +/* + * Redirty pages after a temporary failure. + */ +void cifs_pages_write_redirty(struct inode *inode, loff_t start, unsigned = int len) +{ + struct address_space *mapping =3D inode->i_mapping; + struct folio *folio; + pgoff_t end; + + XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); + + if (!len) + return; + + rcu_read_lock(); + + end =3D (start + len - 1) / PAGE_SIZE; + xas_for_each(&xas, folio, end) { + if (!folio_test_writeback(folio)) { + WARN_ONCE(1, "bad %x @%llx page %lx %lx\n", + len, start, folio_index(folio), end); + continue; + } + + filemap_dirty_folio(folio->mapping, folio); + folio_end_writeback(folio); + } + + rcu_read_unlock(); +} + /* * Mark as invalid, all open files on tree connections since they * were closed when session to server was lost. From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 959CCC05027 for ; Fri, 10 Feb 2023 23:34:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229905AbjBJXes (ORCPT ); Fri, 10 Feb 2023 18:34:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229865AbjBJXec (ORCPT ); Fri, 10 Feb 2023 18:34:32 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89F2B75F68 for ; Fri, 10 Feb 2023 15:32:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071949; 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=ZbR1ERx60swPmHLlvc4O0e0pHhwkZikBmUg0OVIfTvo=; b=OXCxjSn55A5gnE5eaeJMhXiAnQquwJmc6Q4uXcS2qVorF395pqiZcbhOABlKtZNEFdrN+g hUrog8QMgWAzsWqeJEc1zCuBcXw3ywgRXlp6SDlt/cPbjld9+mMdDBtkuCYXwSDMYU+no7 xI4fZBsqvONkupc6FR+00OghKX+un+I= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-70-GqZrSFZvN8esdIMG-wCbww-1; Fri, 10 Feb 2023 18:32:24 -0500 X-MC-Unique: GqZrSFZvN8esdIMG-wCbww-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D09E738060FE; Fri, 10 Feb 2023 23:32:23 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 44BD340398A0; Fri, 10 Feb 2023 23:32:22 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French Subject: [PATCH 06/11] cifs: Add a function to read into an iter from a socket Date: Fri, 10 Feb 2023 23:32:00 +0000 Message-Id: <20230210233205.1517459-7-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a helper function to read data from a socket into the given iterator. Signed-off-by: David Howells cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: Jeff Layton cc: linux-cifs@vger.kernel.org Link: https://lore.kernel.org/r/164928617874.457102.10021662143234315566.st= git@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165211419563.3154751.18431990381145195050.s= tgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165348879662.2106726.16881134187242702351.s= tgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165364826398.3334034.12541600783145647319.s= tgit@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/166126395495.708021.12328677373159554478.st= git@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/166697258876.61150.3530237818849429372.stgi= t@warthog.procyon.org.uk/ # rfc Link: https://lore.kernel.org/r/166732031039.3186319.10691316510079412635.s= tgit@warthog.procyon.org.uk/ # rfc --- fs/cifs/cifsproto.h | 3 +++ fs/cifs/connect.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/fs/cifs/cifsproto.h b/fs/cifs/cifsproto.h index 1207b39686fb..cb7a3fe89278 100644 --- a/fs/cifs/cifsproto.h +++ b/fs/cifs/cifsproto.h @@ -244,6 +244,9 @@ extern int cifs_read_page_from_socket(struct TCP_Server= _Info *server, struct page *page, unsigned int page_offset, unsigned int to_read); +int cifs_read_iter_from_socket(struct TCP_Server_Info *server, + struct iov_iter *iter, + unsigned int to_read); extern int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb); void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx); int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx); diff --git a/fs/cifs/connect.c b/fs/cifs/connect.c index b2a04b4e89a5..152b457b849f 100644 --- a/fs/cifs/connect.c +++ b/fs/cifs/connect.c @@ -765,6 +765,20 @@ cifs_read_page_from_socket(struct TCP_Server_Info *ser= ver, struct page *page, return cifs_readv_from_socket(server, &smb_msg); } =20 +int +cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter= *iter, + unsigned int to_read) +{ + struct msghdr smb_msg =3D { .msg_iter =3D *iter }; + int ret; + + iov_iter_truncate(&smb_msg.msg_iter, to_read); + ret =3D cifs_readv_from_socket(server, &smb_msg); + if (ret > 0) + iov_iter_advance(iter, ret); + return ret; +} + static bool is_smb_response(struct TCP_Server_Info *server, unsigned char type) { From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C044C05027 for ; Fri, 10 Feb 2023 23:34:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229967AbjBJXe6 (ORCPT ); Fri, 10 Feb 2023 18:34:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229828AbjBJXeg (ORCPT ); Fri, 10 Feb 2023 18:34:36 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 652EA78D5C for ; Fri, 10 Feb 2023 15:32:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071951; 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=WQ76YOfJvrsg0wKv9zouswTYcO+Yehs+YRCDNkeW4tw=; b=Jh8VHecsKMHBpVDAXvM6sa7Tz03PKlO7ap3jAcLKu1vKVcL634ECkPvgdTq7G8uuP5aF15 bZkbuoX+pS+zhpac47dDvVN7W//TfUSwkNxn32zs3sxvenNCdpNu4zE8KZOtMf8U4ki0Sy m7/1sTAwi8o2HDo5rlx4LDY2sS/JWag= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-659-ZwVin6t7NfOhMXXOIgl2lg-1; Fri, 10 Feb 2023 18:32:27 -0500 X-MC-Unique: ZwVin6t7NfOhMXXOIgl2lg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9EE4E3C025BA; Fri, 10 Feb 2023 23:32:26 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 737832166B29; Fri, 10 Feb 2023 23:32:24 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French , Paulo Alcantara Subject: [PATCH 07/11] cifs: Change the I/O paths to use an iterator rather than a page list Date: Fri, 10 Feb 2023 23:32:01 +0000 Message-Id: <20230210233205.1517459-8-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently, the cifs I/O paths hand lists of pages from the VM interface routines at the top all the way through the intervening layers to the socket interface at the bottom. This is a problem, however, for interfacing with netfslib which passes an iterator through to the ->issue_read() method (and will pass an iterator through to the ->issue_write() method in future). Netfslib takes over bounce buffering for direct I/O, async I/O and encrypted content, so cifs doesn't need to do that. Netfslib also converts IOVEC-type iterators into BVEC-type iterators if necessary. Further, cifs needs foliating - and folios may come in a variety of sizes, so a page list pointing to an array of heterogeneous pages may cause problems in places such as where crypto is done. Change the cifs I/O paths to hand iov_iter iterators all the way through instead. Notes: (1) Some old routines are #if'd out to be removed in a follow up patch so as to avoid confusing diff, thereby making the diff output easier to follow. I've removed functions that don't overlap with anything added. (2) struct smb_rqst loses rq_pages, rq_offset, rq_npages, rq_pagesz and rq_tailsz which describe the pages forming the buffer; instead there's an rq_iter describing the source buffer and an rq_buffer which is used to hold the buffer for encryption. (3) struct cifs_readdata and cifs_writedata are similarly modified to smb_rqst. The ->read_into_pages() and ->copy_into_pages() are then replaced with passing the iterator directly to the socket. The iterators are stored in these structs so that they are persistent and don't get deallocated when the function returns (unlike if they were stack variables). (4) Buffered writeback is overhauled, borrowing the code from the afs filesystem to gather up contiguous runs of folios. The XARRAY-type iterator is then used to refer directly to the pagecache and can be passed to the socket to transmit data directly from there. This includes: cifs_extend_writeback() cifs_write_back_from_locked_folio() cifs_writepages_region() cifs_writepages() (5) Pages are converted to folios. (6) Direct I/O uses netfs_extract_user_iter() to create a BVEC-type iterator from an IOBUF/UBUF-type source iterator. (7) smb2_get_aead_req() uses netfs_extract_iter_to_sg() to extract page fragments from the iterator into the scatterlists that the crypto layer prefers. (8) smb2_init_transform_rq() attached pages to smb_rqst::rq_buffer, an xarray, to use as a bounce buffer for encryption. An XARRAY-type iterator can then be used to pass the bounce buffer to lower layers. Signed-off-by: David Howells cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: Paulo Alcantara cc: Jeff Layton cc: linux-cifs@vger.kernel.org Link: https://lore.kernel.org/r/164311907995.2806745.400147335497304099.stg= it@warthog.procyon.org.uk/ # rfc Link: https://lore.kernel.org/r/164928620163.457102.11602306234438271112.st= git@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165211420279.3154751.15923591172438186144.s= tgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165348880385.2106726.3220789453472800240.st= git@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165364827111.3334034.934805882842932881.stg= it@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/166126396180.708021.271013668175370826.stgi= t@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/166697259595.61150.5982032408321852414.stgi= t@warthog.procyon.org.uk/ # rfc Link: https://lore.kernel.org/r/166732031756.3186319.12528413619888902872.s= tgit@warthog.procyon.org.uk/ # rfc --- fs/cifs/Kconfig | 1 + fs/cifs/cifsencrypt.c | 28 +- fs/cifs/cifsglob.h | 66 +-- fs/cifs/cifsproto.h | 8 +- fs/cifs/cifssmb.c | 15 +- fs/cifs/file.c | 1191 ++++++++++++++++++++++++++--------------- fs/cifs/fscache.c | 22 +- fs/cifs/fscache.h | 10 +- fs/cifs/misc.c | 128 +---- fs/cifs/smb2ops.c | 362 ++++++------- fs/cifs/smb2pdu.c | 53 +- fs/cifs/smbdirect.c | 262 ++++----- fs/cifs/smbdirect.h | 4 +- fs/cifs/transport.c | 54 +- 14 files changed, 1122 insertions(+), 1082 deletions(-) diff --git a/fs/cifs/Kconfig b/fs/cifs/Kconfig index bbf63a9eb927..4c0d53bf931a 100644 --- a/fs/cifs/Kconfig +++ b/fs/cifs/Kconfig @@ -18,6 +18,7 @@ config CIFS select DNS_RESOLVER select ASN1 select OID_REGISTRY + select NETFS_SUPPORT help This is the client VFS module for the SMB3 family of network file protocols (including the most recent, most secure dialect SMB3.1.1). diff --git a/fs/cifs/cifsencrypt.c b/fs/cifs/cifsencrypt.c index 7be589aeb520..357bd27a7fd1 100644 --- a/fs/cifs/cifsencrypt.c +++ b/fs/cifs/cifsencrypt.c @@ -169,11 +169,11 @@ static int cifs_shash_iter(const struct iov_iter *ite= r, size_t maxsize, } =20 int __cifs_calc_signature(struct smb_rqst *rqst, - struct TCP_Server_Info *server, char *signature, - struct shash_desc *shash) + struct TCP_Server_Info *server, char *signature, + struct shash_desc *shash) { int i; - int rc; + ssize_t rc; struct kvec *iov =3D rqst->rq_iov; int n_vec =3D rqst->rq_nvec; =20 @@ -205,25 +205,9 @@ int __cifs_calc_signature(struct smb_rqst *rqst, } } =20 - /* now hash over the rq_pages array */ - for (i =3D 0; i < rqst->rq_npages; i++) { - void *kaddr; - unsigned int len, offset; - - rqst_page_get_length(rqst, i, &len, &offset); - - kaddr =3D (char *) kmap(rqst->rq_pages[i]) + offset; - - rc =3D crypto_shash_update(shash, kaddr, len); - if (rc) { - cifs_dbg(VFS, "%s: Could not update with payload\n", - __func__); - kunmap(rqst->rq_pages[i]); - return rc; - } - - kunmap(rqst->rq_pages[i]); - } + rc =3D cifs_shash_iter(&rqst->rq_iter, iov_iter_count(&rqst->rq_iter), sh= ash); + if (rc < 0) + return rc; =20 rc =3D crypto_shash_final(shash, signature); if (rc) diff --git a/fs/cifs/cifsglob.h b/fs/cifs/cifsglob.h index 1d893bea4723..893c2e21eb8e 100644 --- a/fs/cifs/cifsglob.h +++ b/fs/cifs/cifsglob.h @@ -216,11 +216,9 @@ static inline void cifs_free_open_info(struct cifs_ope= n_info_data *data) struct smb_rqst { struct kvec *rq_iov; /* array of kvecs */ unsigned int rq_nvec; /* number of kvecs in array */ - struct page **rq_pages; /* pointer to array of page ptrs */ - unsigned int rq_offset; /* the offset to the 1st page */ - unsigned int rq_npages; /* number pages in array */ - unsigned int rq_pagesz; /* page size to use */ - unsigned int rq_tailsz; /* length of last page */ + size_t rq_iter_size; /* Amount of data in ->rq_iter */ + struct iov_iter rq_iter; /* Data iterator */ + struct xarray rq_buffer; /* Page buffer for encryption */ }; =20 struct mid_q_entry; @@ -1428,10 +1426,11 @@ struct cifs_aio_ctx { struct cifsFileInfo *cfile; struct bio_vec *bv; loff_t pos; - unsigned int npages; + unsigned int nr_pinned_pages; ssize_t rc; unsigned int len; unsigned int total_len; + unsigned int bv_need_unpin; /* If ->bv[] needs unpinning */ bool should_dirty; /* * Indicates if this aio_ctx is for direct_io, @@ -1449,28 +1448,18 @@ struct cifs_readdata { struct address_space *mapping; struct cifs_aio_ctx *ctx; __u64 offset; + ssize_t got_bytes; unsigned int bytes; - unsigned int got_bytes; pid_t pid; int result; struct work_struct work; - int (*read_into_pages)(struct TCP_Server_Info *server, - struct cifs_readdata *rdata, - unsigned int len); - int (*copy_into_pages)(struct TCP_Server_Info *server, - struct cifs_readdata *rdata, - struct iov_iter *iter); + struct iov_iter iter; struct kvec iov[2]; struct TCP_Server_Info *server; #ifdef CONFIG_CIFS_SMB_DIRECT struct smbd_mr *mr; #endif - unsigned int pagesz; - unsigned int page_offset; - unsigned int tailsz; struct cifs_credits credits; - unsigned int nr_pages; - struct page **pages; }; =20 /* asynchronous write support */ @@ -1482,6 +1471,8 @@ struct cifs_writedata { struct work_struct work; struct cifsFileInfo *cfile; struct cifs_aio_ctx *ctx; + struct iov_iter iter; + struct bio_vec *bv; __u64 offset; pid_t pid; unsigned int bytes; @@ -1490,12 +1481,7 @@ struct cifs_writedata { #ifdef CONFIG_CIFS_SMB_DIRECT struct smbd_mr *mr; #endif - unsigned int pagesz; - unsigned int page_offset; - unsigned int tailsz; struct cifs_credits credits; - unsigned int nr_pages; - struct page **pages; }; =20 /* @@ -2155,9 +2141,9 @@ static inline void move_cifs_info_to_smb2(struct smb2= _file_all_info *dst, const dst->FileNameLength =3D src->FileNameLength; } =20 -static inline unsigned int cifs_get_num_sgs(const struct smb_rqst *rqst, - int num_rqst, - const u8 *sig) +static inline int cifs_get_num_sgs(const struct smb_rqst *rqst, + int num_rqst, + const u8 *sig) { unsigned int len, skip; unsigned int nents =3D 0; @@ -2177,6 +2163,19 @@ static inline unsigned int cifs_get_num_sgs(const st= ruct smb_rqst *rqst, * rqst[1+].rq_iov[0+] data to be encrypted/decrypted */ for (i =3D 0; i < num_rqst; i++) { + /* We really don't want a mixture of pinned and unpinned pages + * in the sglist. It's hard to keep track of which is what. + * Instead, we convert to a BVEC-type iterator higher up. + */ + if (WARN_ON_ONCE(user_backed_iter(&rqst[i].rq_iter))) + return -EIO; + + /* We also don't want to have any extra refs or pins to clean + * up in the sglist. + */ + if (WARN_ON_ONCE(iov_iter_extract_will_pin(&rqst[i].rq_iter))) + return -EIO; + for (j =3D 0; j < rqst[i].rq_nvec; j++) { struct kvec *iov =3D &rqst[i].rq_iov[j]; =20 @@ -2190,7 +2189,7 @@ static inline unsigned int cifs_get_num_sgs(const str= uct smb_rqst *rqst, } skip =3D 0; } - nents +=3D rqst[i].rq_npages; + nents +=3D iov_iter_npages(&rqst[i].rq_iter, INT_MAX); } nents +=3D DIV_ROUND_UP(offset_in_page(sig) + SMB2_SIGNATURE_SIZE, PAGE_S= IZE); return nents; @@ -2199,9 +2198,9 @@ static inline unsigned int cifs_get_num_sgs(const str= uct smb_rqst *rqst, /* We can not use the normal sg_set_buf() as we will sometimes pass a * stack object as buf. */ -static inline struct scatterlist *cifs_sg_set_buf(struct scatterlist *sg, - const void *buf, - unsigned int buflen) +static inline void cifs_sg_set_buf(struct sg_table *sgtable, + const void *buf, + unsigned int buflen) { unsigned long addr =3D (unsigned long)buf; unsigned int off =3D offset_in_page(addr); @@ -2211,16 +2210,17 @@ static inline struct scatterlist *cifs_sg_set_buf(s= truct scatterlist *sg, do { unsigned int len =3D min_t(unsigned int, buflen, PAGE_SIZE - off); =20 - sg_set_page(sg++, vmalloc_to_page((void *)addr), len, off); + sg_set_page(&sgtable->sgl[sgtable->nents++], + vmalloc_to_page((void *)addr), len, off); =20 off =3D 0; addr +=3D PAGE_SIZE; buflen -=3D len; } while (buflen); } else { - sg_set_page(sg++, virt_to_page(addr), buflen, off); + sg_set_page(&sgtable->sgl[sgtable->nents++], + virt_to_page(addr), buflen, off); } - return sg; } =20 #endif /* _CIFS_GLOB_H */ diff --git a/fs/cifs/cifsproto.h b/fs/cifs/cifsproto.h index cb7a3fe89278..2873f68a051c 100644 --- a/fs/cifs/cifsproto.h +++ b/fs/cifs/cifsproto.h @@ -584,10 +584,7 @@ int cifs_readv_receive(struct TCP_Server_Info *server,= struct mid_q_entry *mid); int cifs_async_writev(struct cifs_writedata *wdata, void (*release)(struct kref *kref)); void cifs_writev_complete(struct work_struct *work); -struct cifs_writedata *cifs_writedata_alloc(unsigned int nr_pages, - work_func_t complete); -struct cifs_writedata *cifs_writedata_direct_alloc(struct page **pages, - work_func_t complete); +struct cifs_writedata *cifs_writedata_alloc(work_func_t complete); void cifs_writedata_release(struct kref *refcount); int cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon, struct cifs_sb_info *cifs_sb, @@ -604,13 +601,10 @@ enum securityEnum cifs_select_sectype(struct TCP_Serv= er_Info *, enum securityEnum); struct cifs_aio_ctx *cifs_aio_ctx_alloc(void); void cifs_aio_ctx_release(struct kref *refcount); -int setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, in= t rw); =20 int cifs_alloc_hash(const char *name, struct shash_desc **sdesc); void cifs_free_hash(struct shash_desc **sdesc); =20 -void rqst_page_get_length(const struct smb_rqst *rqst, unsigned int page, - unsigned int *len, unsigned int *offset); struct cifs_chan * cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server); int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses= *ses); diff --git a/fs/cifs/cifssmb.c b/fs/cifs/cifssmb.c index 23f10e0d6e7e..e6c44a742bf3 100644 --- a/fs/cifs/cifssmb.c +++ b/fs/cifs/cifssmb.c @@ -24,6 +24,7 @@ #include #include #include "cifspdu.h" +#include "cifsfs.h" #include "cifsglob.h" #include "cifsacl.h" #include "cifsproto.h" @@ -1294,11 +1295,8 @@ cifs_readv_callback(struct mid_q_entry *mid) struct TCP_Server_Info *server =3D tcon->ses->server; struct smb_rqst rqst =3D { .rq_iov =3D rdata->iov, .rq_nvec =3D 2, - .rq_pages =3D rdata->pages, - .rq_offset =3D rdata->page_offset, - .rq_npages =3D rdata->nr_pages, - .rq_pagesz =3D rdata->pagesz, - .rq_tailsz =3D rdata->tailsz }; + .rq_iter_size =3D iov_iter_count(&rdata->iter), + .rq_iter =3D rdata->iter }; struct cifs_credits credits =3D { .value =3D 1, .instance =3D 0 }; =20 cifs_dbg(FYI, "%s: mid=3D%llu state=3D%d result=3D%d bytes=3D%u\n", @@ -1737,11 +1735,8 @@ cifs_async_writev(struct cifs_writedata *wdata, =20 rqst.rq_iov =3D iov; rqst.rq_nvec =3D 2; - rqst.rq_pages =3D wdata->pages; - rqst.rq_offset =3D wdata->page_offset; - rqst.rq_npages =3D wdata->nr_pages; - rqst.rq_pagesz =3D wdata->pagesz; - rqst.rq_tailsz =3D wdata->tailsz; + rqst.rq_iter =3D wdata->iter; + rqst.rq_iter_size =3D iov_iter_count(&wdata->iter); =20 cifs_dbg(FYI, "async write at %llu %u bytes\n", wdata->offset, wdata->bytes); diff --git a/fs/cifs/file.c b/fs/cifs/file.c index 5421aa5e35cc..5de37e84a751 100644 --- a/fs/cifs/file.c +++ b/fs/cifs/file.c @@ -36,6 +36,32 @@ #include "cifs_ioctl.h" #include "cached_dir.h" =20 +/* + * Remove the dirty flags from a span of pages. + */ +static void cifs_undirty_folios(struct inode *inode, loff_t start, unsigne= d int len) +{ + struct address_space *mapping =3D inode->i_mapping; + struct folio *folio; + pgoff_t end; + + XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); + + rcu_read_lock(); + + end =3D (start + len - 1) / PAGE_SIZE; + xas_for_each_marked(&xas, folio, end, PAGECACHE_TAG_DIRTY) { + xas_pause(&xas); + rcu_read_unlock(); + folio_lock(folio); + folio_clear_dirty_for_io(folio); + folio_unlock(folio); + rcu_read_lock(); + } + + rcu_read_unlock(); +} + /* * Completion of write to server. */ @@ -2388,7 +2414,6 @@ cifs_writedata_release(struct kref *refcount) if (wdata->cfile) cifsFileInfo_put(wdata->cfile); =20 - kvfree(wdata->pages); kfree(wdata); } =20 @@ -2399,51 +2424,49 @@ cifs_writedata_release(struct kref *refcount) static void cifs_writev_requeue(struct cifs_writedata *wdata) { - int i, rc =3D 0; + int rc =3D 0; struct inode *inode =3D d_inode(wdata->cfile->dentry); struct TCP_Server_Info *server; - unsigned int rest_len; + unsigned int rest_len =3D wdata->bytes; + loff_t fpos =3D wdata->offset; =20 server =3D tlink_tcon(wdata->cfile->tlink)->ses->server; - i =3D 0; - rest_len =3D wdata->bytes; do { struct cifs_writedata *wdata2; - unsigned int j, nr_pages, wsize, tailsz, cur_len; + unsigned int wsize, cur_len; =20 wsize =3D server->ops->wp_retry_size(inode); if (wsize < rest_len) { - nr_pages =3D wsize / PAGE_SIZE; - if (!nr_pages) { - rc =3D -EOPNOTSUPP; + if (wsize < PAGE_SIZE) { + rc =3D -ENOTSUPP; break; } - cur_len =3D nr_pages * PAGE_SIZE; - tailsz =3D PAGE_SIZE; + cur_len =3D min(round_down(wsize, PAGE_SIZE), rest_len); } else { - nr_pages =3D DIV_ROUND_UP(rest_len, PAGE_SIZE); cur_len =3D rest_len; - tailsz =3D rest_len - (nr_pages - 1) * PAGE_SIZE; } =20 - wdata2 =3D cifs_writedata_alloc(nr_pages, cifs_writev_complete); + wdata2 =3D cifs_writedata_alloc(cifs_writev_complete); if (!wdata2) { rc =3D -ENOMEM; break; } =20 - for (j =3D 0; j < nr_pages; j++) { - wdata2->pages[j] =3D wdata->pages[i + j]; - lock_page(wdata2->pages[j]); - clear_page_dirty_for_io(wdata2->pages[j]); - } - wdata2->sync_mode =3D wdata->sync_mode; - wdata2->nr_pages =3D nr_pages; - wdata2->offset =3D page_offset(wdata2->pages[0]); - wdata2->pagesz =3D PAGE_SIZE; - wdata2->tailsz =3D tailsz; - wdata2->bytes =3D cur_len; + wdata2->offset =3D fpos; + wdata2->bytes =3D cur_len; + wdata2->iter =3D wdata->iter; + + iov_iter_advance(&wdata2->iter, fpos - wdata->offset); + iov_iter_truncate(&wdata2->iter, wdata2->bytes); + + if (iov_iter_is_xarray(&wdata2->iter)) + /* Check for pages having been redirtied and clean + * them. We can do this by walking the xarray. If + * it's not an xarray, then it's a DIO and we shouldn't + * be mucking around with the page bits. + */ + cifs_undirty_folios(inode, fpos, cur_len); =20 rc =3D cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &wdata2->cfile); @@ -2458,33 +2481,22 @@ cifs_writev_requeue(struct cifs_writedata *wdata) cifs_writedata_release); } =20 - for (j =3D 0; j < nr_pages; j++) { - unlock_page(wdata2->pages[j]); - if (rc !=3D 0 && !is_retryable_error(rc)) { - SetPageError(wdata2->pages[j]); - end_page_writeback(wdata2->pages[j]); - put_page(wdata2->pages[j]); - } - } - kref_put(&wdata2->refcount, cifs_writedata_release); if (rc) { if (is_retryable_error(rc)) continue; - i +=3D nr_pages; + fpos +=3D cur_len; + rest_len -=3D cur_len; break; } =20 + fpos +=3D cur_len; rest_len -=3D cur_len; - i +=3D nr_pages; - } while (i < wdata->nr_pages); + } while (rest_len > 0); =20 - /* cleanup remaining pages from the original wdata */ - for (; i < wdata->nr_pages; i++) { - SetPageError(wdata->pages[i]); - end_page_writeback(wdata->pages[i]); - put_page(wdata->pages[i]); - } + /* Clean up remaining pages from the original wdata */ + if (iov_iter_is_xarray(&wdata->iter)) + cifs_pages_write_failed(inode, fpos, rest_len); =20 if (rc !=3D 0 && !is_retryable_error(rc)) mapping_set_error(inode->i_mapping, rc); @@ -2497,7 +2509,6 @@ cifs_writev_complete(struct work_struct *work) struct cifs_writedata *wdata =3D container_of(work, struct cifs_writedata, work); struct inode *inode =3D d_inode(wdata->cfile->dentry); - int i =3D 0; =20 if (wdata->result =3D=3D 0) { spin_lock(&inode->i_lock); @@ -2508,45 +2519,24 @@ cifs_writev_complete(struct work_struct *work) } else if (wdata->sync_mode =3D=3D WB_SYNC_ALL && wdata->result =3D=3D -E= AGAIN) return cifs_writev_requeue(wdata); =20 - for (i =3D 0; i < wdata->nr_pages; i++) { - struct page *page =3D wdata->pages[i]; + if (wdata->result =3D=3D -EAGAIN) + cifs_pages_write_redirty(inode, wdata->offset, wdata->bytes); + else if (wdata->result < 0) + cifs_pages_write_failed(inode, wdata->offset, wdata->bytes); + else + cifs_pages_written_back(inode, wdata->offset, wdata->bytes); =20 - if (wdata->result =3D=3D -EAGAIN) - __set_page_dirty_nobuffers(page); - else if (wdata->result < 0) - SetPageError(page); - end_page_writeback(page); - cifs_readpage_to_fscache(inode, page); - put_page(page); - } if (wdata->result !=3D -EAGAIN) mapping_set_error(inode->i_mapping, wdata->result); kref_put(&wdata->refcount, cifs_writedata_release); } =20 -struct cifs_writedata * -cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete) -{ - struct cifs_writedata *writedata =3D NULL; - struct page **pages =3D - kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); - if (pages) { - writedata =3D cifs_writedata_direct_alloc(pages, complete); - if (!writedata) - kvfree(pages); - } - - return writedata; -} - -struct cifs_writedata * -cifs_writedata_direct_alloc(struct page **pages, work_func_t complete) +struct cifs_writedata *cifs_writedata_alloc(work_func_t complete) { struct cifs_writedata *wdata; =20 wdata =3D kzalloc(sizeof(*wdata), GFP_NOFS); if (wdata !=3D NULL) { - wdata->pages =3D pages; kref_init(&wdata->refcount); INIT_LIST_HEAD(&wdata->list); init_completion(&wdata->done); @@ -2555,7 +2545,6 @@ cifs_writedata_direct_alloc(struct page **pages, work= _func_t complete) return wdata; } =20 - static int cifs_partialpagewrite(struct page *page, unsigned from, unsigne= d to) { struct address_space *mapping =3D page->mapping; @@ -2614,6 +2603,7 @@ static int cifs_partialpagewrite(struct page *page, u= nsigned from, unsigned to) return rc; } =20 +#if 0 // TODO: Remove for iov_iter support static struct cifs_writedata * wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping, pgoff_t end, pgoff_t *index, @@ -2919,6 +2909,375 @@ static int cifs_writepages(struct address_space *ma= pping, set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags); return rc; } +#endif + +/* + * Extend the region to be written back to include subsequent contiguously + * dirty pages if possible, but don't sleep while doing so. + */ +static void cifs_extend_writeback(struct address_space *mapping, + long *_count, + loff_t start, + int max_pages, + size_t max_len, + unsigned int *_len) +{ + struct folio_batch batch; + struct folio *folio; + unsigned int psize, nr_pages; + size_t len =3D *_len; + pgoff_t index =3D (start + len) / PAGE_SIZE; + bool stop =3D true; + unsigned int i; + + XA_STATE(xas, &mapping->i_pages, index); + folio_batch_init(&batch); + + do { + /* Firstly, we gather up a batch of contiguous dirty pages + * under the RCU read lock - but we can't clear the dirty flags + * there if any of those pages are mapped. + */ + rcu_read_lock(); + + xas_for_each(&xas, folio, ULONG_MAX) { + stop =3D true; + if (xas_retry(&xas, folio)) + continue; + if (xa_is_value(folio)) + break; + if (folio_index(folio) !=3D index) + break; + if (!folio_try_get_rcu(folio)) { + xas_reset(&xas); + continue; + } + nr_pages =3D folio_nr_pages(folio); + if (nr_pages > max_pages) + break; + + /* Has the page moved or been split? */ + if (unlikely(folio !=3D xas_reload(&xas))) { + folio_put(folio); + break; + } + + if (!folio_trylock(folio)) { + folio_put(folio); + break; + } + if (!folio_test_dirty(folio) || folio_test_writeback(folio)) { + folio_unlock(folio); + folio_put(folio); + break; + } + + max_pages -=3D nr_pages; + psize =3D folio_size(folio); + len +=3D psize; + stop =3D false; + if (max_pages <=3D 0 || len >=3D max_len || *_count <=3D 0) + stop =3D true; + + index +=3D nr_pages; + if (!folio_batch_add(&batch, folio)) + break; + if (stop) + break; + } + + if (!stop) + xas_pause(&xas); + rcu_read_unlock(); + + /* Now, if we obtained any pages, we can shift them to being + * writable and mark them for caching. + */ + if (!folio_batch_count(&batch)) + break; + + for (i =3D 0; i < folio_batch_count(&batch); i++) { + folio =3D batch.folios[i]; + /* The folio should be locked, dirty and not undergoing + * writeback from the loop above. + */ + if (!folio_clear_dirty_for_io(folio)) + WARN_ON(1); + if (folio_start_writeback(folio)) + WARN_ON(1); + + *_count -=3D folio_nr_pages(folio); + folio_unlock(folio); + } + + folio_batch_release(&batch); + cond_resched(); + } while (!stop); + + *_len =3D len; +} + +/* + * Write back the locked page and any subsequent non-locked dirty pages. + */ +static ssize_t cifs_write_back_from_locked_folio(struct address_space *map= ping, + struct writeback_control *wbc, + struct folio *folio, + loff_t start, loff_t end) +{ + struct inode *inode =3D mapping->host; + struct TCP_Server_Info *server; + struct cifs_writedata *wdata; + struct cifs_sb_info *cifs_sb =3D CIFS_SB(inode->i_sb); + struct cifs_credits credits_on_stack; + struct cifs_credits *credits =3D &credits_on_stack; + struct cifsFileInfo *cfile =3D NULL; + unsigned int xid, wsize, len; + loff_t i_size =3D i_size_read(inode); + size_t max_len; + long count =3D wbc->nr_to_write; + int rc; + + /* The folio should be locked, dirty and not undergoing writeback. */ + if (folio_start_writeback(folio)) + WARN_ON(1); + + count -=3D folio_nr_pages(folio); + len =3D folio_size(folio); + + xid =3D get_xid(); + server =3D cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses); + + rc =3D cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile); + if (rc) { + cifs_dbg(VFS, "No writable handle in writepages rc=3D%d\n", rc); + goto err_xid; + } + + rc =3D server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize, + &wsize, credits); + if (rc !=3D 0) + goto err_close; + + wdata =3D cifs_writedata_alloc(cifs_writev_complete); + if (!wdata) { + rc =3D -ENOMEM; + goto err_uncredit; + } + + wdata->sync_mode =3D wbc->sync_mode; + wdata->offset =3D folio_pos(folio); + wdata->pid =3D cfile->pid; + wdata->credits =3D credits_on_stack; + wdata->cfile =3D cfile; + wdata->server =3D server; + cfile =3D NULL; + + /* Find all consecutive lockable dirty pages, stopping when we find a + * page that is not immediately lockable, is not dirty or is missing, + * or we reach the end of the range. + */ + if (start < i_size) { + /* Trim the write to the EOF; the extra data is ignored. Also + * put an upper limit on the size of a single storedata op. + */ + max_len =3D wsize; + max_len =3D min_t(unsigned long long, max_len, end - start + 1); + max_len =3D min_t(unsigned long long, max_len, i_size - start); + + if (len < max_len) { + int max_pages =3D INT_MAX; + +#ifdef CONFIG_CIFS_SMB_DIRECT + if (server->smbd_conn) + max_pages =3D server->smbd_conn->max_frmr_depth; +#endif + max_pages -=3D folio_nr_pages(folio); + + if (max_pages > 0) + cifs_extend_writeback(mapping, &count, start, + max_pages, max_len, &len); + } + len =3D min_t(loff_t, len, max_len); + } + + wdata->bytes =3D len; + + /* We now have a contiguous set of dirty pages, each with writeback + * set; the first page is still locked at this point, but all the rest + * have been unlocked. + */ + folio_unlock(folio); + + if (start < i_size) { + iov_iter_xarray(&wdata->iter, ITER_SOURCE, &mapping->i_pages, + start, len); + + rc =3D adjust_credits(wdata->server, &wdata->credits, wdata->bytes); + if (rc) + goto err_wdata; + + if (wdata->cfile->invalidHandle) + rc =3D -EAGAIN; + else + rc =3D wdata->server->ops->async_writev(wdata, + cifs_writedata_release); + if (rc >=3D 0) { + kref_put(&wdata->refcount, cifs_writedata_release); + goto err_close; + } + } else { + /* The dirty region was entirely beyond the EOF. */ + cifs_pages_written_back(inode, start, len); + rc =3D 0; + } + +err_wdata: + kref_put(&wdata->refcount, cifs_writedata_release); +err_uncredit: + add_credits_and_wake_if(server, credits, 0); +err_close: + if (cfile) + cifsFileInfo_put(cfile); +err_xid: + free_xid(xid); + if (rc =3D=3D 0) { + wbc->nr_to_write =3D count; + } else if (is_retryable_error(rc)) { + cifs_pages_write_redirty(inode, start, len); + } else { + cifs_pages_write_failed(inode, start, len); + mapping_set_error(mapping, rc); + } + /* Indication to update ctime and mtime as close is deferred */ + set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags); + return rc; +} + +/* + * write a region of pages back to the server + */ +static int cifs_writepages_region(struct address_space *mapping, + struct writeback_control *wbc, + loff_t start, loff_t end, loff_t *_next) +{ + struct folio *folio; + struct page *head_page; + ssize_t ret; + int n, skips =3D 0; + + do { + pgoff_t index =3D start / PAGE_SIZE; + + n =3D find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE, + PAGECACHE_TAG_DIRTY, 1, &head_page); + if (!n) + break; + + folio =3D page_folio(head_page); + start =3D folio_pos(folio); /* May regress with THPs */ + + /* At this point we hold neither the i_pages lock nor the + * page lock: the page may be truncated or invalidated + * (changing page->mapping to NULL), or even swizzled + * back from swapper_space to tmpfs file mapping + */ + if (wbc->sync_mode !=3D WB_SYNC_NONE) { + ret =3D folio_lock_killable(folio); + if (ret < 0) { + folio_put(folio); + return ret; + } + } else { + if (!folio_trylock(folio)) { + folio_put(folio); + return 0; + } + } + + if (folio_mapping(folio) !=3D mapping || + !folio_test_dirty(folio)) { + start +=3D folio_size(folio); + folio_unlock(folio); + folio_put(folio); + continue; + } + + if (folio_test_writeback(folio) || + folio_test_fscache(folio)) { + folio_unlock(folio); + if (wbc->sync_mode !=3D WB_SYNC_NONE) { + folio_wait_writeback(folio); +#ifdef CONFIG_CIFS_FSCACHE + folio_wait_fscache(folio); +#endif + } else { + start +=3D folio_size(folio); + } + folio_put(folio); + if (wbc->sync_mode =3D=3D WB_SYNC_NONE) { + if (skips >=3D 5 || need_resched()) + break; + skips++; + } + continue; + } + + if (!folio_clear_dirty_for_io(folio)) + /* We hold the page lock - it should've been dirty. */ + WARN_ON(1); + + ret =3D cifs_write_back_from_locked_folio(mapping, wbc, folio, start, en= d); + folio_put(folio); + if (ret < 0) + return ret; + + start +=3D ret; + cond_resched(); + } while (wbc->nr_to_write > 0); + + *_next =3D start; + return 0; +} + +/* + * Write some of the pending data back to the server + */ +static int cifs_writepages(struct address_space *mapping, + struct writeback_control *wbc) +{ + loff_t start, next; + int ret; + + /* We have to be careful as we can end up racing with setattr() + * truncating the pagecache since the caller doesn't take a lock here + * to prevent it. + */ + + if (wbc->range_cyclic) { + start =3D mapping->writeback_index * PAGE_SIZE; + ret =3D cifs_writepages_region(mapping, wbc, start, LLONG_MAX, &next); + if (ret =3D=3D 0) { + mapping->writeback_index =3D next / PAGE_SIZE; + if (start > 0 && wbc->nr_to_write > 0) { + ret =3D cifs_writepages_region(mapping, wbc, 0, + start, &next); + if (ret =3D=3D 0) + mapping->writeback_index =3D + next / PAGE_SIZE; + } + } + } else if (wbc->range_start =3D=3D 0 && wbc->range_end =3D=3D LLONG_MAX) { + ret =3D cifs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next); + if (wbc->nr_to_write > 0 && ret =3D=3D 0) + mapping->writeback_index =3D next / PAGE_SIZE; + } else { + ret =3D cifs_writepages_region(mapping, wbc, + wbc->range_start, wbc->range_end, &next); + } + + return ret; +} =20 static int cifs_writepage_locked(struct page *page, struct writeback_control *wbc) @@ -2969,6 +3328,7 @@ static int cifs_write_end(struct file *file, struct a= ddress_space *mapping, struct inode *inode =3D mapping->host; struct cifsFileInfo *cfile =3D file->private_data; struct cifs_sb_info *cifs_sb =3D CIFS_SB(cfile->dentry->d_sb); + struct folio *folio =3D page_folio(page); __u32 pid; =20 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) @@ -2979,14 +3339,14 @@ static int cifs_write_end(struct file *file, struct= address_space *mapping, cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n", page, pos, copied); =20 - if (PageChecked(page)) { + if (folio_test_checked(folio)) { if (copied =3D=3D len) - SetPageUptodate(page); - ClearPageChecked(page); - } else if (!PageUptodate(page) && copied =3D=3D PAGE_SIZE) - SetPageUptodate(page); + folio_mark_uptodate(folio); + folio_clear_checked(folio); + } else if (!folio_test_uptodate(folio) && copied =3D=3D PAGE_SIZE) + folio_mark_uptodate(folio); =20 - if (!PageUptodate(page)) { + if (!folio_test_uptodate(folio)) { char *page_data; unsigned offset =3D pos & (PAGE_SIZE - 1); unsigned int xid; @@ -3146,6 +3506,7 @@ int cifs_flush(struct file *file, fl_owner_t id) return rc; } =20 +#if 0 // TODO: Remove for iov_iter support static int cifs_write_allocate_pages(struct page **pages, unsigned long num_pages) { @@ -3186,17 +3547,15 @@ size_t get_numpages(const size_t wsize, const size_= t len, size_t *cur_len) =20 return num_pages; } +#endif =20 static void cifs_uncached_writedata_release(struct kref *refcount) { - int i; struct cifs_writedata *wdata =3D container_of(refcount, struct cifs_writedata, refcount); =20 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release); - for (i =3D 0; i < wdata->nr_pages; i++) - put_page(wdata->pages[i]); cifs_writedata_release(refcount); } =20 @@ -3222,6 +3581,7 @@ cifs_uncached_writev_complete(struct work_struct *wor= k) kref_put(&wdata->refcount, cifs_uncached_writedata_release); } =20 +#if 0 // TODO: Remove for iov_iter support static int wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from, size_t *len, unsigned long *num_pages) @@ -3263,6 +3623,7 @@ wdata_fill_from_iovec(struct cifs_writedata *wdata, s= truct iov_iter *from, *num_pages =3D i + 1; return 0; } +#endif =20 static int cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_li= st, @@ -3334,23 +3695,57 @@ cifs_resend_wdata(struct cifs_writedata *wdata, str= uct list_head *wdata_list, return rc; } =20 +/* + * Select span of a bvec iterator we're going to use. Limit it by both ma= ximum + * size and maximum number of segments. + */ +static size_t cifs_limit_bvec_subset(const struct iov_iter *iter, size_t m= ax_size, + size_t max_segs, unsigned int *_nsegs) +{ + const struct bio_vec *bvecs =3D iter->bvec; + unsigned int nbv =3D iter->nr_segs, ix =3D 0, nsegs =3D 0; + size_t len, span =3D 0, n =3D iter->count; + size_t skip =3D iter->iov_offset; + + if (WARN_ON(!iov_iter_is_bvec(iter)) || n =3D=3D 0) + return 0; + + while (n && ix < nbv && skip) { + len =3D bvecs[ix].bv_len; + if (skip < len) + break; + skip -=3D len; + n -=3D len; + ix++; + } + + while (n && ix < nbv) { + len =3D min3(n, bvecs[ix].bv_len - skip, max_size); + span +=3D len; + nsegs++; + ix++; + if (span >=3D max_size || nsegs >=3D max_segs) + break; + skip =3D 0; + n -=3D len; + } + + *_nsegs =3D nsegs; + return span; +} + static int -cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, +cifs_write_from_iter(loff_t fpos, size_t len, struct iov_iter *from, struct cifsFileInfo *open_file, struct cifs_sb_info *cifs_sb, struct list_head *wdata_list, struct cifs_aio_ctx *ctx) { int rc =3D 0; - size_t cur_len; - unsigned long nr_pages, num_pages, i; + size_t cur_len, max_len; struct cifs_writedata *wdata; - struct iov_iter saved_from =3D *from; - loff_t saved_offset =3D offset; pid_t pid; struct TCP_Server_Info *server; - struct page **pagevec; - size_t start; - unsigned int xid; + unsigned int xid, max_segs =3D INT_MAX; =20 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) pid =3D open_file->pid; @@ -3360,10 +3755,20 @@ cifs_write_from_iter(loff_t offset, size_t len, str= uct iov_iter *from, server =3D cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); xid =3D get_xid(); =20 +#ifdef CONFIG_CIFS_SMB_DIRECT + if (server->smbd_conn) + max_segs =3D server->smbd_conn->max_frmr_depth; +#endif + do { - unsigned int wsize; struct cifs_credits credits_on_stack; struct cifs_credits *credits =3D &credits_on_stack; + unsigned int wsize, nsegs =3D 0; + + if (signal_pending(current)) { + rc =3D -EINTR; + break; + } =20 if (open_file->invalidHandle) { rc =3D cifs_reopen_file(open_file, false); @@ -3378,99 +3783,42 @@ cifs_write_from_iter(loff_t offset, size_t len, str= uct iov_iter *from, if (rc) break; =20 - cur_len =3D min_t(const size_t, len, wsize); - - if (ctx->direct_io) { - ssize_t result; - - result =3D iov_iter_get_pages_alloc2( - from, &pagevec, cur_len, &start); - if (result < 0) { - cifs_dbg(VFS, - "direct_writev couldn't get user pages (rc=3D%zd) iter type %d iov_o= ffset %zd count %zd\n", - result, iov_iter_type(from), - from->iov_offset, from->count); - dump_stack(); - - rc =3D result; - add_credits_and_wake_if(server, credits, 0); - break; - } - cur_len =3D (size_t)result; - - nr_pages =3D - (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE; - - wdata =3D cifs_writedata_direct_alloc(pagevec, - cifs_uncached_writev_complete); - if (!wdata) { - rc =3D -ENOMEM; - for (i =3D 0; i < nr_pages; i++) - put_page(pagevec[i]); - kvfree(pagevec); - add_credits_and_wake_if(server, credits, 0); - break; - } - - - wdata->page_offset =3D start; - wdata->tailsz =3D - nr_pages > 1 ? - cur_len - (PAGE_SIZE - start) - - (nr_pages - 2) * PAGE_SIZE : - cur_len; - } else { - nr_pages =3D get_numpages(wsize, len, &cur_len); - wdata =3D cifs_writedata_alloc(nr_pages, - cifs_uncached_writev_complete); - if (!wdata) { - rc =3D -ENOMEM; - add_credits_and_wake_if(server, credits, 0); - break; - } - - rc =3D cifs_write_allocate_pages(wdata->pages, nr_pages); - if (rc) { - kvfree(wdata->pages); - kfree(wdata); - add_credits_and_wake_if(server, credits, 0); - break; - } - - num_pages =3D nr_pages; - rc =3D wdata_fill_from_iovec( - wdata, from, &cur_len, &num_pages); - if (rc) { - for (i =3D 0; i < nr_pages; i++) - put_page(wdata->pages[i]); - kvfree(wdata->pages); - kfree(wdata); - add_credits_and_wake_if(server, credits, 0); - break; - } + max_len =3D min_t(const size_t, len, wsize); + if (!max_len) { + rc =3D -EAGAIN; + add_credits_and_wake_if(server, credits, 0); + break; + } =20 - /* - * Bring nr_pages down to the number of pages we - * actually used, and free any pages that we didn't use. - */ - for ( ; nr_pages > num_pages; nr_pages--) - put_page(wdata->pages[nr_pages - 1]); + cur_len =3D cifs_limit_bvec_subset(from, max_len, max_segs, &nsegs); + cifs_dbg(FYI, "write_from_iter len=3D%zx/%zx nsegs=3D%u/%lu/%u\n", + cur_len, max_len, nsegs, from->nr_segs, max_segs); + if (cur_len =3D=3D 0) { + rc =3D -EIO; + add_credits_and_wake_if(server, credits, 0); + break; + } =20 - wdata->tailsz =3D cur_len - ((nr_pages - 1) * PAGE_SIZE); + wdata =3D cifs_writedata_alloc(cifs_uncached_writev_complete); + if (!wdata) { + rc =3D -ENOMEM; + add_credits_and_wake_if(server, credits, 0); + break; } =20 wdata->sync_mode =3D WB_SYNC_ALL; - wdata->nr_pages =3D nr_pages; - wdata->offset =3D (__u64)offset; - wdata->cfile =3D cifsFileInfo_get(open_file); - wdata->server =3D server; - wdata->pid =3D pid; - wdata->bytes =3D cur_len; - wdata->pagesz =3D PAGE_SIZE; - wdata->credits =3D credits_on_stack; - wdata->ctx =3D ctx; + wdata->offset =3D (__u64)fpos; + wdata->cfile =3D cifsFileInfo_get(open_file); + wdata->server =3D server; + wdata->pid =3D pid; + wdata->bytes =3D cur_len; + wdata->credits =3D credits_on_stack; + wdata->iter =3D *from; + wdata->ctx =3D ctx; kref_get(&ctx->refcount); =20 + iov_iter_truncate(&wdata->iter, cur_len); + rc =3D adjust_credits(server, &wdata->credits, wdata->bytes); =20 if (!rc) { @@ -3485,16 +3833,14 @@ cifs_write_from_iter(loff_t offset, size_t len, str= uct iov_iter *from, add_credits_and_wake_if(server, &wdata->credits, 0); kref_put(&wdata->refcount, cifs_uncached_writedata_release); - if (rc =3D=3D -EAGAIN) { - *from =3D saved_from; - iov_iter_advance(from, offset - saved_offset); + if (rc =3D=3D -EAGAIN) continue; - } break; } =20 list_add_tail(&wdata->list, wdata_list); - offset +=3D cur_len; + iov_iter_advance(from, cur_len); + fpos +=3D cur_len; len -=3D cur_len; } while (len > 0); =20 @@ -3593,8 +3939,6 @@ static ssize_t __cifs_writev( struct cifs_tcon *tcon; struct cifs_sb_info *cifs_sb; struct cifs_aio_ctx *ctx; - struct iov_iter saved_from =3D *from; - size_t len =3D iov_iter_count(from); int rc; =20 /* @@ -3628,23 +3972,54 @@ static ssize_t __cifs_writev( ctx->iocb =3D iocb; =20 ctx->pos =3D iocb->ki_pos; + ctx->direct_io =3D direct; + ctx->nr_pinned_pages =3D 0; =20 - if (direct) { - ctx->direct_io =3D true; - ctx->iter =3D *from; - ctx->len =3D len; - } else { - rc =3D setup_aio_ctx_iter(ctx, from, ITER_SOURCE); - if (rc) { + if (user_backed_iter(from)) { + /* + * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as + * they contain references to the calling process's virtual + * memory layout which won't be available in an async worker + * thread. This also takes a pin on every folio involved. + */ + rc =3D netfs_extract_user_iter(from, iov_iter_count(from), + &ctx->iter, 0); + if (rc < 0) { kref_put(&ctx->refcount, cifs_aio_ctx_release); return rc; } + + ctx->nr_pinned_pages =3D rc; + ctx->bv =3D (void *)ctx->iter.bvec; + ctx->bv_need_unpin =3D iov_iter_extract_will_pin(&ctx->iter); + } else if ((iov_iter_is_bvec(from) || iov_iter_is_kvec(from)) && + !is_sync_kiocb(iocb)) { + /* + * If the op is asynchronous, we need to copy the list attached + * to a BVEC/KVEC-type iterator, but we assume that the storage + * will be pinned by the caller; in any case, we may or may not + * be able to pin the pages, so we don't try. + */ + ctx->bv =3D (void *)dup_iter(&ctx->iter, from, GFP_KERNEL); + if (!ctx->bv) { + kref_put(&ctx->refcount, cifs_aio_ctx_release); + return -ENOMEM; + } + } else { + /* + * Otherwise, we just pass the iterator down as-is and rely on + * the caller to make sure the pages referred to by the + * iterator don't evaporate. + */ + ctx->iter =3D *from; } =20 + ctx->len =3D iov_iter_count(&ctx->iter); + /* grab a lock here due to read response handlers can access ctx */ mutex_lock(&ctx->aio_mutex); =20 - rc =3D cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from, + rc =3D cifs_write_from_iter(iocb->ki_pos, ctx->len, &ctx->iter, cfile, cifs_sb, &ctx->list, ctx); =20 /* @@ -3787,14 +4162,12 @@ cifs_strict_writev(struct kiocb *iocb, struct iov_i= ter *from) return written; } =20 -static struct cifs_readdata * -cifs_readdata_direct_alloc(struct page **pages, work_func_t complete) +static struct cifs_readdata *cifs_readdata_alloc(work_func_t complete) { struct cifs_readdata *rdata; =20 rdata =3D kzalloc(sizeof(*rdata), GFP_KERNEL); - if (rdata !=3D NULL) { - rdata->pages =3D pages; + if (rdata) { kref_init(&rdata->refcount); INIT_LIST_HEAD(&rdata->list); init_completion(&rdata->done); @@ -3804,27 +4177,14 @@ cifs_readdata_direct_alloc(struct page **pages, wor= k_func_t complete) return rdata; } =20 -static struct cifs_readdata * -cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete) -{ - struct page **pages =3D - kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL); - struct cifs_readdata *ret =3D NULL; - - if (pages) { - ret =3D cifs_readdata_direct_alloc(pages, complete); - if (!ret) - kfree(pages); - } - - return ret; -} - void cifs_readdata_release(struct kref *refcount) { struct cifs_readdata *rdata =3D container_of(refcount, struct cifs_readdata, refcount); + + if (rdata->ctx) + kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release); #ifdef CONFIG_CIFS_SMB_DIRECT if (rdata->mr) { smbd_deregister_mr(rdata->mr); @@ -3834,79 +4194,9 @@ cifs_readdata_release(struct kref *refcount) if (rdata->cfile) cifsFileInfo_put(rdata->cfile); =20 - kvfree(rdata->pages); kfree(rdata); } =20 -static int -cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_page= s) -{ - int rc =3D 0; - struct page *page; - unsigned int i; - - for (i =3D 0; i < nr_pages; i++) { - page =3D alloc_page(GFP_KERNEL|__GFP_HIGHMEM); - if (!page) { - rc =3D -ENOMEM; - break; - } - rdata->pages[i] =3D page; - } - - if (rc) { - unsigned int nr_page_failed =3D i; - - for (i =3D 0; i < nr_page_failed; i++) { - put_page(rdata->pages[i]); - rdata->pages[i] =3D NULL; - } - } - return rc; -} - -static void -cifs_uncached_readdata_release(struct kref *refcount) -{ - struct cifs_readdata *rdata =3D container_of(refcount, - struct cifs_readdata, refcount); - unsigned int i; - - kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release); - for (i =3D 0; i < rdata->nr_pages; i++) { - put_page(rdata->pages[i]); - } - cifs_readdata_release(refcount); -} - -/** - * cifs_readdata_to_iov - copy data from pages in response to an iovec - * @rdata: the readdata response with list of pages holding data - * @iter: destination for our data - * - * This function copies data from a list of pages in a readdata response i= nto - * an array of iovecs. It will first calculate where the data should go - * based on the info in the readdata and then copy the data into that spot. - */ -static int -cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter) -{ - size_t remaining =3D rdata->got_bytes; - unsigned int i; - - for (i =3D 0; i < rdata->nr_pages; i++) { - struct page *page =3D rdata->pages[i]; - size_t copy =3D min_t(size_t, remaining, PAGE_SIZE); - size_t written; - - written =3D copy_page_to_iter(page, 0, copy, iter); - remaining -=3D written; - if (written < copy && iov_iter_count(iter) > 0) - break; - } - return remaining ? -EFAULT : 0; -} - static void collect_uncached_read_data(struct cifs_aio_ctx *ctx); =20 static void @@ -3918,9 +4208,11 @@ cifs_uncached_readv_complete(struct work_struct *wor= k) complete(&rdata->done); collect_uncached_read_data(rdata->ctx); /* the below call can possibly free the last ref to aio ctx */ - kref_put(&rdata->refcount, cifs_uncached_readdata_release); + kref_put(&rdata->refcount, cifs_readdata_release); } =20 +#if 0 // TODO: Remove for iov_iter support + static int uncached_fill_pages(struct TCP_Server_Info *server, struct cifs_readdata *rdata, struct iov_iter *iter, @@ -3994,6 +4286,7 @@ cifs_uncached_copy_into_pages(struct TCP_Server_Info = *server, { return uncached_fill_pages(server, rdata, iter, iter->count); } +#endif =20 static int cifs_resend_rdata(struct cifs_readdata *rdata, struct list_head *rdata_list, @@ -4063,37 +4356,36 @@ static int cifs_resend_rdata(struct cifs_readdata *= rdata, } while (rc =3D=3D -EAGAIN); =20 fail: - kref_put(&rdata->refcount, cifs_uncached_readdata_release); + kref_put(&rdata->refcount, cifs_readdata_release); return rc; } =20 static int -cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_= file, +cifs_send_async_read(loff_t fpos, size_t len, struct cifsFileInfo *open_fi= le, struct cifs_sb_info *cifs_sb, struct list_head *rdata_list, struct cifs_aio_ctx *ctx) { struct cifs_readdata *rdata; - unsigned int npages, rsize; + unsigned int rsize, nsegs, max_segs =3D INT_MAX; struct cifs_credits credits_on_stack; struct cifs_credits *credits =3D &credits_on_stack; - size_t cur_len; + size_t cur_len, max_len; int rc; pid_t pid; struct TCP_Server_Info *server; - struct page **pagevec; - size_t start; - struct iov_iter direct_iov =3D ctx->iter; =20 server =3D cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); =20 +#ifdef CONFIG_CIFS_SMB_DIRECT + if (server->smbd_conn) + max_segs =3D server->smbd_conn->max_frmr_depth; +#endif + if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) pid =3D open_file->pid; else pid =3D current->tgid; =20 - if (ctx->direct_io) - iov_iter_advance(&direct_iov, offset - ctx->pos); - do { if (open_file->invalidHandle) { rc =3D cifs_reopen_file(open_file, true); @@ -4113,78 +4405,37 @@ cifs_send_async_read(loff_t offset, size_t len, str= uct cifsFileInfo *open_file, if (rc) break; =20 - cur_len =3D min_t(const size_t, len, rsize); - - if (ctx->direct_io) { - ssize_t result; - - result =3D iov_iter_get_pages_alloc2( - &direct_iov, &pagevec, - cur_len, &start); - if (result < 0) { - cifs_dbg(VFS, - "Couldn't get user pages (rc=3D%zd) iter type %d iov_offset %zd coun= t %zd\n", - result, iov_iter_type(&direct_iov), - direct_iov.iov_offset, - direct_iov.count); - dump_stack(); - - rc =3D result; - add_credits_and_wake_if(server, credits, 0); - break; - } - cur_len =3D (size_t)result; - - rdata =3D cifs_readdata_direct_alloc( - pagevec, cifs_uncached_readv_complete); - if (!rdata) { - add_credits_and_wake_if(server, credits, 0); - rc =3D -ENOMEM; - break; - } - - npages =3D (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE; - rdata->page_offset =3D start; - rdata->tailsz =3D npages > 1 ? - cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE : - cur_len; - - } else { + max_len =3D min_t(size_t, len, rsize); =20 - npages =3D DIV_ROUND_UP(cur_len, PAGE_SIZE); - /* allocate a readdata struct */ - rdata =3D cifs_readdata_alloc(npages, - cifs_uncached_readv_complete); - if (!rdata) { - add_credits_and_wake_if(server, credits, 0); - rc =3D -ENOMEM; - break; - } - - rc =3D cifs_read_allocate_pages(rdata, npages); - if (rc) { - kvfree(rdata->pages); - kfree(rdata); - add_credits_and_wake_if(server, credits, 0); - break; - } + cur_len =3D cifs_limit_bvec_subset(&ctx->iter, max_len, + max_segs, &nsegs); + cifs_dbg(FYI, "read-to-iter len=3D%zx/%zx nsegs=3D%u/%lu/%u\n", + cur_len, max_len, nsegs, ctx->iter.nr_segs, max_segs); + if (cur_len =3D=3D 0) { + rc =3D -EIO; + add_credits_and_wake_if(server, credits, 0); + break; + } =20 - rdata->tailsz =3D PAGE_SIZE; + rdata =3D cifs_readdata_alloc(cifs_uncached_readv_complete); + if (!rdata) { + add_credits_and_wake_if(server, credits, 0); + rc =3D -ENOMEM; + break; } =20 - rdata->server =3D server; - rdata->cfile =3D cifsFileInfo_get(open_file); - rdata->nr_pages =3D npages; - rdata->offset =3D offset; - rdata->bytes =3D cur_len; - rdata->pid =3D pid; - rdata->pagesz =3D PAGE_SIZE; - rdata->read_into_pages =3D cifs_uncached_read_into_pages; - rdata->copy_into_pages =3D cifs_uncached_copy_into_pages; - rdata->credits =3D credits_on_stack; - rdata->ctx =3D ctx; + rdata->server =3D server; + rdata->cfile =3D cifsFileInfo_get(open_file); + rdata->offset =3D fpos; + rdata->bytes =3D cur_len; + rdata->pid =3D pid; + rdata->credits =3D credits_on_stack; + rdata->ctx =3D ctx; kref_get(&ctx->refcount); =20 + rdata->iter =3D ctx->iter; + iov_iter_truncate(&rdata->iter, cur_len); + rc =3D adjust_credits(server, &rdata->credits, rdata->bytes); =20 if (!rc) { @@ -4196,17 +4447,15 @@ cifs_send_async_read(loff_t offset, size_t len, str= uct cifsFileInfo *open_file, =20 if (rc) { add_credits_and_wake_if(server, &rdata->credits, 0); - kref_put(&rdata->refcount, - cifs_uncached_readdata_release); - if (rc =3D=3D -EAGAIN) { - iov_iter_revert(&direct_iov, cur_len); + kref_put(&rdata->refcount, cifs_readdata_release); + if (rc =3D=3D -EAGAIN) continue; - } break; } =20 list_add_tail(&rdata->list, rdata_list); - offset +=3D cur_len; + iov_iter_advance(&ctx->iter, cur_len); + fpos +=3D cur_len; len -=3D cur_len; } while (len > 0); =20 @@ -4248,22 +4497,6 @@ collect_uncached_read_data(struct cifs_aio_ctx *ctx) list_del_init(&rdata->list); INIT_LIST_HEAD(&tmp_list); =20 - /* - * Got a part of data and then reconnect has - * happened -- fill the buffer and continue - * reading. - */ - if (got_bytes && got_bytes < rdata->bytes) { - rc =3D 0; - if (!ctx->direct_io) - rc =3D cifs_readdata_to_iov(rdata, to); - if (rc) { - kref_put(&rdata->refcount, - cifs_uncached_readdata_release); - continue; - } - } - if (ctx->direct_io) { /* * Re-use rdata as this is a @@ -4280,7 +4513,7 @@ collect_uncached_read_data(struct cifs_aio_ctx *ctx) &tmp_list, ctx); =20 kref_put(&rdata->refcount, - cifs_uncached_readdata_release); + cifs_readdata_release); } =20 list_splice(&tmp_list, &ctx->list); @@ -4288,8 +4521,6 @@ collect_uncached_read_data(struct cifs_aio_ctx *ctx) goto again; } else if (rdata->result) rc =3D rdata->result; - else if (!ctx->direct_io) - rc =3D cifs_readdata_to_iov(rdata, to); =20 /* if there was a short read -- discard anything left */ if (rdata->got_bytes && rdata->got_bytes < rdata->bytes) @@ -4298,7 +4529,7 @@ collect_uncached_read_data(struct cifs_aio_ctx *ctx) ctx->total_len +=3D rdata->got_bytes; } list_del_init(&rdata->list); - kref_put(&rdata->refcount, cifs_uncached_readdata_release); + kref_put(&rdata->refcount, cifs_readdata_release); } =20 if (!ctx->direct_io) @@ -4358,26 +4589,53 @@ static ssize_t __cifs_readv( if (!ctx) return -ENOMEM; =20 - ctx->cfile =3D cifsFileInfo_get(cfile); + ctx->pos =3D offset; + ctx->direct_io =3D direct; + ctx->len =3D len; + ctx->cfile =3D cifsFileInfo_get(cfile); + ctx->nr_pinned_pages =3D 0; =20 if (!is_sync_kiocb(iocb)) ctx->iocb =3D iocb; =20 - if (user_backed_iter(to)) - ctx->should_dirty =3D true; - - if (direct) { - ctx->pos =3D offset; - ctx->direct_io =3D true; - ctx->iter =3D *to; - ctx->len =3D len; - } else { - rc =3D setup_aio_ctx_iter(ctx, to, ITER_DEST); - if (rc) { + if (user_backed_iter(to)) { + /* + * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as + * they contain references to the calling process's virtual + * memory layout which won't be available in an async worker + * thread. This also takes a pin on every folio involved. + */ + rc =3D netfs_extract_user_iter(to, iov_iter_count(to), + &ctx->iter, 0); + if (rc < 0) { kref_put(&ctx->refcount, cifs_aio_ctx_release); return rc; } - len =3D ctx->len; + + ctx->nr_pinned_pages =3D rc; + ctx->bv =3D (void *)ctx->iter.bvec; + ctx->bv_need_unpin =3D iov_iter_extract_will_pin(&ctx->iter); + ctx->should_dirty =3D true; + } else if ((iov_iter_is_bvec(to) || iov_iter_is_kvec(to)) && + !is_sync_kiocb(iocb)) { + /* + * If the op is asynchronous, we need to copy the list attached + * to a BVEC/KVEC-type iterator, but we assume that the storage + * will be retained by the caller; in any case, we may or may + * not be able to pin the pages, so we don't try. + */ + ctx->bv =3D (void *)dup_iter(&ctx->iter, to, GFP_KERNEL); + if (!ctx->bv) { + kref_put(&ctx->refcount, cifs_aio_ctx_release); + return -ENOMEM; + } + } else { + /* + * Otherwise, we just pass the iterator down as-is and rely on + * the caller to make sure the pages referred to by the + * iterator don't evaporate. + */ + ctx->iter =3D *to; } =20 if (direct) { @@ -4639,6 +4897,8 @@ int cifs_file_mmap(struct file *file, struct vm_area_= struct *vma) return rc; } =20 +#if 0 // TODO: Remove for iov_iter support + static void cifs_readv_complete(struct work_struct *work) { @@ -4769,19 +5029,74 @@ cifs_readpages_copy_into_pages(struct TCP_Server_In= fo *server, { return readpages_fill_pages(server, rdata, iter, iter->count); } +#endif + +/* + * Unlock a bunch of folios in the pagecache. + */ +static void cifs_unlock_folios(struct address_space *mapping, pgoff_t firs= t, pgoff_t last) +{ + struct folio *folio; + XA_STATE(xas, &mapping->i_pages, first); + + rcu_read_lock(); + xas_for_each(&xas, folio, last) { + folio_unlock(folio); + } + rcu_read_unlock(); +} + +static void cifs_readahead_complete(struct work_struct *work) +{ + struct cifs_readdata *rdata =3D container_of(work, + struct cifs_readdata, work); + struct folio *folio; + pgoff_t last; + bool good =3D rdata->result =3D=3D 0 || (rdata->result =3D=3D -EAGAIN && = rdata->got_bytes); + + XA_STATE(xas, &rdata->mapping->i_pages, rdata->offset / PAGE_SIZE); + + if (good) + cifs_readahead_to_fscache(rdata->mapping->host, + rdata->offset, rdata->bytes); + + if (iov_iter_count(&rdata->iter) > 0) + iov_iter_zero(iov_iter_count(&rdata->iter), &rdata->iter); + + last =3D (rdata->offset + rdata->bytes - 1) / PAGE_SIZE; + + rcu_read_lock(); + xas_for_each(&xas, folio, last) { + if (good) { + flush_dcache_folio(folio); + folio_mark_uptodate(folio); + } + folio_unlock(folio); + } + rcu_read_unlock(); + + kref_put(&rdata->refcount, cifs_readdata_release); +} =20 static void cifs_readahead(struct readahead_control *ractl) { - int rc; struct cifsFileInfo *open_file =3D ractl->file->private_data; struct cifs_sb_info *cifs_sb =3D CIFS_FILE_SB(ractl->file); struct TCP_Server_Info *server; - pid_t pid; - unsigned int xid, nr_pages, last_batch_size =3D 0, cache_nr_pages =3D 0; - pgoff_t next_cached =3D ULONG_MAX; + unsigned int xid, nr_pages, cache_nr_pages =3D 0; + unsigned int ra_pages; + pgoff_t next_cached =3D ULONG_MAX, ra_index; bool caching =3D fscache_cookie_enabled(cifs_inode_cookie(ractl->mapping-= >host)) && cifs_inode_cookie(ractl->mapping->host)->cache_priv; bool check_cache =3D caching; + pid_t pid; + int rc =3D 0; + + /* Note that readahead_count() lags behind our dequeuing of pages from + * the ractl, wo we have to keep track for ourselves. + */ + ra_pages =3D readahead_count(ractl); + ra_index =3D readahead_index(ractl); =20 xid =3D get_xid(); =20 @@ -4790,22 +5105,21 @@ static void cifs_readahead(struct readahead_control= *ractl) else pid =3D current->tgid; =20 - rc =3D 0; server =3D cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); =20 cifs_dbg(FYI, "%s: file=3D%p mapping=3D%p num_pages=3D%u\n", - __func__, ractl->file, ractl->mapping, readahead_count(ractl)); + __func__, ractl->file, ractl->mapping, ra_pages); =20 /* * Chop the readahead request up into rsize-sized read requests. */ - while ((nr_pages =3D readahead_count(ractl) - last_batch_size)) { - unsigned int i, got, rsize; - struct page *page; + while ((nr_pages =3D ra_pages)) { + unsigned int i, rsize; struct cifs_readdata *rdata; struct cifs_credits credits_on_stack; struct cifs_credits *credits =3D &credits_on_stack; - pgoff_t index =3D readahead_index(ractl) + last_batch_size; + struct folio *folio; + pgoff_t fsize; =20 /* * Find out if we have anything cached in the range of @@ -4814,21 +5128,22 @@ static void cifs_readahead(struct readahead_control= *ractl) if (caching) { if (check_cache) { rc =3D cifs_fscache_query_occupancy( - ractl->mapping->host, index, nr_pages, + ractl->mapping->host, ra_index, nr_pages, &next_cached, &cache_nr_pages); if (rc < 0) caching =3D false; check_cache =3D false; } =20 - if (index =3D=3D next_cached) { + if (ra_index =3D=3D next_cached) { /* * TODO: Send a whole batch of pages to be read * by the cache. */ - struct folio *folio =3D readahead_folio(ractl); - - last_batch_size =3D folio_nr_pages(folio); + folio =3D readahead_folio(ractl); + fsize =3D folio_nr_pages(folio); + ra_pages -=3D fsize; + ra_index +=3D fsize; if (cifs_readpage_from_fscache(ractl->mapping->host, &folio->page) < 0) { /* @@ -4839,8 +5154,8 @@ static void cifs_readahead(struct readahead_control *= ractl) caching =3D false; } folio_unlock(folio); - next_cached++; - cache_nr_pages--; + next_cached +=3D fsize; + cache_nr_pages -=3D fsize; if (cache_nr_pages =3D=3D 0) check_cache =3D true; continue; @@ -4865,8 +5180,9 @@ static void cifs_readahead(struct readahead_control *= ractl) &rsize, credits); if (rc) break; - nr_pages =3D min_t(size_t, rsize / PAGE_SIZE, readahead_count(ractl)); - nr_pages =3D min_t(size_t, nr_pages, next_cached - index); + nr_pages =3D min_t(size_t, rsize / PAGE_SIZE, ra_pages); + if (next_cached !=3D ULONG_MAX) + nr_pages =3D min_t(size_t, nr_pages, next_cached - ra_index); =20 /* * Give up immediately if rsize is too small to read an entire @@ -4879,33 +5195,31 @@ static void cifs_readahead(struct readahead_control= *ractl) break; } =20 - rdata =3D cifs_readdata_alloc(nr_pages, cifs_readv_complete); + rdata =3D cifs_readdata_alloc(cifs_readahead_complete); if (!rdata) { /* best to give up if we're out of mem */ add_credits_and_wake_if(server, credits, 0); break; } =20 - got =3D __readahead_batch(ractl, rdata->pages, nr_pages); - if (got !=3D nr_pages) { - pr_warn("__readahead_batch() returned %u/%u\n", - got, nr_pages); - nr_pages =3D got; - } - - rdata->nr_pages =3D nr_pages; - rdata->bytes =3D readahead_batch_length(ractl); + rdata->offset =3D ra_index * PAGE_SIZE; + rdata->bytes =3D nr_pages * PAGE_SIZE; rdata->cfile =3D cifsFileInfo_get(open_file); rdata->server =3D server; rdata->mapping =3D ractl->mapping; - rdata->offset =3D readahead_pos(ractl); rdata->pid =3D pid; - rdata->pagesz =3D PAGE_SIZE; - rdata->tailsz =3D PAGE_SIZE; - rdata->read_into_pages =3D cifs_readpages_read_into_pages; - rdata->copy_into_pages =3D cifs_readpages_copy_into_pages; rdata->credits =3D credits_on_stack; =20 + for (i =3D 0; i < nr_pages; i++) { + if (!readahead_folio(ractl)) + WARN_ON(1); + } + ra_pages -=3D nr_pages; + ra_index +=3D nr_pages; + + iov_iter_xarray(&rdata->iter, ITER_DEST, &rdata->mapping->i_pages, + rdata->offset, rdata->bytes); + rc =3D adjust_credits(server, &rdata->credits, rdata->bytes); if (!rc) { if (rdata->cfile->invalidHandle) @@ -4916,18 +5230,15 @@ static void cifs_readahead(struct readahead_control= *ractl) =20 if (rc) { add_credits_and_wake_if(server, &rdata->credits, 0); - for (i =3D 0; i < rdata->nr_pages; i++) { - page =3D rdata->pages[i]; - unlock_page(page); - put_page(page); - } + cifs_unlock_folios(rdata->mapping, + rdata->offset / PAGE_SIZE, + (rdata->offset + rdata->bytes - 1) / PAGE_SIZE); /* Fallback to the readpage in error/reconnect cases */ kref_put(&rdata->refcount, cifs_readdata_release); break; } =20 kref_put(&rdata->refcount, cifs_readdata_release); - last_batch_size =3D nr_pages; } =20 free_xid(xid); @@ -4969,10 +5280,6 @@ static int cifs_readpage_worker(struct file *file, s= truct page *page, =20 flush_dcache_page(page); SetPageUptodate(page); - - /* send this page to the cache */ - cifs_readpage_to_fscache(file_inode(file), page); - rc =3D 0; =20 io_error: diff --git a/fs/cifs/fscache.c b/fs/cifs/fscache.c index f6f3a6b75601..47c9f36c11fb 100644 --- a/fs/cifs/fscache.c +++ b/fs/cifs/fscache.c @@ -165,22 +165,16 @@ static int fscache_fallback_read_page(struct inode *i= node, struct page *page) /* * Fallback page writing interface. */ -static int fscache_fallback_write_page(struct inode *inode, struct page *p= age, - bool no_space_allocated_yet) +static int fscache_fallback_write_pages(struct inode *inode, loff_t start,= size_t len, + bool no_space_allocated_yet) { struct netfs_cache_resources cres; struct fscache_cookie *cookie =3D cifs_inode_cookie(inode); struct iov_iter iter; - struct bio_vec bvec[1]; - loff_t start =3D page_offset(page); - size_t len =3D PAGE_SIZE; int ret; =20 memset(&cres, 0, sizeof(cres)); - bvec[0].bv_page =3D page; - bvec[0].bv_offset =3D 0; - bvec[0].bv_len =3D PAGE_SIZE; - iov_iter_bvec(&iter, ITER_SOURCE, bvec, ARRAY_SIZE(bvec), PAGE_SIZE); + iov_iter_xarray(&iter, ITER_SOURCE, &inode->i_mapping->i_pages, start, le= n); =20 ret =3D fscache_begin_write_operation(&cres, cookie); if (ret < 0) @@ -189,7 +183,7 @@ static int fscache_fallback_write_page(struct inode *in= ode, struct page *page, ret =3D cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode), no_space_allocated_yet); if (ret =3D=3D 0) - ret =3D fscache_write(&cres, page_offset(page), &iter, NULL, NULL); + ret =3D fscache_write(&cres, start, &iter, NULL, NULL); fscache_end_operation(&cres); return ret; } @@ -213,12 +207,12 @@ int __cifs_readpage_from_fscache(struct inode *inode,= struct page *page) return 0; } =20 -void __cifs_readpage_to_fscache(struct inode *inode, struct page *page) +void __cifs_readahead_to_fscache(struct inode *inode, loff_t pos, size_t l= en) { - cifs_dbg(FYI, "%s: (fsc: %p, p: %p, i: %p)\n", - __func__, cifs_inode_cookie(inode), page, inode); + cifs_dbg(FYI, "%s: (fsc: %p, p: %llx, l: %zx, i: %p)\n", + __func__, cifs_inode_cookie(inode), pos, len, inode); =20 - fscache_fallback_write_page(inode, page, true); + fscache_fallback_write_pages(inode, pos, len, true); } =20 /* diff --git a/fs/cifs/fscache.h b/fs/cifs/fscache.h index 67b601041f0a..173999610997 100644 --- a/fs/cifs/fscache.h +++ b/fs/cifs/fscache.h @@ -90,7 +90,7 @@ static inline int cifs_fscache_query_occupancy(struct ino= de *inode, } =20 extern int __cifs_readpage_from_fscache(struct inode *pinode, struct page = *ppage); -extern void __cifs_readpage_to_fscache(struct inode *pinode, struct page *= ppage); +extern void __cifs_readahead_to_fscache(struct inode *pinode, loff_t pos, = size_t len); =20 =20 static inline int cifs_readpage_from_fscache(struct inode *inode, @@ -101,11 +101,11 @@ static inline int cifs_readpage_from_fscache(struct i= node *inode, return -ENOBUFS; } =20 -static inline void cifs_readpage_to_fscache(struct inode *inode, - struct page *page) +static inline void cifs_readahead_to_fscache(struct inode *inode, + loff_t pos, size_t len) { if (cifs_inode_cookie(inode)) - __cifs_readpage_to_fscache(inode, page); + __cifs_readahead_to_fscache(inode, pos, len); } =20 #else /* CONFIG_CIFS_FSCACHE */ @@ -141,7 +141,7 @@ cifs_readpage_from_fscache(struct inode *inode, struct = page *page) } =20 static inline -void cifs_readpage_to_fscache(struct inode *inode, struct page *page) {} +void cifs_readahead_to_fscache(struct inode *inode, loff_t pos, size_t len= ) {} =20 #endif /* CONFIG_CIFS_FSCACHE */ =20 diff --git a/fs/cifs/misc.c b/fs/cifs/misc.c index 2a19c7987c5b..967bc3b74def 100644 --- a/fs/cifs/misc.c +++ b/fs/cifs/misc.c @@ -966,16 +966,22 @@ cifs_aio_ctx_release(struct kref *refcount) =20 /* * ctx->bv is only set if setup_aio_ctx_iter() was call successfuly - * which means that iov_iter_get_pages() was a success and thus that - * we have taken reference on pages. + * which means that iov_iter_extract_pages() was a success and thus + * that we may have references or pins on pages that we need to + * release. */ if (ctx->bv) { - unsigned i; + if (ctx->should_dirty || ctx->bv_need_unpin) { + unsigned i; =20 - for (i =3D 0; i < ctx->npages; i++) { - if (ctx->should_dirty) - set_page_dirty(ctx->bv[i].bv_page); - put_page(ctx->bv[i].bv_page); + for (i =3D 0; i < ctx->nr_pinned_pages; i++) { + struct page *page =3D ctx->bv[i].bv_page; + + if (ctx->should_dirty) + set_page_dirty(page); + if (ctx->bv_need_unpin) + unpin_user_page(page); + } } kvfree(ctx->bv); } @@ -983,95 +989,6 @@ cifs_aio_ctx_release(struct kref *refcount) kfree(ctx); } =20 -#define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024) - -int -setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw) -{ - ssize_t rc; - unsigned int cur_npages; - unsigned int npages =3D 0; - unsigned int i; - size_t len; - size_t count =3D iov_iter_count(iter); - unsigned int saved_len; - size_t start; - unsigned int max_pages =3D iov_iter_npages(iter, INT_MAX); - struct page **pages =3D NULL; - struct bio_vec *bv =3D NULL; - - if (iov_iter_is_kvec(iter)) { - memcpy(&ctx->iter, iter, sizeof(*iter)); - ctx->len =3D count; - iov_iter_advance(iter, count); - return 0; - } - - if (array_size(max_pages, sizeof(*bv)) <=3D CIFS_AIO_KMALLOC_LIMIT) - bv =3D kmalloc_array(max_pages, sizeof(*bv), GFP_KERNEL); - - if (!bv) { - bv =3D vmalloc(array_size(max_pages, sizeof(*bv))); - if (!bv) - return -ENOMEM; - } - - if (array_size(max_pages, sizeof(*pages)) <=3D CIFS_AIO_KMALLOC_LIMIT) - pages =3D kmalloc_array(max_pages, sizeof(*pages), GFP_KERNEL); - - if (!pages) { - pages =3D vmalloc(array_size(max_pages, sizeof(*pages))); - if (!pages) { - kvfree(bv); - return -ENOMEM; - } - } - - saved_len =3D count; - - while (count && npages < max_pages) { - rc =3D iov_iter_get_pages2(iter, pages, count, max_pages, &start); - if (rc < 0) { - cifs_dbg(VFS, "Couldn't get user pages (rc=3D%zd)\n", rc); - break; - } - - if (rc > count) { - cifs_dbg(VFS, "get pages rc=3D%zd more than %zu\n", rc, - count); - break; - } - - count -=3D rc; - rc +=3D start; - cur_npages =3D DIV_ROUND_UP(rc, PAGE_SIZE); - - if (npages + cur_npages > max_pages) { - cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n", - npages + cur_npages, max_pages); - break; - } - - for (i =3D 0; i < cur_npages; i++) { - len =3D rc > PAGE_SIZE ? PAGE_SIZE : rc; - bv[npages + i].bv_page =3D pages[i]; - bv[npages + i].bv_offset =3D start; - bv[npages + i].bv_len =3D len - start; - rc -=3D len; - start =3D 0; - } - - npages +=3D cur_npages; - } - - kvfree(pages); - ctx->bv =3D bv; - ctx->len =3D saved_len - count; - ctx->npages =3D npages; - iov_iter_bvec(&ctx->iter, rw, ctx->bv, npages, ctx->len); - return 0; -} - /** * cifs_alloc_hash - allocate hash and hash context together * @name: The name of the crypto hash algo @@ -1129,25 +1046,6 @@ cifs_free_hash(struct shash_desc **sdesc) *sdesc =3D NULL; } =20 -/** - * rqst_page_get_length - obtain the length and offset for a page in smb_r= qst - * @rqst: The request descriptor - * @page: The index of the page to query - * @len: Where to store the length for this page: - * @offset: Where to store the offset for this page - */ -void rqst_page_get_length(const struct smb_rqst *rqst, unsigned int page, - unsigned int *len, unsigned int *offset) -{ - *len =3D rqst->rq_pagesz; - *offset =3D (page =3D=3D 0) ? rqst->rq_offset : 0; - - if (rqst->rq_npages =3D=3D 1 || page =3D=3D rqst->rq_npages-1) - *len =3D rqst->rq_tailsz; - else if (page =3D=3D 0) - *len =3D rqst->rq_pagesz - rqst->rq_offset; -} - void extract_unc_hostname(const char *unc, const char **h, size_t *len) { const char *end; diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c index 7db74354dbeb..744cd7374a43 100644 --- a/fs/cifs/smb2ops.c +++ b/fs/cifs/smb2ops.c @@ -4234,7 +4234,7 @@ fill_transform_hdr(struct smb2_transform_hdr *tr_hdr,= unsigned int orig_len, =20 static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb= _rqst *rqst, int num_rqst, const u8 *sig, u8 **iv, - struct aead_request **req, struct scatterlist **sgl, + struct aead_request **req, struct sg_table *sgt, unsigned int *num_sgs) { unsigned int req_size =3D sizeof(**req) + crypto_aead_reqsize(tfm); @@ -4243,43 +4243,42 @@ static void *smb2_aead_req_alloc(struct crypto_aead= *tfm, const struct smb_rqst u8 *p; =20 *num_sgs =3D cifs_get_num_sgs(rqst, num_rqst, sig); + if (IS_ERR_VALUE((long)(int)*num_sgs)) + return ERR_PTR(*num_sgs); =20 len =3D iv_size; len +=3D crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1); len =3D ALIGN(len, crypto_tfm_ctx_alignment()); len +=3D req_size; len =3D ALIGN(len, __alignof__(struct scatterlist)); - len +=3D *num_sgs * sizeof(**sgl); + len +=3D array_size(*num_sgs, sizeof(struct scatterlist)); =20 - p =3D kmalloc(len, GFP_ATOMIC); + p =3D kvzalloc(len, GFP_NOFS); if (!p) - return NULL; + return ERR_PTR(-ENOMEM); =20 *iv =3D (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1); *req =3D (struct aead_request *)PTR_ALIGN(*iv + iv_size, crypto_tfm_ctx_alignment()); - *sgl =3D (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size, - __alignof__(struct scatterlist)); + sgt->sgl =3D (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size, + __alignof__(struct scatterlist)); return p; } =20 -static void *smb2_get_aead_req(struct crypto_aead *tfm, const struct smb_r= qst *rqst, +static void *smb2_get_aead_req(struct crypto_aead *tfm, struct smb_rqst *r= qst, int num_rqst, const u8 *sig, u8 **iv, struct aead_request **req, struct scatterlist **sgl) { - unsigned int off, len, skip; - struct scatterlist *sg; - unsigned int num_sgs; - unsigned long addr; - int i, j; + struct sg_table sgtable =3D {}; + unsigned int skip, num_sgs, i, j; + ssize_t rc; void *p; =20 - p =3D smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, sgl, &num_sg= s); - if (!p) - return NULL; + p =3D smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, &sgtable, &n= um_sgs); + if (IS_ERR(p)) + return ERR_CAST(p); =20 - sg_init_table(*sgl, num_sgs); - sg =3D *sgl; + sg_init_marker(sgtable.sgl, num_sgs); =20 /* * The first rqst has a transform header where the @@ -4287,30 +4286,29 @@ static void *smb2_get_aead_req(struct crypto_aead *= tfm, const struct smb_rqst *r */ skip =3D 20; =20 - /* Assumes the first rqst has a transform header as the first iov. - * I.e. - * rqst[0].rq_iov[0] is transform header - * rqst[0].rq_iov[1+] data to be encrypted/decrypted - * rqst[1+].rq_iov[0+] data to be encrypted/decrypted - */ for (i =3D 0; i < num_rqst; i++) { - for (j =3D 0; j < rqst[i].rq_nvec; j++) { - struct kvec *iov =3D &rqst[i].rq_iov[j]; + struct iov_iter *iter =3D &rqst[i].rq_iter; + size_t count =3D iov_iter_count(iter); =20 - addr =3D (unsigned long)iov->iov_base + skip; - len =3D iov->iov_len - skip; - sg =3D cifs_sg_set_buf(sg, (void *)addr, len); + for (j =3D 0; j < rqst[i].rq_nvec; j++) { + cifs_sg_set_buf(&sgtable, + rqst[i].rq_iov[j].iov_base + skip, + rqst[i].rq_iov[j].iov_len - skip); =20 /* See the above comment on the 'skip' assignment */ skip =3D 0; } - for (j =3D 0; j < rqst[i].rq_npages; j++) { - rqst_page_get_length(&rqst[i], j, &len, &off); - sg_set_page(sg++, rqst[i].rq_pages[j], len, off); - } + sgtable.orig_nents =3D sgtable.nents; + + rc =3D netfs_extract_iter_to_sg(iter, count, &sgtable, + num_sgs - sgtable.nents, 0); + iov_iter_revert(iter, rc); + sgtable.orig_nents =3D sgtable.nents; } - cifs_sg_set_buf(sg, sig, SMB2_SIGNATURE_SIZE); =20 + cifs_sg_set_buf(&sgtable, sig, SMB2_SIGNATURE_SIZE); + sg_mark_end(&sgtable.sgl[sgtable.nents - 1]); + *sgl =3D sgtable.sgl; return p; } =20 @@ -4398,8 +4396,8 @@ crypt_message(struct TCP_Server_Info *server, int num= _rqst, } =20 creq =3D smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg); - if (unlikely(!creq)) - return -ENOMEM; + if (unlikely(IS_ERR(creq))) + return PTR_ERR(creq); =20 if (!enc) { memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE); @@ -4431,18 +4429,31 @@ crypt_message(struct TCP_Server_Info *server, int n= um_rqst, return rc; } =20 +/* + * Clear a read buffer, discarding the folios which have XA_MARK_0 set. + */ +static void cifs_clear_xarray_buffer(struct xarray *buffer) +{ + struct folio *folio; + + XA_STATE(xas, buffer, 0); + + rcu_read_lock(); + xas_for_each_marked(&xas, folio, ULONG_MAX, XA_MARK_0) { + folio_put(folio); + } + rcu_read_unlock(); + xa_destroy(buffer); +} + void smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst) { - int i, j; + int i; =20 - for (i =3D 0; i < num_rqst; i++) { - if (rqst[i].rq_pages) { - for (j =3D rqst[i].rq_npages - 1; j >=3D 0; j--) - put_page(rqst[i].rq_pages[j]); - kfree(rqst[i].rq_pages); - } - } + for (i =3D 0; i < num_rqst; i++) + if (!xa_empty(&rqst[i].rq_buffer)) + cifs_clear_xarray_buffer(&rqst[i].rq_buffer); } =20 /* @@ -4462,9 +4473,8 @@ static int smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst, struct smb_rqst *new_rq, struct smb_rqst *old_rq) { - struct page **pages; struct smb2_transform_hdr *tr_hdr =3D new_rq[0].rq_iov[0].iov_base; - unsigned int npages; + struct page *page; unsigned int orig_len =3D 0; int i, j; int rc =3D -ENOMEM; @@ -4472,40 +4482,43 @@ smb3_init_transform_rq(struct TCP_Server_Info *serv= er, int num_rqst, for (i =3D 1; i < num_rqst; i++) { struct smb_rqst *old =3D &old_rq[i - 1]; struct smb_rqst *new =3D &new_rq[i]; + struct xarray *buffer =3D &new->rq_buffer; + size_t size =3D iov_iter_count(&old->rq_iter), seg, copied =3D 0; =20 orig_len +=3D smb_rqst_len(server, old); new->rq_iov =3D old->rq_iov; new->rq_nvec =3D old->rq_nvec; =20 - npages =3D old->rq_npages; - if (!npages) - continue; - - pages =3D kmalloc_array(npages, sizeof(struct page *), - GFP_KERNEL); - if (!pages) - goto err_free; - - new->rq_pages =3D pages; - new->rq_npages =3D npages; - new->rq_offset =3D old->rq_offset; - new->rq_pagesz =3D old->rq_pagesz; - new->rq_tailsz =3D old->rq_tailsz; - - for (j =3D 0; j < npages; j++) { - pages[j] =3D alloc_page(GFP_KERNEL|__GFP_HIGHMEM); - if (!pages[j]) - goto err_free; - } + xa_init(buffer); =20 - /* copy pages form the old */ - for (j =3D 0; j < npages; j++) { - unsigned int offset, len; + if (size > 0) { + unsigned int npages =3D DIV_ROUND_UP(size, PAGE_SIZE); =20 - rqst_page_get_length(new, j, &len, &offset); + for (j =3D 0; j < npages; j++) { + void *o; =20 - memcpy_page(new->rq_pages[j], offset, - old->rq_pages[j], offset, len); + rc =3D -ENOMEM; + page =3D alloc_page(GFP_KERNEL|__GFP_HIGHMEM); + if (!page) + goto err_free; + page->index =3D j; + o =3D xa_store(buffer, j, page, GFP_KERNEL); + if (xa_is_err(o)) { + rc =3D xa_err(o); + put_page(page); + goto err_free; + } + + seg =3D min_t(size_t, size - copied, PAGE_SIZE); + if (copy_page_from_iter(page, 0, seg, &old->rq_iter) !=3D seg) { + rc =3D -EFAULT; + goto err_free; + } + copied +=3D seg; + } + iov_iter_xarray(&new->rq_iter, ITER_SOURCE, + buffer, 0, size); + new->rq_iter_size =3D size; } } =20 @@ -4534,12 +4547,12 @@ smb3_is_transform_hdr(void *buf) =20 static int decrypt_raw_data(struct TCP_Server_Info *server, char *buf, - unsigned int buf_data_size, struct page **pages, - unsigned int npages, unsigned int page_data_size, + unsigned int buf_data_size, struct iov_iter *iter, bool is_offloaded) { struct kvec iov[2]; struct smb_rqst rqst =3D {NULL}; + size_t iter_size =3D 0; int rc; =20 iov[0].iov_base =3D buf; @@ -4549,10 +4562,11 @@ decrypt_raw_data(struct TCP_Server_Info *server, ch= ar *buf, =20 rqst.rq_iov =3D iov; rqst.rq_nvec =3D 2; - rqst.rq_pages =3D pages; - rqst.rq_npages =3D npages; - rqst.rq_pagesz =3D PAGE_SIZE; - rqst.rq_tailsz =3D (page_data_size % PAGE_SIZE) ? : PAGE_SIZE; + if (iter) { + rqst.rq_iter =3D *iter; + rqst.rq_iter_size =3D iov_iter_count(iter); + iter_size =3D iov_iter_count(iter); + } =20 rc =3D crypt_message(server, 1, &rqst, 0); cifs_dbg(FYI, "Decrypt message returned %d\n", rc); @@ -4563,73 +4577,37 @@ decrypt_raw_data(struct TCP_Server_Info *server, ch= ar *buf, memmove(buf, iov[1].iov_base, buf_data_size); =20 if (!is_offloaded) - server->total_read =3D buf_data_size + page_data_size; + server->total_read =3D buf_data_size + iter_size; =20 return rc; } =20 static int -read_data_into_pages(struct TCP_Server_Info *server, struct page **pages, - unsigned int npages, unsigned int len) +cifs_copy_pages_to_iter(struct xarray *pages, unsigned int data_size, + unsigned int skip, struct iov_iter *iter) { - int i; - int length; + struct page *page; + unsigned long index; =20 - for (i =3D 0; i < npages; i++) { - struct page *page =3D pages[i]; - size_t n; + xa_for_each(pages, index, page) { + size_t n, len =3D min_t(unsigned int, PAGE_SIZE - skip, data_size); =20 - n =3D len; - if (len >=3D PAGE_SIZE) { - /* enough data to fill the page */ - n =3D PAGE_SIZE; - len -=3D n; - } else { - zero_user(page, len, PAGE_SIZE - len); - len =3D 0; + n =3D copy_page_to_iter(page, skip, len, iter); + if (n !=3D len) { + cifs_dbg(VFS, "%s: something went wrong\n", __func__); + return -EIO; } - length =3D cifs_read_page_from_socket(server, page, 0, n); - if (length < 0) - return length; - server->total_read +=3D length; - } - - return 0; -} - -static int -init_read_bvec(struct page **pages, unsigned int npages, unsigned int data= _size, - unsigned int cur_off, struct bio_vec **page_vec) -{ - struct bio_vec *bvec; - int i; - - bvec =3D kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL); - if (!bvec) - return -ENOMEM; - - for (i =3D 0; i < npages; i++) { - bvec[i].bv_page =3D pages[i]; - bvec[i].bv_offset =3D (i =3D=3D 0) ? cur_off : 0; - bvec[i].bv_len =3D min_t(unsigned int, PAGE_SIZE, data_size); - data_size -=3D bvec[i].bv_len; - } - - if (data_size !=3D 0) { - cifs_dbg(VFS, "%s: something went wrong\n", __func__); - kfree(bvec); - return -EIO; + data_size -=3D n; + skip =3D 0; } =20 - *page_vec =3D bvec; return 0; } =20 static int handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid, - char *buf, unsigned int buf_len, struct page **pages, - unsigned int npages, unsigned int page_data_size, - bool is_offloaded) + char *buf, unsigned int buf_len, struct xarray *pages, + unsigned int pages_len, bool is_offloaded) { unsigned int data_offset; unsigned int data_len; @@ -4638,9 +4616,6 @@ handle_read_data(struct TCP_Server_Info *server, stru= ct mid_q_entry *mid, unsigned int pad_len; struct cifs_readdata *rdata =3D mid->callback_data; struct smb2_hdr *shdr =3D (struct smb2_hdr *)buf; - struct bio_vec *bvec =3D NULL; - struct iov_iter iter; - struct kvec iov; int length; bool use_rdma_mr =3D false; =20 @@ -4729,7 +4704,7 @@ handle_read_data(struct TCP_Server_Info *server, stru= ct mid_q_entry *mid, return 0; } =20 - if (data_len > page_data_size - pad_len) { + if (data_len > pages_len - pad_len) { /* data_len is corrupt -- discard frame */ rdata->result =3D -EIO; if (is_offloaded) @@ -4739,8 +4714,9 @@ handle_read_data(struct TCP_Server_Info *server, stru= ct mid_q_entry *mid, return 0; } =20 - rdata->result =3D init_read_bvec(pages, npages, page_data_size, - cur_off, &bvec); + /* Copy the data to the output I/O iterator. */ + rdata->result =3D cifs_copy_pages_to_iter(pages, pages_len, + cur_off, &rdata->iter); if (rdata->result !=3D 0) { if (is_offloaded) mid->mid_state =3D MID_RESPONSE_MALFORMED; @@ -4748,14 +4724,16 @@ handle_read_data(struct TCP_Server_Info *server, st= ruct mid_q_entry *mid, dequeue_mid(mid, rdata->result); return 0; } + rdata->got_bytes =3D pages_len; =20 - iov_iter_bvec(&iter, ITER_SOURCE, bvec, npages, data_len); } else if (buf_len >=3D data_offset + data_len) { /* read response payload is in buf */ - WARN_ONCE(npages > 0, "read data can be either in buf or in pages"); - iov.iov_base =3D buf + data_offset; - iov.iov_len =3D data_len; - iov_iter_kvec(&iter, ITER_SOURCE, &iov, 1, data_len); + WARN_ONCE(pages && !xa_empty(pages), + "read data can be either in buf or in pages"); + length =3D copy_to_iter(buf + data_offset, data_len, &rdata->iter); + if (length < 0) + return length; + rdata->got_bytes =3D data_len; } else { /* read response payload cannot be in both buf and pages */ WARN_ONCE(1, "buf can not contain only a part of read data"); @@ -4767,26 +4745,18 @@ handle_read_data(struct TCP_Server_Info *server, st= ruct mid_q_entry *mid, return 0; } =20 - length =3D rdata->copy_into_pages(server, rdata, &iter); - - kfree(bvec); - - if (length < 0) - return length; - if (is_offloaded) mid->mid_state =3D MID_RESPONSE_RECEIVED; else dequeue_mid(mid, false); - return length; + return 0; } =20 struct smb2_decrypt_work { struct work_struct decrypt; struct TCP_Server_Info *server; - struct page **ppages; + struct xarray buffer; char *buf; - unsigned int npages; unsigned int len; }; =20 @@ -4795,11 +4765,13 @@ static void smb2_decrypt_offload(struct work_struct= *work) { struct smb2_decrypt_work *dw =3D container_of(work, struct smb2_decrypt_work, decrypt); - int i, rc; + int rc; struct mid_q_entry *mid; + struct iov_iter iter; =20 + iov_iter_xarray(&iter, ITER_DEST, &dw->buffer, 0, dw->len); rc =3D decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_s= ize, - dw->ppages, dw->npages, dw->len, true); + &iter, true); if (rc) { cifs_dbg(VFS, "error decrypting rc=3D%d\n", rc); goto free_pages; @@ -4813,7 +4785,7 @@ static void smb2_decrypt_offload(struct work_struct *= work) mid->decrypted =3D true; rc =3D handle_read_data(dw->server, mid, dw->buf, dw->server->vals->read_rsp_size, - dw->ppages, dw->npages, dw->len, + &dw->buffer, dw->len, true); if (rc >=3D 0) { #ifdef CONFIG_CIFS_STATS2 @@ -4846,10 +4818,7 @@ static void smb2_decrypt_offload(struct work_struct = *work) } =20 free_pages: - for (i =3D dw->npages-1; i >=3D 0; i--) - put_page(dw->ppages[i]); - - kfree(dw->ppages); + cifs_clear_xarray_buffer(&dw->buffer); cifs_small_buf_release(dw->buf); kfree(dw); } @@ -4859,47 +4828,65 @@ static int receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry = **mid, int *num_mids) { + struct page *page; char *buf =3D server->smallbuf; struct smb2_transform_hdr *tr_hdr =3D (struct smb2_transform_hdr *)buf; - unsigned int npages; - struct page **pages; - unsigned int len; + struct iov_iter iter; + unsigned int len, npages; unsigned int buflen =3D server->pdu_size; int rc; int i =3D 0; struct smb2_decrypt_work *dw; =20 + dw =3D kzalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL); + if (!dw) + return -ENOMEM; + xa_init(&dw->buffer); + INIT_WORK(&dw->decrypt, smb2_decrypt_offload); + dw->server =3D server; + *num_mids =3D 1; len =3D min_t(unsigned int, buflen, server->vals->read_rsp_size + sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1; =20 rc =3D cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len); if (rc < 0) - return rc; + goto free_dw; server->total_read +=3D rc; =20 len =3D le32_to_cpu(tr_hdr->OriginalMessageSize) - server->vals->read_rsp_size; + dw->len =3D len; npages =3D DIV_ROUND_UP(len, PAGE_SIZE); =20 - pages =3D kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); - if (!pages) { - rc =3D -ENOMEM; - goto discard_data; - } - + rc =3D -ENOMEM; for (; i < npages; i++) { - pages[i] =3D alloc_page(GFP_KERNEL|__GFP_HIGHMEM); - if (!pages[i]) { - rc =3D -ENOMEM; + void *old; + + page =3D alloc_page(GFP_KERNEL|__GFP_HIGHMEM); + if (!page) + goto discard_data; + page->index =3D i; + old =3D xa_store(&dw->buffer, i, page, GFP_KERNEL); + if (xa_is_err(old)) { + rc =3D xa_err(old); + put_page(page); goto discard_data; } } =20 - /* read read data into pages */ - rc =3D read_data_into_pages(server, pages, npages, len); - if (rc) - goto free_pages; + iov_iter_xarray(&iter, ITER_DEST, &dw->buffer, 0, npages * PAGE_SIZE); + + /* Read the data into the buffer and clear excess bufferage. */ + rc =3D cifs_read_iter_from_socket(server, &iter, dw->len); + if (rc < 0) + goto discard_data; + + server->total_read +=3D rc; + if (rc < npages * PAGE_SIZE) + iov_iter_zero(npages * PAGE_SIZE - rc, &iter); + iov_iter_revert(&iter, npages * PAGE_SIZE); + iov_iter_truncate(&iter, dw->len); =20 rc =3D cifs_discard_remaining_data(server); if (rc) @@ -4912,39 +4899,28 @@ receive_encrypted_read(struct TCP_Server_Info *serv= er, struct mid_q_entry **mid, =20 if ((server->min_offload) && (server->in_flight > 1) && (server->pdu_size >=3D server->min_offload)) { - dw =3D kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL); - if (dw =3D=3D NULL) - goto non_offloaded_decrypt; - dw->buf =3D server->smallbuf; server->smallbuf =3D (char *)cifs_small_buf_get(); =20 - INIT_WORK(&dw->decrypt, smb2_decrypt_offload); - - dw->npages =3D npages; - dw->server =3D server; - dw->ppages =3D pages; - dw->len =3D len; queue_work(decrypt_wq, &dw->decrypt); *num_mids =3D 0; /* worker thread takes care of finding mid */ return -1; } =20 -non_offloaded_decrypt: rc =3D decrypt_raw_data(server, buf, server->vals->read_rsp_size, - pages, npages, len, false); + &iter, false); if (rc) goto free_pages; =20 *mid =3D smb2_find_mid(server, buf); - if (*mid =3D=3D NULL) + if (*mid =3D=3D NULL) { cifs_dbg(FYI, "mid not found\n"); - else { + } else { cifs_dbg(FYI, "mid found\n"); (*mid)->decrypted =3D true; rc =3D handle_read_data(server, *mid, buf, server->vals->read_rsp_size, - pages, npages, len, false); + &dw->buffer, dw->len, false); if (rc >=3D 0) { if (server->ops->is_network_name_deleted) { server->ops->is_network_name_deleted(buf, @@ -4954,9 +4930,9 @@ receive_encrypted_read(struct TCP_Server_Info *server= , struct mid_q_entry **mid, } =20 free_pages: - for (i =3D i - 1; i >=3D 0; i--) - put_page(pages[i]); - kfree(pages); + cifs_clear_xarray_buffer(&dw->buffer); +free_dw: + kfree(dw); return rc; discard_data: cifs_discard_remaining_data(server); @@ -4994,7 +4970,7 @@ receive_encrypted_standard(struct TCP_Server_Info *se= rver, server->total_read +=3D length; =20 buf_size =3D pdu_length - sizeof(struct smb2_transform_hdr); - length =3D decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false); + length =3D decrypt_raw_data(server, buf, buf_size, NULL, false); if (length) return length; =20 @@ -5093,7 +5069,7 @@ smb3_handle_read_data(struct TCP_Server_Info *server,= struct mid_q_entry *mid) char *buf =3D server->large_buf ? server->bigbuf : server->smallbuf; =20 return handle_read_data(server, mid, buf, server->pdu_size, - NULL, 0, 0, false); + NULL, 0, false); } =20 static int diff --git a/fs/cifs/smb2pdu.c b/fs/cifs/smb2pdu.c index c5cb2639b3f1..d55eda476517 100644 --- a/fs/cifs/smb2pdu.c +++ b/fs/cifs/smb2pdu.c @@ -4140,10 +4140,8 @@ smb2_new_read_req(void **buf, unsigned int *total_le= n, struct smbd_buffer_descriptor_v1 *v1; bool need_invalidate =3D server->dialect =3D=3D SMB30_PROT_ID; =20 - rdata->mr =3D smbd_register_mr( - server->smbd_conn, rdata->pages, - rdata->nr_pages, rdata->page_offset, - rdata->tailsz, true, need_invalidate); + rdata->mr =3D smbd_register_mr(server->smbd_conn, &rdata->iter, + true, need_invalidate); if (!rdata->mr) return -EAGAIN; =20 @@ -4200,15 +4198,9 @@ smb2_readv_callback(struct mid_q_entry *mid) (struct smb2_hdr *)rdata->iov[0].iov_base; struct cifs_credits credits =3D { .value =3D 0, .instance =3D 0 }; struct smb_rqst rqst =3D { .rq_iov =3D &rdata->iov[1], - .rq_nvec =3D 1, }; - - if (rdata->got_bytes) { - rqst.rq_pages =3D rdata->pages; - rqst.rq_offset =3D rdata->page_offset; - rqst.rq_npages =3D rdata->nr_pages; - rqst.rq_pagesz =3D rdata->pagesz; - rqst.rq_tailsz =3D rdata->tailsz; - } + .rq_nvec =3D 1, + .rq_iter =3D rdata->iter, + .rq_iter_size =3D iov_iter_count(&rdata->iter), }; =20 WARN_ONCE(rdata->server !=3D mid->server, "rdata server %p !=3D mid server %p", @@ -4226,6 +4218,8 @@ smb2_readv_callback(struct mid_q_entry *mid) if (server->sign && !mid->decrypted) { int rc; =20 + iov_iter_revert(&rqst.rq_iter, rdata->got_bytes); + iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes); rc =3D smb2_verify_signature(&rqst, server); if (rc) cifs_tcon_dbg(VFS, "SMB signature verification returned error =3D %d\n= ", @@ -4568,7 +4562,7 @@ smb2_async_writev(struct cifs_writedata *wdata, req->VolatileFileId =3D io_parms->volatile_fid; req->WriteChannelInfoOffset =3D 0; req->WriteChannelInfoLength =3D 0; - req->Channel =3D 0; + req->Channel =3D SMB2_CHANNEL_NONE; req->Offset =3D cpu_to_le64(io_parms->offset); req->DataOffset =3D cpu_to_le16( offsetof(struct smb2_write_req, Buffer)); @@ -4588,26 +4582,18 @@ smb2_async_writev(struct cifs_writedata *wdata, */ if (smb3_use_rdma_offload(io_parms)) { struct smbd_buffer_descriptor_v1 *v1; + size_t data_size =3D iov_iter_count(&wdata->iter); bool need_invalidate =3D server->dialect =3D=3D SMB30_PROT_ID; =20 - wdata->mr =3D smbd_register_mr( - server->smbd_conn, wdata->pages, - wdata->nr_pages, wdata->page_offset, - wdata->tailsz, false, need_invalidate); + wdata->mr =3D smbd_register_mr(server->smbd_conn, &wdata->iter, + false, need_invalidate); if (!wdata->mr) { rc =3D -EAGAIN; goto async_writev_out; } req->Length =3D 0; req->DataOffset =3D 0; - if (wdata->nr_pages > 1) - req->RemainingBytes =3D - cpu_to_le32( - (wdata->nr_pages - 1) * wdata->pagesz - - wdata->page_offset + wdata->tailsz - ); - else - req->RemainingBytes =3D cpu_to_le32(wdata->tailsz); + req->RemainingBytes =3D cpu_to_le32(data_size); req->Channel =3D SMB2_CHANNEL_RDMA_V1_INVALIDATE; if (need_invalidate) req->Channel =3D SMB2_CHANNEL_RDMA_V1; @@ -4626,19 +4612,14 @@ smb2_async_writev(struct cifs_writedata *wdata, =20 rqst.rq_iov =3D iov; rqst.rq_nvec =3D 1; - rqst.rq_pages =3D wdata->pages; - rqst.rq_offset =3D wdata->page_offset; - rqst.rq_npages =3D wdata->nr_pages; - rqst.rq_pagesz =3D wdata->pagesz; - rqst.rq_tailsz =3D wdata->tailsz; + rqst.rq_iter =3D wdata->iter; + rqst.rq_iter_size =3D iov_iter_count(&rqst.rq_iter); #ifdef CONFIG_CIFS_SMB_DIRECT - if (wdata->mr) { + if (wdata->mr) iov[0].iov_len +=3D sizeof(struct smbd_buffer_descriptor_v1); - rqst.rq_npages =3D 0; - } #endif - cifs_dbg(FYI, "async write at %llu %u bytes\n", - io_parms->offset, io_parms->length); + cifs_dbg(FYI, "async write at %llu %u bytes iter=3D%zx\n", + io_parms->offset, io_parms->length, iov_iter_count(&rqst.rq_iter)); =20 #ifdef CONFIG_CIFS_SMB_DIRECT /* For RDMA read, I/O size is in RemainingBytes not in Length */ diff --git a/fs/cifs/smbdirect.c b/fs/cifs/smbdirect.c index 3e0aacddc291..0eb32bbfc467 100644 --- a/fs/cifs/smbdirect.c +++ b/fs/cifs/smbdirect.c @@ -34,12 +34,6 @@ static int smbd_post_recv( struct smbd_response *response); =20 static int smbd_post_send_empty(struct smbd_connection *info); -static int smbd_post_send_data( - struct smbd_connection *info, - struct kvec *iov, int n_vec, int remaining_data_length); -static int smbd_post_send_page(struct smbd_connection *info, - struct page *page, unsigned long offset, - size_t size, int remaining_data_length); =20 static void destroy_mr_list(struct smbd_connection *info); static int allocate_mr_list(struct smbd_connection *info); @@ -986,24 +980,6 @@ static int smbd_post_send_sgl(struct smbd_connection *= info, return rc; } =20 -/* - * Send a page - * page: the page to send - * offset: offset in the page to send - * size: length in the page to send - * remaining_data_length: remaining data to send in this payload - */ -static int smbd_post_send_page(struct smbd_connection *info, struct page *= page, - unsigned long offset, size_t size, int remaining_data_length) -{ - struct scatterlist sgl; - - sg_init_table(&sgl, 1); - sg_set_page(&sgl, page, size, offset); - - return smbd_post_send_sgl(info, &sgl, size, remaining_data_length); -} - /* * Send an empty message * Empty message is used to extend credits to peer to for keep live @@ -1015,35 +991,6 @@ static int smbd_post_send_empty(struct smbd_connectio= n *info) return smbd_post_send_sgl(info, NULL, 0, 0); } =20 -/* - * Send a data buffer - * iov: the iov array describing the data buffers - * n_vec: number of iov array - * remaining_data_length: remaining data to send following this packet - * in segmented SMBD packet - */ -static int smbd_post_send_data( - struct smbd_connection *info, struct kvec *iov, int n_vec, - int remaining_data_length) -{ - int i; - u32 data_length =3D 0; - struct scatterlist sgl[SMBDIRECT_MAX_SEND_SGE - 1]; - - if (n_vec > SMBDIRECT_MAX_SEND_SGE - 1) { - cifs_dbg(VFS, "Can't fit data to SGL, n_vec=3D%d\n", n_vec); - return -EINVAL; - } - - sg_init_table(sgl, n_vec); - for (i =3D 0; i < n_vec; i++) { - data_length +=3D iov[i].iov_len; - sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len); - } - - return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length); -} - /* * Post a receive request to the transport * The remote peer can only send data when a receive request is posted @@ -1986,6 +1933,42 @@ int smbd_recv(struct smbd_connection *info, struct m= sghdr *msg) return rc; } =20 +/* + * Send the contents of an iterator + * @iter: The iterator to send + * @_remaining_data_length: remaining data to send in this payload + */ +static int smbd_post_send_iter(struct smbd_connection *info, + struct iov_iter *iter, + int *_remaining_data_length) +{ + struct scatterlist sgl[SMBDIRECT_MAX_SEND_SGE - 1]; + unsigned int max_payload =3D info->max_send_size - sizeof(struct smbd_dat= a_transfer); + ssize_t rc; + + /* We're not expecting a user-backed iter */ + WARN_ON(iov_iter_extract_will_pin(iter)); + + do { + struct sg_table sgtable =3D { .sgl =3D sgl }; + size_t maxlen =3D min_t(size_t, *_remaining_data_length, max_payload); + + sg_init_table(sgtable.sgl, ARRAY_SIZE(sgl)); + rc =3D netfs_extract_iter_to_sg(iter, maxlen, + &sgtable, ARRAY_SIZE(sgl), 0); + if (rc < 0) + break; + if (WARN_ON_ONCE(sgtable.nents =3D=3D 0)) + return -EIO; + + sg_mark_end(&sgl[sgtable.nents - 1]); + *_remaining_data_length -=3D rc; + rc =3D smbd_post_send_sgl(info, sgl, rc, *_remaining_data_length); + } while (rc =3D=3D 0 && iov_iter_count(iter) > 0); + + return rc; +} + /* * Send data to transport * Each rqst is transported as a SMBDirect payload @@ -1996,18 +1979,10 @@ int smbd_send(struct TCP_Server_Info *server, int num_rqst, struct smb_rqst *rqst_array) { struct smbd_connection *info =3D server->smbd_conn; - struct kvec vecs[SMBDIRECT_MAX_SEND_SGE - 1]; - int nvecs; - int size; - unsigned int buflen, remaining_data_length; - unsigned int offset, remaining_vec_data_length; - int start, i, j; - int max_iov_size =3D - info->max_send_size - sizeof(struct smbd_data_transfer); - struct kvec *iov; - int rc; struct smb_rqst *rqst; - int rqst_idx; + struct iov_iter iter; + unsigned int remaining_data_length, klen; + int rc, i, rqst_idx; =20 if (info->transport_status !=3D SMBD_CONNECTED) return -EAGAIN; @@ -2034,84 +2009,36 @@ int smbd_send(struct TCP_Server_Info *server, rqst_idx =3D 0; do { rqst =3D &rqst_array[rqst_idx]; - iov =3D rqst->rq_iov; =20 cifs_dbg(FYI, "Sending smb (RDMA): idx=3D%d smb_len=3D%lu\n", - rqst_idx, smb_rqst_len(server, rqst)); - remaining_vec_data_length =3D 0; - for (i =3D 0; i < rqst->rq_nvec; i++) { - remaining_vec_data_length +=3D iov[i].iov_len; - dump_smb(iov[i].iov_base, iov[i].iov_len); - } - - log_write(INFO, "rqst_idx=3D%d nvec=3D%d rqst->rq_npages=3D%d rq_pagesz= =3D%d rq_tailsz=3D%d buflen=3D%lu\n", - rqst_idx, rqst->rq_nvec, - rqst->rq_npages, rqst->rq_pagesz, - rqst->rq_tailsz, smb_rqst_len(server, rqst)); - - start =3D 0; - offset =3D 0; - do { - buflen =3D 0; - i =3D start; - j =3D 0; - while (i < rqst->rq_nvec && - j < SMBDIRECT_MAX_SEND_SGE - 1 && - buflen < max_iov_size) { - - vecs[j].iov_base =3D iov[i].iov_base + offset; - if (buflen + iov[i].iov_len > max_iov_size) { - vecs[j].iov_len =3D - max_iov_size - iov[i].iov_len; - buflen =3D max_iov_size; - offset =3D vecs[j].iov_len; - } else { - vecs[j].iov_len =3D - iov[i].iov_len - offset; - buflen +=3D vecs[j].iov_len; - offset =3D 0; - ++i; - } - ++j; - } + rqst_idx, smb_rqst_len(server, rqst)); + for (i =3D 0; i < rqst->rq_nvec; i++) + dump_smb(rqst->rq_iov[i].iov_base, rqst->rq_iov[i].iov_len); + + log_write(INFO, "RDMA-WR[%u] nvec=3D%d len=3D%u iter=3D%zu rqlen=3D%lu\n= ", + rqst_idx, rqst->rq_nvec, remaining_data_length, + iov_iter_count(&rqst->rq_iter), smb_rqst_len(server, rqst)); + + /* Send the metadata pages. */ + klen =3D 0; + for (i =3D 0; i < rqst->rq_nvec; i++) + klen +=3D rqst->rq_iov[i].iov_len; + iov_iter_kvec(&iter, ITER_SOURCE, rqst->rq_iov, rqst->rq_nvec, klen); + + rc =3D smbd_post_send_iter(info, &iter, &remaining_data_length); + if (rc < 0) + break; =20 - remaining_vec_data_length -=3D buflen; - remaining_data_length -=3D buflen; - log_write(INFO, "sending %s iov[%d] from start=3D%d nvecs=3D%d remainin= g_data_length=3D%d\n", - remaining_vec_data_length > 0 ? - "partial" : "complete", - rqst->rq_nvec, start, j, - remaining_data_length); - - start =3D i; - rc =3D smbd_post_send_data(info, vecs, j, remaining_data_length); - if (rc) - goto done; - } while (remaining_vec_data_length > 0); - - /* now sending pages if there are any */ - for (i =3D 0; i < rqst->rq_npages; i++) { - rqst_page_get_length(rqst, i, &buflen, &offset); - nvecs =3D (buflen + max_iov_size - 1) / max_iov_size; - log_write(INFO, "sending pages buflen=3D%d nvecs=3D%d\n", - buflen, nvecs); - for (j =3D 0; j < nvecs; j++) { - size =3D min_t(unsigned int, max_iov_size, remaining_data_length); - remaining_data_length -=3D size; - log_write(INFO, "sending pages i=3D%d offset=3D%d size=3D%d remaining_= data_length=3D%d\n", - i, j * max_iov_size + offset, size, - remaining_data_length); - rc =3D smbd_post_send_page( - info, rqst->rq_pages[i], - j*max_iov_size + offset, - size, remaining_data_length); - if (rc) - goto done; - } + if (iov_iter_count(&rqst->rq_iter) > 0) { + /* And then the data pages if there are any */ + rc =3D smbd_post_send_iter(info, &rqst->rq_iter, + &remaining_data_length); + if (rc < 0) + break; } + } while (++rqst_idx < num_rqst); =20 -done: /* * As an optimization, we don't wait for individual I/O to finish * before sending the next one. @@ -2315,27 +2242,48 @@ static struct smbd_mr *get_mr(struct smbd_connectio= n *info) goto again; } =20 +/* + * Transcribe the pages from an iterator into an MR scatterlist. + * @iter: The iterator to transcribe + * @_remaining_data_length: remaining data to send in this payload + */ +static int smbd_iter_to_mr(struct smbd_connection *info, + struct iov_iter *iter, + struct scatterlist *sgl, + unsigned int num_pages) +{ + struct sg_table sgtable =3D { .sgl =3D sgl }; + int ret; + + sg_init_table(sgl, num_pages); + + ret =3D netfs_extract_iter_to_sg(iter, iov_iter_count(iter), + &sgtable, num_pages, 0); + WARN_ON(ret < 0); + return ret; +} + /* * Register memory for RDMA read/write - * pages[]: the list of pages to register memory with - * num_pages: the number of pages to register - * tailsz: if non-zero, the bytes to register in the last page + * iter: the buffer to register memory with * writing: true if this is a RDMA write (SMB read), false for RDMA read * need_invalidate: true if this MR needs to be locally invalidated after = I/O * return value: the MR registered, NULL if failed. */ -struct smbd_mr *smbd_register_mr( - struct smbd_connection *info, struct page *pages[], int num_pages, - int offset, int tailsz, bool writing, bool need_invalidate) +struct smbd_mr *smbd_register_mr(struct smbd_connection *info, + struct iov_iter *iter, + bool writing, bool need_invalidate) { struct smbd_mr *smbdirect_mr; - int rc, i; + int rc, num_pages; enum dma_data_direction dir; struct ib_reg_wr *reg_wr; =20 + num_pages =3D iov_iter_npages(iter, info->max_frmr_depth + 1); if (num_pages > info->max_frmr_depth) { log_rdma_mr(ERR, "num_pages=3D%d max_frmr_depth=3D%d\n", num_pages, info->max_frmr_depth); + WARN_ON_ONCE(1); return NULL; } =20 @@ -2344,32 +2292,16 @@ struct smbd_mr *smbd_register_mr( log_rdma_mr(ERR, "get_mr returning NULL\n"); return NULL; } + + dir =3D writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE; + smbdirect_mr->dir =3D dir; smbdirect_mr->need_invalidate =3D need_invalidate; smbdirect_mr->sgl_count =3D num_pages; - sg_init_table(smbdirect_mr->sgl, num_pages); - - log_rdma_mr(INFO, "num_pages=3D0x%x offset=3D0x%x tailsz=3D0x%x\n", - num_pages, offset, tailsz); - - if (num_pages =3D=3D 1) { - sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset); - goto skip_multiple_pages; - } =20 - /* We have at least two pages to register */ - sg_set_page( - &smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset); - i =3D 1; - while (i < num_pages - 1) { - sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0); - i++; - } - sg_set_page(&smbdirect_mr->sgl[i], pages[i], - tailsz ? tailsz : PAGE_SIZE, 0); + log_rdma_mr(INFO, "num_pages=3D0x%x count=3D0x%zx\n", + num_pages, iov_iter_count(iter)); + smbd_iter_to_mr(info, iter, smbdirect_mr->sgl, num_pages); =20 -skip_multiple_pages: - dir =3D writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE; - smbdirect_mr->dir =3D dir; rc =3D ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir); if (!rc) { log_rdma_mr(ERR, "ib_dma_map_sg num_pages=3D%x dir=3D%x rc=3D%x\n", diff --git a/fs/cifs/smbdirect.h b/fs/cifs/smbdirect.h index 207ef979cd51..be2cf18b7fec 100644 --- a/fs/cifs/smbdirect.h +++ b/fs/cifs/smbdirect.h @@ -302,8 +302,8 @@ struct smbd_mr { =20 /* Interfaces to register and deregister MR for RDMA read/write */ struct smbd_mr *smbd_register_mr( - struct smbd_connection *info, struct page *pages[], int num_pages, - int offset, int tailsz, bool writing, bool need_invalidate); + struct smbd_connection *info, struct iov_iter *iter, + bool writing, bool need_invalidate); int smbd_deregister_mr(struct smbd_mr *mr); =20 #else diff --git a/fs/cifs/transport.c b/fs/cifs/transport.c index 83e931824bf2..7ff67a27b361 100644 --- a/fs/cifs/transport.c +++ b/fs/cifs/transport.c @@ -270,26 +270,7 @@ smb_rqst_len(struct TCP_Server_Info *server, struct sm= b_rqst *rqst) for (i =3D 0; i < nvec; i++) buflen +=3D iov[i].iov_len; =20 - /* - * Add in the page array if there is one. The caller needs to make - * sure rq_offset and rq_tailsz are set correctly. If a buffer of - * multiple pages ends at page boundary, rq_tailsz needs to be set to - * PAGE_SIZE. - */ - if (rqst->rq_npages) { - if (rqst->rq_npages =3D=3D 1) - buflen +=3D rqst->rq_tailsz; - else { - /* - * If there is more than one page, calculate the - * buffer length based on rq_offset and rq_tailsz - */ - buflen +=3D rqst->rq_pagesz * (rqst->rq_npages - 1) - - rqst->rq_offset; - buflen +=3D rqst->rq_tailsz; - } - } - + buflen +=3D iov_iter_count(&rqst->rq_iter); return buflen; } =20 @@ -376,23 +357,15 @@ __smb_send_rqst(struct TCP_Server_Info *server, int n= um_rqst, =20 total_len +=3D sent; =20 - /* now walk the page array and send each page in it */ - for (i =3D 0; i < rqst[j].rq_npages; i++) { - struct bio_vec bvec; - - bvec.bv_page =3D rqst[j].rq_pages[i]; - rqst_page_get_length(&rqst[j], i, &bvec.bv_len, - &bvec.bv_offset); - - iov_iter_bvec(&smb_msg.msg_iter, ITER_SOURCE, - &bvec, 1, bvec.bv_len); + if (iov_iter_count(&rqst[j].rq_iter) > 0) { + smb_msg.msg_iter =3D rqst[j].rq_iter; rc =3D smb_send_kvec(server, &smb_msg, &sent); if (rc < 0) break; - total_len +=3D sent; } - } + +} =20 unmask: sigprocmask(SIG_SETMASK, &oldmask, NULL); @@ -1654,11 +1627,11 @@ int cifs_discard_remaining_data(struct TCP_Server_Info *server) { unsigned int rfclen =3D server->pdu_size; - int remaining =3D rfclen + HEADER_PREAMBLE_SIZE(server) - + size_t remaining =3D rfclen + HEADER_PREAMBLE_SIZE(server) - server->total_read; =20 while (remaining > 0) { - int length; + ssize_t length; =20 length =3D cifs_discard_from_socket(server, min_t(size_t, remaining, @@ -1804,10 +1777,15 @@ cifs_readv_receive(struct TCP_Server_Info *server, = struct mid_q_entry *mid) return cifs_readv_discard(server, mid); } =20 - length =3D rdata->read_into_pages(server, rdata, data_len); - if (length < 0) - return length; - +#ifdef CONFIG_CIFS_SMB_DIRECT + if (rdata->mr) + length =3D data_len; /* An RDMA read is already done. */ + else +#endif + length =3D cifs_read_iter_from_socket(server, &rdata->iter, + data_len); + if (length > 0) + rdata->got_bytes +=3D length; server->total_read +=3D length; =20 cifs_dbg(FYI, "total_read=3D%u buflen=3D%u remaining=3D%u\n", From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55616C6379F for ; Fri, 10 Feb 2023 23:35:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230061AbjBJXfF (ORCPT ); Fri, 10 Feb 2023 18:35:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229962AbjBJXeg (ORCPT ); Fri, 10 Feb 2023 18:34:36 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BB5D77BA7 for ; Fri, 10 Feb 2023 15:32:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071951; 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=G8bLco6RK/TV/x/zhGZ3kDinA9d0Eqcu5AZrfy3jfiY=; b=Wrjahy8a57sGR4epUZFtYtST7c2O3fcw3XvSjSSvoOR1vr2qTdPNewpin/oPL8viK79+EW Os+PQFfoITmMLUWaglj0WhQqhYj6Owdeon+EGW57pydaSH2LCjiEWnRPbT3+1i0Kzs90+F WAaDLCjmjbGprpQQZEM57G0MFVdx134= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-315-bkFfxe79OIiLOZbMmTYxwQ-1; Fri, 10 Feb 2023 18:32:30 -0500 X-MC-Unique: bkFfxe79OIiLOZbMmTYxwQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2832B38060FE; Fri, 10 Feb 2023 23:32:29 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5F123140EBF4; Fri, 10 Feb 2023 23:32:27 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French , linux-rdma@vger.kernel.org Subject: [PATCH 08/11] cifs: Build the RDMA SGE list directly from an iterator Date: Fri, 10 Feb 2023 23:32:02 +0000 Message-Id: <20230210233205.1517459-9-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In the depths of the cifs RDMA code, extract part of an iov iterator directly into an SGE list without going through an intermediate scatterlist. Note that this doesn't support extraction from an IOBUF- or UBUF-type iterator (ie. user-supplied buffer). The assumption is that the higher layers will extract those to a BVEC-type iterator first and do whatever is required to stop the pages from going away. Signed-off-by: David Howells cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: Tom Talpey cc: Jeff Layton cc: linux-cifs@vger.kernel.org cc: linux-rdma@vger.kernel.org Link: https://lore.kernel.org/r/166697260361.61150.5064013393408112197.stgi= t@warthog.procyon.org.uk/ # rfc Link: https://lore.kernel.org/r/166732032518.3186319.1859601819981624629.st= git@warthog.procyon.org.uk/ # rfc --- fs/cifs/smbdirect.c | 153 ++++++++++++++++++-------------------------- fs/cifs/smbdirect.h | 3 +- 2 files changed, 63 insertions(+), 93 deletions(-) diff --git a/fs/cifs/smbdirect.c b/fs/cifs/smbdirect.c index 0eb32bbfc467..31c4dc8212c3 100644 --- a/fs/cifs/smbdirect.c +++ b/fs/cifs/smbdirect.c @@ -828,16 +828,16 @@ static int smbd_post_send(struct smbd_connection *inf= o, return rc; } =20 -static int smbd_post_send_sgl(struct smbd_connection *info, - struct scatterlist *sgl, int data_length, int remaining_data_length) +static int smbd_post_send_iter(struct smbd_connection *info, + struct iov_iter *iter, + int *_remaining_data_length) { - int num_sgs; int i, rc; int header_length; + int data_length; struct smbd_request *request; struct smbd_data_transfer *packet; int new_credits; - struct scatterlist *sg; =20 wait_credit: /* Wait for send credits. A SMBD packet needs one credit */ @@ -881,6 +881,30 @@ static int smbd_post_send_sgl(struct smbd_connection *= info, } =20 request->info =3D info; + memset(request->sge, 0, sizeof(request->sge)); + + /* Fill in the data payload to find out how much data we can add */ + if (iter) { + struct smb_extract_to_rdma extract =3D { + .nr_sge =3D 1, + .max_sge =3D SMBDIRECT_MAX_SEND_SGE, + .sge =3D request->sge, + .device =3D info->id->device, + .local_dma_lkey =3D info->pd->local_dma_lkey, + .direction =3D DMA_TO_DEVICE, + }; + + rc =3D smb_extract_iter_to_rdma(iter, *_remaining_data_length, + &extract); + if (rc < 0) + goto err_dma; + data_length =3D rc; + request->num_sge =3D extract.nr_sge; + *_remaining_data_length -=3D data_length; + } else { + data_length =3D 0; + request->num_sge =3D 1; + } =20 /* Fill in the packet header */ packet =3D smbd_request_payload(request); @@ -902,7 +926,7 @@ static int smbd_post_send_sgl(struct smbd_connection *i= nfo, else packet->data_offset =3D cpu_to_le32(24); packet->data_length =3D cpu_to_le32(data_length); - packet->remaining_data_length =3D cpu_to_le32(remaining_data_length); + packet->remaining_data_length =3D cpu_to_le32(*_remaining_data_length); packet->padding =3D 0; =20 log_outgoing(INFO, "credits_requested=3D%d credits_granted=3D%d data_offs= et=3D%d data_length=3D%d remaining_data_length=3D%d\n", @@ -918,7 +942,6 @@ static int smbd_post_send_sgl(struct smbd_connection *i= nfo, if (!data_length) header_length =3D offsetof(struct smbd_data_transfer, padding); =20 - request->num_sge =3D 1; request->sge[0].addr =3D ib_dma_map_single(info->id->device, (void *)packet, header_length, @@ -932,23 +955,6 @@ static int smbd_post_send_sgl(struct smbd_connection *= info, request->sge[0].length =3D header_length; request->sge[0].lkey =3D info->pd->local_dma_lkey; =20 - /* Fill in the packet data payload */ - num_sgs =3D sgl ? sg_nents(sgl) : 0; - for_each_sg(sgl, sg, num_sgs, i) { - request->sge[i+1].addr =3D - ib_dma_map_page(info->id->device, sg_page(sg), - sg->offset, sg->length, DMA_TO_DEVICE); - if (ib_dma_mapping_error( - info->id->device, request->sge[i+1].addr)) { - rc =3D -EIO; - request->sge[i+1].addr =3D 0; - goto err_dma; - } - request->sge[i+1].length =3D sg->length; - request->sge[i+1].lkey =3D info->pd->local_dma_lkey; - request->num_sge++; - } - rc =3D smbd_post_send(info, request); if (!rc) return 0; @@ -987,8 +993,10 @@ static int smbd_post_send_sgl(struct smbd_connection *= info, */ static int smbd_post_send_empty(struct smbd_connection *info) { + int remaining_data_length =3D 0; + info->count_send_empty++; - return smbd_post_send_sgl(info, NULL, 0, 0); + return smbd_post_send_iter(info, NULL, &remaining_data_length); } =20 /* @@ -1933,42 +1941,6 @@ int smbd_recv(struct smbd_connection *info, struct m= sghdr *msg) return rc; } =20 -/* - * Send the contents of an iterator - * @iter: The iterator to send - * @_remaining_data_length: remaining data to send in this payload - */ -static int smbd_post_send_iter(struct smbd_connection *info, - struct iov_iter *iter, - int *_remaining_data_length) -{ - struct scatterlist sgl[SMBDIRECT_MAX_SEND_SGE - 1]; - unsigned int max_payload =3D info->max_send_size - sizeof(struct smbd_dat= a_transfer); - ssize_t rc; - - /* We're not expecting a user-backed iter */ - WARN_ON(iov_iter_extract_will_pin(iter)); - - do { - struct sg_table sgtable =3D { .sgl =3D sgl }; - size_t maxlen =3D min_t(size_t, *_remaining_data_length, max_payload); - - sg_init_table(sgtable.sgl, ARRAY_SIZE(sgl)); - rc =3D netfs_extract_iter_to_sg(iter, maxlen, - &sgtable, ARRAY_SIZE(sgl), 0); - if (rc < 0) - break; - if (WARN_ON_ONCE(sgtable.nents =3D=3D 0)) - return -EIO; - - sg_mark_end(&sgl[sgtable.nents - 1]); - *_remaining_data_length -=3D rc; - rc =3D smbd_post_send_sgl(info, sgl, rc, *_remaining_data_length); - } while (rc =3D=3D 0 && iov_iter_count(iter) > 0); - - return rc; -} - /* * Send data to transport * Each rqst is transported as a SMBDirect payload @@ -2129,10 +2101,10 @@ static void destroy_mr_list(struct smbd_connection = *info) cancel_work_sync(&info->mr_recovery_work); list_for_each_entry_safe(mr, tmp, &info->mr_list, list) { if (mr->state =3D=3D MR_INVALIDATED) - ib_dma_unmap_sg(info->id->device, mr->sgl, - mr->sgl_count, mr->dir); + ib_dma_unmap_sg(info->id->device, mr->sgt.sgl, + mr->sgt.nents, mr->dir); ib_dereg_mr(mr->mr); - kfree(mr->sgl); + kfree(mr->sgt.sgl); kfree(mr); } } @@ -2167,11 +2139,10 @@ static int allocate_mr_list(struct smbd_connection = *info) info->mr_type, info->max_frmr_depth); goto out; } - smbdirect_mr->sgl =3D kcalloc( - info->max_frmr_depth, - sizeof(struct scatterlist), - GFP_KERNEL); - if (!smbdirect_mr->sgl) { + smbdirect_mr->sgt.sgl =3D kcalloc(info->max_frmr_depth, + sizeof(struct scatterlist), + GFP_KERNEL); + if (!smbdirect_mr->sgt.sgl) { log_rdma_mr(ERR, "failed to allocate sgl\n"); ib_dereg_mr(smbdirect_mr->mr); goto out; @@ -2190,7 +2161,7 @@ static int allocate_mr_list(struct smbd_connection *i= nfo) =20 list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) { ib_dereg_mr(smbdirect_mr->mr); - kfree(smbdirect_mr->sgl); + kfree(smbdirect_mr->sgt.sgl); kfree(smbdirect_mr); } return -ENOMEM; @@ -2244,22 +2215,20 @@ static struct smbd_mr *get_mr(struct smbd_connectio= n *info) =20 /* * Transcribe the pages from an iterator into an MR scatterlist. - * @iter: The iterator to transcribe - * @_remaining_data_length: remaining data to send in this payload */ static int smbd_iter_to_mr(struct smbd_connection *info, struct iov_iter *iter, - struct scatterlist *sgl, - unsigned int num_pages) + struct sg_table *sgt, + unsigned int max_sg) { - struct sg_table sgtable =3D { .sgl =3D sgl }; int ret; =20 - sg_init_table(sgl, num_pages); + memset(sgt->sgl, 0, max_sg * sizeof(struct scatterlist)); =20 - ret =3D netfs_extract_iter_to_sg(iter, iov_iter_count(iter), - &sgtable, num_pages, 0); + ret =3D netfs_extract_iter_to_sg(iter, iov_iter_count(iter), sgt, max_sg,= 0); WARN_ON(ret < 0); + if (sgt->nents > 0) + sg_mark_end(&sgt->sgl[sgt->nents - 1]); return ret; } =20 @@ -2296,25 +2265,27 @@ struct smbd_mr *smbd_register_mr(struct smbd_connec= tion *info, dir =3D writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE; smbdirect_mr->dir =3D dir; smbdirect_mr->need_invalidate =3D need_invalidate; - smbdirect_mr->sgl_count =3D num_pages; + smbdirect_mr->sgt.nents =3D 0; + smbdirect_mr->sgt.orig_nents =3D 0; =20 - log_rdma_mr(INFO, "num_pages=3D0x%x count=3D0x%zx\n", - num_pages, iov_iter_count(iter)); - smbd_iter_to_mr(info, iter, smbdirect_mr->sgl, num_pages); + log_rdma_mr(INFO, "num_pages=3D0x%x count=3D0x%zx depth=3D%u\n", + num_pages, iov_iter_count(iter), info->max_frmr_depth); + smbd_iter_to_mr(info, iter, &smbdirect_mr->sgt, info->max_frmr_depth); =20 - rc =3D ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir); + rc =3D ib_dma_map_sg(info->id->device, smbdirect_mr->sgt.sgl, + smbdirect_mr->sgt.nents, dir); if (!rc) { log_rdma_mr(ERR, "ib_dma_map_sg num_pages=3D%x dir=3D%x rc=3D%x\n", num_pages, dir, rc); goto dma_map_error; } =20 - rc =3D ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages, - NULL, PAGE_SIZE); - if (rc !=3D num_pages) { + rc =3D ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgt.sgl, + smbdirect_mr->sgt.nents, NULL, PAGE_SIZE); + if (rc !=3D smbdirect_mr->sgt.nents) { log_rdma_mr(ERR, - "ib_map_mr_sg failed rc =3D %d num_pages =3D %x\n", - rc, num_pages); + "ib_map_mr_sg failed rc =3D %d nents =3D %x\n", + rc, smbdirect_mr->sgt.nents); goto map_mr_error; } =20 @@ -2346,8 +2317,8 @@ struct smbd_mr *smbd_register_mr(struct smbd_connecti= on *info, =20 /* If all failed, attempt to recover this MR by setting it MR_ERROR*/ map_mr_error: - ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl, - smbdirect_mr->sgl_count, smbdirect_mr->dir); + ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgt.sgl, + smbdirect_mr->sgt.nents, smbdirect_mr->dir); =20 dma_map_error: smbdirect_mr->state =3D MR_ERROR; @@ -2414,8 +2385,8 @@ int smbd_deregister_mr(struct smbd_mr *smbdirect_mr) =20 if (smbdirect_mr->state =3D=3D MR_INVALIDATED) { ib_dma_unmap_sg( - info->id->device, smbdirect_mr->sgl, - smbdirect_mr->sgl_count, + info->id->device, smbdirect_mr->sgt.sgl, + smbdirect_mr->sgt.nents, smbdirect_mr->dir); smbdirect_mr->state =3D MR_READY; if (atomic_inc_return(&info->mr_ready_count) =3D=3D 1) diff --git a/fs/cifs/smbdirect.h b/fs/cifs/smbdirect.h index be2cf18b7fec..83f239f376f0 100644 --- a/fs/cifs/smbdirect.h +++ b/fs/cifs/smbdirect.h @@ -288,8 +288,7 @@ struct smbd_mr { struct list_head list; enum mr_state state; struct ib_mr *mr; - struct scatterlist *sgl; - int sgl_count; + struct sg_table sgt; enum dma_data_direction dir; union { struct ib_reg_wr wr; From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8BA84C05027 for ; Fri, 10 Feb 2023 23:35:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230074AbjBJXfI (ORCPT ); Fri, 10 Feb 2023 18:35:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229985AbjBJXel (ORCPT ); Fri, 10 Feb 2023 18:34:41 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 189467A7EE for ; Fri, 10 Feb 2023 15:32:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071955; 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=NOO8aLS0zOpNVPA55FpHi/v1/toiTGDm6ms7/v1npuY=; b=AN7jXeBg9YMhZD7cvp8E+tV2n0WwZ0q28a4gK75ka65NKpIQ02xfBP1613XGC07BdcFW9L wmQZuNO8s9k5fYindtXUJcB9vj05a8vSWUPI5EsVkUkkhK39QWRkQPbo+xavQoiv3yftyO CctOj168Q2knyjRxET25VyabCiYrHio= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-402-7FGl6ThAOsOaJM8txMa-LQ-1; Fri, 10 Feb 2023 18:32:32 -0500 X-MC-Unique: 7FGl6ThAOsOaJM8txMa-LQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6D84138060FE; Fri, 10 Feb 2023 23:32:31 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB30BC158BB; Fri, 10 Feb 2023 23:32:29 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French Subject: [PATCH 09/11] cifs: Remove unused code Date: Fri, 10 Feb 2023 23:32:03 +0000 Message-Id: <20230210233205.1517459-10-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Remove a bunch of functions that are no longer used and are commented out after the conversion to use iterators throughout the I/O path. Signed-off-by: David Howells cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: Jeff Layton cc: linux-cifs@vger.kernel.org Link: https://lore.kernel.org/r/164928621823.457102.8777804402615654773.stg= it@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165211421039.3154751.15199634443157779005.s= tgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165348881165.2106726.2993852968344861224.st= git@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/165364827876.3334034.9331465096417303889.st= git@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/166126396915.708021.2010212654244139442.stg= it@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/166697261080.61150.17513116912567922274.stg= it@warthog.procyon.org.uk/ # rfc Link: https://lore.kernel.org/r/166732033255.3186319.5527423437137895940.st= git@warthog.procyon.org.uk/ # rfc --- fs/cifs/file.c | 606 ------------------------------------------------- 1 file changed, 606 deletions(-) diff --git a/fs/cifs/file.c b/fs/cifs/file.c index 5de37e84a751..f3cedec9d22a 100644 --- a/fs/cifs/file.c +++ b/fs/cifs/file.c @@ -2603,314 +2603,6 @@ static int cifs_partialpagewrite(struct page *page,= unsigned from, unsigned to) return rc; } =20 -#if 0 // TODO: Remove for iov_iter support -static struct cifs_writedata * -wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping, - pgoff_t end, pgoff_t *index, - unsigned int *found_pages) -{ - struct cifs_writedata *wdata; - - wdata =3D cifs_writedata_alloc((unsigned int)tofind, - cifs_writev_complete); - if (!wdata) - return NULL; - - *found_pages =3D find_get_pages_range_tag(mapping, index, end, - PAGECACHE_TAG_DIRTY, tofind, wdata->pages); - return wdata; -} - -static unsigned int -wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages, - struct address_space *mapping, - struct writeback_control *wbc, - pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done) -{ - unsigned int nr_pages =3D 0, i; - struct page *page; - - for (i =3D 0; i < found_pages; i++) { - page =3D wdata->pages[i]; - /* - * At this point we hold neither the i_pages lock nor the - * page lock: the page may be truncated or invalidated - * (changing page->mapping to NULL), or even swizzled - * back from swapper_space to tmpfs file mapping - */ - - if (nr_pages =3D=3D 0) - lock_page(page); - else if (!trylock_page(page)) - break; - - if (unlikely(page->mapping !=3D mapping)) { - unlock_page(page); - break; - } - - if (!wbc->range_cyclic && page->index > end) { - *done =3D true; - unlock_page(page); - break; - } - - if (*next && (page->index !=3D *next)) { - /* Not next consecutive page */ - unlock_page(page); - break; - } - - if (wbc->sync_mode !=3D WB_SYNC_NONE) - wait_on_page_writeback(page); - - if (PageWriteback(page) || - !clear_page_dirty_for_io(page)) { - unlock_page(page); - break; - } - - /* - * This actually clears the dirty bit in the radix tree. - * See cifs_writepage() for more commentary. - */ - set_page_writeback(page); - if (page_offset(page) >=3D i_size_read(mapping->host)) { - *done =3D true; - unlock_page(page); - end_page_writeback(page); - break; - } - - wdata->pages[i] =3D page; - *next =3D page->index + 1; - ++nr_pages; - } - - /* reset index to refind any pages skipped */ - if (nr_pages =3D=3D 0) - *index =3D wdata->pages[0]->index + 1; - - /* put any pages we aren't going to use */ - for (i =3D nr_pages; i < found_pages; i++) { - put_page(wdata->pages[i]); - wdata->pages[i] =3D NULL; - } - - return nr_pages; -} - -static int -wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages, - struct address_space *mapping, struct writeback_control *wbc) -{ - int rc; - - wdata->sync_mode =3D wbc->sync_mode; - wdata->nr_pages =3D nr_pages; - wdata->offset =3D page_offset(wdata->pages[0]); - wdata->pagesz =3D PAGE_SIZE; - wdata->tailsz =3D min(i_size_read(mapping->host) - - page_offset(wdata->pages[nr_pages - 1]), - (loff_t)PAGE_SIZE); - wdata->bytes =3D ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz; - wdata->pid =3D wdata->cfile->pid; - - rc =3D adjust_credits(wdata->server, &wdata->credits, wdata->bytes); - if (rc) - return rc; - - if (wdata->cfile->invalidHandle) - rc =3D -EAGAIN; - else - rc =3D wdata->server->ops->async_writev(wdata, - cifs_writedata_release); - - return rc; -} - -static int -cifs_writepage_locked(struct page *page, struct writeback_control *wbc); - -static int cifs_write_one_page(struct page *page, struct writeback_control= *wbc, - void *data) -{ - struct address_space *mapping =3D data; - int ret; - - ret =3D cifs_writepage_locked(page, wbc); - unlock_page(page); - mapping_set_error(mapping, ret); - return ret; -} - -static int cifs_writepages(struct address_space *mapping, - struct writeback_control *wbc) -{ - struct inode *inode =3D mapping->host; - struct cifs_sb_info *cifs_sb =3D CIFS_SB(inode->i_sb); - struct TCP_Server_Info *server; - bool done =3D false, scanned =3D false, range_whole =3D false; - pgoff_t end, index; - struct cifs_writedata *wdata; - struct cifsFileInfo *cfile =3D NULL; - int rc =3D 0; - int saved_rc =3D 0; - unsigned int xid; - - /* - * If wsize is smaller than the page cache size, default to writing - * one page at a time. - */ - if (cifs_sb->ctx->wsize < PAGE_SIZE) - return write_cache_pages(mapping, wbc, cifs_write_one_page, - mapping); - - xid =3D get_xid(); - if (wbc->range_cyclic) { - index =3D mapping->writeback_index; /* Start from prev offset */ - end =3D -1; - } else { - index =3D wbc->range_start >> PAGE_SHIFT; - end =3D wbc->range_end >> PAGE_SHIFT; - if (wbc->range_start =3D=3D 0 && wbc->range_end =3D=3D LLONG_MAX) - range_whole =3D true; - scanned =3D true; - } - server =3D cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses); - -retry: - while (!done && index <=3D end) { - unsigned int i, nr_pages, found_pages, wsize; - pgoff_t next =3D 0, tofind, saved_index =3D index; - struct cifs_credits credits_on_stack; - struct cifs_credits *credits =3D &credits_on_stack; - int get_file_rc =3D 0; - - if (cfile) - cifsFileInfo_put(cfile); - - rc =3D cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile); - - /* in case of an error store it to return later */ - if (rc) - get_file_rc =3D rc; - - rc =3D server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize, - &wsize, credits); - if (rc !=3D 0) { - done =3D true; - break; - } - - tofind =3D min((wsize / PAGE_SIZE) - 1, end - index) + 1; - - wdata =3D wdata_alloc_and_fillpages(tofind, mapping, end, &index, - &found_pages); - if (!wdata) { - rc =3D -ENOMEM; - done =3D true; - add_credits_and_wake_if(server, credits, 0); - break; - } - - if (found_pages =3D=3D 0) { - kref_put(&wdata->refcount, cifs_writedata_release); - add_credits_and_wake_if(server, credits, 0); - break; - } - - nr_pages =3D wdata_prepare_pages(wdata, found_pages, mapping, wbc, - end, &index, &next, &done); - - /* nothing to write? */ - if (nr_pages =3D=3D 0) { - kref_put(&wdata->refcount, cifs_writedata_release); - add_credits_and_wake_if(server, credits, 0); - continue; - } - - wdata->credits =3D credits_on_stack; - wdata->cfile =3D cfile; - wdata->server =3D server; - cfile =3D NULL; - - if (!wdata->cfile) { - cifs_dbg(VFS, "No writable handle in writepages rc=3D%d\n", - get_file_rc); - if (is_retryable_error(get_file_rc)) - rc =3D get_file_rc; - else - rc =3D -EBADF; - } else - rc =3D wdata_send_pages(wdata, nr_pages, mapping, wbc); - - for (i =3D 0; i < nr_pages; ++i) - unlock_page(wdata->pages[i]); - - /* send failure -- clean up the mess */ - if (rc !=3D 0) { - add_credits_and_wake_if(server, &wdata->credits, 0); - for (i =3D 0; i < nr_pages; ++i) { - if (is_retryable_error(rc)) - redirty_page_for_writepage(wbc, - wdata->pages[i]); - else - SetPageError(wdata->pages[i]); - end_page_writeback(wdata->pages[i]); - put_page(wdata->pages[i]); - } - if (!is_retryable_error(rc)) - mapping_set_error(mapping, rc); - } - kref_put(&wdata->refcount, cifs_writedata_release); - - if (wbc->sync_mode =3D=3D WB_SYNC_ALL && rc =3D=3D -EAGAIN) { - index =3D saved_index; - continue; - } - - /* Return immediately if we received a signal during writing */ - if (is_interrupt_error(rc)) { - done =3D true; - break; - } - - if (rc !=3D 0 && saved_rc =3D=3D 0) - saved_rc =3D rc; - - wbc->nr_to_write -=3D nr_pages; - if (wbc->nr_to_write <=3D 0) - done =3D true; - - index =3D next; - } - - if (!scanned && !done) { - /* - * We hit the last page and there is more work to be done: wrap - * back to the start of the file - */ - scanned =3D true; - index =3D 0; - goto retry; - } - - if (saved_rc !=3D 0) - rc =3D saved_rc; - - if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) - mapping->writeback_index =3D index; - - if (cfile) - cifsFileInfo_put(cfile); - free_xid(xid); - /* Indication to update ctime and mtime as close is deferred */ - set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags); - return rc; -} -#endif - /* * Extend the region to be written back to include subsequent contiguously * dirty pages if possible, but don't sleep while doing so. @@ -3506,49 +3198,6 @@ int cifs_flush(struct file *file, fl_owner_t id) return rc; } =20 -#if 0 // TODO: Remove for iov_iter support -static int -cifs_write_allocate_pages(struct page **pages, unsigned long num_pages) -{ - int rc =3D 0; - unsigned long i; - - for (i =3D 0; i < num_pages; i++) { - pages[i] =3D alloc_page(GFP_KERNEL|__GFP_HIGHMEM); - if (!pages[i]) { - /* - * save number of pages we have already allocated and - * return with ENOMEM error - */ - num_pages =3D i; - rc =3D -ENOMEM; - break; - } - } - - if (rc) { - for (i =3D 0; i < num_pages; i++) - put_page(pages[i]); - } - return rc; -} - -static inline -size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len) -{ - size_t num_pages; - size_t clen; - - clen =3D min_t(const size_t, len, wsize); - num_pages =3D DIV_ROUND_UP(clen, PAGE_SIZE); - - if (cur_len) - *cur_len =3D clen; - - return num_pages; -} -#endif - static void cifs_uncached_writedata_release(struct kref *refcount) { @@ -3581,50 +3230,6 @@ cifs_uncached_writev_complete(struct work_struct *wo= rk) kref_put(&wdata->refcount, cifs_uncached_writedata_release); } =20 -#if 0 // TODO: Remove for iov_iter support -static int -wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from, - size_t *len, unsigned long *num_pages) -{ - size_t save_len, copied, bytes, cur_len =3D *len; - unsigned long i, nr_pages =3D *num_pages; - - save_len =3D cur_len; - for (i =3D 0; i < nr_pages; i++) { - bytes =3D min_t(const size_t, cur_len, PAGE_SIZE); - copied =3D copy_page_from_iter(wdata->pages[i], 0, bytes, from); - cur_len -=3D copied; - /* - * If we didn't copy as much as we expected, then that - * may mean we trod into an unmapped area. Stop copying - * at that point. On the next pass through the big - * loop, we'll likely end up getting a zero-length - * write and bailing out of it. - */ - if (copied < bytes) - break; - } - cur_len =3D save_len - cur_len; - *len =3D cur_len; - - /* - * If we have no data to send, then that probably means that - * the copy above failed altogether. That's most likely because - * the address in the iovec was bogus. Return -EFAULT and let - * the caller free anything we allocated and bail out. - */ - if (!cur_len) - return -EFAULT; - - /* - * i + 1 now represents the number of pages we actually used in - * the copy phase above. - */ - *num_pages =3D i + 1; - return 0; -} -#endif - static int cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_li= st, struct cifs_aio_ctx *ctx) @@ -4211,83 +3816,6 @@ cifs_uncached_readv_complete(struct work_struct *wor= k) kref_put(&rdata->refcount, cifs_readdata_release); } =20 -#if 0 // TODO: Remove for iov_iter support - -static int -uncached_fill_pages(struct TCP_Server_Info *server, - struct cifs_readdata *rdata, struct iov_iter *iter, - unsigned int len) -{ - int result =3D 0; - unsigned int i; - unsigned int nr_pages =3D rdata->nr_pages; - unsigned int page_offset =3D rdata->page_offset; - - rdata->got_bytes =3D 0; - rdata->tailsz =3D PAGE_SIZE; - for (i =3D 0; i < nr_pages; i++) { - struct page *page =3D rdata->pages[i]; - size_t n; - unsigned int segment_size =3D rdata->pagesz; - - if (i =3D=3D 0) - segment_size -=3D page_offset; - else - page_offset =3D 0; - - - if (len <=3D 0) { - /* no need to hold page hostage */ - rdata->pages[i] =3D NULL; - rdata->nr_pages--; - put_page(page); - continue; - } - - n =3D len; - if (len >=3D segment_size) - /* enough data to fill the page */ - n =3D segment_size; - else - rdata->tailsz =3D len; - len -=3D n; - - if (iter) - result =3D copy_page_from_iter( - page, page_offset, n, iter); -#ifdef CONFIG_CIFS_SMB_DIRECT - else if (rdata->mr) - result =3D n; -#endif - else - result =3D cifs_read_page_from_socket( - server, page, page_offset, n); - if (result < 0) - break; - - rdata->got_bytes +=3D result; - } - - return result !=3D -ECONNABORTED && rdata->got_bytes > 0 ? - rdata->got_bytes : result; -} - -static int -cifs_uncached_read_into_pages(struct TCP_Server_Info *server, - struct cifs_readdata *rdata, unsigned int len) -{ - return uncached_fill_pages(server, rdata, NULL, len); -} - -static int -cifs_uncached_copy_into_pages(struct TCP_Server_Info *server, - struct cifs_readdata *rdata, - struct iov_iter *iter) -{ - return uncached_fill_pages(server, rdata, iter, iter->count); -} -#endif - static int cifs_resend_rdata(struct cifs_readdata *rdata, struct list_head *rdata_list, struct cifs_aio_ctx *ctx) @@ -4897,140 +4425,6 @@ int cifs_file_mmap(struct file *file, struct vm_are= a_struct *vma) return rc; } =20 -#if 0 // TODO: Remove for iov_iter support - -static void -cifs_readv_complete(struct work_struct *work) -{ - unsigned int i, got_bytes; - struct cifs_readdata *rdata =3D container_of(work, - struct cifs_readdata, work); - - got_bytes =3D rdata->got_bytes; - for (i =3D 0; i < rdata->nr_pages; i++) { - struct page *page =3D rdata->pages[i]; - - if (rdata->result =3D=3D 0 || - (rdata->result =3D=3D -EAGAIN && got_bytes)) { - flush_dcache_page(page); - SetPageUptodate(page); - } else - SetPageError(page); - - if (rdata->result =3D=3D 0 || - (rdata->result =3D=3D -EAGAIN && got_bytes)) - cifs_readpage_to_fscache(rdata->mapping->host, page); - - unlock_page(page); - - got_bytes -=3D min_t(unsigned int, PAGE_SIZE, got_bytes); - - put_page(page); - rdata->pages[i] =3D NULL; - } - kref_put(&rdata->refcount, cifs_readdata_release); -} - -static int -readpages_fill_pages(struct TCP_Server_Info *server, - struct cifs_readdata *rdata, struct iov_iter *iter, - unsigned int len) -{ - int result =3D 0; - unsigned int i; - u64 eof; - pgoff_t eof_index; - unsigned int nr_pages =3D rdata->nr_pages; - unsigned int page_offset =3D rdata->page_offset; - - /* determine the eof that the server (probably) has */ - eof =3D CIFS_I(rdata->mapping->host)->server_eof; - eof_index =3D eof ? (eof - 1) >> PAGE_SHIFT : 0; - cifs_dbg(FYI, "eof=3D%llu eof_index=3D%lu\n", eof, eof_index); - - rdata->got_bytes =3D 0; - rdata->tailsz =3D PAGE_SIZE; - for (i =3D 0; i < nr_pages; i++) { - struct page *page =3D rdata->pages[i]; - unsigned int to_read =3D rdata->pagesz; - size_t n; - - if (i =3D=3D 0) - to_read -=3D page_offset; - else - page_offset =3D 0; - - n =3D to_read; - - if (len >=3D to_read) { - len -=3D to_read; - } else if (len > 0) { - /* enough for partial page, fill and zero the rest */ - zero_user(page, len + page_offset, to_read - len); - n =3D rdata->tailsz =3D len; - len =3D 0; - } else if (page->index > eof_index) { - /* - * The VFS will not try to do readahead past the - * i_size, but it's possible that we have outstanding - * writes with gaps in the middle and the i_size hasn't - * caught up yet. Populate those with zeroed out pages - * to prevent the VFS from repeatedly attempting to - * fill them until the writes are flushed. - */ - zero_user(page, 0, PAGE_SIZE); - flush_dcache_page(page); - SetPageUptodate(page); - unlock_page(page); - put_page(page); - rdata->pages[i] =3D NULL; - rdata->nr_pages--; - continue; - } else { - /* no need to hold page hostage */ - unlock_page(page); - put_page(page); - rdata->pages[i] =3D NULL; - rdata->nr_pages--; - continue; - } - - if (iter) - result =3D copy_page_from_iter( - page, page_offset, n, iter); -#ifdef CONFIG_CIFS_SMB_DIRECT - else if (rdata->mr) - result =3D n; -#endif - else - result =3D cifs_read_page_from_socket( - server, page, page_offset, n); - if (result < 0) - break; - - rdata->got_bytes +=3D result; - } - - return result !=3D -ECONNABORTED && rdata->got_bytes > 0 ? - rdata->got_bytes : result; -} - -static int -cifs_readpages_read_into_pages(struct TCP_Server_Info *server, - struct cifs_readdata *rdata, unsigned int len) -{ - return readpages_fill_pages(server, rdata, NULL, len); -} - -static int -cifs_readpages_copy_into_pages(struct TCP_Server_Info *server, - struct cifs_readdata *rdata, - struct iov_iter *iter) -{ - return readpages_fill_pages(server, rdata, iter, iter->count); -} -#endif - /* * Unlock a bunch of folios in the pagecache. */ From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98A01C636D7 for ; Fri, 10 Feb 2023 23:35:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230006AbjBJXfl (ORCPT ); Fri, 10 Feb 2023 18:35:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229866AbjBJXep (ORCPT ); Fri, 10 Feb 2023 18:34:45 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 159CB7E3F9 for ; Fri, 10 Feb 2023 15:32:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071960; 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=nghgmX+bncPINiD3wsT1CpIOvgAN1XoBCxD2UY2cfe0=; b=JRR9BoHJFgkN8eZUt3KufHI98+nLQhX1LxME6RYjZ2qyrX8fCW8lyBo/UJcTAhPV/W9eCc xp2mE02pF4UNFhO4TOWQt1D2ZpTgYNL2YtLuAa/jKBMyglC86ICsQ6hRaiWTRXPZr+D+oi xgME4xCqH6HHTMMUi+18Rw6/oUKEYZU= 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-444-vDZId1jcMpaD_5ERbR-ZKQ-1; Fri, 10 Feb 2023 18:32:34 -0500 X-MC-Unique: vDZId1jcMpaD_5ERbR-ZKQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 97A6585A588; Fri, 10 Feb 2023 23:32:33 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 09F322026D68; Fri, 10 Feb 2023 23:32:31 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Steve French Subject: [PATCH 10/11] cifs: DIO to/from KVEC-type iterators should now work Date: Fri, 10 Feb 2023 23:32:04 +0000 Message-Id: <20230210233205.1517459-11-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" DIO to/from KVEC-type iterators should now work as the iterator is passed down to the socket in non-RDMA/non-crypto mode and in RDMA or crypto mode care is taken to handle vmap/vmalloc correctly and not take page refs when building a scatterlist. Signed-off-by: David Howells cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: Tom Talpey cc: Jeff Layton cc: linux-cifs@vger.kernel.org --- fs/cifs/file.c | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/fs/cifs/file.c b/fs/cifs/file.c index f3cedec9d22a..a5d54ae6aaa6 100644 --- a/fs/cifs/file.c +++ b/fs/cifs/file.c @@ -3546,16 +3546,6 @@ static ssize_t __cifs_writev( struct cifs_aio_ctx *ctx; int rc; =20 - /* - * iov_iter_get_pages_alloc doesn't work with ITER_KVEC. - * In this case, fall back to non-direct write function. - * this could be improved by getting pages directly in ITER_KVEC - */ - if (direct && iov_iter_is_kvec(from)) { - cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n"); - direct =3D false; - } - rc =3D generic_write_checks(iocb, from); if (rc <=3D 0) return rc; @@ -4089,16 +4079,6 @@ static ssize_t __cifs_readv( loff_t offset =3D iocb->ki_pos; struct cifs_aio_ctx *ctx; =20 - /* - * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC, - * fall back to data copy read path - * this could be improved by getting pages directly in ITER_KVEC - */ - if (direct && iov_iter_is_kvec(to)) { - cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n"); - direct =3D false; - } - len =3D iov_iter_count(to); if (!len) return 0; From nobody Fri Sep 12 08:58:00 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D31FCC05027 for ; Fri, 10 Feb 2023 23:35:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230084AbjBJXfM (ORCPT ); Fri, 10 Feb 2023 18:35:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229987AbjBJXel (ORCPT ); Fri, 10 Feb 2023 18:34:41 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A0EC7E020 for ; Fri, 10 Feb 2023 15:32:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676071959; 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=aLmvd4pDzTYQinLjtfbwp5ScV5jm5Tl15qoPAkwo1Wg=; b=axb5sUMTFwqnNTI0sYeiDXXsyzK2aa5xInecpoqb01aJs6YmJJ5orK1opMpb5lfxOYhO8C BOimzoCBMn2h/fG2/v/iHHjh4qrXZxcdmOhSC6SPBzQhZA6ZrzlJDyV82z4pW7IfMWYCsP PiBqFOkstucO+h6RsoKVdW/rupLQojw= 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-588-wILtai1rOhmKtgpOFoLSzg-1; Fri, 10 Feb 2023 18:32:36 -0500 X-MC-Unique: wILtai1rOhmKtgpOFoLSzg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 08152101A521; Fri, 10 Feb 2023 23:32:36 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 56FF0492B00; Fri, 10 Feb 2023 23:32:34 +0000 (UTC) From: David Howells To: Steve French Cc: David Howells , Al Viro , Shyam Prasad N , Rohith Surabattula , Tom Talpey , Stefan Metzmacher , Christoph Hellwig , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Long Li , Namjae Jeon Subject: [PATCH 11/11] cifs: Fix problem with encrypted RDMA data read Date: Fri, 10 Feb 2023 23:32:05 +0000 Message-Id: <20230210233205.1517459-12-dhowells@redhat.com> In-Reply-To: <20230210233205.1517459-1-dhowells@redhat.com> References: <20230210233205.1517459-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When the cifs client is talking to the ksmbd server by RDMA and the ksmbd server has "smb3 encryption =3D yes" in its config file, the normal PDU stream is encrypted, but the directly-delivered data isn't in the stream (and isn't encrypted), but is rather delivered by DDP/RDMA packets (at least with IWarp). Currently, the direct delivery fails with: buf can not contain only a part of read data WARNING: CPU: 0 PID: 4619 at fs/cifs/smb2ops.c:4731 handle_read_data+0x3= 93/0x405 ... RIP: 0010:handle_read_data+0x393/0x405 ... smb3_handle_read_data+0x30/0x37 receive_encrypted_standard+0x141/0x224 cifs_demultiplex_thread+0x21a/0x63b kthread+0xe7/0xef ret_from_fork+0x22/0x30 The problem apparently stemming from the fact that it's trying to manage the decryption, but the data isn't in the smallbuf, the bigbuf or the page array). This can be fixed simply by inserting an extra case into handle_read_data() that checks to see if use_rdma_mr is true, and if it is, just setting rdata->got_bytes to the length of data delivered and allowing normal continuation. This can be seen in an IWarp packet trace. With the upstream code, it does a DDP/RDMA packet, which produces the warning above and then retries, retrieving the data inline, spread across several SMBDirect messages that get glued together into a single PDU. With the patch applied, only the DDP/RDMA packet is seen. Note that this doesn't happen if the server isn't told to encrypt stuff and it does also happen with softRoCE. Signed-off-by: David Howells cc: Steve French cc: Tom Talpey cc: Long Li cc: Namjae Jeon cc: Stefan Metzmacher cc: linux-cifs@vger.kernel.org Link: https://lore.kernel.org/r/166855224228.1998592.2212551359609792175.st= git@warthog.procyon.org.uk/ # v1 --- fs/cifs/smb2ops.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c index 744cd7374a43..636175850ca7 100644 --- a/fs/cifs/smb2ops.c +++ b/fs/cifs/smb2ops.c @@ -4734,6 +4734,9 @@ handle_read_data(struct TCP_Server_Info *server, stru= ct mid_q_entry *mid, if (length < 0) return length; rdata->got_bytes =3D data_len; + } else if (use_rdma_mr) { + /* The data was delivered directly by RDMA. */ + rdata->got_bytes =3D data_len; } else { /* read response payload cannot be in both buf and pages */ WARN_ONCE(1, "buf can not contain only a part of read data");