Skip to content

Commit 66231ad

Browse files
Ming Leiaxboe
authored andcommitted
block: null_blk: fix 'Invalid parameters' when loading module
On ARM64, the default page size has been 64K on some distributions, and we should allow ARM64 people to play null_blk. This patch fixes the issue by extend page bitmap size for supporting other non-4KB PAGE_SIZE. Cc: Bart Van Assche <[email protected]> Cc: Shaohua Li <[email protected]> Cc: Kyungchan Koh <[email protected]>, Cc: weiping zhang <[email protected]> Cc: Yi Zhang <[email protected]> Reported-by: Yi Zhang <[email protected]> Signed-off-by: Ming Lei <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent b5d013b commit 66231ad

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

drivers/block/null_blk.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ enum nullb_device_flags {
7474
NULLB_DEV_FL_CACHE = 3,
7575
};
7676

77+
#define MAP_SZ ((PAGE_SIZE >> SECTOR_SHIFT) + 2)
7778
/*
7879
* nullb_page is a page in memory for nullb devices.
7980
*
@@ -88,10 +89,10 @@ enum nullb_device_flags {
8889
*/
8990
struct nullb_page {
9091
struct page *page;
91-
unsigned long bitmap;
92+
DECLARE_BITMAP(bitmap, MAP_SZ);
9293
};
93-
#define NULLB_PAGE_LOCK (sizeof(unsigned long) * 8 - 1)
94-
#define NULLB_PAGE_FREE (sizeof(unsigned long) * 8 - 2)
94+
#define NULLB_PAGE_LOCK (MAP_SZ - 1)
95+
#define NULLB_PAGE_FREE (MAP_SZ - 2)
9596

9697
struct nullb_device {
9798
struct nullb *nullb;
@@ -733,7 +734,7 @@ static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
733734
if (!t_page->page)
734735
goto out_freepage;
735736

736-
t_page->bitmap = 0;
737+
memset(t_page->bitmap, 0, sizeof(t_page->bitmap));
737738
return t_page;
738739
out_freepage:
739740
kfree(t_page);
@@ -743,13 +744,20 @@ static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
743744

744745
static void null_free_page(struct nullb_page *t_page)
745746
{
746-
__set_bit(NULLB_PAGE_FREE, &t_page->bitmap);
747-
if (test_bit(NULLB_PAGE_LOCK, &t_page->bitmap))
747+
__set_bit(NULLB_PAGE_FREE, t_page->bitmap);
748+
if (test_bit(NULLB_PAGE_LOCK, t_page->bitmap))
748749
return;
749750
__free_page(t_page->page);
750751
kfree(t_page);
751752
}
752753

754+
static bool null_page_empty(struct nullb_page *page)
755+
{
756+
int size = MAP_SZ - 2;
757+
758+
return find_first_bit(page->bitmap, size) == size;
759+
}
760+
753761
static void null_free_sector(struct nullb *nullb, sector_t sector,
754762
bool is_cache)
755763
{
@@ -764,9 +772,9 @@ static void null_free_sector(struct nullb *nullb, sector_t sector,
764772

765773
t_page = radix_tree_lookup(root, idx);
766774
if (t_page) {
767-
__clear_bit(sector_bit, &t_page->bitmap);
775+
__clear_bit(sector_bit, t_page->bitmap);
768776

769-
if (!t_page->bitmap) {
777+
if (null_page_empty(t_page)) {
770778
ret = radix_tree_delete_item(root, idx, t_page);
771779
WARN_ON(ret != t_page);
772780
null_free_page(ret);
@@ -837,7 +845,7 @@ static struct nullb_page *__null_lookup_page(struct nullb *nullb,
837845
t_page = radix_tree_lookup(root, idx);
838846
WARN_ON(t_page && t_page->page->index != idx);
839847

840-
if (t_page && (for_write || test_bit(sector_bit, &t_page->bitmap)))
848+
if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
841849
return t_page;
842850

843851
return NULL;
@@ -900,10 +908,10 @@ static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
900908

901909
t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);
902910

903-
__clear_bit(NULLB_PAGE_LOCK, &c_page->bitmap);
904-
if (test_bit(NULLB_PAGE_FREE, &c_page->bitmap)) {
911+
__clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
912+
if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
905913
null_free_page(c_page);
906-
if (t_page && t_page->bitmap == 0) {
914+
if (t_page && null_page_empty(t_page)) {
907915
ret = radix_tree_delete_item(&nullb->dev->data,
908916
idx, t_page);
909917
null_free_page(t_page);
@@ -919,11 +927,11 @@ static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
919927

920928
for (i = 0; i < PAGE_SECTORS;
921929
i += (nullb->dev->blocksize >> SECTOR_SHIFT)) {
922-
if (test_bit(i, &c_page->bitmap)) {
930+
if (test_bit(i, c_page->bitmap)) {
923931
offset = (i << SECTOR_SHIFT);
924932
memcpy(dst + offset, src + offset,
925933
nullb->dev->blocksize);
926-
__set_bit(i, &t_page->bitmap);
934+
__set_bit(i, t_page->bitmap);
927935
}
928936
}
929937

@@ -960,10 +968,10 @@ static int null_make_cache_space(struct nullb *nullb, unsigned long n)
960968
* We found the page which is being flushed to disk by other
961969
* threads
962970
*/
963-
if (test_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap))
971+
if (test_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap))
964972
c_pages[i] = NULL;
965973
else
966-
__set_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap);
974+
__set_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap);
967975
}
968976

969977
one_round = 0;
@@ -1016,7 +1024,7 @@ static int copy_to_nullb(struct nullb *nullb, struct page *source,
10161024
kunmap_atomic(dst);
10171025
kunmap_atomic(src);
10181026

1019-
__set_bit(sector & SECTOR_MASK, &t_page->bitmap);
1027+
__set_bit(sector & SECTOR_MASK, t_page->bitmap);
10201028

10211029
if (is_fua)
10221030
null_free_sector(nullb, sector, true);
@@ -1846,10 +1854,6 @@ static int __init null_init(void)
18461854
struct nullb *nullb;
18471855
struct nullb_device *dev;
18481856

1849-
/* check for nullb_page.bitmap */
1850-
if (sizeof(unsigned long) * 8 - 2 < (PAGE_SIZE >> SECTOR_SHIFT))
1851-
return -EINVAL;
1852-
18531857
if (g_bs > PAGE_SIZE) {
18541858
pr_warn("null_blk: invalid block size\n");
18551859
pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);

0 commit comments

Comments
 (0)