summaryrefslogtreecommitdiffstats
path: root/drivers/block/brd.c
diff options
context:
space:
mode:
authorKeith Busch <kbusch@kernel.org>2024-04-29 12:23:08 +0200
committerJens Axboe <axboe@kernel.dk>2024-05-14 05:47:51 +0200
commit9ead7efc6f3f2b46c4ec68209bca4888cfbd4c19 (patch)
tree0d37dea37e26de6b60b6072a928cd5c6c33f8d1b /drivers/block/brd.c
parentnull_blk: Fix two sparse warnings (diff)
downloadlinux-9ead7efc6f3f2b46c4ec68209bca4888cfbd4c19.tar.xz
linux-9ead7efc6f3f2b46c4ec68209bca4888cfbd4c19.zip
brd: implement discard support
The ramdisk memory utilization can only go up when data is written to new pages. Implement discard to provide the possibility to reduce memory usage for pages no longer in use. Aligned discards will free the associated pages, if any, and determinisitically return zeroed data until written again. Signed-off-by: Keith Busch <kbusch@kernel.org> Link: https://lore.kernel.org/r/20240429102308.147627-1-kbusch@meta.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'drivers/block/brd.c')
-rw-r--r--drivers/block/brd.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index b900fe9e0030..558d8e670566 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -222,6 +222,23 @@ out:
return err;
}
+static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
+{
+ sector_t aligned_sector = (sector + PAGE_SECTORS) & ~PAGE_SECTORS;
+ struct page *page;
+
+ size -= (aligned_sector - sector) * SECTOR_SIZE;
+ xa_lock(&brd->brd_pages);
+ while (size >= PAGE_SIZE && aligned_sector < rd_size * 2) {
+ page = __xa_erase(&brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT);
+ if (page)
+ __free_page(page);
+ aligned_sector += PAGE_SECTORS;
+ size -= PAGE_SIZE;
+ }
+ xa_unlock(&brd->brd_pages);
+}
+
static void brd_submit_bio(struct bio *bio)
{
struct brd_device *brd = bio->bi_bdev->bd_disk->private_data;
@@ -229,6 +246,12 @@ static void brd_submit_bio(struct bio *bio)
struct bio_vec bvec;
struct bvec_iter iter;
+ if (unlikely(op_is_discard(bio->bi_opf))) {
+ brd_do_discard(brd, sector, bio->bi_iter.bi_size);
+ bio_endio(bio);
+ return;
+ }
+
bio_for_each_segment(bvec, bio, iter) {
unsigned int len = bvec.bv_len;
int err;
@@ -309,6 +332,9 @@ static int brd_alloc(int i)
* is harmless)
*/
.physical_block_size = PAGE_SIZE,
+ .max_hw_discard_sectors = UINT_MAX,
+ .max_discard_segments = 1,
+ .discard_granularity = PAGE_SIZE,
};
list_for_each_entry(brd, &brd_devices, brd_list)