btrfs: refactor how we handle btrfs_io_context and slightly reduce memory usage for both btrfs_bio and btrfs_io_context
cover btrfs: refactor how we handle btrfs_io_context and slightly reduce memory usage for both btrfs_bio and btrfs_io_context
Currently btrfs_io_context is utilized as both bio->bi_private for mirrored stripes, and stripes mapping for RAID56.
This makes some members like btrfs_io_context::private to be there.
But in fact, since almost all bios in btrfs have btrfs_bio structure appended, we don't really need to reuse bio::bi_private for mirrored profiles.
So this patchset will:
- Introduce btrfs_bio::bioc member
So that btrfs_io_context don't need to hold @private. This modification is still a net increase for memory usage, just a trade-off between btrfs_io_context and btrfs_bio.
- Replace btrfs_bio::device with btrfs_bio::stripe_num This reclaim the memory usage increase for btrfs_bio.
But unfortunately, due to the short life time of btrfs_io_context, we don't have as good device status accounting as the old code.
Now for csum error, we can no longer distinguish source and target device of dev-replace.
This is the biggest blockage, and that's why it's RFC.
The result of the patchset is:
btrfs_bio: size: 240 -> 240btrfs_io_context: size: 88 -> 80
Although to really reduce btrfs_bio, the main target should be csum_inline.
Qu Wenruo (3):
btrfs: add btrfs_bio::bioc pointer for further modification
btrfs: remove redundant parameters for submit_stripe_bio()
btrfs: replace btrfs_bio::device member with stripe_num
fs/btrfs/compression.c | 6 ++---- fs/btrfs/ctree.h | 2 ++ fs/btrfs/extent_io.c | 2 -- fs/btrfs/inode.c | 30 +++++++++++++++++++++++++++--- fs/btrfs/raid56.c | 1 - fs/btrfs/volumes.c | 23 +++++++++++++---------- fs/btrfs/volumes.h | 11 ++++++++--- 7 files changed, 52 insertions(+), 23 deletions(-)
Signature · 1 line
2.33.0
RFC PATCH 1/3 btrfs: add btrfs_bio::bioc pointer for further modification
Currently we use btrfs_io_context for dual purposes:
- As bio->private for mirror based bio submission
For those profiles, we assign bio->private to btrfs_io_context and save the old private/endio, and utilize bioc::stripes_pending.
- As pure stripe maps for RAID56
For RAID56, btrfs will assemble its own raid_bio for physical submission. In that case, btrfs_io_context only provides stripe mapping, thus no need to utilize things like end_io/private/stripes_pending.
To make future members modifications, here we do a small change, by introducing btrfs_bio::bioc pointer.
This modification will increase memory usage for btrfs_bio by 8 bytes, but reduces btrfs_io_context by 8 bytes.
Overall it's still a net increase as btrfs_bio will be created for each stripe, while btrfs_io_context exists once for all those involved stripes.
This memory usage will be reduced by later commits.
--- fs/btrfs/volumes.c | 7 +++---- fs/btrfs/volumes.h | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-)
fs/btrfs/volumes.c +3 -4
@@ -6557,16 +6557,16 @@ int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,static inline void btrfs_end_bioc (struct btrfs_io_context *bioc,struct bio *bio) { - bio->bi_private = bioc->private; bio->bi_end_io = bioc->end_io;bio_endio (bio); +btrfs_bio (bio)->bioc =NULL ;btrfs_put_bioc (bioc); }static void btrfs_end_bio (struct bio *bio) { -struct btrfs_io_context *bioc = bio->bi_private; +struct btrfs_io_context *bioc =btrfs_bio (bio)->bioc;int is_orig_bio =0 ;if (bio->bi_status) {
@@ -6624,7 +6624,7 @@ static void submit_stripe_bio(struct btrfs_io_context *bioc, struct bio *bio, {struct btrfs_fs_info *fs_info = bioc->fs_info; - bio->bi_private = bioc; +btrfs_bio (bio)->bioc = bioc;btrfs_bio (bio)->device = dev; bio->bi_end_io = btrfs_end_bio; bio->bi_iter.bi_sector = physical >>9 ;
@@ -6697,7 +6697,6 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, total_devs = bioc->num_stripes; bioc->orig_bio = first_bio; - bioc->private = first_bio->bi_private; bioc->end_io = first_bio->bi_end_io; bioc->fs_info = fs_info;atomic_set (&bioc->stripes_pending, bioc->num_stripes);
fs/btrfs/volumes.h +2 -1
@@ -311,6 +311,8 @@ struct btrfs_fs_devices {struct btrfs_bio {unsigned int mirror_num; +struct btrfs_io_context *bioc; +/* @device is for stripe IO submission. */ struct btrfs_device *device;u64 logical;
@@ -367,7 +369,6 @@ struct btrfs_io_context {u64 map_type;/* get from map_lookup->type */ bio_end_io_t *end_io;struct bio *orig_bio; -void *private;atomic_t error;int max_errors;int num_stripes;
Signature · 1 line
2.33.0
RFC PATCH 2/3 btrfs: remove redundant parameters for submit_stripe_bio()
In reply to Qu WenruoFunction submit_stripe_bio() is to submit bio using provided btrfs_io_context, which will map the logical address to physical address, and set the target device.
All the required info is already in bioc, including the device and physical address, we only need to know which stripe we're targeting at.
This patch will replace @physical and @dev parameters with @stripe_nr.
--- fs/btrfs/volumes.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
fs/btrfs/volumes.c +6 -4
@@ -6620,9 +6620,11 @@ static void btrfs_end_bio(struct bio *bio) }static void submit_stripe_bio (struct btrfs_io_context *bioc,struct bio *bio, -u64 physical,struct btrfs_device *dev) +unsigned int stripe_nr) {struct btrfs_fs_info *fs_info = bioc->fs_info; +struct btrfs_device *dev = bioc->stripes[stripe_nr].dev; +const u64 physical = bioc->stripes[stripe_nr].physical;btrfs_bio (bio)->bioc = bioc;btrfs_bio (bio)->device = dev;
@@ -6674,7 +6676,6 @@ static void bioc_error(struct btrfs_io_context *bioc, struct bio *bio, u64 logicblk_status_t btrfs_map_bio (struct btrfs_fs_info *fs_info,struct bio *bio,int mirror_num) { -struct btrfs_device *dev;struct bio *first_bio = bio;u64 logical = bio->bi_iter.bi_sector <<9 ;u64 length =0 ;
@@ -6725,7 +6726,8 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, }for (dev_nr =0 ; dev_nr < total_devs; dev_nr++) { - dev = bioc->stripes[dev_nr].dev; +struct btrfs_device *dev = bioc->stripes[dev_nr].dev; + if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || (btrfs_op(first_bio) == BTRFS_MAP_WRITE &&
@@ -6739,7 +6741,7 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,else bio = first_bio; -submit_stripe_bio (bioc, bio, bioc->stripes[dev_nr].physical, dev); +submit_stripe_bio (bioc, bio, dev_nr); }btrfs_bio_counter_dec (fs_info);return BLK_STS_OK ;
Signature · 1 line
2.33.0
RFC PATCH 3/3 btrfs: replace btrfs_bio::device member with stripe_num
In reply to Qu WenruoStructure btrfs_bio uses @device member to record which the underlying physical device is for each stripe.
However btrfs_bio::device is only utilized by two types of call sites:
- btrfs_end_bio()
- check_data_sum() and check_compressed_csum()
Both types of call sites are just to update device status.
For btrfs_end_bio(), in the context we can easily grab bioc from btrfs_bio, and with the new @stripe_num member, we can easily grab the stripe and its device.
This is as good as the original code.
For check_data_sum() and check_compressed_csum(), we have btrfs_bio::mirror_num, and can afford to do a logical->physical mapping lookup to grab the device.
Unfortunately this path is not as good as the original code, as we can't distinguish target and source device for dev-replace.
But this should save us 8 bytes from btrfs_bio, thus I still think the benefit can cover the shortcoming.
--- fs/btrfs/compression.c | 6 ++---- fs/btrfs/ctree.h | 2 ++ fs/btrfs/extent_io.c | 2 -- fs/btrfs/inode.c | 30 +++++++++++++++++++++++++++--- fs/btrfs/raid56.c | 1 - fs/btrfs/volumes.c | 6 ++++-- fs/btrfs/volumes.h | 10 +++++++--- 7 files changed, 42 insertions(+), 15 deletions(-)
fs/btrfs/compression.c +2 -4
@@ -179,10 +179,8 @@ static int check_compressed_csum(struct btrfs_inode *inode, struct bio *bio,if (memcmp (&csum, cb_sum, csum_size) !=0 ) {btrfs_print_data_csum_error (inode, disk_start, csum, cb_sum, cb->mirror_num); -if (btrfs_bio (bio)->device) -btrfs_dev_stat_inc_and_print ( -btrfs_bio (bio)->device, -BTRFS_DEV_STAT_CORRUPTION_ERRS ); +btrfs_dev_stat_inc_and_print_by_bbio (fs_info, +btrfs_bio (bio));return -EIO ; } cb_sum += csum_size;
fs/btrfs/ctree.h +2 -0
@@ -3141,6 +3141,8 @@ u64 btrfs_file_extent_end(const struct btrfs_path *path);/* inode.c */ blk_status_t btrfs_submit_data_bio (struct inode *inode,struct bio *bio,int mirror_num,unsigned long bio_flags); +void btrfs_dev_stat_inc_and_print_by_bbio (struct btrfs_fs_info *fs_info, +struct btrfs_bio *bbio);unsigned int btrfs_verify_data_csum (struct btrfs_bio *bbio,u32 bio_offset,struct page *page,u64 start,u64 end);
fs/btrfs/extent_io.c +0 -2
@@ -3331,8 +3331,6 @@ static int alloc_new_bio(struct btrfs_inode *inode, ret =PTR_ERR (device); goto error; } - -btrfs_bio (bio)->device = device; }return 0 ; error:
fs/btrfs/inode.c +27 -3
@@ -3206,6 +3206,32 @@ void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode, finish_ordered_fn, uptodate); } +/* +* This is a cold path for csum mismatch case, we can afford to a +* logical->physical address lookup to locate the device using mirror_num. +*/ +void btrfs_dev_stat_inc_and_print_by_bbio (struct btrfs_fs_info *fs_info, +struct btrfs_bio *bbio) +{ +struct btrfs_io_context *bioc; +struct btrfs_device *dev; +u64 logical = bbio->logical; +u64 length = fs_info->sectorsize; +unsigned int mirror_num = bbio->mirror_num; +int ret; + + ret =btrfs_map_block (fs_info,BTRFS_MAP_READ , logical, &length, &bioc, + mirror_num); +/* Failed to do such map, just skip the dev status update */ +if (ret <0 ) +return ; + dev = bioc->stripes[0 ].dev; +btrfs_put_bioc (bioc); +if (dev) +btrfs_dev_stat_inc_and_print (dev, +BTRFS_DEV_STAT_CORRUPTION_ERRS ); +} +/* * check_data_csum - verify checksum of one sector of uncompressed data * @inode: inode
@@ -3248,9 +3274,7 @@ static int check_data_csum(struct inode *inode, struct btrfs_bio *bbio, zeroit:btrfs_print_data_csum_error (BTRFS_I (inode), start, csum, csum_expected, bbio->mirror_num); -if (bbio->device) -btrfs_dev_stat_inc_and_print (bbio->device, -BTRFS_DEV_STAT_CORRUPTION_ERRS ); +btrfs_dev_stat_inc_and_print_by_bbio (fs_info, bbio);memset (kaddr + pgoff,1 , len);flush_dcache_page (page);kunmap_atomic (kaddr);
fs/btrfs/raid56.c +0 -1
@@ -1105,7 +1105,6 @@ static int rbio_add_io_page(struct btrfs_raid_bio *rbio,/* put a new bio on the list */ bio =btrfs_bio_alloc (bio_max_len >>PAGE_SHIFT ?:1 ); -btrfs_bio (bio)->device = stripe->dev; bio->bi_iter.bi_size =0 ;bio_set_dev (bio, stripe->dev->bdev); bio->bi_iter.bi_sector = disk_start >>9 ;
fs/btrfs/volumes.c +4 -2
@@ -6573,8 +6573,10 @@ static void btrfs_end_bio(struct bio *bio)atomic_inc (&bioc->error);if (bio->bi_status ==BLK_STS_IOERR || bio->bi_status ==BLK_STS_TARGET ) { -struct btrfs_device *dev =btrfs_bio (bio)->device; +struct btrfs_bio *bbio =btrfs_bio (bio); +struct btrfs_device *dev; + dev = bioc->stripes[bbio->stripe_num].dev;ASSERT (dev->bdev);if (btrfs_op (bio) ==BTRFS_MAP_WRITE ) btrfs_dev_stat_inc_and_print(dev,
@@ -6627,7 +6629,7 @@ static void submit_stripe_bio(struct btrfs_io_context *bioc, struct bio *bio,const u64 physical = bioc->stripes[stripe_nr].physical;btrfs_bio (bio)->bioc = bioc; -btrfs_bio (bio)->device = dev; +btrfs_bio (bio)->stripe_num = stripe_nr; bio->bi_end_io = btrfs_end_bio; bio->bi_iter.bi_sector = physical >>9 ;/*
fs/btrfs/volumes.h +7 -3
@@ -310,11 +310,15 @@ struct btrfs_fs_devices {*/ struct btrfs_bio {unsigned int mirror_num; +/* +* @stripe_num is for stripe IO submission, under most case it should +* be the same as @mirror_num, but for dev replace case it can be +* different as we need to submit the bio to both source and target +* device. +*/ +unsigned int stripe_num;struct btrfs_io_context *bioc; - -/* @device is for stripe IO submission. */ -struct btrfs_device *device;u64 logical;u8 *csum;u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE ];
Signature · 1 line
2.33.0
Quoted 19 lines · Qu Wenruo
Currently
btrfs_io_contextis utilized as bothbio->bi_privatefor mirrored stripes, and stripes mapping for RAID56.This makes some members like
btrfs_io_context::privateto be there.But in fact, since almost all bios in btrfs have
btrfs_biostructure appended, we don't really need to reusebio::bi_privatefor mirrored profiles.So this patchset will:
- Introduce
btrfs_bio::biocmember
So thatbtrfs_io_contextdon't need to hold @private. This modification is still a net increase for memory usage, just a trade-off betweenbtrfs_io_contextandbtrfs_bio.- Replace
btrfs_bio::devicewithbtrfs_bio::stripe_numThis reclaim the memory usage increase forbtrfs_bio.
Quoted 7 lines
But unfortunately, due to the short life time of
btrfs_io_context, we don't have as good device status accounting as the old code.Now for csum error, we can no longer distinguish source and target device of dev-replace.
This is the biggest blockage, and that's why it's RFC.
OK, here I'm over-thinking.
The truth is, dev-replace only affects two types of operations, WRITE and GET_READ_MIRRORS.
The former case won't need to bother csum mismatch, while the latter case won't result a bio used directly for read.
Thus it brings no behavior change.
Thanks,
Qu
Quoted 23 lines
The result of the patchset is:
btrfs_bio: size: 240 -> 240btrfs_io_context: size: 88 -> 80Although to really reduce
btrfs_bio, the main target should becsum_inline.Qu Wenruo (3):
btrfs: addbtrfs_bio::biocpointer for further modification
btrfs: remove redundant parameters forsubmit_stripe_bio()
btrfs: replacebtrfs_bio::devicemember withstripe_numfs/btrfs/compression.c | 6 ++---- fs/btrfs/ctree.h | 2 ++ fs/btrfs/extent_io.c | 2 -- fs/btrfs/inode.c | 30 +++++++++++++++++++++++++++--- fs/btrfs/raid56.c | 1 - fs/btrfs/volumes.c | 23 +++++++++++++---------- fs/btrfs/volumes.h | 11 ++++++++--- 7 files changed, 52 insertions(+), 23 deletions(-)
Quoted 32 lines · Qu Wenruo
Currently
btrfs_io_contextis utilized as bothbio->bi_privatefor mirrored stripes, and stripes mapping for RAID56.This makes some members like
btrfs_io_context::privateto be there.But in fact, since almost all bios in btrfs have
btrfs_biostructure appended, we don't really need to reusebio::bi_privatefor mirrored profiles.So this patchset will:
- Introduce
btrfs_bio::biocmember
So thatbtrfs_io_contextdon't need to hold @private. This modification is still a net increase for memory usage, just a trade-off betweenbtrfs_io_contextandbtrfs_bio.- Replace
btrfs_bio::devicewithbtrfs_bio::stripe_numThis reclaim the memory usage increase forbtrfs_bio.But unfortunately, due to the short life time of
btrfs_io_context, we don't have as good device status accounting as the old code.Now for csum error, we can no longer distinguish source and target device of dev-replace.
This is the biggest blockage, and that's why it's RFC.
The result of the patchset is:
btrfs_bio: size: 240 -> 240btrfs_io_context: size: 88 -> 80
Quoted 7 lines
Although to really reduce
btrfs_bio, the main target should becsum_inline.Qu Wenruo (3):
btrfs: addbtrfs_bio::biocpointer for further modification
btrfs: remove redundant parameters forsubmit_stripe_bio()
btrfs: replacebtrfs_bio::devicemember withstripe_num
Can you please refresh the patchset on top current misc-next?
Quoted 36 lines · David Sterba
Quoted 35 lines · Qu Wenruo
Currently
btrfs_io_contextis utilized as bothbio->bi_privatefor mirrored stripes, and stripes mapping for RAID56.This makes some members like
btrfs_io_context::privateto be there.But in fact, since almost all bios in btrfs have
btrfs_biostructure appended, we don't really need to reusebio::bi_privatefor mirrored profiles.So this patchset will:
- Introduce
btrfs_bio::biocmember
So thatbtrfs_io_contextdon't need to hold @private. This modification is still a net increase for memory usage, just a trade-off betweenbtrfs_io_contextandbtrfs_bio.- Replace
btrfs_bio::devicewithbtrfs_bio::stripe_numThis reclaim the memory usage increase forbtrfs_bio.But unfortunately, due to the short life time of
btrfs_io_context, we don't have as good device status accounting as the old code.Now for csum error, we can no longer distinguish source and target device of dev-replace.
This is the biggest blockage, and that's why it's RFC.
The result of the patchset is:
btrfs_bio: size: 240 -> 240btrfs_io_context: size: 88 -> 80Although to really reduce
btrfs_bio, the main target should becsum_inline.
Quoted 7 lines
Quoted 4 lines
Qu Wenruo (3):
btrfs: addbtrfs_bio::biocpointer for further modification
btrfs: remove redundant parameters forsubmit_stripe_bio()
btrfs: replacebtrfs_bio::devicemember withstripe_numCan you please refresh the patchset on top current misc-next?
Please discard the patchset for now.
Unfortunately I have found one critical member abuses making such refactor unfeasible (at least for now).
- btrfs_bio::logical abuse
Direct IO uses btrfs_bio::logical as file_offset, while no other call sites really utilize btrfs_bio::logical at all.
This means, we can't simply rely on btrfs_map_bio() to verify if btrfs_bio::logical is correctly initialized.
This is a big alert, if we can't do ASSERT() to verify if one member is properly initialized, then it's just going to happen.
This also means, for direct IO, we can't use @mirror_num with @logical to grab the device with IO failure.
So for now, although it may save 8 bytes, unless we solve the DIO mess, we can't continue.
Thanks,
Qu
Quoted 59 lines · Qu Wenruo
Quoted 36 lines · David Sterba
Quoted 35 lines · Qu Wenruo
Currently
btrfs_io_contextis utilized as bothbio->bi_privatefor mirrored stripes, and stripes mapping for RAID56.This makes some members like
btrfs_io_context::privateto be there.But in fact, since almost all bios in btrfs have
btrfs_biostructure appended, we don't really need to reusebio::bi_privatefor mirrored profiles.So this patchset will:
- Introduce
btrfs_bio::biocmember
So thatbtrfs_io_contextdon't need to hold @private. This modification is still a net increase for memory usage, just a trade-off betweenbtrfs_io_contextandbtrfs_bio.- Replace
btrfs_bio::devicewithbtrfs_bio::stripe_numThis reclaim the memory usage increase forbtrfs_bio.But unfortunately, due to the short life time of
btrfs_io_context, we don't have as good device status accounting as the old code.Now for csum error, we can no longer distinguish source and target device of dev-replace.
This is the biggest blockage, and that's why it's RFC.
The result of the patchset is:
btrfs_bio: size: 240 -> 240btrfs_io_context: size: 88 -> 80Although to really reduce
btrfs_bio, the main target should becsum_inline.Quoted 7 lines
Quoted 4 lines
Qu Wenruo (3):
btrfs: addbtrfs_bio::biocpointer for further modification
btrfs: remove redundant parameters forsubmit_stripe_bio()
btrfs: replacebtrfs_bio::devicemember withstripe_numCan you please refresh the patchset on top current misc-next?
Please discard the patchset for now.
Unfortunately I have found one critical member abuses making such refactor unfeasible (at least for now).
-
btrfs_bio::logicalabuse
Direct IO usesbtrfs_bio::logicalasfile_offset, while no other call sites really utilizebtrfs_bio::logicalat all.This means, we can't simply rely on
btrfs_map_bio()to verify ifbtrfs_bio::logicalis correctly initialized.
Quoted 8 lines
This is a big alert, if we can't do
ASSERT()to verify if one member is properly initialized, then it's just going to happen.This also means, for direct IO, we can't use @mirror_num with @logical to grab the device with IO failure.
So for now, although it may save 8 bytes, unless we solve the DIO mess, we can't continue.
Ok, thanks.