From nobody Tue Sep 16 05:15:13 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 42616C5479D for ; Sat, 7 Jan 2023 00:36:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236639AbjAGAgd (ORCPT ); Fri, 6 Jan 2023 19:36:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236584AbjAGAfU (ORCPT ); Fri, 6 Jan 2023 19:35:20 -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 EBB6A84BC9 for ; Fri, 6 Jan 2023 16:34:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673051668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OhwJrX+/uggIQK7yrDCabQsdugOCpi4Tzwg2wYNTyQg=; b=GeEcMfq/mZi+5kdD4iJvuPJpg/Ri5mIo8fGNRLeMQ3sc6HI3XPrNV9twCpqUWQYOz0jTXh Z0XxsZHSL4FJd2SKZhBBcl8POcbvnv1xsqW4InPSIx8KgpeDUtTqeGCb2cUyzgTekrxZCw v9YXOKRgarvP9czZ5peJw2C5KVImY7o= 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-26-Cddw6XzOOKa2Yet_lD6Qbw-1; Fri, 06 Jan 2023 19:34:23 -0500 X-MC-Unique: Cddw6XzOOKa2Yet_lD6Qbw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 640A7802D19; Sat, 7 Jan 2023 00:34:23 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.87]) by smtp.corp.redhat.com (Postfix) with ESMTP id 17AD1492B07; Sat, 7 Jan 2023 00:34:21 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH v4 7/7] iov_iter, block: Make bio structs pin pages rather than ref'ing if appropriate From: David Howells To: Al Viro Cc: Jens Axboe , Christoph Hellwig , Matthew Wilcox , Logan Gunthorpe , dhowells@redhat.com, Christoph Hellwig , Matthew Wilcox , Jens Axboe , Jeff Layton , Logan Gunthorpe , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Date: Sat, 07 Jan 2023 00:34:21 +0000 Message-ID: <167305166150.1521586.10220949115402059720.stgit@warthog.procyon.org.uk> In-Reply-To: <167305160937.1521586.133299343565358971.stgit@warthog.procyon.org.uk> References: <167305160937.1521586.133299343565358971.stgit@warthog.procyon.org.uk> User-Agent: StGit/1.5 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Convert the block layer's bio code to use iov_iter_extract_pages() instead of iov_iter_get_pages(). This will pin pages or leave them unaltered rather than getting a ref on them as appropriate to the source iterator. A field, bi_cleanup_mode, is added to the bio struct that gets set by iov_iter_extract_pages() with FOLL_* flags indicating what cleanup is necessary. FOLL_GET -> put_page(), FOLL_PIN -> unpin_user_page(). Other flags could also be used in future. Newly allocated bio structs have bi_cleanup_mode set to FOLL_GET to indicate that attached pages are ref'd by default. Cloning sets it to 0. __bio_iov_iter_get_pages() overrides it to what iov_iter_extract_pages() indicates. [!] Note that this is tested a bit with ext4, but nothing else. Signed-off-by: David Howells cc: Al Viro cc: Jens Axboe cc: Christoph Hellwig cc: Matthew Wilcox cc: Logan Gunthorpe --- block/bio.c | 47 +++++++++++++++++++++++++++++++++--------= ---- include/linux/blk_types.h | 1 + 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/block/bio.c b/block/bio.c index 5f96fcae3f75..eafcbeba0bab 100644 --- a/block/bio.c +++ b/block/bio.c @@ -243,6 +243,11 @@ static void bio_free(struct bio *bio) * Users of this function have their own bio allocation. Subsequently, * they must remember to pair any call to bio_init() with bio_uninit() * when IO has completed, or when the bio is released. + * + * We set the initial assumption that pages attached to the bio will be + * released with put_page() by setting bi_cleanup_mode to FOLL_GET, but th= is + * should be set to FOLL_PIN if the page should be unpinned instead; if the + * pages should not be put or unpinned, this should be set to 0 */ void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *= table, unsigned short max_vecs, blk_opf_t opf) @@ -274,6 +279,7 @@ void bio_init(struct bio *bio, struct block_device *bde= v, struct bio_vec *table, #ifdef CONFIG_BLK_DEV_INTEGRITY bio->bi_integrity =3D NULL; #endif + bio->bi_cleanup_mode =3D FOLL_GET; bio->bi_vcnt =3D 0; =20 atomic_set(&bio->__bi_remaining, 1); @@ -302,6 +308,7 @@ void bio_reset(struct bio *bio, struct block_device *bd= ev, blk_opf_t opf) { bio_uninit(bio); memset(bio, 0, BIO_RESET_BYTES); + bio->bi_cleanup_mode =3D FOLL_GET; atomic_set(&bio->__bi_remaining, 1); bio->bi_bdev =3D bdev; if (bio->bi_bdev) @@ -814,6 +821,7 @@ static int __bio_clone(struct bio *bio, struct bio *bio= _src, gfp_t gfp) bio_set_flag(bio, BIO_CLONED); bio->bi_ioprio =3D bio_src->bi_ioprio; bio->bi_iter =3D bio_src->bi_iter; + bio->bi_cleanup_mode =3D 0; =20 if (bio->bi_bdev) { if (bio->bi_bdev =3D=3D bio_src->bi_bdev && @@ -1168,6 +1176,18 @@ bool bio_add_folio(struct bio *bio, struct folio *fo= lio, size_t len, return bio_add_page(bio, &folio->page, len, off) > 0; } =20 +/* + * Clean up a page according to the mode indicated by iov_iter_extract_pag= es(), + * where the page is may be pinned or may have a ref taken on it. + */ +static void bio_release_page(struct bio *bio, struct page *page) +{ + if (bio->bi_cleanup_mode & FOLL_PIN) + unpin_user_page(page); + if (bio->bi_cleanup_mode & FOLL_GET) + put_page(page); +} + void __bio_release_pages(struct bio *bio, bool mark_dirty) { struct bvec_iter_all iter_all; @@ -1176,7 +1196,7 @@ void __bio_release_pages(struct bio *bio, bool mark_d= irty) bio_for_each_segment_all(bvec, bio, iter_all) { if (mark_dirty && !PageCompound(bvec->bv_page)) set_page_dirty_lock(bvec->bv_page); - put_page(bvec->bv_page); + bio_release_page(bio, bvec->bv_page); } } EXPORT_SYMBOL_GPL(__bio_release_pages); @@ -1213,7 +1233,7 @@ static int bio_iov_add_page(struct bio *bio, struct p= age *page, } =20 if (same_page) - put_page(page); + bio_release_page(bio, page); return 0; } =20 @@ -1227,7 +1247,7 @@ static int bio_iov_add_zone_append_page(struct bio *b= io, struct page *page, queue_max_zone_append_sectors(q), &same_page) !=3D len) return -EINVAL; if (same_page) - put_page(page); + bio_release_page(bio, page); return 0; } =20 @@ -1238,10 +1258,10 @@ static int bio_iov_add_zone_append_page(struct bio = *bio, struct page *page, * @bio: bio to add pages to * @iter: iov iterator describing the region to be mapped * - * Pins pages from *iter and appends them to @bio's bvec array. The - * pages will have to be released using put_page() when done. - * For multi-segment *iter, this function only adds pages from the - * next non-empty segment of the iov iterator. + * Pins pages from *iter and appends them to @bio's bvec array. The pages= will + * have to be released using put_page() or unpin_user_page() when done as + * according to bi_cleanup_mode. For multi-segment *iter, this function o= nly + * adds pages from the next non-empty segment of the iov iterator. */ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) { @@ -1273,9 +1293,10 @@ static int __bio_iov_iter_get_pages(struct bio *bio,= struct iov_iter *iter) * result to ensure the bio's total size is correct. The remainder of * the iov data will be picked up in the next bio iteration. */ - size =3D iov_iter_get_pages(iter, pages, - UINT_MAX - bio->bi_iter.bi_size, - nr_pages, &offset, gup_flags); + size =3D iov_iter_extract_pages(iter, &pages, + UINT_MAX - bio->bi_iter.bi_size, + nr_pages, gup_flags, + &offset, &bio->bi_cleanup_mode); if (unlikely(size <=3D 0)) return size ? size : -EFAULT; =20 @@ -1308,7 +1329,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, = struct iov_iter *iter) iov_iter_revert(iter, left); out: while (i < nr_pages) - put_page(pages[i++]); + bio_release_page(bio, pages[i++]); =20 return ret; } @@ -1489,8 +1510,8 @@ void bio_set_pages_dirty(struct bio *bio) * the BIO and re-dirty the pages in process context. * * It is expected that bio_check_pages_dirty() will wholly own the BIO from - * here on. It will run one put_page() against each page and will run one - * bio_put() against the BIO. + * here on. It will run one put_page() or unpin_user_page() against each = page + * and will run one bio_put() against the BIO. */ =20 static void bio_dirty_fn(struct work_struct *work); diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index 99be590f952f..883f873a01ef 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -289,6 +289,7 @@ struct bio { #endif }; =20 + unsigned int bi_cleanup_mode; /* How to clean up pages */ unsigned short bi_vcnt; /* how many bio_vec's */ =20 /*