From patchwork Sun Oct 27 14:21:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13852661 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4CE1D13561 for ; Sun, 27 Oct 2024 14:22:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F2FC8D0007; Sun, 27 Oct 2024 10:22:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A2918D0001; Sun, 27 Oct 2024 10:22:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F38038D0007; Sun, 27 Oct 2024 10:22:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CA5798D0001 for ; Sun, 27 Oct 2024 10:22:51 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 862FA1C7BDF for ; Sun, 27 Oct 2024 14:22:26 +0000 (UTC) X-FDA: 82719597870.26.1AA8242 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 16E5EA000A for ; Sun, 27 Oct 2024 14:22:33 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=r+zfRmfg; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730038797; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tALG9wpKBtQB3tqH+0SVvlMPUAnleSl370kVmfpw6cg=; b=zxHbsZnGrceEKc6fEhShBakLDceUuMGskd0RwoTDION/hoqSk/dK9OefiblunR4dE+G8Y/ 78Ugq3EjQcoXzagisnYngcxyFhYAUB6XZ1QmJV4VhbqB6fYGvm3N6K6NmBDR7f+Uo7nF3T 4rYS1tl2rwo/y1VhlsQufaE39rsgtAg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730038797; a=rsa-sha256; cv=none; b=X+no1fwKe5p/jTdPZgpvxr2ROrrKt0FYddkwa99bjD+rVkrpaJ6qtdQMSSg4GIqOoI06/m IfbhXBZzPE3NRectcP/1uxxK4AwrjQvda0dlXyKxjy+pfDvobNccizazmBertR/AjBnxn1 1y7YwvfaIfSpd8AFVxVsmI7RueAANBo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=r+zfRmfg; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 237F95C5830; Sun, 27 Oct 2024 14:22:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 270F5C4CEC3; Sun, 27 Oct 2024 14:22:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730038968; bh=Ig+DtUOzk0wRj0C/EWVGPiHozm6V2fkXLKm30AePHJc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r+zfRmfgUjBdKG0sjFH7EFWB14iiJjvEXA1eTMDEC2NPGsxgK3GK1ozAFere43Kk3 Olw8VnLKVDFHXG1xCYeSjKw7t2cyNGOGv1SB0qzIaO7Hjg9oYHlDJVIAPTr9/UTSPh iBN4VQUVqSBI7htWJrXD6Vj/LvGOfrORvquuGrGjQyhyT4sD/QndXQDzjQPGyamfu0 9FwwvLYJyeH8muOh/ZbuS9SltR1FQiEn/M9pPcFgU0ARzo04FYi2f55G1NgIoF90fU gugpxORRZ1GMtHC/dWj350ZIJ+YXqS8sneoRix0wJR8tDnk8guw7Mz4U+sUsPYyPVa 1pQyQWXPE7NcA== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 1/7] block: share more code for bio addition helpers Date: Sun, 27 Oct 2024 16:21:54 +0200 Message-ID: X-Mailer: git-send-email 2.46.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 16E5EA000A X-Stat-Signature: q1z5be8ziqacm9we7iwpf9rourkuqonf X-Rspam-User: X-HE-Tag: 1730038953-87281 X-HE-Meta: U2FsdGVkX1/2XrY6UofNc/PlthbSY6zjaQohlycTLVApXO8/3GOL3HV0CPUbBK22hHggvyVywfUZtFo3EP/WuFvHl9FzAXMUH4EzxkRr8Zax3Ak3/iTASt3ZFMuVxkq5mc8jxdg3TZUSiQbsaVUCFDAJ3jQtNQfg63sjOpx60RZIpI60cs5DppfL2XTSOFLziJd5BiGHzdOqpC8UZ35SFx6rhd8wccCZijdhDdOxi7eBRDvam/fqRQE9zsjt7fUaAmqzTSvppB8OHV36BJy4I6sn0OUyv3wmdayWArEo5c5LzsPfPCuk4u3T+oudfwlDGoIpNNDa622XICOyJ2I+SohXKudygmuF/zFxi2QwgxTApH3nQVy32jwiDexERXEC/17R5KlEkfTAleAya6Aak3PsnNvSYnzUdPEoUK6tby/aE/t+sP6b5jfs/VsxOfHFPCmgJO4wFS1hoOUC0Fa5WgQyEyXHKoCZFJ9zAFG21ttSenPeCFLoZHi7Ai+iyq/DwSzgp0pw5yhh8abzEqLDe5RdMKCgWXNxdKDCnvdchU/FO5uOB5eE3zmAMgTr+DbQFePx1AkSb4e8/p6+7Iw3WMDHzRDJ8rk9eyfl4o6FBIzJfhzMukuAF8pENnwXCi133MorGWzmkK6/MDqPlU7bftl5i5HtctzaCHnuu56meXPOxvGUAwfFXcBB7yGM2Z5BOxSMfFEwkpYadRPqq4uCIwCXbTSs24tlgnY0zLtzVr8IVldy+e0vs8QmZPtAhXyFyi2rdDknYSSWOiK10DSV3b+3BPTqoYwTeP4AoiM9L9NwDB78uvK1MaIhvSrbjuCFJ9bzfzVzd68NvK7g5iT3U8Sb/hz7C/0uJzMiZt7m4otI7/DGqVcZNSK9rYkAR8IGOT20yK02JGI8pmrhXzTqE+fNXIbQTdzGGxAANlAoRzMOxz4migb7lFGXnH9tSuoWU/sdBEjJ20pkuhseoVV OZzTYOJy oPpBGxjwuG9wYkdbpfNzAfDP25PfYDUb7n0TdLmsC9UU8a64EgI5XuN3hlUCsM9U/3+Ju493wA7/H6+A1pOu3Tv4ilXrJfPkNsGuhezx30vGm1yX98hcbBpGSg6JXf91m/PcxhdwBdJnDZjzFZay+ieO9YdskP1DafKuIrfu0aGgzKlFY59luwCMZAkInr6zFao5yHSH4JWh2wdDEa0cGZcz9f6QqqBZ5mK8xmdAB9PnCz3U= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Christoph Hellwig __bio_iov_iter_get_pages currently open codes adding pages to the bio, which duplicates a lot of code from bio_add_page and bio_add_zone_append_page. Add bio_add_page_int and bio_add_zone_append_page_int helpers that pass down the same_page output argument so that __bio_iov_iter_get_pages can reuse the main add bio to page helpers. Note that I'd normally call these helpers __bio_add_page and __bio_add_zone_append_page, but the former is already taken for an exported API. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky --- block/bio.c | 114 +++++++++++++++++++++++----------------------------- 1 file changed, 51 insertions(+), 63 deletions(-) diff --git a/block/bio.c b/block/bio.c index ac4d77c88932..2d3bc8bfb071 100644 --- a/block/bio.c +++ b/block/bio.c @@ -1064,6 +1064,19 @@ int bio_add_pc_page(struct request_queue *q, struct bio *bio, } EXPORT_SYMBOL(bio_add_pc_page); +static int bio_add_zone_append_page_int(struct bio *bio, struct page *page, + unsigned int len, unsigned int offset, bool *same_page) +{ + struct block_device *bdev = bio->bi_bdev; + + if (WARN_ON_ONCE(bio_op(bio) != REQ_OP_ZONE_APPEND)) + return 0; + if (WARN_ON_ONCE(!bdev_is_zoned(bdev))) + return 0; + return bio_add_hw_page(bdev_get_queue(bdev), bio, page, len, offset, + bdev_max_zone_append_sectors(bdev), same_page); +} + /** * bio_add_zone_append_page - attempt to add page to zone-append bio * @bio: destination bio @@ -1083,17 +1096,9 @@ EXPORT_SYMBOL(bio_add_pc_page); int bio_add_zone_append_page(struct bio *bio, struct page *page, unsigned int len, unsigned int offset) { - struct request_queue *q = bdev_get_queue(bio->bi_bdev); bool same_page = false; - if (WARN_ON_ONCE(bio_op(bio) != REQ_OP_ZONE_APPEND)) - return 0; - - if (WARN_ON_ONCE(!bdev_is_zoned(bio->bi_bdev))) - return 0; - - return bio_add_hw_page(q, bio, page, len, offset, - queue_max_zone_append_sectors(q), &same_page); + return bio_add_zone_append_page_int(bio, page, len, offset, &same_page); } EXPORT_SYMBOL_GPL(bio_add_zone_append_page); @@ -1119,20 +1124,9 @@ void __bio_add_page(struct bio *bio, struct page *page, } EXPORT_SYMBOL_GPL(__bio_add_page); -/** - * bio_add_page - attempt to add page(s) to bio - * @bio: destination bio - * @page: start page to add - * @len: vec entry length, may cross pages - * @offset: vec entry offset relative to @page, may cross pages - * - * Attempt to add page(s) to the bio_vec maplist. This will only fail - * if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio. - */ -int bio_add_page(struct bio *bio, struct page *page, - unsigned int len, unsigned int offset) +static int bio_add_page_int(struct bio *bio, struct page *page, + unsigned int len, unsigned int offset, bool *same_page) { - bool same_page = false; if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED))) return 0; @@ -1141,7 +1135,7 @@ int bio_add_page(struct bio *bio, struct page *page, if (bio->bi_vcnt > 0 && bvec_try_merge_page(&bio->bi_io_vec[bio->bi_vcnt - 1], - page, len, offset, &same_page)) { + page, len, offset, same_page)) { bio->bi_iter.bi_size += len; return len; } @@ -1151,6 +1145,24 @@ int bio_add_page(struct bio *bio, struct page *page, __bio_add_page(bio, page, len, offset); return len; } + +/** + * bio_add_page - attempt to add page(s) to bio + * @bio: destination bio + * @page: start page to add + * @len: vec entry length, may cross pages + * @offset: vec entry offset relative to @page, may cross pages + * + * Attempt to add page(s) to the bio_vec maplist. Will only fail if the + * bio is full, or it is incorrectly used on a cloned bio. + */ +int bio_add_page(struct bio *bio, struct page *page, + unsigned int len, unsigned int offset) +{ + bool same_page = false; + + return bio_add_page_int(bio, page, len, offset, &same_page); +} EXPORT_SYMBOL(bio_add_page); void bio_add_folio_nofail(struct bio *bio, struct folio *folio, size_t len, @@ -1224,41 +1236,6 @@ void bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter) bio_set_flag(bio, BIO_CLONED); } -static int bio_iov_add_folio(struct bio *bio, struct folio *folio, size_t len, - size_t offset) -{ - bool same_page = false; - - if (WARN_ON_ONCE(bio->bi_iter.bi_size > UINT_MAX - len)) - return -EIO; - - if (bio->bi_vcnt > 0 && - bvec_try_merge_page(&bio->bi_io_vec[bio->bi_vcnt - 1], - folio_page(folio, 0), len, offset, - &same_page)) { - bio->bi_iter.bi_size += len; - if (same_page && bio_flagged(bio, BIO_PAGE_PINNED)) - unpin_user_folio(folio, 1); - return 0; - } - bio_add_folio_nofail(bio, folio, len, offset); - return 0; -} - -static int bio_iov_add_zone_append_folio(struct bio *bio, struct folio *folio, - size_t len, size_t offset) -{ - struct request_queue *q = bdev_get_queue(bio->bi_bdev); - bool same_page = false; - - if (bio_add_hw_folio(q, bio, folio, len, offset, - queue_max_zone_append_sectors(q), &same_page) != len) - return -EINVAL; - if (same_page && bio_flagged(bio, BIO_PAGE_PINNED)) - unpin_user_folio(folio, 1); - return 0; -} - static unsigned int get_contig_folio_len(unsigned int *num_pages, struct page **pages, unsigned int i, struct folio *folio, size_t left, @@ -1353,6 +1330,8 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) for (left = size, i = 0; left > 0; left -= len, i += num_pages) { struct page *page = pages[i]; struct folio *folio = page_folio(page); + struct page *first_page = folio_page(folio, 0); + bool same_page = false; folio_offset = ((size_t)folio_page_idx(folio, page) << PAGE_SHIFT) + offset; @@ -1366,12 +1345,21 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) folio, left, offset); if (bio_op(bio) == REQ_OP_ZONE_APPEND) { - ret = bio_iov_add_zone_append_folio(bio, folio, len, - folio_offset); - if (ret) + if (bio_add_zone_append_page_int(bio, first_page, len, + folio_offset, &same_page) != len) { + ret = -EINVAL; + break; + } + } else { + if (bio_add_page_int(bio, folio_page(folio, 0), len, + folio_offset, &same_page) != len) { + ret = -EINVAL; break; - } else - bio_iov_add_folio(bio, folio, len, folio_offset); + } + } + + if (same_page && bio_flagged(bio, BIO_PAGE_PINNED)) + unpin_user_folio(folio, 1); offset = 0; } From patchwork Sun Oct 27 14:21:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13852662 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD2A2D13562 for ; Sun, 27 Oct 2024 14:22:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43C6F8D0008; Sun, 27 Oct 2024 10:22:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3275D8D0001; Sun, 27 Oct 2024 10:22:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17F088D0008; Sun, 27 Oct 2024 10:22:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id EA8328D0001 for ; Sun, 27 Oct 2024 10:22:55 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C094C81C3A for ; Sun, 27 Oct 2024 14:22:36 +0000 (UTC) X-FDA: 82719598500.28.268746C Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf02.hostedemail.com (Postfix) with ESMTP id C545580016 for ; Sun, 27 Oct 2024 14:22:11 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=t8n4IOYj; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730038858; a=rsa-sha256; cv=none; b=1Ku4gTltfY7iAqqQYEYnG7bOtOnW/c1Q2LPFDawZl6qVFc0UbY4aRZY5857kInbKrroIoF 7UejKGJ0HA1dUE13IGNtbmpkYQjzqgjW0s5KVuVFV878sJ57nYgpXkmetkodUdZi0t54R3 HND8Ub+aUr1VZR26KVpVNRAxhKmkvcc= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=t8n4IOYj; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730038858; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=N/ZvKcy7ulyp8F2napwd3K9yKAl+F6g0GRqyTkyWeuU=; b=C059v+TA68PVliojOY17x/w/C7IBdnqwDcpvqMVcP9rKkeWjFZy5wI2MDHbjbfuAnIj+6y QPFziX+9KYuHrd1cgOmI8Q63QfhhFHzTvQG1I4LYC4nGeCgpbMcjRTzFo/Vn08FArwL5wX t0e0z2OKNZsIOkwYaco9T6zpF/yASHA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 3425E5C5846; Sun, 27 Oct 2024 14:22:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 334A2C4CEE5; Sun, 27 Oct 2024 14:22:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730038973; bh=xs6fvM8ovmipSkvK4QHs63+5zfVZxbyL5606zTIt2PA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t8n4IOYjkU+t+CyiP5M3EN8uu0+Fhy6+/w3iTz0Q2atQ8SbLeJALE1g9BoSjijj8S PhFXdSulUwQafrHIn9TUpPBoI8HneaOXwC+JJ/0p97sfDVhUC3RNK3PE04HkzCC/4J cWhbJk1wzm2NbKHYjlRlEPHMWjX6NITYLwV/dH6J33RTjaJ7fOIVz0CcVhtzsLzNex /phptkTCnClXhaWaw4eIffed64MThYl7FGZrLdyzju3U2a2z40qKT8ylqHMywRXe7K zG/3VBzIUTnaCnNjKwQ0rxt3cmXGFfMIET1vHtTft+96gIQ+u7gpzegw5AhZuk1QEm Ay7ibRcVje6YQ== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 2/7] block: don't merge different kinds of P2P transfers in a single bio Date: Sun, 27 Oct 2024 16:21:55 +0200 Message-ID: <34d44537a65aba6ede215a8ad882aeee028b423a.1730037261.git.leon@kernel.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: C545580016 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: cfmq7p7ofbkn818wkzzwy433hagmtmn7 X-HE-Tag: 1730038931-954611 X-HE-Meta: U2FsdGVkX19iVVKT2RESXlwETuZqmFlY4qpAZkKjIhipAB3dOft3ugazK9xE5Hjp5doM2pPEL9YhrLabcsw0KrdsxzUDLgVa2tPBPAbi3NrFaxFT5eetH72qgtir7c4kH9XnTJ4jXKX4nS39krZxXvnIBY+sMo0DKPK19VAITik6p/q0s6njMLQxhFnZlY3qIhyRzVdjbjGA8usVvvYl1bbRX6Vos+eXOZm3LCZbyXp5OoGOSsD46FDYC3BDrPv44g1S89ItR5XfbFgwKIzE/iRv6qgL0q04fOqg7rbdbs8vNFjZXjVoQgSsmogqSz5eAOrrMK088NH01iHszp6IPApQpwdK6q+B30w7o6YKIcDHKtZA+Y7fWBZ999HAg9f1tS8yq/nrwza0/F8foeXC4wLTpHD0nYIQppW+45YJn2mkuU2bEsnrx6nfDjJEEckHkIeuw3KFAlZtwNToL5+Po9ZoztXPuMvwFMscjt3RbFklBn65ZATPeWbc6qwGDuZq4NZufsDVxxqcNIVl1bFhoY7X3E5/7Yr0ev/031AuR22Ul/dXCk0UeKx3B5FFNYc1+NXQdNy+pjxNyZeq0fg9WzNVPo9joeupYb3z4sQF0fwZQFWH267jXkxFKsX+SQz51h2Y7BjJ/4VpS+wLNbN2MioVkS65+tO2Cjo9t8rI1d5kmgdJ67qK7OaB7Kaw4sybUOkkHBFki6Dz4McEaePVHE40/u4VGt4wGBOUkMwxDRwrf1MpEm+X5iuBKYKsZcxSzMubWg1x268yuLGgNDV7UQZ5dzoje2gYN3803xdts8YaHTXsz4nW1JhdkpNd3oKGvyEgv3abuN0sYxfHPqyvqRDT2Txu4MI8SP8ZmAVwHpcS1E0ufIHy4F5G9SebpwvUQk57tWSA85U2dnZ590O0Td0CFcYzZ4xF3O+JfBp9MYTb1ZMe04Fz6pP6oiv3OpS8Q5subEUsbIuyNPpEcYu xLyz9Nv3 rXrSgz8T4ah+8SAoMyWeaYtamiKAnC8jJZzLORi8KfkVaNDLq5+2ZFxhG5o0EIJ/9gG3xVBwvtwZhZUKvr6pFSB1aN2pPvgiTaPaorOoMpFMblppyA9KCBICPBrdi1XlMnemRIr1rn1pStT2upL/WU0ShhDnL/k+S/HEv5xZwQYqdLWB5h011YS538HG3qW1j4Y9TWHVI6U5VXyVess8Cauvjc9xdd07zo9P2B5MZQUG7T6I= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Christoph Hellwig To get out of the dma mapping helpers having to check every segment for it's P2P status, ensure that bios either contain P2P transfers or non-P2P transfers, and that a P2P bio only contains ranges from a single device. This means we do the page zone access in the bio add path where it should be still page hot, and will only have do the fairly expensive P2P topology lookup once per bio down in the dma mapping path, and only for already marked bios. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky Reviewed-by: Logan Gunthorpe --- block/bio.c | 36 +++++++++++++++++++++++++++++------- block/blk-map.c | 32 ++++++++++++++++++++++++-------- include/linux/blk_types.h | 2 ++ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/block/bio.c b/block/bio.c index 2d3bc8bfb071..943a6d78cb3e 100644 --- a/block/bio.c +++ b/block/bio.c @@ -928,8 +928,6 @@ static bool bvec_try_merge_page(struct bio_vec *bv, struct page *page, return false; if (xen_domain() && !xen_biovec_phys_mergeable(bv, page)) return false; - if (!zone_device_pages_have_same_pgmap(bv->bv_page, page)) - return false; *same_page = ((vec_end_addr & PAGE_MASK) == ((page_addr + off) & PAGE_MASK)); @@ -993,6 +991,14 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio, if (bio->bi_vcnt > 0) { struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1]; + /* + * When doing ZONE_DEVICE-based P2P transfers, all pages in a + * bio must be P2P pages from the same device. + */ + if ((bio->bi_opf & REQ_P2PDMA) && + !zone_device_pages_have_same_pgmap(bv->bv_page, page)) + return 0; + if (bvec_try_merge_hw_page(q, bv, page, len, offset, same_page)) { bio->bi_iter.bi_size += len; @@ -1009,6 +1015,9 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio, */ if (bvec_gap_to_prev(&q->limits, bv, offset)) return 0; + } else { + if (is_pci_p2pdma_page(page)) + bio->bi_opf |= REQ_P2PDMA | REQ_NOMERGE; } bvec_set_page(&bio->bi_io_vec[bio->bi_vcnt], page, len, offset); @@ -1133,11 +1142,24 @@ static int bio_add_page_int(struct bio *bio, struct page *page, if (bio->bi_iter.bi_size > UINT_MAX - len) return 0; - if (bio->bi_vcnt > 0 && - bvec_try_merge_page(&bio->bi_io_vec[bio->bi_vcnt - 1], - page, len, offset, same_page)) { - bio->bi_iter.bi_size += len; - return len; + if (bio->bi_vcnt > 0) { + struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1]; + + /* + * When doing ZONE_DEVICE-based P2P transfers, all pages in a + * bio must be P2P pages from the same device. + */ + if ((bio->bi_opf & REQ_P2PDMA) && + !zone_device_pages_have_same_pgmap(bv->bv_page, page)) + return 0; + + if (bvec_try_merge_page(bv, page, len, offset, same_page)) { + bio->bi_iter.bi_size += len; + return len; + } + } else { + if (is_pci_p2pdma_page(page)) + bio->bi_opf |= REQ_P2PDMA | REQ_NOMERGE; } if (bio->bi_vcnt >= bio->bi_max_vecs) diff --git a/block/blk-map.c b/block/blk-map.c index 0e1167b23934..03192b1ca6ea 100644 --- a/block/blk-map.c +++ b/block/blk-map.c @@ -568,6 +568,7 @@ static int blk_rq_map_user_bvec(struct request *rq, const struct iov_iter *iter) const struct queue_limits *lim = &q->limits; unsigned int nsegs = 0, bytes = 0; struct bio *bio; + int error; size_t i; if (!nr_iter || (nr_iter >> SECTOR_SHIFT) > queue_max_hw_sectors(q)) @@ -588,15 +589,30 @@ static int blk_rq_map_user_bvec(struct request *rq, const struct iov_iter *iter) for (i = 0; i < nr_segs; i++) { struct bio_vec *bv = &bvecs[i]; - /* - * If the queue doesn't support SG gaps and adding this - * offset would create a gap, fallback to copy. - */ - if (bvprvp && bvec_gap_to_prev(lim, bvprvp, bv->bv_offset)) { - blk_mq_map_bio_put(bio); - return -EREMOTEIO; + error = -EREMOTEIO; + if (bvprvp) { + /* + * If the queue doesn't support SG gaps and adding this + * offset would create a gap, fallback to copy. + */ + if (bvec_gap_to_prev(lim, bvprvp, bv->bv_offset)) + goto put_bio; + + /* + * When doing ZONE_DEVICE-based P2P transfers, all pages + * in a bio must be P2P pages, and from the same device. + */ + if ((bio->bi_opf & REQ_P2PDMA) && + zone_device_pages_have_same_pgmap(bvprvp->bv_page, + bv->bv_page)) + goto put_bio; + } else { + if (is_pci_p2pdma_page(bv->bv_page)) + bio->bi_opf |= REQ_P2PDMA | REQ_NOMERGE; } + /* check full condition */ + error = -EINVAL; if (nsegs >= nr_segs || bytes > UINT_MAX - bv->bv_len) goto put_bio; if (bytes + bv->bv_len > nr_iter) @@ -611,7 +627,7 @@ static int blk_rq_map_user_bvec(struct request *rq, const struct iov_iter *iter) return 0; put_bio: blk_mq_map_bio_put(bio); - return -EINVAL; + return error; } /** diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index dce7615c35e7..94cf146e8ce6 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -378,6 +378,7 @@ enum req_flag_bits { __REQ_DRV, /* for driver use */ __REQ_FS_PRIVATE, /* for file system (submitter) use */ __REQ_ATOMIC, /* for atomic write operations */ + __REQ_P2PDMA, /* contains P2P DMA pages */ /* * Command specific flags, keep last: */ @@ -410,6 +411,7 @@ enum req_flag_bits { #define REQ_DRV (__force blk_opf_t)(1ULL << __REQ_DRV) #define REQ_FS_PRIVATE (__force blk_opf_t)(1ULL << __REQ_FS_PRIVATE) #define REQ_ATOMIC (__force blk_opf_t)(1ULL << __REQ_ATOMIC) +#define REQ_P2PDMA (__force blk_opf_t)(1ULL << __REQ_P2PDMA) #define REQ_NOUNMAP (__force blk_opf_t)(1ULL << __REQ_NOUNMAP) From patchwork Sun Oct 27 14:21:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13852668 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C73A0D13562 for ; Sun, 27 Oct 2024 14:23:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 526F38D000E; Sun, 27 Oct 2024 10:23:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D6C28D0001; Sun, 27 Oct 2024 10:23:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 350A58D000E; Sun, 27 Oct 2024 10:23:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 14F048D0001 for ; Sun, 27 Oct 2024 10:23:20 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C58F71A1C21 for ; Sun, 27 Oct 2024 14:22:40 +0000 (UTC) X-FDA: 82719598752.08.8990C7B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 43705180018 for ; Sun, 27 Oct 2024 14:23:02 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GU2WiE85; spf=pass (imf06.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730038919; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DwhhRHXOs/NzUCpd1QbXTeegPlkywkyQe+nKJxEfOkU=; b=iMv9I4C+vPzkkkb5wGOmSW/G1byVdq72J4BGRQ3PYXi9oilH4uc7m2jcLJsuV4d6Iu4Eb+ 6EGPRhM2CY+S/TMZQUB90ykHvFcV5oHiHDbOjg17gGBcB2xFox6x9kccl6uI4+Fs4nmj1S 2v9Hs2U03r3hkO6eM6mSkiiGF1vNeCA= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GU2WiE85; spf=pass (imf06.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730038919; a=rsa-sha256; cv=none; b=o/28dXiYDkgTPBSM0mkCO3NpfWIIhW/YR/wufC53pbelqMoaWHnG+sMcTXvlp6fMI7g+FH KoUtu/m3mHUzLwhvvH2Mw9wK/73p802yNUazXpQp/e2YQnYAHKo1jJEMoXp2r7hP0jNAys iiCge84t47di9l40v0d2ttty7/QleJ4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 3B6F35C5833; Sun, 27 Oct 2024 14:22:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 38793C4CEC3; Sun, 27 Oct 2024 14:23:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730038997; bh=ZAkEvCDpOQpT4FqrkcEPmIofeDg0rb/b5+lEupSm25s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GU2WiE85QkyrGZabT77HMzH4RzcXBtUoK7DX8razab6E7GlTeZ2LQTeNI7ZRCEXcF +9yMHm9zDo08nuNcPNZNSsuaYkBUL9ipGLwLInfLmk0HejWEa8WPu1R8UqdKOsPV74 Z5wpY7UYyju99OwvD8w7GPbHNbCOK/zLgPk0s2GMr97M3wN8lKofvcDQjgjNlAIopH eIYUU6O634Ehr1WlX/W3GKJbeoFI2GpQCheDt+O+VjGAz5sar3RgcEymVxHQmnOdc4 uO+VYU+gZUk1F3VsP3juUi2vi+UCZ40ddUDVpK0TRukr1wrJIDD2IoyIOcFlFa0aTT I1l2OPb8MTV4g== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 3/7] blk-mq: add a dma mapping iterator Date: Sun, 27 Oct 2024 16:21:56 +0200 Message-ID: <9b7c6203fbbae90821f2220dba1bd600e3bd23ba.1730037261.git.leon@kernel.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 43705180018 X-Stat-Signature: 5ywgf8suow8g38xkhtpui1fzsuqdty9b X-HE-Tag: 1730038982-400653 X-HE-Meta: U2FsdGVkX1/jCQ0XmEtAlIrNDsE9+MfwqLcc847F1nUJdZnrqUT/L/7lQkGmPbxtRyRslLItbOJTOqjLDlHXEe5Vs3itfZA6glTp1orJrnHlRcfWvCu6aQjVhY0+9yG3m3dhnR2xXqwUhBYdA3glMp6T7Nd5Jaj3a07r4f5TZvZmJ76MIYnkp2B8TNzYv++8yyPS6Wp5V5v32KVb7iJmWkLFBnlCnkH82I5fPZEYoJKnLuJhOJb/veXeRAHKj+TNAuoRzebyJUwFxeuDufDH057mPbHGqhA77b16JDO3x67q7QjhX3fdNY4PVHFcVloJKsJWinBXrWfgU8M9XXxJEWxfiyL5FkzDOgI04SP2NPHAY19HhJgwl2FWzan3Q91bGj5HHH5/WvHhTWIocNbJyrFUc1/W7SSO5T3gZ0iliH7jfZ2Fp2J7MqBYAMsBBIJIwlluWn+bOlRbf0RZUvkSApQKDyGjNUqiWzj2omqoF525lDlw+e93iL1LeTmljXDvAJPc/TzmOYNgpoXsKzfzc1yCql3VbwF6nKuGXr3VB+MS2XTcXnlwrvI1MJvN8KCPGlwvRAbZH0tJNhcmoMaI7mt8xtkuPD+GKc2BTn/GK3MO56wyiPN+PqND+4SQfPPh0Z+7I6oKAahtd5DIhbvLiXx90TAiJxbrj15egXRLUn7oWD5sPkpFePO7VGyPlLCDGauLZWNQCH5hgmiaKmKdDjyh/LT6Rc1CtX/Ww7NZFkXINbKcf1EvCYlZxG8BM1CqLR7xuT4nSIW2vhJPexD4Fwg+2RSWP9pyZoD7iMEziNY+8FAkB8o2KXjrGv5w+V/1SAf+Q1PrxgjOvp3gcV1drdG69C1YwdMf1qYrEp1LgLetpiL/hFw3BWf3rXK/MCiJqkTmkoAIOofpf3p9lC6lFwfQnrzGqEhABtG7a0xviaBVHzKNuLZ3yDF8MPX9FgwXfOcg5xY5C4zibfU/3rF pyP2XqB2 YYwGi4SpxpEKhbEFHrchPpzpY8dl4YGXHmCAxT5nmdKZkdy7SMPuco/XCRAeAN2+QrPjq6J+94PUHwhzeKcHS8624QmLLJyTJe+kEpYuXGSvA8XYHm/eNDS6ovSbtBLl2bxeCSO/1s38ScDwXLf8SkY/IzsidKao8lobSKtRXaBMC9EH7A7BxuhyGXJfHA3+bh8GfPucOeeZBr4zMJf+WGamSn7rfeHOwF1Du2sE9HgYtkZc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Christoph Hellwig blk_rq_map_sg is maze of nested loops. Untangle it by creating an iterator that returns [paddr,len] tuples for DMA mapping, and then implement the DMA logic on top of this. This not only removes code at the source level, but also generates nicer binary code: $ size block/blk-merge.o.* text data bss dec hex filename 10001 432 0 10433 28c1 block/blk-merge.o.new 10317 468 0 10785 2a21 block/blk-merge.o.old Last but not least it will be used as a building block for a new DMA mapping helper that doesn't rely on struct scatterlist. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky --- block/blk-merge.c | 182 ++++++++++++++++++++-------------------------- 1 file changed, 77 insertions(+), 105 deletions(-) diff --git a/block/blk-merge.c b/block/blk-merge.c index ad763ec313b6..b63fd754a5de 100644 --- a/block/blk-merge.c +++ b/block/blk-merge.c @@ -451,137 +451,109 @@ unsigned int blk_recalc_rq_segments(struct request *rq) return nr_phys_segs; } -static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, - struct scatterlist *sglist) +struct phys_vec { + phys_addr_t paddr; + u32 len; +}; + +static bool blk_map_iter_next(struct request *req, + struct req_iterator *iter, struct phys_vec *vec) { - if (!*sg) - return sglist; + unsigned int max_size; + struct bio_vec bv; /* - * If the driver previously mapped a shorter list, we could see a - * termination bit prematurely unless it fully inits the sg table - * on each mapping. We KNOW that there must be more entries here - * or the driver would be buggy, so force clear the termination bit - * to avoid doing a full sg_init_table() in drivers for each command. + * For special payload requests there only is a single segment. Return + * it now and make sure blk_phys_iter_next stop iterating. */ - sg_unmark_end(*sg); - return sg_next(*sg); -} + if (req->rq_flags & RQF_SPECIAL_PAYLOAD) { + if (!iter->bio) + return false; + vec->paddr = bvec_phys(&req->special_vec); + vec->len = req->special_vec.bv_len; + iter->bio = NULL; + return true; + } -static unsigned blk_bvec_map_sg(struct request_queue *q, - struct bio_vec *bvec, struct scatterlist *sglist, - struct scatterlist **sg) -{ - unsigned nbytes = bvec->bv_len; - unsigned nsegs = 0, total = 0; + if (!iter->iter.bi_size) + return false; - while (nbytes > 0) { - unsigned offset = bvec->bv_offset + total; - unsigned len = get_max_segment_size(&q->limits, - bvec_phys(bvec) + total, nbytes); - struct page *page = bvec->bv_page; + bv = mp_bvec_iter_bvec(iter->bio->bi_io_vec, iter->iter); + vec->paddr = bvec_phys(&bv); + max_size = get_max_segment_size(&req->q->limits, vec->paddr, UINT_MAX); + bv.bv_len = min(bv.bv_len, max_size); + bio_advance_iter_single(iter->bio, &iter->iter, bv.bv_len); - /* - * Unfortunately a fair number of drivers barf on scatterlists - * that have an offset larger than PAGE_SIZE, despite other - * subsystems dealing with that invariant just fine. For now - * stick to the legacy format where we never present those from - * the block layer, but the code below should be removed once - * these offenders (mostly MMC/SD drivers) are fixed. - */ - page += (offset >> PAGE_SHIFT); - offset &= ~PAGE_MASK; + /* + * If we are entirely done with this bi_io_vec entry, check if the next + * one could be merged into it. This typically happens when moving to + * the next bio, but some callers also don't pack bvecs tight. + */ + while (!iter->iter.bi_size || !iter->iter.bi_bvec_done) { + struct bio_vec next; + + if (!iter->iter.bi_size) { + if (!iter->bio->bi_next) + break; + iter->bio = iter->bio->bi_next; + iter->iter = iter->bio->bi_iter; + } - *sg = blk_next_sg(sg, sglist); - sg_set_page(*sg, page, len, offset); + next = mp_bvec_iter_bvec(iter->bio->bi_io_vec, iter->iter); + if (bv.bv_len + next.bv_len > max_size || + !biovec_phys_mergeable(req->q, &bv, &next)) + break; - total += len; - nbytes -= len; - nsegs++; + bv.bv_len += next.bv_len; + bio_advance_iter_single(iter->bio, &iter->iter, next.bv_len); } - return nsegs; + vec->len = bv.bv_len; + return true; } -static inline int __blk_bvec_map_sg(struct bio_vec bv, - struct scatterlist *sglist, struct scatterlist **sg) -{ - *sg = blk_next_sg(sg, sglist); - sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); - return 1; -} +#define blk_phys_to_page(_paddr) \ + (pfn_to_page(__phys_to_pfn(_paddr))) -/* only try to merge bvecs into one sg if they are from two bios */ -static inline bool -__blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec, - struct bio_vec *bvprv, struct scatterlist **sg) +static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, + struct scatterlist *sglist) { - - int nbytes = bvec->bv_len; - if (!*sg) - return false; - - if ((*sg)->length + nbytes > queue_max_segment_size(q)) - return false; - - if (!biovec_phys_mergeable(q, bvprv, bvec)) - return false; - - (*sg)->length += nbytes; - - return true; -} - -static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, - struct scatterlist *sglist, - struct scatterlist **sg) -{ - struct bio_vec bvec, bvprv = { NULL }; - struct bvec_iter iter; - int nsegs = 0; - bool new_bio = false; - - for_each_bio(bio) { - bio_for_each_bvec(bvec, bio, iter) { - /* - * Only try to merge bvecs from two bios given we - * have done bio internal merge when adding pages - * to bio - */ - if (new_bio && - __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg)) - goto next_bvec; - - if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE) - nsegs += __blk_bvec_map_sg(bvec, sglist, sg); - else - nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg); - next_bvec: - new_bio = false; - } - if (likely(bio->bi_iter.bi_size)) { - bvprv = bvec; - new_bio = true; - } - } + return sglist; - return nsegs; + /* + * If the driver previously mapped a shorter list, we could see a + * termination bit prematurely unless it fully inits the sg table + * on each mapping. We KNOW that there must be more entries here + * or the driver would be buggy, so force clear the termination bit + * to avoid doing a full sg_init_table() in drivers for each command. + */ + sg_unmark_end(*sg); + return sg_next(*sg); } /* - * map a request to scatterlist, return number of sg entries setup. Caller - * must make sure sg can hold rq->nr_phys_segments entries + * Map a request to scatterlist, return number of sg entries setup. Caller + * must make sure sg can hold rq->nr_phys_segments entries. */ int __blk_rq_map_sg(struct request_queue *q, struct request *rq, struct scatterlist *sglist, struct scatterlist **last_sg) { + struct req_iterator iter = { + .bio = rq->bio, + .iter = rq->bio->bi_iter, + }; + struct phys_vec vec; int nsegs = 0; - if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) - nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg); - else if (rq->bio) - nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg); + while (blk_map_iter_next(rq, &iter, &vec)) { + struct page *page = blk_phys_to_page(vec.paddr); + unsigned int offset = offset_in_page(vec.paddr); + + *last_sg = blk_next_sg(last_sg, sglist); + sg_set_page(*last_sg, page, vec.len, offset); + nsegs++; + } if (*last_sg) sg_mark_end(*last_sg); From patchwork Sun Oct 27 14:21:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13852664 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7183ED13562 for ; Sun, 27 Oct 2024 14:23:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F114C8D000A; Sun, 27 Oct 2024 10:23:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E743C8D0001; Sun, 27 Oct 2024 10:23:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9D3C8D000A; Sun, 27 Oct 2024 10:23:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A987E8D0001 for ; Sun, 27 Oct 2024 10:23:03 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 600D61C7DA1 for ; Sun, 27 Oct 2024 14:22:38 +0000 (UTC) X-FDA: 82719598374.30.9E267D9 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf20.hostedemail.com (Postfix) with ESMTP id 637981C0009 for ; Sun, 27 Oct 2024 14:22:34 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hPuXERdP; spf=pass (imf20.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730038903; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2fRwzbqH+ITcj85M0q9I6GDwC2QBgqZvYtRXSCXlc9Y=; b=Xv98KvXfeZTfsgWImUHHfEpEV2yXxRqSFa2rJMhA34NWqQu12O/M7a6Adq/aDB4pP8bc5F 0hNPIht8ZluCCwebfqgRmo8bHdKNoGEGhjPv0VbW1Xcm8p8yM3oHhG+NyD6YkdiCCgYZSo sv28172+COPywt1FX1aBbOT3GIBVp+4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hPuXERdP; spf=pass (imf20.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730038903; a=rsa-sha256; cv=none; b=7FXNC7Mfk97/Cj8kHUdvywk3GKDCrCz7PfjonPtT1KRAIjO2+DsSlsUH9Ml9rG+kBFUGJd XFENaxqRlQREhz9bpnBISKNtNTpPL3vOgo4pedtcE3yMbh/SXfEeikcsrylvAJtjK517ZH LQapR5E+aLx4N07XyeyWbnbPzR91h/s= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 2686FA40FAA; Sun, 27 Oct 2024 14:21:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2BD05C4CEE5; Sun, 27 Oct 2024 14:23:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730038981; bh=9DR3LFJjJhjhbu472Va//Xe8BphJFty4JMYW8TX4X1I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hPuXERdPFKyRKnnAub1JPkt8oeVENyiRQqNq5FYQjoxSMzpqfJc+ZPwdfx3HpJ2ko ymCeTGgIafCN3eHEOH4LA53Ephvv179t+7iRHX9jMk5+g3q6etTegx4HBpUpGsZJtJ esLWLUFB4m/qz8vXShOVP9ErGfuP6u+imE23Bd1qqQWqmU3ohI2AWmmjIOz2SdrPeN ALS21r1jA5TJ6duS26kzeQfH3r1ude1LVXDVyvi+e2har7IMxbiyHVvpQ/SoDTkQ44 QBAhVRb6Cbecg847TMNxQ9odCamJhWK3ZUPTmyBaq1+638ICOVMOp/C6RWcDXPstKE 3UzxbmWuiI4rA== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 4/7] blk-mq: add scatterlist-less DMA mapping helpers Date: Sun, 27 Oct 2024 16:21:57 +0200 Message-ID: <383557d0fa1aa393dbab4e1daec94b6cced384ab.1730037261.git.leon@kernel.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 637981C0009 X-Stat-Signature: 4ws8ny8rctgq4hpzqrbyijec8jshro57 X-HE-Tag: 1730038954-676552 X-HE-Meta: U2FsdGVkX1/3caOwj8wYH/IaSy2sHWgKGqLTtgPnsXFQl2plnMxgWtPl3a307W+NsGCFKlso9kf9SIMeRdhydzsbevtmXylCWv6ap1DcklWNceZ+luE4wB0uRWejBanD+nyglBLkzTe/k89YYinNDt//HAMxaXLn1GQ/5xdFjmth5Djl/PKCY5q5SmudbrCKDcP468ZA+Mm9oYteZugIGViZqgQppoS5MHbVT3dIiVBul0dFVx35T5uTAr9Se8RAs8LJ3m6oNB2h9XcOu5yt/5/ucgngOHGl/+TMqdYw2WBaxGTLK+btdW5F+hFeHIrGoh6aMjbBxHRwXfRzGMWyrLC8atj6l9kNThL7+ADdYua8FIR+8mMBO5d8ONODsTZYfcUJCGO9ji0RWVAzdOXYodkNpTizdv9xfU5sy+nY/ZZgSlx7QTuj2X0Lui0orj9ekOUlxCEDdskiEI1bBDz4X3fqCxfsNixq5SLPOKznzlvMLjFUSqADSfKTh1CnW/bCWC3A1QyzXNIP10r/uqeFq6brZnTPQ+xpDyMwlKspOo/oW4ZY9n9pNcyXPMZbkxnkrKnwYz8ZDG/KwNjP0hJk8wkpg4f74Yrqqs/jNe2kIGdK4ouW0F02ig6an4RWAeg1CPZ1DzEy+Llh3HCsCUtkJqk4pI+uUsr1Ucm5n8d2/J5CJ+b1iEKmrFg1FyADsT4BZa4iozV7tQIrd6rCIVyVJGroHN+rJ+Cj1suCHyPlPvm56xSemkRl0MSmjyXsMoDJ6ptcdvAX83ANLjWUvH59cPBtuaInbeTM2iuljWR6Z+o1pU1ffaUBsMIeg6pXR03Kfddfubnx3wLqjEs28wLZqdrnYwwd089oPR4vd+ELqWWpkz1VQb10AcMngggv/dNkK5f0+bPcsm2RKleZBhBZkOmgh+M1DKrnBG4BIWFYsWX/LOSurhoUBXqlRGlKT4YJ6Rhm6lBmXrJ0Ulw/jkz zroocK/W L0u9rhWU9qvNBm0IJZr1zTP/y7sBpRg9ZfgcMToDFwGBCPu/q/gHgFjcaWcpYmjxyGAAfoOhbPVQ7HJzZzzyeyLnIMUm+lxtFKDyaMttV3k+LFwKwfwR1teGHaKtgFiQ8nooU1gl4wTszMCxUjdLzDR4KHPxgkx2kyfG7M1fHf+vpM+A6nljyco1SHzrd3I309d7Lp9pcITZrRyWOzMMk/9TCdjfVywYZEXv0UazZLztZ3NU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Christoph Hellwig Add a new blk_rq_dma_map / blk_rq_dma_unmap pair that does away with the wasteful scatterlist structure. Instead it uses the mapping iterator to either add segments to the IOVA for IOMMU operations, or just maps them one by one for the direct mapping. For the IOMMU case instead of a scatterlist with an entry for each segment, only a single [dma_addr,len] pair needs to be stored for processing a request, and for the direct mapping the per-segment allocation shrinks from [page,offset,len,dma_addr,dma_len] to just [dma_addr,len]. The major downside of this API is that the IOVA collapsing only works when the driver sets a virt_boundary that matches the IOMMU granule. Note that struct blk_dma_vec, struct blk_dma_mapping and blk_rq_dma_unmap aren't really block specific, but for they are kept with the only mapping routine to keep things simple. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky --- block/blk-merge.c | 163 +++++++++++++++++++++++++++++++++++++ include/linux/blk-mq-dma.h | 64 +++++++++++++++ 2 files changed, 227 insertions(+) create mode 100644 include/linux/blk-mq-dma.h diff --git a/block/blk-merge.c b/block/blk-merge.c index b63fd754a5de..77e5a3d208fc 100644 --- a/block/blk-merge.c +++ b/block/blk-merge.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include #include @@ -515,6 +517,167 @@ static bool blk_map_iter_next(struct request *req, #define blk_phys_to_page(_paddr) \ (pfn_to_page(__phys_to_pfn(_paddr))) +/* + * The IOVA-based DMA API wants to be able to coalesce at the minimal IOMMU page + * size granularity (which is guaranteed to be <= PAGE_SIZE and usually 4k), so + * we need to ensure our segments are aligned to this as well. + * + * Note that there is no point in using the slightly more complicated IOVA based + * path for single segment mappings. + */ +static inline bool blk_can_dma_map_iova(struct request *req, + struct device *dma_dev) +{ + return !((queue_virt_boundary(req->q) + 1) & + dma_get_merge_boundary(dma_dev)); +} + +static bool blk_dma_map_bus(struct request *req, struct device *dma_dev, + struct blk_dma_iter *iter, struct phys_vec *vec) +{ + iter->addr = pci_p2pdma_bus_addr_map(&iter->p2pdma, vec->paddr); + iter->len = vec->len; + return true; +} + +static bool blk_dma_map_direct(struct request *req, struct device *dma_dev, + struct blk_dma_iter *iter, struct phys_vec *vec) +{ + iter->addr = dma_map_page(dma_dev, blk_phys_to_page(vec->paddr), + offset_in_page(vec->paddr), vec->len, rq_dma_dir(req)); + if (dma_mapping_error(dma_dev, iter->addr)) { + iter->status = BLK_STS_RESOURCE; + return false; + } + iter->len = vec->len; + return true; +} + +static bool blk_rq_dma_map_iova(struct request *req, struct device *dma_dev, + struct dma_iova_state *state, struct blk_dma_iter *iter, + struct phys_vec *vec) +{ + enum dma_data_direction dir = rq_dma_dir(req); + unsigned int mapped = 0; + int error = 0; + + iter->addr = state->addr; + iter->len = dma_iova_size(state); + + do { + error = dma_iova_link(dma_dev, state, vec->paddr, mapped, + vec->len, dir, 0); + if (error) + break; + mapped += vec->len; + } while (blk_map_iter_next(req, &iter->iter, vec)); + + error = dma_iova_sync(dma_dev, state, 0, mapped, error); + if (error) { + iter->status = errno_to_blk_status(error); + return false; + } + + return true; +} + +/** + * blk_rq_dma_map_iter_start - map the first DMA segment for a request + * @req: request to map + * @dma_dev: device to map to + * @state: DMA IOVA state + * @iter: block layer DMA iterator + * + * Start DMA mapping @req to @dma_dev. @state and @iter are provided by the + * caller and don't need to be initialized. @state needs to be stored for use + * at unmap time, @iter is only needed at map time. + * + * Returns %false if there is no segment to map, including due to an error, or + * %true it it did map a segment. + * + * If a segment was mapped, the DMA address for it is returned in @iter.addr and + * the length in @iter.len. If no segment was mapped the status code is + * returned in @iter.status. + * + * The caller can call blk_rq_dma_map_coalesce() to check if further segments + * need to be mapped after this, or go straight to blk_rq_dma_map_iter_next() + * to try to map the following segments. + */ +bool blk_rq_dma_map_iter_start(struct request *req, struct device *dma_dev, + struct dma_iova_state *state, struct blk_dma_iter *iter) +{ + unsigned int total_len = blk_rq_payload_bytes(req); + struct phys_vec vec; + + iter->iter.bio = req->bio; + iter->iter.iter = req->bio->bi_iter; + memset(&iter->p2pdma, 0, sizeof(iter->p2pdma)); + iter->status = BLK_STS_OK; + + /* + * Grab the first segment ASAP because we'll need it to check for P2P + * transfers. + */ + if (!blk_map_iter_next(req, &iter->iter, &vec)) + return false; + + if (IS_ENABLED(CONFIG_PCI_P2PDMA) && (req->cmd_flags & REQ_P2PDMA)) { + switch (pci_p2pdma_state(&iter->p2pdma, dma_dev, + blk_phys_to_page(vec.paddr))) { + case PCI_P2PDMA_MAP_BUS_ADDR: + return blk_dma_map_bus(req, dma_dev, iter, &vec); + case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE: + /* + * P2P transfers through the host bridge are treated the + * same as non-P2P transfers below and during unmap. + */ + req->cmd_flags &= ~REQ_P2PDMA; + break; + default: + iter->status = BLK_STS_INVAL; + return false; + } + } + + if (blk_can_dma_map_iova(req, dma_dev) && + dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len)) + return blk_rq_dma_map_iova(req, dma_dev, state, iter, &vec); + return blk_dma_map_direct(req, dma_dev, iter, &vec); +} +EXPORT_SYMBOL_GPL(blk_rq_dma_map_iter_start); + +/** + * blk_rq_dma_map_iter_next - map the next DMA segment for a request + * @req: request to map + * @dma_dev: device to map to + * @state: DMA IOVA state + * @iter: block layer DMA iterator + * + * Iterate to the next mapping after a previous call to + * blk_rq_dma_map_iter_start(). See there for a detailed description of the + * arguments. + * + * Returns %false if there is no segment to map, including due to an error, or + * %true it it did map a segment. + * + * If a segment was mapped, the DMA address for it is returned in @iter.addr and + * the length in @iter.len. If no segment was mapped the status code is + * returned in @iter.status. + */ +bool blk_rq_dma_map_iter_next(struct request *req, struct device *dma_dev, + struct dma_iova_state *state, struct blk_dma_iter *iter) +{ + struct phys_vec vec; + + if (!blk_map_iter_next(req, &iter->iter, &vec)) + return false; + + if (iter->p2pdma.map == PCI_P2PDMA_MAP_BUS_ADDR) + return blk_dma_map_bus(req, dma_dev, iter, &vec); + return blk_dma_map_direct(req, dma_dev, iter, &vec); +} +EXPORT_SYMBOL_GPL(blk_rq_dma_map_iter_next); + static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, struct scatterlist *sglist) { diff --git a/include/linux/blk-mq-dma.h b/include/linux/blk-mq-dma.h new file mode 100644 index 000000000000..f1dae14e8203 --- /dev/null +++ b/include/linux/blk-mq-dma.h @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef BLK_MQ_DMA_H +#define BLK_MQ_DMA_H + +#include +#include + +struct blk_dma_iter { + /* Output address range for this iteration */ + dma_addr_t addr; + u32 len; + + /* Status code. Only valid when blk_rq_dma_map_iter_* returned false */ + blk_status_t status; + + /* Internal to blk_rq_dma_map_iter_* */ + struct req_iterator iter; + struct pci_p2pdma_map_state p2pdma; +}; + +bool blk_rq_dma_map_iter_start(struct request *req, struct device *dma_dev, + struct dma_iova_state *state, struct blk_dma_iter *iter); +bool blk_rq_dma_map_iter_next(struct request *req, struct device *dma_dev, + struct dma_iova_state *state, struct blk_dma_iter *iter); + +/** + * blk_rq_dma_map_coalesce - were all segments coalesced? + * @state: DMA state to check + * + * Returns true if blk_rq_dma_map_iter_start coalesced all segments into a + * single DMA range. + */ +static inline bool blk_rq_dma_map_coalesce(struct dma_iova_state *state) +{ + return dma_use_iova(state); +} + +/** + * blk_rq_dma_map_coalesce - try to DMA unmap a request + * @req: request to unmap + * @dma_dev: device to unmap from + * @state: DMA IOVA state + * + * Returns %false if the callers needs to manually unmap every DMA segment + * mapped using @iter or %true if no work is left to be done. + */ +static inline bool blk_rq_dma_unmap(struct request *req, struct device *dma_dev, + struct dma_iova_state *state) +{ + if (req->cmd_flags & REQ_P2PDMA) + return true; + + if (dma_use_iova(state)) { + dma_iova_destroy(dma_dev, state, rq_dma_dir(req), 0); + return true; + } + + if (!dma_need_unmap(dma_dev)) + return true; + + return false; +} + +#endif /* BLK_MQ_DMA_H */ From patchwork Sun Oct 27 14:21:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13852665 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F6B8D13562 for ; Sun, 27 Oct 2024 14:23:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E9CF98D000B; Sun, 27 Oct 2024 10:23:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E4C238D0001; Sun, 27 Oct 2024 10:23:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9E988D000B; Sun, 27 Oct 2024 10:23:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id AA5C58D0001 for ; Sun, 27 Oct 2024 10:23:07 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 695CB1A1C42 for ; Sun, 27 Oct 2024 14:22:28 +0000 (UTC) X-FDA: 82719598668.15.8094FFA Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf05.hostedemail.com (Postfix) with ESMTP id B73E0100004 for ; Sun, 27 Oct 2024 14:22:23 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fcGU8E7n; spf=pass (imf05.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730038907; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Mjq6zdSNSWlDPgvZBL8m4liCS82S9MHPHsVhY49Am9o=; b=FfnU/PQBLFBnRiHleM9HsKql8ROZYXohE9YuaUIock2cb8JNov66+l7N9AkcGXc0LpS7Vd UsClKnz7RvO4ZgIe3rDbrLFVGjfnSLy0B5wF+ouMy5q4Ffs77xE65utZlYvlogxXUOR8K3 Zq3xNN5iQiorn1J7qu7PlXznzRDP20U= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fcGU8E7n; spf=pass (imf05.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730038907; a=rsa-sha256; cv=none; b=CcX1m9d2KkJOwmAszuV4svR1W2OCd5vFJHnP5c7slVtw+teBFHxaoobKm1s8bJ/u0tOr9T w1MJucspitshBA1+lgpB+aIMXMXAElWfokR19GOi34qUaoBW5RKVgUM536aBPHo86wafTP R9DUyK+C35WReosMgNG4PRVW5BH0WTM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 25857A40FAC; Sun, 27 Oct 2024 14:21:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 30CB6C4CEC3; Sun, 27 Oct 2024 14:23:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730038985; bh=tSv2Q/UIDmsmBR/B+0LlfVehWktc2fzMNx0dgOtrb5I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fcGU8E7nqAZ/TN3lckAgvc+gp+UfN38AiQA0fW96+skp6qRwatz6+Tp+Y9GZAXoZv 32gYhCzvwkTALLTPnjKbgOaQPHt8sGsNEdt9KJbNhKApAh7jv24SRFA3hGiLfrQYgb G/Po80vNtP9F8BeE8e4uWTkMLLm5fivWyKQHJxEIt/mCjmNIb97v/FnvfgqfoT4TTh rz7W2/MVey+NJeDPA+D1i7fdC/b2byuhn0NKXfgpQS+wRQ6AryyTiMSut6bA7FGvnc bh/vGMOJ+JsUoHC7zVNhazkcjoWtkM0V5Idz20gkDCG7oEUw/cZU4OFtBrphpw8Tfp o+KYa1jtD4fhg== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 5/7] nvme-pci: remove struct nvme_descriptor Date: Sun, 27 Oct 2024 16:21:58 +0200 Message-ID: <31fe216877a270c00bc79cddace3a9f4b3ade50c.1730037261.git.leon@kernel.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: B73E0100004 X-Stat-Signature: 77jbc375b38xiwcbtfauysp4hydcu3d4 X-HE-Tag: 1730038943-598958 X-HE-Meta: U2FsdGVkX1+Calerz6KIrCtaWQKhspU3Rc3N7A8RHxJ3Uy2jyyH+3qALhCg3mTTQSuMVZdov3F0Fk2XwQ/QGs5qAYT1RD3SXpuaN4DJTf6MdbsRP9CfyS4hQzQoO3x9GMeBjXrFjQM1d62hs5ffk9CHerLjw43Bp4+3enWSuvsPliYJJuQprnM44f5tiUAXuynDb0GYh1PW9vxYf5pNQI6Pb2QT5oZwmDChnjISmY4fVt/SXLZqny1N2CYnUn1FTOo2BnqwKslDc+Zv1xTIgg9wL1xnjWD0j7a3G/RlbNsdxpzzAnX6J56Bk0KpQRpzRssrCG84ewochKWuRviCAa5EWYjyhAIl/Lq4JnnJgmoAPcH5SAfWFP0vOH+DBH9x9Qzle2lwzEeOuaYLL5CyUrxU8W+8Z7eNsm0igXV5DSEtA4azR8TwaySMknmv2f7fyV6B2NpwAPaTxVHg/0CXJUQwmiE51t6jiiE0BWwldkzv9w0LPw5MtvbitccLNDhAkp2qrdBI108A+tc3anPZJFKXL0admtYH58bWk3PUO683l7sjHnyZFb3T7roh83ToI4ps+w2WYYXAPPO/Bx6Cv7T3dr5Ph4/GeqIgx56mWid7dlQhcF+CKo2y9ixcXc90vc8TTrfF69Lso8c9lAez/6w+Bapw/VIzk8Knp5rYho0UC1B0w7d5dZxl6nZjWHvr9hqrxiz6GPAnODakIJuxUYPTvrahEMpOwG2ntvNWLIwD05P2wgxV53gdQ4/i0OmUrPNPS3lm3F3jcpogVlQwryDpxyPWlGFHg9BKgqw8COcGSlrtvbITtfkeiML4GMHBK4gB+BBaWsAUxN7GKRP4o4Mk/CWLJv3QTvzu/9FbewOWm4x/juCEqADvyr8cq1OO4iVAmvmPQ/N4NKhWzfzqveeMC1C6hIij4MnCLZXikKdJPgtHdWnvn5ydD8r90qgVL8rL/iMXaICUim/Bh/7L cz+t7yQA CwIL3sJ/r3/K9WkttFBcsQ1kYG6bs+eFiMpqnPbUFU3FrDAHsioMvwCvjfP3FbxY8QtLndArHeMfiURYBbuSGPY1HEm5C71Imow0+izMrjTdpXb+t3uivOJ5bZdwB4WNHoCNLgPUjhLuwxo9zF1MXHGJUx1RQhKPTttwvy5NkG1E6FfxeQVwkUGUh+cuQ0cTW/StfgRGERHcTcg6rmfqOPTwWqiBgtJjfiwCNf8COuru/BhsaKbenEqAfb5zFxCETPENr1cFpcIx5BAeYY9H10mN6gcypIbzHJ2DXjSVOaaVPdAsXagcNnN/wrJpRIGiWmtL2 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Christoph Hellwig There is no real point in having a union of two pointer types here, just use a void pointer as we mix and match types between the arms of the union between the allocation and freeing side already. Also rename the nr_allocations field to nr_descriptors to better describe what it does. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky --- drivers/nvme/host/pci.c | 51 ++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index 4b9fda0b1d9a..ba077a42cbba 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -43,7 +43,8 @@ */ #define NVME_MAX_KB_SZ 8192 #define NVME_MAX_SEGS 128 -#define NVME_MAX_NR_ALLOCATIONS 5 + +#define NVME_MAX_NR_DESCRIPTORS 5 static int use_threaded_interrupts; module_param(use_threaded_interrupts, int, 0444); @@ -216,28 +217,20 @@ struct nvme_queue { struct completion delete_done; }; -union nvme_descriptor { - struct nvme_sgl_desc *sg_list; - __le64 *prp_list; -}; - /* * The nvme_iod describes the data in an I/O. - * - * The sg pointer contains the list of PRP/SGL chunk allocations in addition - * to the actual struct scatterlist. */ struct nvme_iod { struct nvme_request req; struct nvme_command cmd; bool aborted; - s8 nr_allocations; /* PRP list pool allocations. 0 means small - pool in use */ + /* # of PRP/SGL descriptors: (0 for small pool) */ + s8 nr_descriptors; unsigned int dma_len; /* length of single DMA segment mapping */ dma_addr_t first_dma; dma_addr_t meta_dma; struct sg_table sgt; - union nvme_descriptor list[NVME_MAX_NR_ALLOCATIONS]; + void *descriptors[NVME_MAX_NR_DESCRIPTORS]; }; static inline unsigned int nvme_dbbuf_size(struct nvme_dev *dev) @@ -528,8 +521,8 @@ static void nvme_free_prps(struct nvme_dev *dev, struct request *req) dma_addr_t dma_addr = iod->first_dma; int i; - for (i = 0; i < iod->nr_allocations; i++) { - __le64 *prp_list = iod->list[i].prp_list; + for (i = 0; i < iod->nr_descriptors; i++) { + __le64 *prp_list = iod->descriptors[i]; dma_addr_t next_dma_addr = le64_to_cpu(prp_list[last_prp]); dma_pool_free(dev->prp_page_pool, prp_list, dma_addr); @@ -551,11 +544,11 @@ static void nvme_unmap_data(struct nvme_dev *dev, struct request *req) dma_unmap_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req), 0); - if (iod->nr_allocations == 0) - dma_pool_free(dev->prp_small_pool, iod->list[0].sg_list, + if (iod->nr_descriptors == 0) + dma_pool_free(dev->prp_small_pool, iod->descriptors[0], iod->first_dma); - else if (iod->nr_allocations == 1) - dma_pool_free(dev->prp_page_pool, iod->list[0].sg_list, + else if (iod->nr_descriptors == 1) + dma_pool_free(dev->prp_page_pool, iod->descriptors[0], iod->first_dma); else nvme_free_prps(dev, req); @@ -613,18 +606,18 @@ static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE); if (nprps <= (256 / 8)) { pool = dev->prp_small_pool; - iod->nr_allocations = 0; + iod->nr_descriptors = 0; } else { pool = dev->prp_page_pool; - iod->nr_allocations = 1; + iod->nr_descriptors = 1; } prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); if (!prp_list) { - iod->nr_allocations = -1; + iod->nr_descriptors = -1; return BLK_STS_RESOURCE; } - iod->list[0].prp_list = prp_list; + iod->descriptors[0] = prp_list; iod->first_dma = prp_dma; i = 0; for (;;) { @@ -633,7 +626,7 @@ static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); if (!prp_list) goto free_prps; - iod->list[iod->nr_allocations++].prp_list = prp_list; + iod->descriptors[iod->nr_descriptors++] = prp_list; prp_list[0] = old_prp_list[i - 1]; old_prp_list[i - 1] = cpu_to_le64(prp_dma); i = 1; @@ -703,19 +696,19 @@ static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev, if (entries <= (256 / sizeof(struct nvme_sgl_desc))) { pool = dev->prp_small_pool; - iod->nr_allocations = 0; + iod->nr_descriptors = 0; } else { pool = dev->prp_page_pool; - iod->nr_allocations = 1; + iod->nr_descriptors = 1; } sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma); if (!sg_list) { - iod->nr_allocations = -1; + iod->nr_descriptors = -1; return BLK_STS_RESOURCE; } - iod->list[0].sg_list = sg_list; + iod->descriptors[0] = sg_list; iod->first_dma = sgl_dma; nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries); @@ -841,7 +834,7 @@ static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req) blk_status_t ret; iod->aborted = false; - iod->nr_allocations = -1; + iod->nr_descriptors = -1; iod->sgt.nents = 0; ret = nvme_setup_cmd(req->q->queuedata, req); @@ -3626,7 +3619,7 @@ static int __init nvme_init(void) BUILD_BUG_ON(IRQ_AFFINITY_MAX_SETS < 2); BUILD_BUG_ON(NVME_MAX_SEGS > SGES_PER_PAGE); BUILD_BUG_ON(sizeof(struct scatterlist) * NVME_MAX_SEGS > PAGE_SIZE); - BUILD_BUG_ON(nvme_pci_npages_prp() > NVME_MAX_NR_ALLOCATIONS); + BUILD_BUG_ON(nvme_pci_npages_prp() > NVME_MAX_NR_DESCRIPTORS); return pci_register_driver(&nvme_driver); } From patchwork Sun Oct 27 14:21:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13852666 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55FF0D13562 for ; Sun, 27 Oct 2024 14:23:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8CF58D000C; Sun, 27 Oct 2024 10:23:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D3CC28D0001; Sun, 27 Oct 2024 10:23:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB44B8D000C; Sun, 27 Oct 2024 10:23:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9B7A38D0001 for ; Sun, 27 Oct 2024 10:23:11 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E54C9A1C58 for ; Sun, 27 Oct 2024 14:22:31 +0000 (UTC) X-FDA: 82719598290.03.9F7E1CA Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf13.hostedemail.com (Postfix) with ESMTP id EE2C82000F for ; Sun, 27 Oct 2024 14:22:46 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eiJIF0ZF; spf=pass (imf13.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730038936; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hnHLxYQ+J1zQEl3MZT/YTSEtWDueiC94+si4jpjdUes=; b=qtX9nEheB7E0NmTQna+rA9HxvZ6u3MFuF3DrgmfzJxgl8x1CHWzzI6s3c2UpQLApb0iZPn 8kf+jlwOyf1BBHZsVJSx0WqnqqILFNKN7xibgN2qnaA6XDlwv1kCGnvAXP7+/1BZME+MmP nonBn5eFKVGUDERD+5H0E+qPJQt+V+o= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eiJIF0ZF; spf=pass (imf13.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730038936; a=rsa-sha256; cv=none; b=PFz7VeCZIKV+Jgz0ov+bVonKOWoiayrw9ZpC5lA3QLj9lFtR0of6s+ptNZo74qJq+sY34J +KuxrW5iUCKy3m4nGeo1eZjJjpGn0Lud4MwO9TQL8z5KB2IbXPRYJBDtpYcJwId8vSSDni +CWHNOWsAu7Q6PtX+WqMPVNXh2Cf6dg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 25DD4A40FB2; Sun, 27 Oct 2024 14:21:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29F16C4CEC3; Sun, 27 Oct 2024 14:23:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730038989; bh=ApIelQA2oAfFsx+G9CAFnUZptSSapywa7pTJgnwTMSc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eiJIF0ZFLHGKP8f4jSGly33bDKFCG2di1eSRLyav3A3c0jDbi3wFuI/4gTIyAJqsP 6BsfaifyFf9MsSzeY+5fbfpU0b6hcssFg4v/xfeYYw6VwQWGLLeo3B/keDpUA4VwIn +NmMzdDiyGF3OZZgTiwOFjAj7pV52518FgsFTHu7WSP5J++G+dgRues27OYr/5xhtM pnBlX75XHyWUZvAl9+fd09fuOyo/S0uvjf0rdnRkSmwZeoJZcK471IFO5l/4Q3+qwV p+epEDOkoend5m1LL4+a9d5gYc5UJxwnsS3xd1VeOpI9zP/h1Z21Z2tQwfEqqppaO5 FYRlcndyyO9gg== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 6/7] nvme-pci: use a better encoding for small prp pool allocations Date: Sun, 27 Oct 2024 16:21:59 +0200 Message-ID: X-Mailer: git-send-email 2.46.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: h97fauiznnhnc4cn5ac6jxjajtt4dern X-Rspamd-Queue-Id: EE2C82000F X-Rspamd-Server: rspam11 X-HE-Tag: 1730038966-425337 X-HE-Meta: U2FsdGVkX1/u71FUa0L+iNnphOTB1YG9DH3Gp9rjJvaCQEhtQtjwa2FquRjoMa3B4xTrqyC+D68rdAE4E06rHGaJbERflZTKHjN41R+6wxD+IM++N2boWqsG+28D90gDbLl/SdvZAJgPckXLN1RoQEgATei1l+7eprYhNt1v/SBzizCueeovO0Oxp91gbdOPTtPeSknyQYuNjWERZ0CI3vQ9Prce/4DyQiw9D3LQ4u4zHFe5vuWf6SiahePqj+Ys7oqygPpeIJZGqmTTH70kTiWlBU48gN2Pf9yRTX/nMIF7yxIku2T7PKY73RvU6E0eS1SLjiKPfEr6cxwuDl8CYQ0rXgORzrkLEVVDTeltWbWIGgFal4yjsV8Cs1gTUj/otrKmrjMdR26YE2Of4O9aofQS4eA4KOjM/wXfQEoVfDr/ZZmReBW0FfoErxNYvoS9qbDDepqPhVvN8Vg7MiHJiBkdmy0fxY6ZpeAqhfU1mJ8HqTdjO6eSOgjPR0nEyYQTGLuwDKo/Xbj9JovQQO0FL+q0CE6fi38kdonz9SgAk0knkrcb/xqKKV3w3EOwDhKyFnSYwOB4sY3LF7vdUuKlGkX/QmfAC+vAepJZmEMQnkyOcrkPAOnRKb/CuszgGeIXiiE99WwcbKvLloy7mOsqRsi9JoauXGMFONRhwe+ZCBWIgZOnwLbQlZDbn/WoueVI6fpSmfvb57pjDEqw1KoqnYf+uuRuDIIMxliN9Mx2MY/avHF/FRdEuCtWR6BPAWXCNKK9BaP1NGn+5BXNtcLXll4RxwTwMnlGg667Uz9Do62Y9jULVOWVm+N4pHCwnp4naTGLugkfuxeT1uIjlR40UBDffL5o+vHjUamkugKVaO9G1wTp5EmUvOF47QV/t5ZYnzrqPPX92cFoCnEl2bzzQtwjzI7ThSc1gjIZDoIa8QSeCv49e3lewPgetsI5jphzPkb62aS4/RXhAGgXsOB yCGTpy53 G+hBFi6MgBsl2DRR9m6E47NLf+cv3yqhCFn+LbYLYVABvJwIls4LREfzsUULVKwhJ4S1ebPZ52eUAhiZY0HHpRtApJLd7KlwtoyDx+1koshnFYgYhPCNhIR6pGKU7egUxPAe9JUvfrkAVGZ7J6D8iaKLxvC+CvJJZd4UXZ6qR90F1eS/gNYGgBPUPRHzohscBjpeIYonE/Q/Xjx7JVJ4lJEIixdSVXdQ4pUR1u7OjjjGEl4o= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Christoph Hellwig There is plenty of unused space in the iod next to nr_descriptors. Add a separate bool (which could be condensed to a single bit once we start running out of space) to encode that the transfer is using the full page sized pool, and use a normal 0..n count for the number of descriptors. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky --- drivers/nvme/host/pci.c | 85 +++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index ba077a42cbba..79cd65a5f311 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -45,6 +45,7 @@ #define NVME_MAX_SEGS 128 #define NVME_MAX_NR_DESCRIPTORS 5 +#define NVME_SMALL_DESCRIPTOR_SIZE 256 static int use_threaded_interrupts; module_param(use_threaded_interrupts, int, 0444); @@ -224,8 +225,8 @@ struct nvme_iod { struct nvme_request req; struct nvme_command cmd; bool aborted; - /* # of PRP/SGL descriptors: (0 for small pool) */ - s8 nr_descriptors; + u8 nr_descriptors; /* # of PRP/SGL descriptors */ + bool large_descriptors; /* uses the full page sized descriptor pool */ unsigned int dma_len; /* length of single DMA segment mapping */ dma_addr_t first_dma; dma_addr_t meta_dma; @@ -514,13 +515,27 @@ static inline bool nvme_pci_use_sgls(struct nvme_dev *dev, struct request *req, return true; } -static void nvme_free_prps(struct nvme_dev *dev, struct request *req) +static inline struct dma_pool *nvme_dma_pool(struct nvme_dev *dev, + struct nvme_iod *iod) +{ + if (iod->large_descriptors) + return dev->prp_page_pool; + return dev->prp_small_pool; +} + +static void nvme_free_descriptors(struct nvme_dev *dev, struct request *req) { const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1; struct nvme_iod *iod = blk_mq_rq_to_pdu(req); dma_addr_t dma_addr = iod->first_dma; int i; + if (iod->nr_descriptors == 1) { + dma_pool_free(nvme_dma_pool(dev, iod), iod->descriptors[0], + dma_addr); + return; + } + for (i = 0; i < iod->nr_descriptors; i++) { __le64 *prp_list = iod->descriptors[i]; dma_addr_t next_dma_addr = le64_to_cpu(prp_list[last_prp]); @@ -543,15 +558,7 @@ static void nvme_unmap_data(struct nvme_dev *dev, struct request *req) WARN_ON_ONCE(!iod->sgt.nents); dma_unmap_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req), 0); - - if (iod->nr_descriptors == 0) - dma_pool_free(dev->prp_small_pool, iod->descriptors[0], - iod->first_dma); - else if (iod->nr_descriptors == 1) - dma_pool_free(dev->prp_page_pool, iod->descriptors[0], - iod->first_dma); - else - nvme_free_prps(dev, req); + nvme_free_descriptors(dev, req); mempool_free(iod->sgt.sgl, dev->iod_mempool); } @@ -573,7 +580,6 @@ static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, struct request *req, struct nvme_rw_command *cmnd) { struct nvme_iod *iod = blk_mq_rq_to_pdu(req); - struct dma_pool *pool; int length = blk_rq_payload_bytes(req); struct scatterlist *sg = iod->sgt.sgl; int dma_len = sg_dma_len(sg); @@ -581,7 +587,7 @@ static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, int offset = dma_addr & (NVME_CTRL_PAGE_SIZE - 1); __le64 *prp_list; dma_addr_t prp_dma; - int nprps, i; + int i; length -= (NVME_CTRL_PAGE_SIZE - offset); if (length <= 0) { @@ -603,27 +609,23 @@ static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, goto done; } - nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE); - if (nprps <= (256 / 8)) { - pool = dev->prp_small_pool; - iod->nr_descriptors = 0; - } else { - pool = dev->prp_page_pool; - iod->nr_descriptors = 1; - } + if (DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE) > + NVME_SMALL_DESCRIPTOR_SIZE / sizeof(__le64)) + iod->large_descriptors = true; - prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); - if (!prp_list) { - iod->nr_descriptors = -1; + prp_list = dma_pool_alloc(nvme_dma_pool(dev, iod), GFP_ATOMIC, + &prp_dma); + if (!prp_list) return BLK_STS_RESOURCE; - } - iod->descriptors[0] = prp_list; + iod->descriptors[iod->nr_descriptors++] = prp_list; iod->first_dma = prp_dma; i = 0; for (;;) { if (i == NVME_CTRL_PAGE_SIZE >> 3) { __le64 *old_prp_list = prp_list; - prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); + + prp_list = dma_pool_alloc(dev->prp_page_pool, + GFP_ATOMIC, &prp_dma); if (!prp_list) goto free_prps; iod->descriptors[iod->nr_descriptors++] = prp_list; @@ -650,7 +652,7 @@ static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma); return BLK_STS_OK; free_prps: - nvme_free_prps(dev, req); + nvme_free_descriptors(dev, req); return BLK_STS_RESOURCE; bad_sgl: WARN(DO_ONCE(nvme_print_sgl, iod->sgt.sgl, iod->sgt.nents), @@ -679,7 +681,6 @@ static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev, struct request *req, struct nvme_rw_command *cmd) { struct nvme_iod *iod = blk_mq_rq_to_pdu(req); - struct dma_pool *pool; struct nvme_sgl_desc *sg_list; struct scatterlist *sg = iod->sgt.sgl; unsigned int entries = iod->sgt.nents; @@ -694,21 +695,13 @@ static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev, return BLK_STS_OK; } - if (entries <= (256 / sizeof(struct nvme_sgl_desc))) { - pool = dev->prp_small_pool; - iod->nr_descriptors = 0; - } else { - pool = dev->prp_page_pool; - iod->nr_descriptors = 1; - } + if (entries > NVME_SMALL_DESCRIPTOR_SIZE / sizeof(*sg_list)) + iod->large_descriptors = true; - sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma); - if (!sg_list) { - iod->nr_descriptors = -1; + sg_list = dma_pool_alloc(nvme_dma_pool(dev, iod), GFP_ATOMIC, &sgl_dma); + if (!sg_list) return BLK_STS_RESOURCE; - } - - iod->descriptors[0] = sg_list; + iod->descriptors[iod->nr_descriptors++] = sg_list; iod->first_dma = sgl_dma; nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries); @@ -834,7 +827,8 @@ static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req) blk_status_t ret; iod->aborted = false; - iod->nr_descriptors = -1; + iod->nr_descriptors = 0; + iod->large_descriptors = false; iod->sgt.nents = 0; ret = nvme_setup_cmd(req->q->queuedata, req); @@ -2694,7 +2688,8 @@ static int nvme_setup_prp_pools(struct nvme_dev *dev) /* Optimisation for I/Os between 4k and 128k */ dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev, - 256, 256, 0); + NVME_SMALL_DESCRIPTOR_SIZE, + NVME_SMALL_DESCRIPTOR_SIZE, 0); if (!dev->prp_small_pool) { dma_pool_destroy(dev->prp_page_pool); return -ENOMEM; From patchwork Sun Oct 27 14:22:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13852667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB9DFD13562 for ; Sun, 27 Oct 2024 14:23:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3ED5F8D000D; Sun, 27 Oct 2024 10:23:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 39D9C8D0001; Sun, 27 Oct 2024 10:23:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F04D8D000D; Sun, 27 Oct 2024 10:23:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E85168D0001 for ; Sun, 27 Oct 2024 10:23:15 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A01FA1C76CD for ; Sun, 27 Oct 2024 14:22:50 +0000 (UTC) X-FDA: 82719598920.24.A8493D9 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf07.hostedemail.com (Postfix) with ESMTP id 4304B40019 for ; Sun, 27 Oct 2024 14:22:43 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ptsQ2SUL; spf=pass (imf07.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730038838; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qt/WOdHegdaTG+mHl3e4+Eo0EpFRQ4iMUDGo23BMo9k=; b=q+FEd21YpZW1pBbfYNBIqfrt8t9HIFlj1Vx8wkGF6nySyEG50ehuCsDQhxhJ90hW4o8gA5 5/3xx/gd3pLiyUI/aFJSDUB1/nW6mnXYqUGSl7fsiOY3YKPDDn9ORL87nQOUY2qocue4PS 6jtc4VmeKaiqkJ0/zt47e7+NWVCmcYA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730038838; a=rsa-sha256; cv=none; b=nNAE+9ELTDv58txwhMNuH0zwJlwuqC3cWBnUZ40PpTTzNhQrAkwK+uGHj2Y9AplGffVBEd EJdULz7Nzjmykwo9GdNNY9iUWxunThMF7Hkd/bA9lHQHtQB8zGTJPHhSW9Nb7fPGi6h51d LNCjFVjEk63d9bhLI2xDcWpA7Fh+QVo= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ptsQ2SUL; spf=pass (imf07.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 2632DA40FA9; Sun, 27 Oct 2024 14:21:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29A8FC4CEE6; Sun, 27 Oct 2024 14:23:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730038993; bh=mGSLC0i0j/C6wQh6DJGfX8WLshJrc4MOVDi7g2OudPw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ptsQ2SULkjspInuBxCoAkFPYY7jouQyTCXw99hTQsjsyguomCiYwPAhX2ulgbDfVD v1Q4Ve/tne6hzcG5k0jykyby4bLlF2mRUXpswpsqXN8vqFQcQg07bdvLO6byu01DcS clbAWWn3zHoII1UOFysYLMz9+H1SG3FiCpfweUYqXrGy9a77XXW0xU2gwgdROT2Nhf ptDji064KNRa3Ddvj171WP7r35qZULRqe1pbNJfHlYsdUXBb5J6BanoovA3bZ4hYG/ 8HpJLLqa7rP+PaY92IHdwfikA3lXG6yT29ILuWbDlX6DTfEIce6r1dR2MTd142WuMe wmbBQoGCtKJ0Q== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 7/7] nvme-pci: convert to blk_rq_dma_map Date: Sun, 27 Oct 2024 16:22:00 +0200 Message-ID: <6038b47007ae804f0795e5f9d9cbc9c4a63a15b2.1730037261.git.leon@kernel.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 4304B40019 X-Stat-Signature: p4h6dm4gjp5cnndwa977ruwg6quunncs X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1730038963-377177 X-HE-Meta: U2FsdGVkX18vOSbEWf0xpNPgm7WjdEa6zkxSjFe6CYrTdQloyG809hFafznEKwC8BznZG6YB7KlUIaVRXz8Zhu+dZW54knEPvQRnLvszU7yNNvgv8PPw+BzuoscSl+UCfIsIu2RtdR/NeZO5EymPCIhM4vo9VyrXTyuInmAfhLcVQI6cFZ5Gv4yU8Iz7VMKiuhgd+MXR/Lz2R+NPv2enZmrmc/ca5+grSw3IGIXGTVrhkOuS8Pd1ynp1tuAfMKd7ohNKJ7q5BjcuAqfb3HX7fKocEyMFu9hjlEpCJdt3yDwnMbpREJPqTfdo6hl2pUQ3Z37dN8zZ90MtgWoyljhNDWJV7jh0IRkv8ou4TSQfJelQJasSUKghT8T5Z7RqmNn9PbWdys17B16QD/JkvqlhnkJUvhpsLozfL0tiTBMPajl6uxSnL3iE3YGVvx963mYY9GIlX47OM2gPsSfp990qclSzo728/jqLlc0VgFxCh8nLA/lR+D2rjx8dgGjyCe2/0XeL105qH7lclD4Js5Aig/xAEPKytS232cBwIw8mRuylWLB85gXuU5x1evnKVN4yWgKl3sF/+NNUikvNDyxeiunIdX04qjzaeAap7WCK92omzKXZ0+stc6/dJHcAJxd9wzHo0u12qCImrRnLKDS75ZvMmoh+OZ9Z6vQ+U0ID2Y5rSMtk6T1iGMjahZCojtm4XPlvWPifb4rUYIeFk1lvchhf+vXXqcQtxKysKwlsN+9ZSq+ZIvAOS9yawcYP+T6YEPU4O1o6n04m/7egk30+/iN/lj4yfSBuuXVK5x5vksRVaRuvmD2uzlzj5EYDb8Nztt2YgvBMURnRp8uU5ZiEEOxyZZtYYvqIpU6PYlA7VlhJomyWcWgdTcry7b3wpWSru4Swks9k3zWO/8lMqAZop01ZDmVAS9MWdlbpRzx3LjdFdDR1coIWmkfFe3xShaj7ory8llc8ZmLTfpI95ix Az2kKlPF zzhcePu1uF7XI4CEEFrvwPkzYpIJ3IsjT+3OgyBbpmUbv8Q4tjN29j4k41dV6ZwA6YuBWVoR3u/nr6IUIuNhxG0iAbTFNlj6ulbju+HXFBT/sxCzy+8VL36rHeWT5SCYTkrlpjBK9x/43Rau8k5N1xua8k0zyhaRBzByU8vS/8VnhHqrhZhYyzczGBabdJ88d5g8dHDbvvoOqkz5ZO4IzX8FPa9ZWlo1JuBP/fqLOLQe8VAT03kA9noWYiaftywyVIdpr7P46ZR5LmYJbGBhax0+Q6asigLN3usmi X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Christoph Hellwig Use the blk_rq_dma_map API to DMA map requests instead of scatterlists. This also removes the fast path single segment code as the blk_rq_dma_map naturally inlines single IOVA segment mappings into the preallocated structure. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky --- drivers/nvme/host/pci.c | 382 +++++++++++++++++++++------------------- 1 file changed, 205 insertions(+), 177 deletions(-) diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index 79cd65a5f311..f41db1efecb1 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -27,7 +27,6 @@ #include #include #include -#include #include "trace.h" #include "nvme.h" @@ -227,10 +226,9 @@ struct nvme_iod { bool aborted; u8 nr_descriptors; /* # of PRP/SGL descriptors */ bool large_descriptors; /* uses the full page sized descriptor pool */ - unsigned int dma_len; /* length of single DMA segment mapping */ - dma_addr_t first_dma; + unsigned int total_len; /* length of the entire transfer */ dma_addr_t meta_dma; - struct sg_table sgt; + struct dma_iova_state dma_state; void *descriptors[NVME_MAX_NR_DESCRIPTORS]; }; @@ -527,9 +525,14 @@ static void nvme_free_descriptors(struct nvme_dev *dev, struct request *req) { const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1; struct nvme_iod *iod = blk_mq_rq_to_pdu(req); - dma_addr_t dma_addr = iod->first_dma; + dma_addr_t dma_addr; int i; + if (iod->cmd.common.flags & NVME_CMD_SGL_METABUF) + dma_addr = le64_to_cpu(iod->cmd.common.dptr.sgl.addr); + else + dma_addr = le64_to_cpu(iod->cmd.common.dptr.prp2); + if (iod->nr_descriptors == 1) { dma_pool_free(nvme_dma_pool(dev, iod), iod->descriptors[0], dma_addr); @@ -545,67 +548,143 @@ static void nvme_free_descriptors(struct nvme_dev *dev, struct request *req) } } -static void nvme_unmap_data(struct nvme_dev *dev, struct request *req) +static void nvme_free_prps(struct nvme_dev *dev, struct request *req) { struct nvme_iod *iod = blk_mq_rq_to_pdu(req); - - if (iod->dma_len) { - dma_unmap_page(dev->dev, iod->first_dma, iod->dma_len, - rq_dma_dir(req)); + enum dma_data_direction dir = rq_dma_dir(req); + int length = iod->total_len; + dma_addr_t dma_addr; + int prp_len, nprps, i, desc; + __le64 *prp_list; + dma_addr_t dma_start; + u32 dma_len; + + dma_addr = le64_to_cpu(iod->cmd.common.dptr.prp1); + prp_len = NVME_CTRL_PAGE_SIZE - (dma_addr & (NVME_CTRL_PAGE_SIZE - 1)); + prp_len = min(length, prp_len); + length -= prp_len; + if (!length) { + dma_unmap_page(dev->dev, dma_addr, prp_len, dir); return; } - WARN_ON_ONCE(!iod->sgt.nents); + dma_start = dma_addr; + dma_len = prp_len; - dma_unmap_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req), 0); - nvme_free_descriptors(dev, req); - mempool_free(iod->sgt.sgl, dev->iod_mempool); + dma_addr = le64_to_cpu(iod->cmd.common.dptr.prp2); + if (length <= NVME_CTRL_PAGE_SIZE) { + if (dma_addr != dma_start + dma_len) { + dma_unmap_page(dev->dev, dma_start, dma_len, dir); + dma_start = dma_addr; + dma_len = 0; + } + dma_len += length; + goto done; + } + + nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE); + i = 0; + desc = 0; + prp_list = iod->descriptors[desc]; + do { + if (i == NVME_CTRL_PAGE_SIZE >> 3) { + prp_list = iod->descriptors[++desc]; + i = 0; + } + + dma_addr = le64_to_cpu(prp_list[i++]); + if (dma_addr != dma_start + dma_len) { + dma_unmap_page(dev->dev, dma_start, dma_len, dir); + dma_start = dma_addr; + dma_len = 0; + } + prp_len = min(length, NVME_CTRL_PAGE_SIZE); + dma_len += prp_len; + length -= prp_len; + } while (length); +done: + dma_unmap_page(dev->dev, dma_start, dma_len, dir); } -static void nvme_print_sgl(struct scatterlist *sgl, int nents) +static void nvme_free_sgls(struct nvme_dev *dev, struct request *req) { - int i; - struct scatterlist *sg; + struct nvme_iod *iod = blk_mq_rq_to_pdu(req); + dma_addr_t sqe_dma_addr = le64_to_cpu(iod->cmd.common.dptr.sgl.addr); + unsigned int sqe_dma_len = le32_to_cpu(iod->cmd.common.dptr.sgl.length); + struct nvme_sgl_desc *sg_list = iod->descriptors[0]; + enum dma_data_direction dir = rq_dma_dir(req); + + if (iod->nr_descriptors) { + unsigned int nr_entries = sqe_dma_len / sizeof(*sg_list), i; + + for (i = 0; i < nr_entries; i++) + dma_unmap_page(dev->dev, le64_to_cpu(sg_list[i].addr), + le32_to_cpu(sg_list[i].length), dir); + } else { + dma_unmap_page(dev->dev, sqe_dma_addr, sqe_dma_len, dir); + } +} + +static void nvme_unmap_data(struct nvme_dev *dev, struct request *req) +{ + struct nvme_iod *iod = blk_mq_rq_to_pdu(req); - for_each_sg(sgl, sg, nents, i) { - dma_addr_t phys = sg_phys(sg); - pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d " - "dma_address:%pad dma_length:%d\n", - i, &phys, sg->offset, sg->length, &sg_dma_address(sg), - sg_dma_len(sg)); + if (!blk_rq_dma_unmap(req, dev->dev, &iod->dma_state)) { + if (iod->cmd.common.flags & NVME_CMD_SGL_METABUF) + nvme_free_sgls(dev, req); + else + nvme_free_prps(dev, req); } + + if (iod->nr_descriptors) + nvme_free_descriptors(dev, req); } static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, struct request *req, struct nvme_rw_command *cmnd) { struct nvme_iod *iod = blk_mq_rq_to_pdu(req); - int length = blk_rq_payload_bytes(req); - struct scatterlist *sg = iod->sgt.sgl; - int dma_len = sg_dma_len(sg); - u64 dma_addr = sg_dma_address(sg); - int offset = dma_addr & (NVME_CTRL_PAGE_SIZE - 1); + unsigned int length = blk_rq_payload_bytes(req); + struct blk_dma_iter iter; + dma_addr_t prp1_dma, prp2_dma = 0; + unsigned int prp_len, i; __le64 *prp_list; - dma_addr_t prp_dma; - int i; - length -= (NVME_CTRL_PAGE_SIZE - offset); - if (length <= 0) { - iod->first_dma = 0; + if (!blk_rq_dma_map_iter_start(req, dev->dev, &iod->dma_state, &iter)) + return iter.status; + + /* + * PRP1 always points to the start of the DMA transfers. + * + * This is the only PRP (except for the list entries) that could be + * non-aligned. + */ + prp1_dma = iter.addr; + prp_len = min(length, NVME_CTRL_PAGE_SIZE - + (iter.addr & (NVME_CTRL_PAGE_SIZE - 1))); + iod->total_len += prp_len; + iter.addr += prp_len; + iter.len -= prp_len; + length -= prp_len; + if (!length) goto done; - } - dma_len -= (NVME_CTRL_PAGE_SIZE - offset); - if (dma_len) { - dma_addr += (NVME_CTRL_PAGE_SIZE - offset); - } else { - sg = sg_next(sg); - dma_addr = sg_dma_address(sg); - dma_len = sg_dma_len(sg); + if (!iter.len) { + if (!blk_rq_dma_map_iter_next(req, dev->dev, &iod->dma_state, + &iter)) { + if (WARN_ON_ONCE(!iter.status)) + goto bad_sgl; + goto done; + } } + /* + * PRP2 is usually a list, but can point to data if all data to be + * transferred fits into PRP1 + PRP2: + */ if (length <= NVME_CTRL_PAGE_SIZE) { - iod->first_dma = dma_addr; + prp2_dma = iter.addr; + iod->total_len += length; goto done; } @@ -614,58 +693,83 @@ static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, iod->large_descriptors = true; prp_list = dma_pool_alloc(nvme_dma_pool(dev, iod), GFP_ATOMIC, - &prp_dma); - if (!prp_list) - return BLK_STS_RESOURCE; + &prp2_dma); + if (!prp_list) { + iter.status = BLK_STS_RESOURCE; + goto done; + } iod->descriptors[iod->nr_descriptors++] = prp_list; - iod->first_dma = prp_dma; + i = 0; for (;;) { + prp_list[i++] = cpu_to_le64(iter.addr); + prp_len = min(length, NVME_CTRL_PAGE_SIZE); + if (WARN_ON_ONCE(iter.len < prp_len)) + goto bad_sgl; + + iod->total_len += prp_len; + iter.addr += prp_len; + iter.len -= prp_len; + length -= prp_len; + if (!length) + break; + + if (iter.len == 0) { + if (!blk_rq_dma_map_iter_next(req, dev->dev, + &iod->dma_state, &iter)) { + if (WARN_ON_ONCE(!iter.status)) + goto bad_sgl; + goto done; + } + } + + /* + * If we've filled the entire descriptor, allocate a new that is + * pointed to be the last entry in the previous PRP list. To + * accommodate for that move the last actual entry to the new + * descriptor. + */ if (i == NVME_CTRL_PAGE_SIZE >> 3) { __le64 *old_prp_list = prp_list; + dma_addr_t prp_list_dma; prp_list = dma_pool_alloc(dev->prp_page_pool, - GFP_ATOMIC, &prp_dma); - if (!prp_list) - goto free_prps; + GFP_ATOMIC, &prp_list_dma); + if (!prp_list) { + iter.status = BLK_STS_RESOURCE; + goto done; + } iod->descriptors[iod->nr_descriptors++] = prp_list; + prp_list[0] = old_prp_list[i - 1]; - old_prp_list[i - 1] = cpu_to_le64(prp_dma); + old_prp_list[i - 1] = cpu_to_le64(prp_list_dma); i = 1; } - prp_list[i++] = cpu_to_le64(dma_addr); - dma_len -= NVME_CTRL_PAGE_SIZE; - dma_addr += NVME_CTRL_PAGE_SIZE; - length -= NVME_CTRL_PAGE_SIZE; - if (length <= 0) - break; - if (dma_len > 0) - continue; - if (unlikely(dma_len < 0)) - goto bad_sgl; - sg = sg_next(sg); - dma_addr = sg_dma_address(sg); - dma_len = sg_dma_len(sg); } + done: - cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sgt.sgl)); - cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma); - return BLK_STS_OK; -free_prps: - nvme_free_descriptors(dev, req); - return BLK_STS_RESOURCE; + /* + * nvme_unmap_data uses the DPT field in the SQE to tear down the + * mapping, so initialize it even for failures. + */ + cmnd->dptr.prp1 = cpu_to_le64(prp1_dma); + cmnd->dptr.prp2 = cpu_to_le64(prp2_dma); + if (unlikely(iter.status)) + nvme_unmap_data(dev, req); + return iter.status; + bad_sgl: - WARN(DO_ONCE(nvme_print_sgl, iod->sgt.sgl, iod->sgt.nents), - "Invalid SGL for payload:%d nents:%d\n", - blk_rq_payload_bytes(req), iod->sgt.nents); + dev_err_once(dev->dev, + "Incorrectly formed request for payload:%d nents:%d\n", + blk_rq_payload_bytes(req), blk_rq_nr_phys_segments(req)); return BLK_STS_IOERR; } static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge, - struct scatterlist *sg) + struct blk_dma_iter *iter) { - sge->addr = cpu_to_le64(sg_dma_address(sg)); - sge->length = cpu_to_le32(sg_dma_len(sg)); + sge->addr = cpu_to_le64(iter->addr); + sge->length = cpu_to_le32(iter->len); sge->type = NVME_SGL_FMT_DATA_DESC << 4; } @@ -681,17 +785,21 @@ static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev, struct request *req, struct nvme_rw_command *cmd) { struct nvme_iod *iod = blk_mq_rq_to_pdu(req); + unsigned int entries = blk_rq_nr_phys_segments(req); struct nvme_sgl_desc *sg_list; - struct scatterlist *sg = iod->sgt.sgl; - unsigned int entries = iod->sgt.nents; + struct blk_dma_iter iter; dma_addr_t sgl_dma; - int i = 0; + unsigned int mapped = 0; /* setting the transfer type as SGL */ cmd->flags = NVME_CMD_SGL_METABUF; - if (entries == 1) { - nvme_pci_sgl_set_data(&cmd->dptr.sgl, sg); + if (!blk_rq_dma_map_iter_start(req, dev->dev, &iod->dma_state, &iter)) + return iter.status; + + if (entries == 1 || blk_rq_dma_map_coalesce(&iod->dma_state)) { + nvme_pci_sgl_set_data(&cmd->dptr.sgl, &iter); + iod->total_len += iter.len; return BLK_STS_OK; } @@ -702,110 +810,30 @@ static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev, if (!sg_list) return BLK_STS_RESOURCE; iod->descriptors[iod->nr_descriptors++] = sg_list; - iod->first_dma = sgl_dma; - nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries); do { - nvme_pci_sgl_set_data(&sg_list[i++], sg); - sg = sg_next(sg); - } while (--entries > 0); - - return BLK_STS_OK; -} - -static blk_status_t nvme_setup_prp_simple(struct nvme_dev *dev, - struct request *req, struct nvme_rw_command *cmnd, - struct bio_vec *bv) -{ - struct nvme_iod *iod = blk_mq_rq_to_pdu(req); - unsigned int offset = bv->bv_offset & (NVME_CTRL_PAGE_SIZE - 1); - unsigned int first_prp_len = NVME_CTRL_PAGE_SIZE - offset; - - iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0); - if (dma_mapping_error(dev->dev, iod->first_dma)) - return BLK_STS_RESOURCE; - iod->dma_len = bv->bv_len; - - cmnd->dptr.prp1 = cpu_to_le64(iod->first_dma); - if (bv->bv_len > first_prp_len) - cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma + first_prp_len); - else - cmnd->dptr.prp2 = 0; - return BLK_STS_OK; -} - -static blk_status_t nvme_setup_sgl_simple(struct nvme_dev *dev, - struct request *req, struct nvme_rw_command *cmnd, - struct bio_vec *bv) -{ - struct nvme_iod *iod = blk_mq_rq_to_pdu(req); + if (WARN_ON_ONCE(mapped == entries)) { + iter.status = BLK_STS_IOERR; + break; + } + nvme_pci_sgl_set_data(&sg_list[mapped++], &iter); + iod->total_len += iter.len; + } while (blk_rq_dma_map_iter_next(req, dev->dev, &iod->dma_state, + &iter)); - iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0); - if (dma_mapping_error(dev->dev, iod->first_dma)) - return BLK_STS_RESOURCE; - iod->dma_len = bv->bv_len; + nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, mapped); - cmnd->flags = NVME_CMD_SGL_METABUF; - cmnd->dptr.sgl.addr = cpu_to_le64(iod->first_dma); - cmnd->dptr.sgl.length = cpu_to_le32(iod->dma_len); - cmnd->dptr.sgl.type = NVME_SGL_FMT_DATA_DESC << 4; - return BLK_STS_OK; + if (unlikely(iter.status)) + nvme_free_sgls(dev, req); + return iter.status; } static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req, struct nvme_command *cmnd) { - struct nvme_iod *iod = blk_mq_rq_to_pdu(req); - blk_status_t ret = BLK_STS_RESOURCE; - int rc; - - if (blk_rq_nr_phys_segments(req) == 1) { - struct nvme_queue *nvmeq = req->mq_hctx->driver_data; - struct bio_vec bv = req_bvec(req); - - if (!is_pci_p2pdma_page(bv.bv_page)) { - if ((bv.bv_offset & (NVME_CTRL_PAGE_SIZE - 1)) + - bv.bv_len <= NVME_CTRL_PAGE_SIZE * 2) - return nvme_setup_prp_simple(dev, req, - &cmnd->rw, &bv); - - if (nvmeq->qid && sgl_threshold && - nvme_ctrl_sgl_supported(&dev->ctrl)) - return nvme_setup_sgl_simple(dev, req, - &cmnd->rw, &bv); - } - } - - iod->dma_len = 0; - iod->sgt.sgl = mempool_alloc(dev->iod_mempool, GFP_ATOMIC); - if (!iod->sgt.sgl) - return BLK_STS_RESOURCE; - sg_init_table(iod->sgt.sgl, blk_rq_nr_phys_segments(req)); - iod->sgt.orig_nents = blk_rq_map_sg(req->q, req, iod->sgt.sgl); - if (!iod->sgt.orig_nents) - goto out_free_sg; - - rc = dma_map_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req), - DMA_ATTR_NO_WARN); - if (rc) { - if (rc == -EREMOTEIO) - ret = BLK_STS_TARGET; - goto out_free_sg; - } - - if (nvme_pci_use_sgls(dev, req, iod->sgt.nents)) - ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw); - else - ret = nvme_pci_setup_prps(dev, req, &cmnd->rw); - if (ret != BLK_STS_OK) - goto out_unmap_sg; - return BLK_STS_OK; - -out_unmap_sg: - dma_unmap_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req), 0); -out_free_sg: - mempool_free(iod->sgt.sgl, dev->iod_mempool); - return ret; + if (nvme_pci_use_sgls(dev, req, blk_rq_nr_phys_segments(req))) + return nvme_pci_setup_sgls(dev, req, &cmnd->rw); + return nvme_pci_setup_prps(dev, req, &cmnd->rw); } static blk_status_t nvme_map_metadata(struct nvme_dev *dev, struct request *req, @@ -829,7 +857,7 @@ static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req) iod->aborted = false; iod->nr_descriptors = 0; iod->large_descriptors = false; - iod->sgt.nents = 0; + iod->total_len = 0; ret = nvme_setup_cmd(req->q->queuedata, req); if (ret)