brd discard patches
Subsystems (3)
Mikulas Patocka
maintainerv3 cover brd discard patches
Hi
Here I'm submitting the ramdisk discard patches for the next merge window. If you want to make some more changes, please let me now.
Changes since version 2:
There are no functional changes, I only moved the switch statement conversion to a separate patch, so that it's easier to review.
Patch 1: introduce a switch statement in brd_submit_bio Patch 2: extend the rcu regions to cover read and write Patch 3: enable discard
Patch 4: implement write zeroes
Mikulas
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Mikulas Patocka
maintainerPATCH v3 1/4 brd: use a switch statement in brd_submit_bio
Use a switch statement in brd_submit_bio, so that the code will look better when we add support for more bio operations.
--- drivers/block/brd.c | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-)
drivers/block/brd.c +26 -17
@@ -243,29 +243,38 @@ out:static void brd_submit_bio (struct bio *bio) {struct brd_device *brd = bio->bi_bdev->bd_disk->private_data; -sector_t sector = bio->bi_iter.bi_sector; +sector_t sector;struct bio_vec bvec;struct bvec_iter iter; -bio_for_each_segment (bvec, bio, iter) { -unsigned int len = bvec.bv_len; -int err; +switch (bio_op (bio)) { +case REQ_OP_READ : +case REQ_OP_WRITE : + sector = bio->bi_iter.bi_sector; +bio_for_each_segment (bvec, bio, iter) { +unsigned int len = bvec.bv_len; +int err; -/* Don't support un-aligned buffer */ -WARN_ON_ONCE ((bvec.bv_offset & (SECTOR_SIZE -1 )) || - (len & (SECTOR_SIZE -1 ))); +/* Don't support un-aligned buffer */ +WARN_ON_ONCE ((bvec.bv_offset & (SECTOR_SIZE -1 )) || + (len & (SECTOR_SIZE -1 ))); - err =brd_do_bvec (brd, bvec.bv_page, len, bvec.bv_offset, - bio->bi_opf, sector); -if (err) { -if (err == -ENOMEM && bio->bi_opf &REQ_NOWAIT ) { -bio_wouldblock_error (bio); -return ; + err =brd_do_bvec (brd, bvec.bv_page, len, bvec.bv_offset, + bio->bi_opf, sector); +if (err) { +if (err == -ENOMEM && bio->bi_opf &REQ_NOWAIT ) { +bio_wouldblock_error (bio); +return ; + } +bio_io_error (bio); +return ; + } + sector += len >>SECTOR_SHIFT ; } -bio_io_error (bio); -return ; - } - sector += len >>SECTOR_SHIFT ; +break ; +default : + bio->bi_status =BLK_STS_NOTSUPP ; +break ; }bio_endio (bio);
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Mikulas Patocka
maintainerPATCH v3 2/4 brd: extend the rcu regions to cover read and write
In reply to Mikulas PatockaThis patch extends the rcu regions, so that lookup followed by a read or write of a page is done inside rcu read lock. This is needed for the following patch that enables discard.
--- drivers/block/brd.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/block/brd.c +8 -0
@@ -150,23 +150,27 @@ static void copy_to_brd(struct brd_devicsize_t copy; copy =min_t (size_t, n,PAGE_SIZE - offset); +rcu_read_lock (); page =brd_lookup_page (brd, sector);BUG_ON (!page); dst =kmap_atomic (page);memcpy (dst + offset, src, copy);kunmap_atomic (dst); +rcu_read_unlock ();if (copy < n) { src += copy; sector += copy >>SECTOR_SHIFT ; copy = n - copy; +rcu_read_lock (); page =brd_lookup_page (brd, sector);BUG_ON (!page); dst =kmap_atomic (page);memcpy (dst, src, copy);kunmap_atomic (dst); +rcu_read_unlock (); } }
@@ -182,6 +186,7 @@ static void copy_from_brd(void *dst, strsize_t copy; copy =min_t (size_t, n,PAGE_SIZE - offset); +rcu_read_lock (); page =brd_lookup_page (brd, sector);if (page) { src =kmap_atomic (page);
@@ -189,11 +194,13 @@ static void copy_from_brd(void *dst, strkunmap_atomic (src); }else memset (dst,0 , copy); +rcu_read_unlock ();if (copy < n) { dst += copy; sector += copy >>SECTOR_SHIFT ; copy = n - copy; +rcu_read_lock (); page =brd_lookup_page (brd, sector);if (page) { src =kmap_atomic (page);
@@ -201,6 +208,7 @@ static void copy_from_brd(void *dst, strkunmap_atomic (src); }else memset (dst,0 , copy); +rcu_read_unlock (); } }
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Mikulas Patocka
maintainerPATCH v3 3/4 brd: enable discard
In reply to Mikulas PatockaThis patch implements discard in the brd driver. We use RCU to free the page, so that if there are any concurrent readers or writes, they won't touch the page after it is freed.
Calling "call_rcu" for each page is inefficient, so we attempt to batch multiple pages to a single "call_rcu" call.
Note that we replace "BUG_ON(!page);" with "if (page) ..." in copy_to_brd
- the page can't be NULL under normal circumstances, it can only be NULL if REQ_OP_WRITE races with REQ_OP_DISCARD. If these two bios race with each other on the same page, the result is undefined, so we can handle this race condition just by skipping the copying.
--- drivers/block/brd.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 131 insertions(+), 13 deletions(-)
drivers/block/brd.c +131 -13
@@ -46,6 +46,8 @@ struct brd_device {u64 brd_nr_pages; }; +static bool discard; +/* * Look up and return a brd's page for a given sector. */
@@ -100,6 +102,54 @@ static int brd_insert_page(struct brd_dereturn ret; } +struct free_page_batch { +struct rcu_head rcu; +struct list_head list; +}; + +static void brd_free_page_rcu (struct rcu_head *head) +{ +__free_page (container_of(head ,struct page ,rcu_head )); +} + +static void brd_free_pages_rcu (struct rcu_head *head) +{ + struct free_page_batch *batch = container_of(head, struct free_page_batch, rcu); + +while (!list_empty (&batch->list)) { + struct page *page = list_entry(batch->list.prev, struct page, lru); + +list_del (&page->lru); + +__free_page (page); + } + +kfree (batch); +} + +static void brd_free_page (struct brd_device *brd,sector_t sector, +struct free_page_batch **batch) +{ +struct page *page; +pgoff_t idx; + + idx = sector >>PAGE_SECTORS_SHIFT ; + page =xa_erase (&brd->brd_pages, idx); + +if (page) { +BUG_ON (page->index != idx); +if (!*batch) { + *batch =kmalloc (sizeof (struct free_page_batch ),GFP_NOIO ); +if (unlikely (!*batch)) { +call_rcu (&page->rcu_head, brd_free_page_rcu); +return ; + } +INIT_LIST_HEAD (&(*batch)->list); + } +list_add (&page->lru, &(*batch)->list); + } +} +/* * Free all backing store pages and xarray. This must only be called when * there are no other users of the device.
@@ -152,11 +202,11 @@ static void copy_to_brd(struct brd_devic copy =min_t (size_t, n,PAGE_SIZE - offset);rcu_read_lock (); page =brd_lookup_page (brd, sector); -BUG_ON (!page); - - dst =kmap_atomic (page); -memcpy (dst + offset, src, copy); -kunmap_atomic (dst); +if (page) { + dst =kmap_atomic (page); +memcpy (dst + offset, src, copy); +kunmap_atomic (dst); + }rcu_read_unlock ();if (copy < n) {
@@ -165,11 +215,11 @@ static void copy_to_brd(struct brd_devic copy = n - copy;rcu_read_lock (); page =brd_lookup_page (brd, sector); -BUG_ON (!page); - - dst =kmap_atomic (page); -memcpy (dst, src, copy); -kunmap_atomic (dst); +if (page) { + dst =kmap_atomic (page); +memcpy (dst, src, copy); +kunmap_atomic (dst); + }rcu_read_unlock (); } }
@@ -248,6 +298,34 @@ out:return err; } +void brd_do_discard (struct brd_device *brd,struct bio *bio) +{ +struct free_page_batch *batch =NULL ; +sector_t sector, len, front_pad; + +if (unlikely (!discard)) { + bio->bi_status =BLK_STS_NOTSUPP ; +return ; + } + + sector = bio->bi_iter.bi_sector; + len =bio_sectors (bio); + front_pad = -sector & (PAGE_SECTORS -1 ); + sector += front_pad; +if (unlikely (len <= front_pad)) +return ; + len -= front_pad; + len =round_down (len,PAGE_SECTORS ); +while (len) { +brd_free_page (brd, sector, &batch); + sector +=PAGE_SECTORS ; + len -=PAGE_SECTORS ; +cond_resched (); + } +if (batch) +call_rcu (&batch->rcu, brd_free_pages_rcu); +} +static void brd_submit_bio (struct bio *bio) {struct brd_device *brd = bio->bi_bdev->bd_disk->private_data;
@@ -280,6 +358,9 @@ static void brd_submit_bio(struct bio *b sector += len >>SECTOR_SHIFT ; }break ; +case REQ_OP_DISCARD : +brd_do_discard (brd, bio); +break ;default : bio->bi_status =BLK_STS_NOTSUPP ;break ;
@@ -293,6 +374,40 @@ static const struct block_device_operati .submit_bio = brd_submit_bio, }; +static LIST_HEAD (brd_devices); +static struct dentry *brd_debugfs_dir; + +static void brd_set_discard_limits (struct brd_device *brd) +{ +struct request_queue *queue = brd->brd_disk->queue; +if (discard) { + queue->limits.discard_granularity =PAGE_SIZE ; +blk_queue_max_discard_sectors (queue,round_down (UINT_MAX ,PAGE_SECTORS )); + }else { + queue->limits.discard_granularity =0 ; +blk_queue_max_discard_sectors (queue,0 ); + } +} + +static int discard_set_bool (const char *val,const struct kernel_param *kp) +{ +struct brd_device *brd; + +int r =param_set_bool (val, kp); +if (r) +return r; + +list_for_each_entry (brd, &brd_devices, brd_list) +brd_set_discard_limits (brd); + +return 0 ; +} + +static const struct kernel_param_ops discard_ops = { + .set = discard_set_bool, + .get = param_get_bool, +}; +/* * And now the modules code and kernel interface. */
@@ -308,6 +423,10 @@ static int max_part = 1;module_param (max_part, int,0444 );MODULE_PARM_DESC (max_part,"Num Minors to reserve between devices" ); +static bool discard = false; +module_param_cb (discard, &discard_ops, &discard,0644 ); +MODULE_PARM_DESC (discard,"Support discard" ); +MODULE_LICENSE ("GPL" );MODULE_ALIAS_BLOCKDEV_MAJOR (RAMDISK_MAJOR );MODULE_ALIAS ("rd" );
@@ -326,9 +445,6 @@ __setup("ramdisk_size=", ramdisk_size);* The device scheme is derived from loop.c. Keep them in synch where possible * (should share code eventually). */ -static LIST_HEAD (brd_devices); -static struct dentry *brd_debugfs_dir; -static int brd_alloc (int i) {struct brd_device *brd;
@@ -373,6 +489,8 @@ static int brd_alloc(int i)*/ blk_queue_physical_block_size (disk->queue,PAGE_SIZE ); +brd_set_discard_limits (brd); +/* Tell the block layer that this is not a rotational device */ blk_queue_flag_set (QUEUE_FLAG_NONROT , disk->queue);blk_queue_flag_set (QUEUE_FLAG_SYNCHRONOUS , disk->queue);
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Mikulas Patocka
maintainerPATCH v3 4/4 brd: implement write zeroes
In reply to Mikulas PatockaThis patch implements REQ_OP_WRITE_ZEROES on brd. Write zeroes will free the pages just like discard, but the difference is that it writes zeroes to the preceding and following page if the range is not aligned on page boundary.
--- drivers/block/brd.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/block/brd.c +18 -3
@@ -301,7 +301,8 @@ out:void brd_do_discard (struct brd_device *brd,struct bio *bio) {struct free_page_batch *batch =NULL ; -sector_t sector, len, front_pad; +bool zero_padding =bio_op (bio) ==REQ_OP_WRITE_ZEROES ; +sector_t sector, len, front_pad, end_pad;if (unlikely (!discard)) { bio->bi_status =BLK_STS_NOTSUPP ;
@@ -311,11 +312,22 @@ void brd_do_discard(struct brd_device *b sector = bio->bi_iter.bi_sector; len =bio_sectors (bio); front_pad = -sector & (PAGE_SECTORS -1 ); + +if (zero_padding &&unlikely (front_pad !=0 )) +copy_to_brd (brd,page_address (ZERO_PAGE (0 )), + sector,min (len, front_pad) <<SECTOR_SHIFT ); + sector += front_pad;if (unlikely (len <= front_pad))return ; len -= front_pad; - len =round_down (len,PAGE_SECTORS ); + + end_pad = len & (PAGE_SECTORS -1 ); +if (zero_padding &&unlikely (end_pad !=0 )) +copy_to_brd (brd,page_address (ZERO_PAGE (0 )), + sector + len - end_pad, end_pad <<SECTOR_SHIFT ); + len -= end_pad; +while (len) {brd_free_page (brd, sector, &batch); sector +=PAGE_SECTORS ;
@@ -359,6 +371,7 @@ static void brd_submit_bio(struct bio *b }break ;case REQ_OP_DISCARD : +case REQ_OP_WRITE_ZEROES :brd_do_discard (brd, bio);break ;default :
@@ -383,9 +396,11 @@ static void brd_set_discard_limits(strucif (discard) { queue->limits.discard_granularity =PAGE_SIZE ;blk_queue_max_discard_sectors (queue,round_down (UINT_MAX ,PAGE_SECTORS )); +blk_queue_max_write_zeroes_sectors (queue,round_down (UINT_MAX ,PAGE_SECTORS )); }else { queue->limits.discard_granularity =0 ;blk_queue_max_discard_sectors (queue,0 ); +blk_queue_max_write_zeroes_sectors (queue,0 ); } }
@@ -425,7 +440,7 @@ MODULE_PARM_DESC(max_part, "Num Minors tstatic bool discard = false;module_param_cb (discard, &discard_ops, &discard,0644 ); -MODULE_PARM_DESC (discard,"Support discard" ); +MODULE_PARM_DESC (discard,"Support discard and write zeroes" );MODULE_LICENSE ("GPL" );MODULE_ALIAS_BLOCKDEV_MAJOR (RAMDISK_MAJOR );
--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
friendly ping...
Quoted 16 lines · Mikulas Patocka
Hi
Here I'm submitting the ramdisk discard patches for the next merge window. If you want to make some more changes, please let me now.
Changes since version 2:
There are no functional changes, I only moved the switch statement conversion to a separate patch, so that it's easier to review.Patch 1: introduce a switch statement in
brd_submit_bioPatch 2: extend the rcu regions to cover read and write Patch 3: enable discard
Patch 4: implement write zeroesMikulas
Signature · 2 lines
Thanks, Nan
Mikulas Patocka
maintainerQuoted 1 line · Li Nan
friendly ping...
Jens? Do you want this patch series or not?
Mikulas
Quoted 22 lines · Mikulas Patocka
Quoted 16 lines
Hi
Here I'm submitting the ramdisk discard patches for the next merge window. If you want to make some more changes, please let me now.
Changes since version 2:
There are no functional changes, I only moved the switch statement conversion to a separate patch, so that it's easier to review.Patch 1: introduce a switch statement in
brd_submit_bioPatch 2: extend the rcu regions to cover read and write Patch 3: enable discard
Patch 4: implement write zeroesMikulas
Signature · 2 lines
Thanks, Nan
Hi Mikulas,
Quoted 4 lines · Mikulas Patocka
Hi
Here I'm submitting the ramdisk discard patches for the next merge window. If you want to make some more changes, please let me now.
brd discard is removed in f09a06a193d9 ("brd: remove discard support") in 2017 because it is just driver private write_zero, and user can get same result with fallocate(FALLOC_FL_ZERO_RANGE).
Also you only mentioned the motivation in V1 cover-letter:
``` Zdenek asked me to write it, because we use brd in the lvm2 testsuite and it would be benefical to run the testsuite with discard enabled in order to test discard handling. ```
But we have lots of test disks with discard support: loop, scsi_debug, null_blk, ublk, ..., so one requestion is that why brd discard is a must for lvm2 testsuite to cover (lvm)discard handling?
The reason why brd didn't support discard by freeing pages is writeback deadlock risk, see:
commit f09a06a193d9 ("brd: remove discard support")
-static void discard_from_brd(struct brd_device *brd,
- sector_t sector, size_t n)
-{
- while (n >= PAGE_SIZE) {
- /*
- * Don't want to actually discard pages here because - * re-allocating the pages can result in writeback - * deadlocks under heavy load. - */
- if (0)
- brd_free_page(brd, sector);
- else
- brd_zero_page(brd, sector);
- sector += PAGE_SIZE >> SECTOR_SHIFT;
- n -= PAGE_SIZE;
- }
-}
However, you didn't mention how your patches address this potential risk, care to document it? I can't find any related words about this problem.
BTW, your patches looks more complicated than the original removed discard implementation. And if the above questions get addressed, I am happy to provide review on the following patches.
Thanks,
Ming
Mikulas Patocka
maintainerHi
Quoted 22 lines · Ming Lei
Hi Mikulas,
Quoted 4 lines · Mikulas Patocka
Hi
Here I'm submitting the ramdisk discard patches for the next merge window. If you want to make some more changes, please let me now.
brd discard is removed in
f09a06a193d9("brd: remove discard support") in 2017 because it is just driver privatewrite_zero, and user can get same result withfallocate(FALLOC_FL_ZERO_RANGE).Also you only mentioned the motivation in V1 cover-letter:
``` Zdenek asked me to write it, because we use brd in the lvm2 testsuite and it would be benefical to run the testsuite with discard enabled in order to test discard handling. ```
Quoted 3 lines
But we have lots of test disks with discard support: loop,
scsi_debug,null_blk, ublk, ..., so one requestion is that why brd discard is a must for lvm2 testsuite to cover (lvm)discard handling?
We should ask Zdeněk Kabeláč about it - he is expert about the lvm2 testsuite.
Quoted 23 lines
The reason why brd didn't support discard by freeing pages is writeback deadlock risk, see:
commit
f09a06a193d9("brd: remove discard support")(quoted diff) +0
-17-static void discard_from_brd(struct brd_device *brd, - sector_t sector, size_t n) -{ - while (n >= PAGE_SIZE) { - /* - * Don't want to actually discard pages here because - * re-allocating the pages can result in writeback - * deadlocks under heavy load. - */ - if (0) - brd_free_page(brd, sector); - else - brd_zero_page(brd, sector); - sector += PAGE_SIZE >> SECTOR_SHIFT; - n -= PAGE_SIZE; - } -}
Quoted 3 lines
However, you didn't mention how your patches address this potential risk, care to document it? I can't find any related words about this problem.
The writeback deadlock can happen even without discard - if the machine runs out of memory while writing data to a ramdisk. But the probability is increased when discard is used, because pages are freed and re-allocated more often.
Generally, the admin should make sure that the machine has enough available memory when creating a ramdisk - then, the deadlock can't happen.
Ramdisk has no limit on the number of allocated pages, so when it runs out of memory, the oom killer will try to kill unrelated processes and the machine will hang. If there is risk of overflowing the available memory, the admin should use tmpfs instead of a ramdisk - tmpfs can be configured with a limit and it can also swap out pages.
Quoted 3 lines
BTW, your patches looks more complicated than the original removed discard implementation. And if the above questions get addressed, I am happy to provide review on the following patches.
My patches actually free the discarded pages. The original discard implementation just overwrote the pages with zeroes without freeing them.
Mikulas
Quoted 5 lines
Thanks,
Ming
Quoted 57 lines · Mikulas Patocka
Hi
Quoted 22 lines · Ming Lei
Hi Mikulas,
Quoted 4 lines · Mikulas Patocka
Hi
Here I'm submitting the ramdisk discard patches for the next merge window. If you want to make some more changes, please let me now.
brd discard is removed in
f09a06a193d9("brd: remove discard support") in 2017 because it is just driver privatewrite_zero, and user can get same result withfallocate(FALLOC_FL_ZERO_RANGE).Also you only mentioned the motivation in V1 cover-letter:
``` Zdenek asked me to write it, because we use brd in the lvm2 testsuite and it would be benefical to run the testsuite with discard enabled in order to test discard handling. ```Quoted 3 lines
But we have lots of test disks with discard support: loop,
scsi_debug,null_blk, ublk, ..., so one requestion is that why brd discard is a must for lvm2 testsuite to cover (lvm)discard handling?We should ask Zdeněk Kabeláč about it - he is expert about the lvm2 testsuite.
Quoted 23 lines
The reason why brd didn't support discard by freeing pages is writeback deadlock risk, see:
commit
f09a06a193d9("brd: remove discard support")(quoted diff) +0
-17-static void discard_from_brd(struct brd_device *brd, - sector_t sector, size_t n) -{ - while (n >= PAGE_SIZE) { - /* - * Don't want to actually discard pages here because - * re-allocating the pages can result in writeback - * deadlocks under heavy load. - */ - if (0) - brd_free_page(brd, sector); - else - brd_zero_page(brd, sector); - sector += PAGE_SIZE >> SECTOR_SHIFT; - n -= PAGE_SIZE; - } -}
Quoted 8 lines
Quoted 3 lines
However, you didn't mention how your patches address this potential risk, care to document it? I can't find any related words about this problem.
The writeback deadlock can happen even without discard - if the machine runs out of memory while writing data to a ramdisk. But the probability is increased when discard is used, because pages are freed and re-allocated more often.
Yeah, I agree, what I meant is that this thing needs to be documented, given discard is re-introduced, and the original deadlock comment isn't addressed
Quoted 11 lines
Generally, the admin should make sure that the machine has enough available memory when creating a ramdisk - then, the deadlock can't happen.
Ramdisk has no limit on the number of allocated pages, so when it runs out of memory, the oom killer will try to kill unrelated processes and the machine will hang. If there is risk of overflowing the available memory, the admin should use tmpfs instead of a ramdisk - tmpfs can be configured with a limit and it can also swap out pages.
Quoted 6 lines
Quoted 3 lines
BTW, your patches looks more complicated than the original removed discard implementation. And if the above questions get addressed, I am happy to provide review on the following patches.
My patches actually free the discarded pages. The original discard implementation just overwrote the pages with zeroes without freeing them.
The original implementation supports to discard by freeing pages, and it is just bypassed unconditionally by:
if (0)
brd_free_page(brd, sector);
else
brd_zero_page(brd, sector);However, page could be freed by discard when it is being consumed in brd_do_bvec().
Maybe your patch of "brd: extend the rcu regions to cover read and write" can be simplified a bit, such as:
- grab rcu read lock in brd_do_bvec()
- release the rcu read lock when allocating page via alloc_page() in brd_insert_page()
- change free page by rcu
Or avoid it by holding page reference:
- grabbing page reference in brd_lookup_page() if it is called from copy_to_brd() or copy_from_brd(), and drop it after it is consumed
Thanks,
Ming