brd: fix discard end sector
brd_do_discard() just aligned start sector to page, this can only work
if the discard size if at least one page. For example:
blkdiscard /dev/ram0 -o 5120 -l 1024
In this case, size = (1024 - (8192 - 5120)), which is a huge value.
Fix the problem by round_down() the end sector.
Fixes: 9ead7efc6f ("brd: implement discard support")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20250506061756.2970934-4-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
parent
d4099f8893
commit
a26a339a65
|
|
@ -167,18 +167,21 @@ static void brd_free_one_page(struct rcu_head *head)
|
|||
static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
|
||||
{
|
||||
sector_t aligned_sector = round_up(sector, PAGE_SECTORS);
|
||||
sector_t aligned_end = round_down(
|
||||
sector + (size >> SECTOR_SHIFT), PAGE_SECTORS);
|
||||
struct page *page;
|
||||
|
||||
size -= (aligned_sector - sector) * SECTOR_SIZE;
|
||||
if (aligned_end <= aligned_sector)
|
||||
return;
|
||||
|
||||
xa_lock(&brd->brd_pages);
|
||||
while (size >= PAGE_SIZE && aligned_sector < rd_size * 2) {
|
||||
while (aligned_sector < aligned_end && aligned_sector < rd_size * 2) {
|
||||
page = __xa_erase(&brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT);
|
||||
if (page) {
|
||||
call_rcu(&page->rcu_head, brd_free_one_page);
|
||||
brd->brd_nr_pages--;
|
||||
}
|
||||
aligned_sector += PAGE_SECTORS;
|
||||
size -= PAGE_SIZE;
|
||||
}
|
||||
xa_unlock(&brd->brd_pages);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue