Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3437607pxb; Mon, 1 Nov 2021 13:50:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxdX44mEbF48PoWJvjK48p5A3lm/mJY2Elwt/OQfTml0bqKByqIrZgfoD/pwTUPj2DpPat X-Received: by 2002:a17:906:c015:: with SMTP id e21mr39436205ejz.113.1635799837850; Mon, 01 Nov 2021 13:50:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635799837; cv=none; d=google.com; s=arc-20160816; b=WtWrP6Ie+uSvhpMUUYkC/Dbae2oBu7+FkUJpRODgKlB/e/3wLMI8l7mHzzNszCuSRR iz6gHRl5AZIWXe9ccmm8pn8Fi3SYLjLvySOFpka9zN9jBcGe8EBbdy54XNW+r5gozVbR k6URfdA0C7mGTw+0znj73GKQwPM1Vkr6j4tjnBWPghBPstkEpqQgxBnji6HpVpf/z/vv 8q8ZvCqPXqN/coWrKnRn4a10MrUnkfjML+/AOreP11JGrh6ypWIv0SdRF/0yS+i1w+uS uy3sky3yntCi+ZRm32pSl9GMPzENdY2rW9+63wQ1I6MuSfXyQSvBEMqo7GrGr0XaohTX vzvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Yfh6QFkYFNf1hB1wN2Qa8u5OYVZY/rqRttfqSaSDPAk=; b=VQPnUZLkWyYernTqbabEsXv2pbQCgEKmv5ptnkTCZmX52HXaM9mGqHwCi2IIpxubcz 783GZ1ddRwf3k+QRTAvgT0Gy8wWML/EOWyXtJj6e5Rt3yfnFhdle07LHGut7KC06xO3q QgIWSLkwJQAhiAJpyfrg72BYhMbgJ/TrJGuafB8akj6zzAYUjGY17Rcjfog5EhujhHZk wHQzMcSXmZiD+w+N1J/5Ebv96PD5B4tvMBJGfvFKWpdv6HVPTJQjnh6mNaioKbzdnUov jScUYzIw9h4w3gsdEZQXHFThFpwZKxbgDkocBcOZ4kXdDNYzJmMgK19HGG/fTxNXMCkz TL8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=XXFD84TS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 29si19260521ejf.281.2021.11.01.13.50.13; Mon, 01 Nov 2021 13:50:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=XXFD84TS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229501AbhKAUuN (ORCPT + 99 others); Mon, 1 Nov 2021 16:50:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbhKAUuM (ORCPT ); Mon, 1 Nov 2021 16:50:12 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECE4BC061714; Mon, 1 Nov 2021 13:47:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=Yfh6QFkYFNf1hB1wN2Qa8u5OYVZY/rqRttfqSaSDPAk=; b=XXFD84TScwxPXKCJ8uKRi7o/aX MyOFVXX58Y3dmSlmaIeJxWlpdIE1qoUPXdjayosfgSb3sb8cLqHCN+dBkkdBrAgnfcy2yYjkjbzEr 2jGK7mxbH9LSY2TrOvYvnqoQzQPn9imLh0EfRGRsHqCcKKt9d0xyK8Lv894SLU5gFOCA9FfA+nSB8 jo425+SrvDiUvM4O120virdEp8MSAcdnJKMDr7zArKOOSwOy8R+pcmUOHk8RZp4JnKQ0a8nSFqhHT h4qJPsUAHeRIVSKrj5XVB9RcxknF7FhMp2JO3As6gJkv5uLSUHmB1BqU/PH8faR3hnwWUrByDYylN M0NlS+2A==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1mheAL-0040Wx-KD; Mon, 01 Nov 2021 20:45:02 +0000 From: "Matthew Wilcox (Oracle)" To: "Darrick J. Wong" Cc: "Matthew Wilcox (Oracle)" , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Jens Axboe , Christoph Hellwig Subject: [PATCH 03/21] block: Add bio_for_each_folio_all() Date: Mon, 1 Nov 2021 20:39:11 +0000 Message-Id: <20211101203929.954622-4-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211101203929.954622-1-willy@infradead.org> References: <20211101203929.954622-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allow callers to iterate over each folio instead of each page. The bio need not have been constructed using folios originally. Signed-off-by: Matthew Wilcox (Oracle) --- Documentation/core-api/kernel-api.rst | 1 + include/linux/bio.h | 53 ++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/Documentation/core-api/kernel-api.rst b/Documentation/core-api/kernel-api.rst index 2e7186805148..7f0cb604b6ab 100644 --- a/Documentation/core-api/kernel-api.rst +++ b/Documentation/core-api/kernel-api.rst @@ -279,6 +279,7 @@ Accounting Framework Block Devices ============= +.. kernel-doc:: include/linux/bio.h .. kernel-doc:: block/blk-core.c :export: diff --git a/include/linux/bio.h b/include/linux/bio.h index a783cac49978..43b252a99334 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -166,7 +166,7 @@ static inline void bio_advance(struct bio *bio, unsigned int nbytes) */ #define bio_for_each_bvec_all(bvl, bio, i) \ for (i = 0, bvl = bio_first_bvec_all(bio); \ - i < (bio)->bi_vcnt; i++, bvl++) \ + i < (bio)->bi_vcnt; i++, bvl++) #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len) @@ -260,6 +260,57 @@ static inline struct bio_vec *bio_last_bvec_all(struct bio *bio) return &bio->bi_io_vec[bio->bi_vcnt - 1]; } +/** + * struct folio_iter - State for iterating all folios in a bio. + * @folio: The current folio we're iterating. NULL after the last folio. + * @offset: The byte offset within the current folio. + * @length: The number of bytes in this iteration (will not cross folio + * boundary). + */ +struct folio_iter { + struct folio *folio; + size_t offset; + size_t length; + /* private: for use by the iterator */ + size_t _seg_count; + int _i; +}; + +static inline +void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i) +{ + struct bio_vec *bvec = bio_first_bvec_all(bio) + i; + + fi->folio = page_folio(bvec->bv_page); + fi->offset = bvec->bv_offset + + PAGE_SIZE * (bvec->bv_page - &fi->folio->page); + fi->_seg_count = bvec->bv_len; + fi->length = min(folio_size(fi->folio) - fi->offset, fi->_seg_count); + fi->_i = i; +} + +static inline void bio_next_folio(struct folio_iter *fi, struct bio *bio) +{ + fi->_seg_count -= fi->length; + if (fi->_seg_count) { + fi->folio = folio_next(fi->folio); + fi->offset = 0; + fi->length = min(folio_size(fi->folio), fi->_seg_count); + } else if (fi->_i + 1 < bio->bi_vcnt) { + bio_first_folio(fi, bio, fi->_i + 1); + } else { + fi->folio = NULL; + } +} + +/** + * bio_for_each_folio_all - Iterate over each folio in a bio. + * @fi: struct folio_iter which is updated for each folio. + * @bio: struct bio to iterate over. + */ +#define bio_for_each_folio_all(fi, bio) \ + for (bio_first_folio(&fi, bio, 0); fi.folio; bio_next_folio(&fi, bio)) + enum bip_flags { BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */ BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */ -- 2.33.0