From: Chaitanya Kulkarni <kch@nvidia.com>
To: <linux-block@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Cc: <axboe@kernel.dk>, <kch@nvidia.com>,
<damien.lemoal@opensource.wdc.com>, <johannes.thumshirn@wdc.com>,
<bvanassche@acm.org>, <ming.lei@redhat.com>,
<shinichiro.kawasaki@wdc.com>, <vincent.fu@samsung.com>,
<yukuai3@huawei.com>
Subject: [PATCH V2 2/7] null_blk: code cleaup
Date: Wed, 5 Oct 2022 20:18:24 -0700 [thread overview]
Message-ID: <20221006031829.37741-3-kch@nvidia.com> (raw)
In-Reply-To: <20221006031829.37741-1-kch@nvidia.com>
Introduce and use two new macros for calculating the page index from
given sector and index (offset) of the sector in the page.
The newly added macros makes code easy to read with meaningful name and
explanation comments attached to it.
While at it adjust the code in the null_free_sector() to return early
to get rid of the extra identation.
Signed-off-by: Chaitanya Kulkarni <kch@nvidia.com>
---
drivers/block/null_blk/main.c | 37 ++++++++++++++++++++---------------
1 file changed, 21 insertions(+), 16 deletions(-)
diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c
index 2d592b4eb815..fa47fab279c3 100644
--- a/drivers/block/null_blk/main.c
+++ b/drivers/block/null_blk/main.c
@@ -14,6 +14,11 @@
#undef pr_fmt
#define pr_fmt(fmt) "null_blk: " fmt
+/* Gives page index for which this sector belongs to. */
+#define PAGE_IDX_FROM_SECT(sect) (sect >> PAGE_SECTORS_SHIFT)
+/* Gives index (offset) of the sector within page. */
+#define SECT_OFFSET_IN_PAGE(sect) ((sect & SECTOR_MASK) << SECTOR_SHIFT)
+
#define FREE_BATCH 16
#define TICKS_PER_SEC 50ULL
@@ -860,20 +865,20 @@ static void null_free_sector(struct nullb *nullb, sector_t sector,
struct radix_tree_root *root;
root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
- idx = sector >> PAGE_SECTORS_SHIFT;
+ idx = PAGE_IDX_FROM_SECT(sector);
sector_bit = (sector & SECTOR_MASK);
t_page = radix_tree_lookup(root, idx);
- if (t_page) {
- __clear_bit(sector_bit, t_page->bitmap);
-
- if (null_page_empty(t_page)) {
- ret = radix_tree_delete_item(root, idx, t_page);
- WARN_ON(ret != t_page);
- null_free_page(ret);
- if (is_cache)
- nullb->dev->curr_cache -= PAGE_SIZE;
- }
+ if (!t_page)
+ return;
+ __clear_bit(sector_bit, t_page->bitmap);
+
+ if (null_page_empty(t_page)) {
+ ret = radix_tree_delete_item(root, idx, t_page);
+ WARN_ON(ret != t_page);
+ null_free_page(ret);
+ if (is_cache)
+ nullb->dev->curr_cache -= PAGE_SIZE;
}
}
@@ -885,11 +890,11 @@ static void null_zero_sector(struct nullb_device *d, sector_t sect,
unsigned int offset;
void *dest;
- t_page = radix_tree_lookup(root, sect >> PAGE_SECTORS_SHIFT);
+ t_page = radix_tree_lookup(root, PAGE_IDX_FROM_SECT(sect));
if (!t_page)
return;
- offset = (sect & SECTOR_MASK) << SECTOR_SHIFT;
+ offset = SECT_OFFSET_IN_PAGE(sect);
dest = kmap_atomic(t_page->page);
memset(dest + offset, 0, SECTOR_SIZE * nr_sects);
kunmap_atomic(dest);
@@ -949,7 +954,7 @@ static struct nullb_page *__null_lookup_page(struct nullb *nullb,
struct nullb_page *t_page;
struct radix_tree_root *root;
- idx = sector >> PAGE_SECTORS_SHIFT;
+ idx = PAGE_IDX_FROM_SECT(sector);
sector_bit = (sector & SECTOR_MASK);
root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
@@ -1125,7 +1130,7 @@ static int copy_to_nullb(struct nullb *nullb, struct page *source,
if (null_cache_active(nullb) && !is_fua)
null_make_cache_space(nullb, PAGE_SIZE);
- offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
+ offset = SECT_OFFSET_IN_PAGE(sector);
t_page = null_insert_page(nullb, sector,
!null_cache_active(nullb) || is_fua);
if (!t_page)
@@ -1159,7 +1164,7 @@ static int copy_from_nullb(struct nullb *nullb, struct page *dest,
while (count < n) {
temp = min_t(size_t, nullb->dev->blocksize, n - count);
- offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
+ offset = SECT_OFFSET_IN_PAGE(sector);
t_page = null_lookup_page(nullb, sector, false,
!null_cache_active(nullb));
--
2.29.0
next prev parent reply other threads:[~2022-10-06 3:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-06 3:18 [PATCH V2 0/7] null_blk: allow REQ_OP_WRITE_ZEROES and cleanup Chaitanya Kulkarni
2022-10-06 3:18 ` [PATCH V2 1/7] null_blk: allow REQ_OP_WRITE_ZEROES Chaitanya Kulkarni
2022-10-06 3:18 ` Chaitanya Kulkarni [this message]
2022-10-06 3:18 ` [PATCH V2 3/7] null_blk: initialize cmd->bio in __alloc_cmd() Chaitanya Kulkarni
2022-11-15 0:39 ` Bart Van Assche
2022-10-06 3:18 ` [PATCH V2 4/7] null_blk: don't use magic numbers in the code Chaitanya Kulkarni
2022-11-15 0:40 ` Bart Van Assche
2022-11-15 3:49 ` Chaitanya Kulkarni
2022-10-06 3:18 ` [PATCH V2 5/7] null_blk: remove extra space in switch condition Chaitanya Kulkarni
2022-10-06 3:18 ` [PATCH V2 6/7] null_blk: add param to set max disacrd sectors Chaitanya Kulkarni
2022-10-06 3:18 ` [PATCH V2 7/7] null_blk: add param to set max write-zeroes sects Chaitanya Kulkarni
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221006031829.37741-3-kch@nvidia.com \
--to=kch@nvidia.com \
--cc=axboe@kernel.dk \
--cc=bvanassche@acm.org \
--cc=damien.lemoal@opensource.wdc.com \
--cc=johannes.thumshirn@wdc.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ming.lei@redhat.com \
--cc=shinichiro.kawasaki@wdc.com \
--cc=vincent.fu@samsung.com \
--cc=yukuai3@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox