bcache kbuild cleanups
Subsystems (2)
Hi Coly and Liang,
can you review this series to sort out the bcache superblock reading for larger page sizes? I don't have bcache test setup so this is compile tested only.
PATCH 1/7 bcache: cached_dev_free needs to put the sb page
From: Liang Chen <[email protected]>
Same as cache device, the buffer page needs to be put while freeing cached_dev. Otherwise a page would be leaked every time a cached_dev is stopped.
--- drivers/md/bcache/super.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/md/bcache/super.c +3 -0
@@ -1275,6 +1275,9 @@ static void cached_dev_free(struct closure *cl)mutex_unlock (&bch_register_lock); +if (dc->sb_bio.bi_inline_vecs[0 ].bv_page) +put_page (bio_first_page_all (&dc->sb_bio)); +if (!IS_ERR_OR_NULL (dc->bdev))blkdev_put (dc->bdev,FMODE_READ |FMODE_WRITE |FMODE_EXCL );
Signature · 1 line
2.20.1
PATCH 2/7 bcache: use a separate data structure for the on-disk super block
In reply to Christoph HellwigSplit out an on-disk version struct cache_sb with the proper endianess annotations. This fixes a fair chunk of sparse warnings, but there are some left due to the way the checksum is defined.
--- drivers/md/bcache/super.c | 6 ++--- include/uapi/linux/bcache.h | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-)
drivers/md/bcache/super.c +3 -3
@@ -63,14 +63,14 @@ static const char *read_super(struct cache_sb *sb, struct block_device *bdev, struct page **res) {const char *err; -struct cache_sb *s; +struct cache_sb_disk *s;struct buffer_head *bh =__bread (bdev,1 ,SB_SIZE );unsigned int i;if (!bh)return "IO error" ; - s = (struct cache_sb *) bh->b_data; + s = (struct cache_sb_disk *)bh->b_data; sb->offset =le64_to_cpu (s->offset); sb->version =le64_to_cpu (s->version);
@@ -209,7 +209,7 @@ static void write_bdev_super_endio(struct bio *bio)static void __write_super (struct cache_sb *sb,struct bio *bio) { -struct cache_sb *out =page_address (bio_first_page_all (bio)); +struct cache_sb_disk *out =page_address (bio_first_page_all (bio));unsigned int i; bio->bi_iter.bi_sector =SB_SECTOR ;
include/uapi/linux/bcache.h +51 -0
57 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 3/7 bcache: rework error unwinding in register_bcache
In reply to Christoph HellwigSplit the successful and error return path, and use one goto label for each resource to unwind. This also fixes some small errors like leaking the module reference count in the reboot case (which seems entirely harmless) or printing the wrong warning messages for early failures.
--- drivers/md/bcache/super.c | 75 +++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 30 deletions(-)
drivers/md/bcache/super.c +45 -30
@@ -2375,29 +2375,33 @@ static bool bch_is_open(struct block_device *bdev)static ssize_t register_bcache (struct kobject *k,struct kobj_attribute *attr,const char *buffer,size_t size) { -ssize_t ret = -EINVAL ; -const char *err ="cannot allocate memory" ; -char *path =NULL ; -struct cache_sb *sb =NULL ; +const char *err; +char *path; +struct cache_sb *sb;struct block_device *bdev =NULL ; -struct page *sb_page =NULL ; +struct page *sb_page; +ssize_t ret; + ret = -EBUSY ;if (!try_module_get (THIS_MODULE )) -return -EBUSY ; + goto out;/* For latest state of bcache_is_reboot */ smp_mb ();if (bcache_is_reboot) -return -EBUSY ; + goto out_module_put; + ret = -ENOMEM ; + err ="cannot allocate memory" ; path =kstrndup (buffer, size,GFP_KERNEL );if (!path) - goto err; + goto out_module_put; sb =kmalloc (sizeof (struct cache_sb ),GFP_KERNEL );if (!sb) - goto err; + goto out_free_path; + ret = -EINVAL ; err ="failed to open device" ; bdev = blkdev_get_by_path(strim(path), FMODE_READ|FMODE_WRITE|FMODE_EXCL,
@@ -2414,57 +2418,68 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,if (!IS_ERR (bdev))bdput (bdev);if (attr == &ksysfs_register_quiet) - goto quiet_out; + goto done; } - goto err; + goto out_free_sb; } err ="failed to set blocksize" ;if (set_blocksize (bdev,4096 )) - goto err_close; + goto out_blkdev_put; err =read_super (sb, bdev, &sb_page);if (err) - goto err_close; + goto out_blkdev_put; err ="failed to register device" ;if (SB_IS_BDEV (sb)) {struct cached_dev *dc =kzalloc (sizeof (*dc),GFP_KERNEL );if (!dc) - goto err_close; + goto out_put_sb_page;mutex_lock (&bch_register_lock); ret =register_bdev (sb, sb_page, bdev, dc);mutex_unlock (&bch_register_lock);/* blkdev_put() will be called in cached_dev_free() */ -if (ret <0 ) - goto err; +if (ret <0 ) { + bdev =NULL ; + goto out_put_sb_page; + } }else {struct cache *ca =kzalloc (sizeof (*ca),GFP_KERNEL );if (!ca) - goto err_close; + goto out_put_sb_page;/* blkdev_put() will be called in bch_cache_release() */ -if (register_cache (sb, sb_page, bdev, ca) !=0 ) - goto err; +if (register_cache (sb, sb_page, bdev, ca) !=0 ) { + bdev =NULL ; + goto out_put_sb_page; + } } -quiet_out: - ret = size; -out: -if (sb_page) -put_page (sb_page); + +put_page (sb_page); +done:kfree (sb);kfree (path);module_put (THIS_MODULE ); -return ret; - -err_close: -blkdev_put (bdev,FMODE_READ |FMODE_WRITE |FMODE_EXCL ); -err: +return size; + +out_put_sb_page: +put_page (sb_page); +out_blkdev_put: +if (bdev) +blkdev_put (bdev,FMODE_READ |FMODE_WRITE |FMODE_EXCL ); +out_free_sb: +kfree (sb); +out_free_path: +kfree (path); +out_module_put: +module_put (THIS_MODULE ); +out:pr_info ("error %s: %s" , path, err); - goto out; +return ret; }
Signature · 1 line
2.20.1
PATCH 4/7 bcache: transfer the sb_page reference to register_{bdev,cache}
In reply to Christoph HellwigAvoid an extra reference count roundtrip by transferring the sb_page ownership to the lower level register helpers.
--- drivers/md/bcache/super.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-)
drivers/md/bcache/super.c +6 -15
@@ -1368,8 +1368,6 @@ static int register_bdev(struct cache_sb *sb, struct page *sb_page,bio_init (&dc->sb_bio, dc->sb_bio.bi_inline_vecs,1 );bio_first_bvec_all (&dc->sb_bio)->bv_page = sb_page; -get_page (sb_page); -if (cached_dev_init (dc, sb->block_size <<9 )) goto err;
@@ -2275,7 +2273,6 @@ static int register_cache(struct cache_sb *sb, struct page *sb_page,bio_init (&ca->sb_bio, ca->sb_bio.bi_inline_vecs,1 );bio_first_bvec_all (&ca->sb_bio)->bv_page = sb_page; -get_page (sb_page);if (blk_queue_discard (bdev_get_queue (bdev))) ca->discard =CACHE_DISCARD (&ca->sb);
@@ -2378,7 +2375,7 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,const char *err;char *path;struct cache_sb *sb; -struct block_device *bdev =NULL ; +struct block_device *bdev;struct page *sb_page;ssize_t ret;
@@ -2442,10 +2439,8 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, ret =register_bdev (sb, sb_page, bdev, dc);mutex_unlock (&bch_register_lock);/* blkdev_put() will be called in cached_dev_free() */ -if (ret <0 ) { - bdev =NULL ; - goto out_put_sb_page; - } +if (ret <0 ) + goto out_free_sb; }else {struct cache *ca =kzalloc (sizeof (*ca),GFP_KERNEL );
@@ -2453,13 +2448,10 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, goto out_put_sb_page;/* blkdev_put() will be called in bch_cache_release() */ -if (register_cache (sb, sb_page, bdev, ca) !=0 ) { - bdev =NULL ; - goto out_put_sb_page; - } +if (register_cache (sb, sb_page, bdev, ca) !=0 ) + goto out_free_sb; } -put_page (sb_page); done:kfree (sb);kfree (path);
@@ -2469,8 +2461,7 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, out_put_sb_page:put_page (sb_page); out_blkdev_put: -if (bdev) -blkdev_put (bdev,FMODE_READ |FMODE_WRITE |FMODE_EXCL ); +blkdev_put (bdev,FMODE_READ |FMODE_WRITE |FMODE_EXCL ); out_free_sb:kfree (sb); out_free_path:
Signature · 1 line
2.20.1
PATCH 5/7 bcache: return a pointer to the on-disk sb from read_super
In reply to Christoph HellwigReturning the properly typed actual data structure insteaf of the containing struct page will save the callers some work going forward.
--- drivers/md/bcache/super.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/md/bcache/super.c +11 -11
@@ -60,7 +60,7 @@ struct workqueue_struct *bch_journal_wq;/* Superblock */ static const char *read_super (struct cache_sb *sb,struct block_device *bdev, -struct page **res) +struct cache_sb_disk **res) {const char *err;struct cache_sb_disk *s;
@@ -191,7 +191,7 @@ static const char *read_super(struct cache_sb *sb, struct block_device *bdev, err =NULL ;get_page (bh->b_page); - *res = bh->b_page; + *res = s; err:put_bh (bh);return err;
@@ -1353,7 +1353,7 @@ static int cached_dev_init(struct cached_dev *dc, unsigned int block_size)/* Cached device - bcache superblock */ -static int register_bdev (struct cache_sb *sb,struct page *sb_page, +static int register_bdev (struct cache_sb *sb,struct cache_sb_disk *sb_disk,struct block_device *bdev,struct cached_dev *dc) {
@@ -1367,7 +1367,7 @@ static int register_bdev(struct cache_sb *sb, struct page *sb_page, dc->bdev->bd_holder = dc;bio_init (&dc->sb_bio, dc->sb_bio.bi_inline_vecs,1 ); -bio_first_bvec_all (&dc->sb_bio)->bv_page = sb_page; +bio_first_bvec_all (&dc->sb_bio)->bv_page =virt_to_page (sb_disk);if (cached_dev_init (dc, sb->block_size <<9 )) goto err;
@@ -2260,7 +2260,7 @@ static int cache_alloc(struct cache *ca)return ret; } -static int register_cache (struct cache_sb *sb,struct page *sb_page, +static int register_cache (struct cache_sb *sb,struct cache_sb_disk *sb_disk,struct block_device *bdev,struct cache *ca) {const char *err =NULL ;/* must be set for any error case */
@@ -2272,7 +2272,7 @@ static int register_cache(struct cache_sb *sb, struct page *sb_page, ca->bdev->bd_holder = ca;bio_init (&ca->sb_bio, ca->sb_bio.bi_inline_vecs,1 ); -bio_first_bvec_all (&ca->sb_bio)->bv_page = sb_page; +bio_first_bvec_all (&ca->sb_bio)->bv_page =virt_to_page (sb_disk);if (blk_queue_discard (bdev_get_queue (bdev))) ca->discard =CACHE_DISCARD (&ca->sb);
@@ -2375,8 +2375,8 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,const char *err;char *path;struct cache_sb *sb; +struct cache_sb_disk *sb_disk;struct block_device *bdev; -struct page *sb_page;ssize_t ret; ret = -EBUSY ;
@@ -2424,7 +2424,7 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,if (set_blocksize (bdev,4096 )) goto out_blkdev_put; - err =read_super (sb, bdev, &sb_page); + err =read_super (sb, bdev, &sb_disk);if (err) goto out_blkdev_put;
@@ -2436,7 +2436,7 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, goto out_put_sb_page;mutex_lock (&bch_register_lock); - ret =register_bdev (sb, sb_page, bdev, dc); + ret =register_bdev (sb, sb_disk, bdev, dc);mutex_unlock (&bch_register_lock);/* blkdev_put() will be called in cached_dev_free() */ if (ret <0 )
@@ -2448,7 +2448,7 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, goto out_put_sb_page;/* blkdev_put() will be called in bch_cache_release() */ -if (register_cache (sb, sb_page, bdev, ca) !=0 ) +if (register_cache (sb, sb_disk, bdev, ca) !=0 ) goto out_free_sb; }
@@ -2459,7 +2459,7 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,return size; out_put_sb_page: -put_page (sb_page); +put_page (virt_to_page (sb_disk)); out_blkdev_put:blkdev_put (bdev,FMODE_READ |FMODE_WRITE |FMODE_EXCL ); out_free_sb:
Signature · 1 line
2.20.1
PATCH 6/7 bcache: store a pointer to the on-disk sb in the cache and cached_dev structures
In reply to Christoph HellwigThis allows to properly build the superblock bio including the offset in the page using the normal bio helpers. This fixes writing the superblock for page sizes larger than 4k where the sb write bio would need an offset in the bio_vec.
--- drivers/md/bcache/bcache.h | 2 ++ drivers/md/bcache/super.c | 32 +++++++++++++------------------- 2 files changed, 15 insertions(+), 19 deletions(-)
drivers/md/bcache/bcache.h +2 -0
@@ -301,6 +301,7 @@ struct cached_dev {struct block_device *bdev;struct cache_sb sb; +struct cache_sb_disk *sb_disk;struct bio sb_bio;struct bio_vec sb_bv[1 ];struct closure sb_write;
@@ -403,6 +404,7 @@ enum alloc_reserve {struct cache {struct cache_set *set;struct cache_sb sb; +struct cache_sb_disk *sb_disk;struct bio sb_bio;struct bio_vec sb_bv[1 ];
drivers/md/bcache/super.c +13 -19
@@ -207,15 +207,15 @@ static void write_bdev_super_endio(struct bio *bio)closure_put (&dc->sb_write); } -static void __write_super (struct cache_sb *sb,struct bio *bio) +static void __write_super (struct cache_sb *sb,struct cache_sb_disk *out, +struct bio *bio) { -struct cache_sb_disk *out =page_address (bio_first_page_all (bio));unsigned int i; + bio->bi_opf =REQ_OP_WRITE |REQ_SYNC |REQ_META ; bio->bi_iter.bi_sector =SB_SECTOR ; - bio->bi_iter.bi_size =SB_SIZE ; -bio_set_op_attrs (bio,REQ_OP_WRITE ,REQ_SYNC |REQ_META ); -bch_bio_map (bio,NULL ); +__bio_add_page (bio,virt_to_page (out),SB_SIZE , +offset_in_page (out)); out->offset =cpu_to_le64 (sb->offset); out->version =cpu_to_le64 (sb->version);
@@ -257,14 +257,14 @@ void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)down (&dc->sb_write_mutex);closure_init (cl, parent); -bio_reset (bio); +bio_init (bio, dc->sb_bv,1 );bio_set_dev (bio, dc->bdev); bio->bi_end_io = write_bdev_super_endio; bio->bi_private = dc;closure_get (cl);/* I/O request sent to backing device */ -__write_super (&dc->sb, bio); +__write_super (&dc->sb, dc->sb_disk, bio);closure_return_with_destructor (cl, bch_write_bdev_super_unlock); }
@@ -306,13 +306,13 @@ void bcache_write_super(struct cache_set *c)SET_CACHE_SYNC (&ca->sb,CACHE_SYNC (&c->sb)); -bio_reset (bio); +bio_init (bio, ca->sb_bv,1 );bio_set_dev (bio, ca->bdev); bio->bi_end_io = write_super_endio; bio->bi_private = ca;closure_get (cl); -__write_super (&ca->sb, bio); +__write_super (&ca->sb, ca->sb_disk, bio); }closure_return_with_destructor (cl, bcache_write_super_unlock);
@@ -1275,8 +1275,8 @@ static void cached_dev_free(struct closure *cl)mutex_unlock (&bch_register_lock); -if (dc->sb_bio.bi_inline_vecs[0 ].bv_page) -put_page (bio_first_page_all (&dc->sb_bio)); +if (dc->sb_disk) +put_page (virt_to_page (dc->sb_disk));if (!IS_ERR_OR_NULL (dc->bdev))blkdev_put (dc->bdev,FMODE_READ |FMODE_WRITE |FMODE_EXCL );
@@ -1366,9 +1366,6 @@ static int register_bdev(struct cache_sb *sb, struct cache_sb_disk *sb_disk, dc->bdev = bdev; dc->bdev->bd_holder = dc; -bio_init (&dc->sb_bio, dc->sb_bio.bi_inline_vecs,1 ); -bio_first_bvec_all (&dc->sb_bio)->bv_page =virt_to_page (sb_disk); -if (cached_dev_init (dc, sb->block_size <<9 )) goto err;
@@ -2137,8 +2134,8 @@ void bch_cache_release(struct kobject *kobj)for (i =0 ; i <RESERVE_NR ; i++)free_fifo (&ca->free[i]); -if (ca->sb_bio.bi_inline_vecs[0 ].bv_page) -put_page (bio_first_page_all (&ca->sb_bio)); +if (ca->sb_disk) +put_page (virt_to_page (ca->sb_disk));if (!IS_ERR_OR_NULL (ca->bdev))blkdev_put (ca->bdev,FMODE_READ |FMODE_WRITE |FMODE_EXCL );
@@ -2271,9 +2268,6 @@ static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk, ca->bdev = bdev; ca->bdev->bd_holder = ca; -bio_init (&ca->sb_bio, ca->sb_bio.bi_inline_vecs,1 ); -bio_first_bvec_all (&ca->sb_bio)->bv_page =virt_to_page (sb_disk); -if (blk_queue_discard (bdev_get_queue (bdev))) ca->discard =CACHE_DISCARD (&ca->sb);
Signature · 1 line
2.20.1
PATCH 7/7 bcache: use read_cache_page_gfp to read the superblock
In reply to Christoph HellwigAvoid a pointless dependency on buffer heads in bcache by simply open coding reading a single page. Also add a SB_OFFSET define for the byte offset of the superblock instead of using magic numbers.
--- drivers/md/bcache/super.c | 16 +++++++--------- include/uapi/linux/bcache.h | 1 + 2 files changed, 8 insertions(+), 9 deletions(-)
drivers/md/bcache/super.c +7 -9
@@ -15,7 +15,6 @@#include "writeback.h" #include <linux/blkdev.h> -#include <linux/buffer_head.h> #include <linux/debugfs.h> #include <linux/genhd.h> #include <linux/idr.h>
@@ -64,13 +63,14 @@ static const char *read_super(struct cache_sb *sb, struct block_device *bdev, {const char *err;struct cache_sb_disk *s; -struct buffer_head *bh =__bread (bdev,1 ,SB_SIZE ); +struct page *page;unsigned int i; -if (!bh) + page =read_cache_page_gfp (bdev->bd_inode->i_mapping, +SB_OFFSET >>PAGE_SHIFT ,GFP_KERNEL ); +if (IS_ERR (page))return "IO error" ; - - s = (struct cache_sb_disk *)bh->b_data; + s =page_address (page) +offset_in_page (SB_OFFSET ); sb->offset =le64_to_cpu (s->offset); sb->version =le64_to_cpu (s->version);
@@ -188,12 +188,10 @@ static const char *read_super(struct cache_sb *sb, struct block_device *bdev, } sb->last_mount = (u32 )ktime_get_real_seconds (); - err =NULL ; - -get_page (bh->b_page); *res = s; +return NULL ; err: -put_bh (bh); +put_page (page);return err; }
include/uapi/linux/bcache.h +1 -0
@@ -148,6 +148,7 @@ static inline struct bkey *bkey_idx(const struct bkey *k, unsigned int nr_keys)#define BCACHE_SB_MAX_VERSION 4#define SB_SECTOR 8 +#define SB_OFFSET (SB_SECTOR << SECTOR_SHIFT)#define SB_SIZE 4096#define SB_LABEL_SIZE 32#define SB_JOURNAL_BUCKETS 256U
Signature · 1 line
2.20.1
Quoted 6 lines · Christoph Hellwig
Hi Coly and Liang,
can you review this series to sort out the bcache superblock reading for larger page sizes? I don't have bcache test setup so this is compile tested only.
Hi Christoph,
At first glance the patches are good. I will add them into my for-test directory and test with other development patches.
Thanks for handling this.
Signature · 1 line
Coly Li
Quoted 6 lines
Hi Coly and Liang,
can you review this series to sort out the bcache superblock reading for larger page sizes? I don't have bcache test setup so this is compile tested only.
Hi Christoph,
Thanks for making the patches. I looked through them, but didn't see where cache and cached_dev have their sb_disk assigned. That would be an issue when __write_super tries to add the corresponding page to the bio. Not sure if there is there anything I missed.
Liang
Quoted 6 lines · Liang C
Quoted 6 lines
Hi Coly and Liang,
can you review this series to sort out the bcache superblock reading for larger page sizes? I don't have bcache test setup so this is compile tested only.
Quoted 7 lines
Hi Christoph,
Thanks for making the patches. I looked through them, but didn't see where cache and
cached_devhave theirsb_diskassigned. That would be an issue when__write_supertries to add the corresponding page to the bio. Not sure if there is there anything I missed.
Yes, that was missing.