mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pankaj Raghav <kernel@pankajraghav.com>
To: minchan@kernel.org, senozhatsky@chromium.org
Cc: linux-kernel@vger.kernel.org, axboe@kernel.dk,
	p.raghav@samsung.com, linux-block@vger.kernel.org,
	kernel@pankajraghav.com, gost.dev@samsung.com
Subject: [PATCH 2/5] zram: encapsulate writeback to the backing bdev in a function
Date: Mon, 11 Sep 2023 15:34:27 +0200	[thread overview]
Message-ID: <20230911133430.1824564-3-kernel@pankajraghav.com> (raw)
In-Reply-To: <20230911133430.1824564-1-kernel@pankajraghav.com>

From: Pankaj Raghav <p.raghav@samsung.com>

Encapsulate the flushing data to the backing bdev in writeback in a
separate function writeback_flush_to_bdev(). This is in preparation for
adding batching IO support to writeback_store().

No functional changes.

Signed-off-by: Pankaj Raghav <p.raghav@samsung.com>
---
 drivers/block/zram/zram_drv.c | 125 ++++++++++++++++++----------------
 1 file changed, 68 insertions(+), 57 deletions(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index eaf9e227778e..bd93ed653b99 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -635,14 +635,78 @@ static bool writeback_prep_or_skip_index(struct zram *zram, int mode,
 	return ret;
 }
 
+static int writeback_flush_to_bdev(struct zram *zram, unsigned long index,
+				   struct page *page, unsigned long *blk_idx)
+{
+	struct bio bio;
+	struct bio_vec bio_vec;
+	int ret;
+
+	bio_init(&bio, zram->bdev, &bio_vec, 1, REQ_OP_WRITE | REQ_SYNC);
+	bio.bi_iter.bi_sector = *blk_idx * (PAGE_SIZE >> 9);
+	__bio_add_page(&bio, page, PAGE_SIZE, 0);
+
+	/*
+	 * XXX: A single page IO would be inefficient for write
+	 * but it would be not bad as starter.
+	 */
+	ret = submit_bio_wait(&bio);
+	if (ret) {
+		zram_slot_lock(zram, index);
+		zram_clear_flag(zram, index, ZRAM_UNDER_WB);
+		zram_clear_flag(zram, index, ZRAM_IDLE);
+		zram_slot_unlock(zram, index);
+		/*
+		 * BIO errors are not fatal, we continue and simply
+		 * attempt to writeback the remaining objects (pages).
+		 * At the same time we need to signal user-space that
+		 * some writes (at least one, but also could be all of
+		 * them) were not successful and we do so by returning
+		 * the most recent BIO error.
+		 */
+		return ret;
+	}
+
+	atomic64_inc(&zram->stats.bd_writes);
+	/*
+	 * We released zram_slot_lock so need to check if the slot was
+	 * changed. If there is freeing for the slot, we can catch it
+	 * easily by zram_allocated.
+	 * A subtle case is the slot is freed/reallocated/marked as
+	 * ZRAM_IDLE again. To close the race, idle_store doesn't
+	 * mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB.
+	 * Thus, we could close the race by checking ZRAM_IDLE bit.
+	 */
+	zram_slot_lock(zram, index);
+	if (!zram_allocated(zram, index) ||
+	    !zram_test_flag(zram, index, ZRAM_IDLE)) {
+		zram_clear_flag(zram, index, ZRAM_UNDER_WB);
+		zram_clear_flag(zram, index, ZRAM_IDLE);
+		goto skip;
+	}
+
+	zram_free_page(zram, index);
+	zram_clear_flag(zram, index, ZRAM_UNDER_WB);
+	zram_set_flag(zram, index, ZRAM_WB);
+	zram_set_element(zram, index, *blk_idx);
+	atomic64_inc(&zram->stats.pages_stored);
+	*blk_idx = 0;
+
+	spin_lock(&zram->wb_limit_lock);
+	if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
+		zram->bd_wb_limit -= 1UL << (PAGE_SHIFT - 12);
+	spin_unlock(&zram->wb_limit_lock);
+skip:
+	zram_slot_unlock(zram, index);
+	return 0;
+}
+
 static ssize_t writeback_store(struct device *dev,
 		struct device_attribute *attr, const char *buf, size_t len)
 {
 	struct zram *zram = dev_to_zram(dev);
 	unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
 	unsigned long index = 0;
-	struct bio bio;
-	struct bio_vec bio_vec;
 	struct page *page;
 	ssize_t ret = len;
 	int mode, err;
@@ -713,63 +777,10 @@ static ssize_t writeback_store(struct device *dev,
 			continue;
 		}
 
-		bio_init(&bio, zram->bdev, &bio_vec, 1,
-			 REQ_OP_WRITE | REQ_SYNC);
-		bio.bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9);
-		__bio_add_page(&bio, page, PAGE_SIZE, 0);
+		err = writeback_flush_to_bdev(zram, index, page, &blk_idx);
 
-		/*
-		 * XXX: A single page IO would be inefficient for write
-		 * but it would be not bad as starter.
-		 */
-		err = submit_bio_wait(&bio);
-		if (err) {
-			zram_slot_lock(zram, index);
-			zram_clear_flag(zram, index, ZRAM_UNDER_WB);
-			zram_clear_flag(zram, index, ZRAM_IDLE);
-			zram_slot_unlock(zram, index);
-			/*
-			 * BIO errors are not fatal, we continue and simply
-			 * attempt to writeback the remaining objects (pages).
-			 * At the same time we need to signal user-space that
-			 * some writes (at least one, but also could be all of
-			 * them) were not successful and we do so by returning
-			 * the most recent BIO error.
-			 */
+		if (err)
 			ret = err;
-			continue;
-		}
-
-		atomic64_inc(&zram->stats.bd_writes);
-		/*
-		 * We released zram_slot_lock so need to check if the slot was
-		 * changed. If there is freeing for the slot, we can catch it
-		 * easily by zram_allocated.
-		 * A subtle case is the slot is freed/reallocated/marked as
-		 * ZRAM_IDLE again. To close the race, idle_store doesn't
-		 * mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB.
-		 * Thus, we could close the race by checking ZRAM_IDLE bit.
-		 */
-		zram_slot_lock(zram, index);
-		if (!zram_allocated(zram, index) ||
-			  !zram_test_flag(zram, index, ZRAM_IDLE)) {
-			zram_clear_flag(zram, index, ZRAM_UNDER_WB);
-			zram_clear_flag(zram, index, ZRAM_IDLE);
-			goto next;
-		}
-
-		zram_free_page(zram, index);
-		zram_clear_flag(zram, index, ZRAM_UNDER_WB);
-		zram_set_flag(zram, index, ZRAM_WB);
-		zram_set_element(zram, index, blk_idx);
-		blk_idx = 0;
-		atomic64_inc(&zram->stats.pages_stored);
-		spin_lock(&zram->wb_limit_lock);
-		if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
-			zram->bd_wb_limit -=  1UL << (PAGE_SHIFT - 12);
-		spin_unlock(&zram->wb_limit_lock);
-next:
-		zram_slot_unlock(zram, index);
 	}
 
 	if (blk_idx)
