zonefs: move super block reading from page to folio
Subsystems (2)
Johannes Thumshirn
reviewerPATCH 1/1 zonefs: move super block reading from page to folio
From: Johannes Thumshirn <[email protected]>
Move reading of the on-disk superblock from page to kmalloc()ed memory.
--- fs/zonefs/super.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
fs/zonefs/super.c +15 -15
@@ -1111,28 +1111,28 @@ static int zonefs_read_super(struct super_block *sb)struct zonefs_sb_info *sbi =ZONEFS_SB (sb);struct zonefs_super *super;u32 crc, stored_crc; -struct page *page;struct bio_vec bio_vec;struct bio bio; +struct folio *folio;int ret; - page =alloc_page (GFP_KERNEL ); -if (!page) + super =kzalloc (ZONEFS_SUPER_SIZE ,GFP_KERNEL ); +if (!super)return -ENOMEM ; + folio =virt_to_folio (super);bio_init (&bio, sb->s_bdev, &bio_vec,1 ,REQ_OP_READ ); bio.bi_iter.bi_sector =0 ; -__bio_add_page (&bio, page,PAGE_SIZE ,0 ); +bio_add_folio_nofail (&bio, folio,ZONEFS_SUPER_SIZE , +offset_in_folio (folio, super)); ret =submit_bio_wait (&bio);if (ret) - goto free_page; - - super =page_address (page); + goto free_super; ret = -EINVAL ;if (le32_to_cpu (super->s_magic) !=ZONEFS_MAGIC ) - goto free_page; + goto free_super; stored_crc =le32_to_cpu (super->s_crc); super->s_crc =0 ;
@@ -1140,14 +1140,14 @@ static int zonefs_read_super(struct super_block *sb)if (crc != stored_crc) {zonefs_err (sb,"Invalid checksum (Expected 0x%08x, got 0x%08x)" , crc, stored_crc); - goto free_page; + goto free_super; } sbi->s_features =le64_to_cpu (super->s_features);if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES ) {zonefs_err (sb,"Unknown features set 0x%llx\n" , sbi->s_features); - goto free_page; + goto free_super; }if (sbi->s_features &ZONEFS_F_UID ) {
@@ -1155,7 +1155,7 @@ static int zonefs_read_super(struct super_block *sb) le32_to_cpu(super->s_uid));if (!uid_valid (sbi->s_uid)) {zonefs_err (sb,"Invalid UID feature\n" ); - goto free_page; + goto free_super; } }
@@ -1164,7 +1164,7 @@ static int zonefs_read_super(struct super_block *sb) le32_to_cpu(super->s_gid));if (!gid_valid (sbi->s_gid)) {zonefs_err (sb,"Invalid GID feature\n" ); - goto free_page; + goto free_super; } }
@@ -1173,14 +1173,14 @@ static int zonefs_read_super(struct super_block *sb)if (memchr_inv (super->s_reserved,0 ,sizeof (super->s_reserved))) {zonefs_err (sb,"Reserved area is being used\n" ); - goto free_page; + goto free_super; }import_uuid (&sbi->s_uuid, super->s_uuid); ret =0 ; -free_page: -__free_page (page); +free_super: +kfree (super);return ret; }
Signature · 1 line
2.35.3
Quoted 5 lines · Johannes Thumshirn
From: Johannes Thumshirn <[email protected]>
Move reading of the on-disk superblock from page to kmalloc()ed memory.
Signed-off-by Johannes Thumshirn <[email protected]>
Looks fine.
Quoted 102 lines
--- fs/zonefs/super.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)fs/zonefs/super.c +15
-15@@ -1111,28 +1111,28 @@ static int zonefs_read_super(struct super_block *sb)struct zonefs_sb_info *sbi =ZONEFS_SB (sb);struct zonefs_super *super;u32 crc, stored_crc; -struct page *page;struct bio_vec bio_vec;struct bio bio; +struct folio *folio;int ret; - page =alloc_page (GFP_KERNEL ); -if (!page) + super =kzalloc (ZONEFS_SUPER_SIZE ,GFP_KERNEL ); +if (!super)return -ENOMEM ; + folio =virt_to_folio (super);bio_init (&bio, sb->s_bdev, &bio_vec,1 ,REQ_OP_READ ); bio.bi_iter.bi_sector =0 ; -__bio_add_page (&bio, page,PAGE_SIZE ,0 ); +bio_add_folio_nofail (&bio, folio,ZONEFS_SUPER_SIZE , +offset_in_folio (folio, super)); ret =submit_bio_wait (&bio);if (ret) - goto free_page; - - super =page_address (page); + goto free_super; ret = -EINVAL ;if (le32_to_cpu (super->s_magic) !=ZONEFS_MAGIC ) - goto free_page; + goto free_super; stored_crc =le32_to_cpu (super->s_crc); super->s_crc =0 ;@@ -1140,14 +1140,14 @@ static int zonefs_read_super(struct super_block *sb)if (crc != stored_crc) {zonefs_err (sb,"Invalid checksum (Expected 0x%08x, got 0x%08x)" , crc, stored_crc); - goto free_page; + goto free_super; } sbi->s_features =le64_to_cpu (super->s_features);if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES ) {zonefs_err (sb,"Unknown features set 0x%llx\n" , sbi->s_features); - goto free_page; + goto free_super; }if (sbi->s_features &ZONEFS_F_UID ) {@@ -1155,7 +1155,7 @@ static int zonefs_read_super(struct super_block *sb) le32_to_cpu(super->s_uid));if (!uid_valid (sbi->s_uid)) {zonefs_err (sb,"Invalid UID feature\n" ); - goto free_page; + goto free_super; } }@@ -1164,7 +1164,7 @@ static int zonefs_read_super(struct super_block *sb) le32_to_cpu(super->s_gid));if (!gid_valid (sbi->s_gid)) {zonefs_err (sb,"Invalid GID feature\n" ); - goto free_page; + goto free_super; } }@@ -1173,14 +1173,14 @@ static int zonefs_read_super(struct super_block *sb)if (memchr_inv (super->s_reserved,0 ,sizeof (super->s_reserved))) {zonefs_err (sb,"Reserved area is being used\n" ); - goto free_page; + goto free_super; }import_uuid (&sbi->s_uuid, super->s_uuid); ret =0 ; -free_page: -__free_page (page); +free_super: +kfree (super);return ret; }Signature · 1 line
2.35.3
Quoted 3 lines · Johannes Thumshirn
From: Johannes Thumshirn <[email protected]>
Move reading of the on-disk superblock from page to kmalloc()ed memory.
No, this is wrong.
fs/zonefs/super.c static int zonefs_read_super(struct super_block *sb) · line 1123
Quoted 5 lines
fs/zonefs/super.c +3
-0+ super =kzalloc (ZONEFS_SUPER_SIZE ,GFP_KERNEL ); +if (!super)return -ENOMEM ; + folio =virt_to_folio (super);
This will stop working at some point. It'll return NULL once we get to the memdesc future (because the memdesc will be a slab, not a folio).
fs/zonefs/super.c static int zonefs_read_super(struct super_block *sb) · line 1127
Quoted 5 lines
fs/zonefs/super.c +2
-1bio_init (&bio, sb->s_bdev, &bio_vec,1 ,REQ_OP_READ ); bio.bi_iter.bi_sector =0 ; -__bio_add_page (&bio, page,PAGE_SIZE ,0 ); +bio_add_folio_nofail (&bio, folio,ZONEFS_SUPER_SIZE , +offset_in_folio (folio, super));
It also doesn't solve the problem of trying to read 4KiB from a device with 16KiB sectors. We'll have to fail the bio because there isn't enough memory in the bio to store one block.
I think the right way to handle this is to call read_mapping_folio(). That will allocate a folio in the page cache for you (obeying the minimum folio size). Then you can examine the contents. It should actually remove code from zonefs. Don't forget to call folio_put() when you're done with it (either at unmount or at the end of mount if you copy what you need elsewhere).
Quoted 26 lines · Matthew Wilcox
Quoted 3 lines · Johannes Thumshirn
From: Johannes Thumshirn <[email protected]>
Move reading of the on-disk superblock from page to kmalloc()ed memory.
No, this is wrong.
fs/zonefs/super.c static int zonefs_read_super(struct super_block *sb) · line 1123
Quoted 5 lines
fs/zonefs/super.c +3
-0+ super =kzalloc (ZONEFS_SUPER_SIZE ,GFP_KERNEL ); +if (!super)return -ENOMEM ; + folio =virt_to_folio (super);This will stop working at some point. It'll return NULL once we get to the memdesc future (because the memdesc will be a slab, not a folio).
fs/zonefs/super.c static int zonefs_read_super(struct super_block *sb) · line 1127
Quoted 5 lines
bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ); bio.bi_iter.bi_sector = 0;fs/zonefs/super.c +2
-1-__bio_add_page (&bio, page,PAGE_SIZE ,0 ); +bio_add_folio_nofail (&bio, folio,ZONEFS_SUPER_SIZE , +offset_in_folio (folio, super));It also doesn't solve the problem of trying to read 4KiB from a device with 16KiB sectors. We'll have to fail the bio because there isn't enough memory in the bio to store one block.
Quoted 7 lines
I think the right way to handle this is to call
read_mapping_folio(). That will allocate a folio in the page cache for you (obeying the minimum folio size). Then you can examine the contents. It should actually remove code from zonefs. Don't forget to callfolio_put()when you're done with it (either at unmount or at the end of mount if you copy what you need elsewhere).
Hmm but read mapping folio needs an inode for the address_space. Or does the block device inode work here?
Quoted 10 lines · Johannes Thumshirn
Quoted 6 lines · Matthew Wilcox
I think the right way to handle this is to call
read_mapping_folio(). That will allocate a folio in the page cache for you (obeying the minimum folio size). Then you can examine the contents. It should actually remove code from zonefs. Don't forget to callfolio_put()when you're done with it (either at unmount or at the end of mount if you copy what you need elsewhere).Hmm but read mapping folio needs an inode for the
address_space. Or does the block device inode work here?
Sorry, yes, should have been explicit. Read it using the bdev's address_space.
Quoted 9 lines · Matthew Wilcox
Quoted 8 lines · Johannes Thumshirn
Quoted 6 lines · Matthew Wilcox
I think the right way to handle this is to call
read_mapping_folio(). That will allocate a folio in the page cache for you (obeying the minimum folio size). Then you can examine the contents. It should actually remove code from zonefs. Don't forget to callfolio_put()when you're done with it (either at unmount or at the end of mount if you copy what you need elsewhere).
Quoted 6 lines
Quoted 2 lines
Hmm but read mapping folio needs an inode for the
address_space. Or does the block device inode work here?Sorry, yes, should have been explicit. Read it using the bdev's
address_space.
Ah OK then it's easy.
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Johannes Thumshirn
From: Johannes Thumshirn <[email protected]>
Move reading of the on-disk superblock from page to kmalloc()ed memory.
No, this is wrong.
Quoted 8 lines
fs/zonefs/super.c static int zonefs_read_super(struct super_block *sb) · line 1123
Quoted 5 lines
fs/zonefs/super.c +3
-0+ super =kzalloc (ZONEFS_SUPER_SIZE ,GFP_KERNEL ); +if (!super)return -ENOMEM ; + folio =virt_to_folio (super);This will stop working at some point. It'll return NULL once we get to the memdesc future (because the memdesc will be a slab, not a folio).
Hmmm, xfs_buf.c plays a similar trick here for sub-page buffers. I'm assuming that will get ported to ... whatever the memdesc future holds?
Quoted 10 lines
fs/zonefs/super.c static int zonefs_read_super(struct super_block *sb) · line 1127
Quoted 5 lines
fs/zonefs/super.c +2
-1bio_init (&bio, sb->s_bdev, &bio_vec,1 ,REQ_OP_READ ); bio.bi_iter.bi_sector =0 ; -__bio_add_page (&bio, page,PAGE_SIZE ,0 ); +bio_add_folio_nofail (&bio, folio,ZONEFS_SUPER_SIZE , +offset_in_folio (folio, super));It also doesn't solve the problem of trying to read 4KiB from a device with 16KiB sectors. We'll have to fail the bio because there isn't enough memory in the bio to store one block.
Quoted 6 lines
I think the right way to handle this is to call
read_mapping_folio(). That will allocate a folio in the page cache for you (obeying the minimum folio size). Then you can examine the contents. It should actually remove code from zonefs. Don't forget to callfolio_put()when you're done with it (either at unmount or at the end of mount if you copy what you need elsewhere).
The downside of using bd_mapping is that userspace can scribble all over the folio contents. For zonefs that's less of a big deal because it only reads it once, but for everyone else (e.g. ext4) it's been a huge problem. I guess you could always do max(ZONEFS_SUPER_SIZE, block_size(sb->s_bdev)) if you don't want to use the pagecache.
<more mumbling about buffer caches>
--D
Damien Le Moal
maintainerQuoted 37 lines · Darrick J. Wong
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Johannes Thumshirn
From: Johannes Thumshirn <[email protected]>
Move reading of the on-disk superblock from page to kmalloc()ed memory.
No, this is wrong.
Quoted 8 lines
fs/zonefs/super.c static int zonefs_read_super(struct super_block *sb) · line 1123
Quoted 5 lines
fs/zonefs/super.c +3
-0+ super =kzalloc (ZONEFS_SUPER_SIZE ,GFP_KERNEL ); +if (!super)return -ENOMEM ; + folio =virt_to_folio (super);This will stop working at some point. It'll return NULL once we get to the memdesc future (because the memdesc will be a slab, not a folio).
Hmmm,
xfs_buf.cplays a similar trick here for sub-page buffers. I'm assuming that will get ported to ... whatever the memdesc future holds?Quoted 10 lines
fs/zonefs/super.c static int zonefs_read_super(struct super_block *sb) · line 1127
Quoted 5 lines
fs/zonefs/super.c +2
-1bio_init (&bio, sb->s_bdev, &bio_vec,1 ,REQ_OP_READ ); bio.bi_iter.bi_sector =0 ; -__bio_add_page (&bio, page,PAGE_SIZE ,0 ); +bio_add_folio_nofail (&bio, folio,ZONEFS_SUPER_SIZE , +offset_in_folio (folio, super));It also doesn't solve the problem of trying to read 4KiB from a device with 16KiB sectors. We'll have to fail the bio because there isn't enough memory in the bio to store one block.
Quoted 6 lines
I think the right way to handle this is to call
read_mapping_folio(). That will allocate a folio in the page cache for you (obeying the minimum folio size). Then you can examine the contents. It should actually remove code from zonefs. Don't forget to callfolio_put()when you're done with it (either at unmount or at the end of mount if you copy what you need elsewhere).
Quoted 3 lines
The downside of using
bd_mappingis that userspace can scribble all over the folio contents. For zonefs that's less of a big deal because it only reads it once, but for everyone else (e.g. ext4) it's been a huge
Yes, and zonefs super block is read-only, we never update it after formatting.
Quoted 2 lines
problem. I guess you could always do
max(ZONEFS_SUPER_SIZE, block_size(sb->s_bdev))if you don't want to use the pagecache.
Good point. ZONEFS_SUPER_SIZE is 4K and given that I only know of 512e and 4K zoned block devices, this is not an issue yet. But better safe than sorry, so doing the max() thing you propose is better. Will patch that.
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 5 lines · Damien Le Moal
Quoted 5 lines
Quoted 2 lines
This will stop working at some point. It'll return NULL once we get to the memdesc future (because the memdesc will be a slab, not a folio).
Hmmm,
xfs_buf.cplays a similar trick here for sub-page buffers. I'm assuming that will get ported to ... whatever the memdesc future holds?
I don't think it does, exactly? Are you referring to kmem_to_page()? That will continue to work. You're not trying to get a folio from a slab allocation; that will start to fail.
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
I think the right way to handle this is to call
read_mapping_folio(). That will allocate a folio in the page cache for you (obeying the minimum folio size). Then you can examine the contents. It should actually remove code from zonefs. Don't forget to callfolio_put()when you're done with it (either at unmount or at the end of mount if you copy what you need elsewhere).The downside of using
bd_mappingis that userspace can scribble all over the folio contents. For zonefs that's less of a big deal because it only reads it once, but for everyone else (e.g. ext4) it's been a huge
Quoted 8 lines
Yes, and zonefs super block is read-only, we never update it after formatting.
Quoted 2 lines
problem. I guess you could always do
max(ZONEFS_SUPER_SIZE, block_size(sb->s_bdev))if you don't want to use the pagecache.Good point.
ZONEFS_SUPER_SIZEis 4K and given that I only know of 512e and 4K zoned block devices, this is not an issue yet. But better safe than sorry, so doing themax()thing you propose is better. Will patch that.
I think you should use read_mapping_folio() for now instead of complicating zonefs. Once there's a grand new buffer cache, switch to that, but I don't think you're introducing a significant vulnerability by using the block device's page cache.
Damien Le Moal
maintainerQuoted 27 lines · Matthew Wilcox
Quoted 5 lines · Damien Le Moal
Quoted 5 lines
Quoted 2 lines
This will stop working at some point. It'll return NULL once we get to the memdesc future (because the memdesc will be a slab, not a folio).
Hmmm,
xfs_buf.cplays a similar trick here for sub-page buffers. I'm assuming that will get ported to ... whatever the memdesc future holds?I don't think it does, exactly? Are you referring to
kmem_to_page()? That will continue to work. You're not trying to get a folio from a slab allocation; that will start to fail.Quoted 16 lines
Quoted 10 lines
Quoted 6 lines
I think the right way to handle this is to call
read_mapping_folio(). That will allocate a folio in the page cache for you (obeying the minimum folio size). Then you can examine the contents. It should actually remove code from zonefs. Don't forget to callfolio_put()when you're done with it (either at unmount or at the end of mount if you copy what you need elsewhere).The downside of using
bd_mappingis that userspace can scribble all over the folio contents. For zonefs that's less of a big deal because it only reads it once, but for everyone else (e.g. ext4) it's been a hugeYes, and zonefs super block is read-only, we never update it after formatting.
Quoted 2 lines
problem. I guess you could always do
max(ZONEFS_SUPER_SIZE, block_size(sb->s_bdev))if you don't want to use the pagecache.
Quoted 8 lines
Quoted 3 lines
Good point.
ZONEFS_SUPER_SIZEis 4K and given that I only know of 512e and 4K zoned block devices, this is not an issue yet. But better safe than sorry, so doing themax()thing you propose is better. Will patch that.I think you should use
read_mapping_folio()for now instead of complicating zonefs. Once there's a grand new buffer cache, switch to that, but I don't think you're introducing a significant vulnerability by using the block device's page cache.
I was not really thinking about vulnerability here, but rather compatibility with devices having a block size larger than 4K... But given that these are rare (at best), a fix for a more intelligent ZONEFS_SUPER_SIZE is not urgent, and not hard at all anyway.
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 10 lines · Matthew Wilcox
Quoted 5 lines · Damien Le Moal
Quoted 5 lines
Quoted 2 lines
This will stop working at some point. It'll return NULL once we get to the memdesc future (because the memdesc will be a slab, not a folio).
Hmmm,
xfs_buf.cplays a similar trick here for sub-page buffers. I'm assuming that will get ported to ... whatever the memdesc future holds?I don't think it does, exactly? Are you referring to
kmem_to_page()? That will continue to work. You're not trying to get a folio from a slab allocation; that will start to fail.
The point is that we doing block I/O on a slab allocation is heavily used, and zonefs does this. If you dislike going through the folio we can just keep using pages in zonefs for now.
Eventually I'll get around lifting the logic to greedily build a bio from arbitrary kernel virtual addresses from various places in XFS into common code and we can convert to that.
Quoted 4 lines
I think you should use
read_mapping_folio()for now instead of complicating zonefs. Once there's a grand new buffer cache, switch to that, but I don't think you're introducing a significant vulnerability by using the block device's page cache.
Please don't add pointless uses of the bdev page cache for users that don't need caching. This just creates more headaches later on to untangle it again.
Damien Le Moal
maintainerQuoted 20 lines · Christoph Hellwig
Quoted 10 lines · Matthew Wilcox
Quoted 5 lines · Damien Le Moal
Quoted 5 lines
Quoted 2 lines
This will stop working at some point. It'll return NULL once we get to the memdesc future (because the memdesc will be a slab, not a folio).
Hmmm,
xfs_buf.cplays a similar trick here for sub-page buffers. I'm assuming that will get ported to ... whatever the memdesc future holds?I don't think it does, exactly? Are you referring to
kmem_to_page()? That will continue to work. You're not trying to get a folio from a slab allocation; that will start to fail.The point is that we doing block I/O on a slab allocation is heavily used, and zonefs does this. If you dislike going through the folio we can just keep using pages in zonefs for now.
Eventually I'll get around lifting the logic to greedily build a bio from arbitrary kernel virtual addresses from various places in XFS into common code and we can convert to that.
Quoted 8 lines
Quoted 4 lines
I think you should use
read_mapping_folio()for now instead of complicating zonefs. Once there's a grand new buffer cache, switch to that, but I don't think you're introducing a significant vulnerability by using the block device's page cache.Please don't add pointless uses of the bdev page cache for users that don't need caching. This just creates more headaches later on to untangle it again.
OK. Will drop this patch then.
Signature · 2 lines
Damien Le Moal Western Digital Research