Skip to content

Commit 9ead7ef

Browse files
keithbuschaxboe
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 2526055 commit 9ead7ef

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

drivers/block/brd.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,36 @@ static int brd_do_bvec(struct brd_device *brd, struct page *page,
222222
return err;
223223
}
224224

225+
static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
226+
{
227+
sector_t aligned_sector = (sector + PAGE_SECTORS) & ~PAGE_SECTORS;
228+
struct page *page;
229+
230+
size -= (aligned_sector - sector) * SECTOR_SIZE;
231+
xa_lock(&brd->brd_pages);
232+
while (size >= PAGE_SIZE && aligned_sector < rd_size * 2) {
233+
page = __xa_erase(&brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT);
234+
if (page)
235+
__free_page(page);
236+
aligned_sector += PAGE_SECTORS;
237+
size -= PAGE_SIZE;
238+
}
239+
xa_unlock(&brd->brd_pages);
240+
}
241+
225242
static void brd_submit_bio(struct bio *bio)
226243
{
227244
struct brd_device *brd = bio->bi_bdev->bd_disk->private_data;
228245
sector_t sector = bio->bi_iter.bi_sector;
229246
struct bio_vec bvec;
230247
struct bvec_iter iter;
231248

249+
if (unlikely(op_is_discard(bio->bi_opf))) {
250+
brd_do_discard(brd, sector, bio->bi_iter.bi_size);
251+
bio_endio(bio);
252+
return;
253+
}
254+
232255
bio_for_each_segment(bvec, bio, iter) {
233256
unsigned int len = bvec.bv_len;
234257
int err;
@@ -309,6 +332,9 @@ static int brd_alloc(int i)
309332
* is harmless)
310333
*/
311334
.physical_block_size = PAGE_SIZE,
335+
.max_hw_discard_sectors = UINT_MAX,
336+
.max_discard_segments = 1,
337+
.discard_granularity = PAGE_SIZE,
312338
};
313339

314340
list_for_each_entry(brd, &brd_devices, brd_list)

0 commit comments

Comments
 (0)