-- 
2.40.1


  parent reply	other threads:[~2023-09-11 21:17 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20230911133442eucas1p2f773a475e0a6dc1a448c63884d58c8d3@eucas1p2.samsung.com>
2023-09-11 13:34 ` [PATCH 0/5] Improve zram writeback performance Pankaj Raghav
2023-09-11 13:34   ` [PATCH 1/5] zram: move index preparation to a separate function in writeback_store Pankaj Raghav
2023-09-11 13:34   ` Pankaj Raghav [this message]
2023-09-11 13:34   ` [PATCH 3/5] zram: add alloc_block_bdev_range() and free_block_bdev_range() Pankaj Raghav
2023-09-11 13:34   ` [PATCH 4/5] zram: batch IOs during writeback to improve performance Pankaj Raghav
2023-09-11 13:34   ` [PATCH 5/5] zram: don't overload blk_idx variable in writeback_store() Pankaj Raghav
2023-09-18 13:53   ` [PATCH 0/5] Improve zram writeback performance Pankaj Raghav
2023-09-19  0:33     ` Sergey Senozhatsky
2023-09-19 14:20       ` Pankaj Raghav
2024-09-25 15:53       ` Jassi Brar
2024-09-26  4:33         ` Sergey Senozhatsky
2024-09-29 22:21           ` Jassi Brar
2024-09-26  4:41   ` Sergey Senozhatsky

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=20230911133430.1824564-3-kernel@pankajraghav.com \
    --to=kernel@pankajraghav.com \
    --cc=axboe@kernel.dk \
    --cc=gost.dev@samsung.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=minchan@kernel.org \
    --cc=p.raghav@samsung.com \
    --cc=senozhatsky@chromium.org \
    /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

all inboxes | Powered by JetHome®