Re: [PATCH v2 00/15] sysctl: Remove sentinel elements from drivers

2023-10-10 Thread Luis Chamberlain
On Mon, Oct 02, 2023 at 10:55:17AM +0200, Joel Granados via B4 Relay wrote:
> Changes in v2:
> - Left the dangling comma in the ctl_table arrays.
> - Link to v1: 
> https://lore.kernel.org/r/20230928-jag-sysctl_remove_empty_elem_drivers-v1-0-e59120fca...@samsung.com

Thanks! Pushed onto sysctl-next for wider testing.

  Luis



Re: [PATCH 0/7] sysctl: slowly deprecate register_sysctl_table()

2023-03-09 Thread Luis Chamberlain
On Thu, Mar 02, 2023 at 12:46:05PM -0800, Luis Chamberlain wrote:
> I'm happy to take these via sysctl-next [0] but since
> I don' think register_sysctl_table() will be nuked on v6.4 I think
> it's fine for each of these to go into each respective tree. I can
> pick up last stragglers on sysctl-next. If you want me to take this
> via sysctl-next too, just let me know and I'm happy to do that. Either
> way works.

As I noted I've dropped the following already-picked-up patches from
my queue:

ipmi: simplify sysctl registration
sgi-xp: simplify sysctl registration
tty: simplify sysctl registration

I've taken the rest now through sysctl-next:

scsi: simplify sysctl registration with register_sysctl()
hv: simplify sysctl registration
md: simplify sysctl registration
xen: simplify sysctl registration for balloon

If a maintainer would prefer to take one on through their
tree fine by me too, just let me know and I'll drop the patch.

  Luis



[PATCH 3/7] hv: simplify sysctl registration

2023-03-02 Thread Luis Chamberlain
register_sysctl_table() is a deprecated compatibility wrapper.
register_sysctl() can do the directory creation for you so just use
that.

Signed-off-by: Luis Chamberlain 
---
 drivers/hv/vmbus_drv.c | 11 +--
 1 file changed, 1 insertion(+), 10 deletions(-)

diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
index d24dd65b33d4..229353f1e9c2 100644
--- a/drivers/hv/vmbus_drv.c
+++ b/drivers/hv/vmbus_drv.c
@@ -1460,15 +1460,6 @@ static struct ctl_table hv_ctl_table[] = {
{}
 };
 
-static struct ctl_table hv_root_table[] = {
-   {
-   .procname   = "kernel",
-   .mode   = 0555,
-   .child  = hv_ctl_table
-   },
-   {}
-};
-
 /*
  * vmbus_bus_init -Main vmbus driver initialization routine.
  *
@@ -1547,7 +1538,7 @@ static int vmbus_bus_init(void)
 * message recording won't be available in isolated
 * guests should the following registration fail.
 */
-   hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
+   hv_ctl_table_hdr = register_sysctl("kernel", hv_ctl_table);
if (!hv_ctl_table_hdr)
pr_err("Hyper-V: sysctl table register error");
 
-- 
2.39.1




[PATCH 0/7] sysctl: slowly deprecate register_sysctl_table()

2023-03-02 Thread Luis Chamberlain
As the large array of sysctls in kernel/sysctl.c is reduced we get to
the point of wanting to optimize how we register sysctls by only dealing
with flat simple structures, with no subdirectories. In particular the
last empty element should not be needed. We'll get there, and save some
memory, but as we move forward that path will be come the more relevant
path to use in the sysctl registration. It is much simpler as it avoids
recursion.

Turns out we can also convert existing users of register_sysctl_table()
which just need their subdirectories created for them. This effort
addresses most users of register_sysctl_table() in drivers/ except
parport -- that needs a bit more review.

This is part of the process to deprecate older sysctl users which uses
APIs which can incur recursion, but don't need it [0]. This is the
second effort.

Yes -- we'll get to the point *each* of these conversions means saving
one empty syctl, but that change needs a bit more careful review before
merging. But since these conversion are also deleting tables for
subdirectories, the delta in size of the kernel should not incrase
really.

The most complex change is the sgi-xp change which does deal with
a case where we have a subdirectory with an entry, I just split
that in two registrations. No point in keeping recursion just for
a few minor if we can simplify code around. More eyeballs / review /
testing on that change is appreciated.

Sending these out early so they can get tested properly early on
linux-next. I'm happy to take these via sysctl-next [0] but since
I don' think register_sysctl_table() will be nuked on v6.4 I think
it's fine for each of these to go into each respective tree. I can
pick up last stragglers on sysctl-next. If you want me to take this
via sysctl-next too, just let me know and I'm happy to do that. Either
way works.

[0] https://lkml.kernel.org/r/20230302202826.776286-1-mcg...@kernel.org

Luis Chamberlain (7):
  scsi: simplify sysctl registration with register_sysctl()
  ipmi: simplify sysctl registration
  hv: simplify sysctl registration
  md: simplify sysctl registration
  sgi-xp: simplify sysctl registration
  tty: simplify sysctl registration
  xen: simplify sysctl registration for balloon

 drivers/char/ipmi/ipmi_poweroff.c | 16 +---
 drivers/hv/vmbus_drv.c| 11 +--
 drivers/md/md.c   | 22 +-
 drivers/misc/sgi-xp/xpc_main.c| 24 ++--
 drivers/scsi/scsi_sysctl.c| 16 +---
 drivers/tty/tty_io.c  | 20 +---
 drivers/xen/balloon.c | 20 +---
 7 files changed, 16 insertions(+), 113 deletions(-)

-- 
2.39.1




[PATCH 6/7] tty: simplify sysctl registration

2023-03-02 Thread Luis Chamberlain
register_sysctl_table() is a deprecated compatibility wrapper.
register_sysctl_init() can do the directory creation for you so just use
that

Signed-off-by: Luis Chamberlain 
---
 drivers/tty/tty_io.c | 20 +---
 1 file changed, 1 insertion(+), 19 deletions(-)

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 36fb945fdad4..766750e355ac 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -3614,31 +3614,13 @@ static struct ctl_table tty_table[] = {
{ }
 };
 
-static struct ctl_table tty_dir_table[] = {
-   {
-   .procname   = "tty",
-   .mode   = 0555,
-   .child  = tty_table,
-   },
-   { }
-};
-
-static struct ctl_table tty_root_table[] = {
-   {
-   .procname   = "dev",
-   .mode   = 0555,
-   .child  = tty_dir_table,
-   },
-   { }
-};
-
 /*
  * Ok, now we can initialize the rest of the tty devices and can count
  * on memory allocations, interrupts etc..
  */
 int __init tty_init(void)
 {
-   register_sysctl_table(tty_root_table);
+   register_sysctl_init("dev/tty", tty_table);
cdev_init(_cdev, _fops);
if (cdev_add(_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
-- 
2.39.1




[PATCH 7/7] xen: simplify sysctl registration for balloon

2023-03-02 Thread Luis Chamberlain
register_sysctl_table() is a deprecated compatibility wrapper.
register_sysctl_init() can do the directory creation for you so just
use that.

Signed-off-by: Luis Chamberlain 
---
 drivers/xen/balloon.c | 20 +---
 1 file changed, 1 insertion(+), 19 deletions(-)

diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c
index 617a7f4f07a8..586a1673459e 100644
--- a/drivers/xen/balloon.c
+++ b/drivers/xen/balloon.c
@@ -97,24 +97,6 @@ static struct ctl_table balloon_table[] = {
{ }
 };
 
-static struct ctl_table balloon_root[] = {
-   {
-   .procname   = "balloon",
-   .mode   = 0555,
-   .child  = balloon_table,
-   },
-   { }
-};
-
-static struct ctl_table xen_root[] = {
-   {
-   .procname   = "xen",
-   .mode   = 0555,
-   .child  = balloon_root,
-   },
-   { }
-};
-
 #else
 #define xen_hotplug_unpopulated 0
 #endif
@@ -747,7 +729,7 @@ static int __init balloon_init(void)
 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
set_online_page_callback(_online_page);
register_memory_notifier(_memory_nb);
-   register_sysctl_table(xen_root);
+   register_sysctl_init("xen/balloon", balloon_table);
 #endif
 
balloon_add_regions();
-- 
2.39.1




[PATCH 1/7] scsi: simplify sysctl registration with register_sysctl()

2023-03-02 Thread Luis Chamberlain
register_sysctl_table() is a deprecated compatibility wrapper.
register_sysctl() can do the directory creation for you so just use that.

Signed-off-by: Luis Chamberlain 
---
 drivers/scsi/scsi_sysctl.c | 16 +---
 1 file changed, 1 insertion(+), 15 deletions(-)

diff --git a/drivers/scsi/scsi_sysctl.c b/drivers/scsi/scsi_sysctl.c
index 7259704a7f52..7f0914ea168f 100644
--- a/drivers/scsi/scsi_sysctl.c
+++ b/drivers/scsi/scsi_sysctl.c
@@ -21,25 +21,11 @@ static struct ctl_table scsi_table[] = {
{ }
 };
 
-static struct ctl_table scsi_dir_table[] = {
-   { .procname = "scsi",
- .mode = 0555,
- .child= scsi_table },
-   { }
-};
-
-static struct ctl_table scsi_root_table[] = {
-   { .procname = "dev",
- .mode = 0555,
- .child= scsi_dir_table },
-   { }
-};
-
 static struct ctl_table_header *scsi_table_header;
 
 int __init scsi_init_sysctl(void)
 {
-   scsi_table_header = register_sysctl_table(scsi_root_table);
+   scsi_table_header = register_sysctl("dev/scsi", scsi_table);
if (!scsi_table_header)
return -ENOMEM;
return 0;
-- 
2.39.1




[PATCH 2/7] ipmi: simplify sysctl registration

2023-03-02 Thread Luis Chamberlain
register_sysctl_table() is a deprecated compatibility wrapper.
register_sysctl() can do the directory creation for you so just use
that.

Signed-off-by: Luis Chamberlain 
---
 drivers/char/ipmi/ipmi_poweroff.c | 16 +---
 1 file changed, 1 insertion(+), 15 deletions(-)

diff --git a/drivers/char/ipmi/ipmi_poweroff.c 
b/drivers/char/ipmi/ipmi_poweroff.c
index 163ec9749e55..870659d91db2 100644
--- a/drivers/char/ipmi/ipmi_poweroff.c
+++ b/drivers/char/ipmi/ipmi_poweroff.c
@@ -659,20 +659,6 @@ static struct ctl_table ipmi_table[] = {
{ }
 };
 
-static struct ctl_table ipmi_dir_table[] = {
-   { .procname = "ipmi",
- .mode = 0555,
- .child= ipmi_table },
-   { }
-};
-
-static struct ctl_table ipmi_root_table[] = {
-   { .procname = "dev",
- .mode = 0555,
- .child= ipmi_dir_table },
-   { }
-};
-
 static struct ctl_table_header *ipmi_table_header;
 #endif /* CONFIG_PROC_FS */
 
@@ -689,7 +675,7 @@ static int __init ipmi_poweroff_init(void)
pr_info("Power cycle is enabled\n");
 
 #ifdef CONFIG_PROC_FS
-   ipmi_table_header = register_sysctl_table(ipmi_root_table);
+   ipmi_table_header = register_sysctl("dev/ipmi", ipmi_table);
if (!ipmi_table_header) {
pr_err("Unable to register powercycle sysctl\n");
rv = -ENOMEM;
-- 
2.39.1




[PATCH 4/7] md: simplify sysctl registration

2023-03-02 Thread Luis Chamberlain
register_sysctl_table() is a deprecated compatibility wrapper.
register_sysctl() can do the directory creation for you so just use
that.

Signed-off-by: Luis Chamberlain 
---
 drivers/md/md.c | 22 +-
 1 file changed, 1 insertion(+), 21 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 927a43db5dfb..546b1b81eb28 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -322,26 +322,6 @@ static struct ctl_table raid_table[] = {
{ }
 };
 
-static struct ctl_table raid_dir_table[] = {
-   {
-   .procname   = "raid",
-   .maxlen = 0,
-   .mode   = S_IRUGO|S_IXUGO,
-   .child  = raid_table,
-   },
-   { }
-};
-
-static struct ctl_table raid_root_table[] = {
-   {
-   .procname   = "dev",
-   .maxlen = 0,
-   .mode   = 0555,
-   .child  = raid_dir_table,
-   },
-   {  }
-};
-
 static int start_readonly;
 
 /*
@@ -9650,7 +9630,7 @@ static int __init md_init(void)
mdp_major = ret;
 
register_reboot_notifier(_notifier);
-   raid_table_header = register_sysctl_table(raid_root_table);
+   raid_table_header = register_sysctl("dev/raid", raid_table);
 
md_geninit();
return 0;
-- 
2.39.1




[PATCH 5/7] sgi-xp: simplify sysctl registration

2023-03-02 Thread Luis Chamberlain
Although this driver is a good use case for having a directory
that is not other directories and then subdirectories with more
entries, the usage of register_sysctl_table() can recurse and
increases complexity so to avoid that just split out the
registration to each directory with its own entries.

register_sysctl_table() is a deprecated compatibility wrapper.
register_sysctl() can do the directory creation for you so just use
that.

Signed-off-by: Luis Chamberlain 
---
 drivers/misc/sgi-xp/xpc_main.c | 24 ++--
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/drivers/misc/sgi-xp/xpc_main.c b/drivers/misc/sgi-xp/xpc_main.c
index b2c3c22fc13c..6da509d692bb 100644
--- a/drivers/misc/sgi-xp/xpc_main.c
+++ b/drivers/misc/sgi-xp/xpc_main.c
@@ -93,7 +93,7 @@ int xpc_disengage_timelimit = XPC_DISENGAGE_DEFAULT_TIMELIMIT;
 static int xpc_disengage_min_timelimit;/* = 0 */
 static int xpc_disengage_max_timelimit = 120;
 
-static struct ctl_table xpc_sys_xpc_hb_dir[] = {
+static struct ctl_table xpc_sys_xpc_hb[] = {
{
 .procname = "hb_interval",
 .data = _hb_interval,
@@ -112,11 +112,7 @@ static struct ctl_table xpc_sys_xpc_hb_dir[] = {
 .extra2 = _hb_check_max_interval},
{}
 };
-static struct ctl_table xpc_sys_xpc_dir[] = {
-   {
-.procname = "hb",
-.mode = 0555,
-.child = xpc_sys_xpc_hb_dir},
+static struct ctl_table xpc_sys_xpc[] = {
{
 .procname = "disengage_timelimit",
 .data = _disengage_timelimit,
@@ -127,14 +123,9 @@ static struct ctl_table xpc_sys_xpc_dir[] = {
 .extra2 = _disengage_max_timelimit},
{}
 };
-static struct ctl_table xpc_sys_dir[] = {
-   {
-.procname = "xpc",
-.mode = 0555,
-.child = xpc_sys_xpc_dir},
-   {}
-};
+
 static struct ctl_table_header *xpc_sysctl;
+static struct ctl_table_header *xpc_sysctl_hb;
 
 /* non-zero if any remote partition disengage was timed out */
 int xpc_disengage_timedout;
@@ -1041,6 +1032,8 @@ xpc_do_exit(enum xp_retval reason)
 
if (xpc_sysctl)
unregister_sysctl_table(xpc_sysctl);
+   if (xpc_sysctl_hb)
+   unregister_sysctl_table(xpc_sysctl_hb);
 
xpc_teardown_partitions();
 
@@ -1243,7 +1236,8 @@ xpc_init(void)
goto out_1;
}
 
-   xpc_sysctl = register_sysctl_table(xpc_sys_dir);
+   xpc_sysctl = register_sysctl("xpc", xpc_sys_xpc);
+   xpc_sysctl_hb = register_sysctl("xpc/hb", xpc_sys_xpc_hb);
 
/*
 * Fill the partition reserved page with the information needed by
@@ -1308,6 +1302,8 @@ xpc_init(void)
(void)unregister_die_notifier(_die_notifier);
(void)unregister_reboot_notifier(_reboot_notifier);
 out_2:
+   if (xpc_sysctl_hb)
+   unregister_sysctl_table(xpc_sysctl_hb);
if (xpc_sysctl)
unregister_sysctl_table(xpc_sysctl);
 
-- 
2.39.1




Re: [PATCH 1/9] scsi/sd: add error handling support for add_disk()

2021-10-18 Thread Luis Chamberlain
On Sat, Oct 16, 2021 at 10:51:48PM -0400, Martin K. Petersen wrote:
> 
> Luis,
> 
> > We never checked for errors on add_disk() as this function returned
> > void. Now that this is fixed, use the shiny new error handling.
> >
> > As with the error handling for device_add() we follow the same logic
> > and just put the device so that cleanup is done via the
> > scsi_disk_release().
> 
> Acked-by: Martin K. Petersen 

Thanks, would you like Jens to pick this up and the other scsi/sr patch
or are you taking it through your tree?

  Luis



[PATCH 6/9] m68k/emu/nfblock: add error handling support for add_disk()

2021-10-15 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Reviewed-by: Geert Uytterhoeven 
Acked-by: Geert Uytterhoeven 
Signed-off-by: Luis Chamberlain 
---
 arch/m68k/emu/nfblock.c | 9 +++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/m68k/emu/nfblock.c b/arch/m68k/emu/nfblock.c
index 9a8394e96388..4de5a6087034 100644
--- a/arch/m68k/emu/nfblock.c
+++ b/arch/m68k/emu/nfblock.c
@@ -100,6 +100,7 @@ static int __init nfhd_init_one(int id, u32 blocks, u32 
bsize)
 {
struct nfhd_device *dev;
int dev_id = id - NFHD_DEV_OFFSET;
+   int err = -ENOMEM;
 
pr_info("nfhd%u: found device with %u blocks (%u bytes)\n", dev_id,
blocks, bsize);
@@ -130,16 +131,20 @@ static int __init nfhd_init_one(int id, u32 blocks, u32 
bsize)
sprintf(dev->disk->disk_name, "nfhd%u", dev_id);
set_capacity(dev->disk, (sector_t)blocks * (bsize / 512));
blk_queue_logical_block_size(dev->disk->queue, bsize);
-   add_disk(dev->disk);
+   err = add_disk(dev->disk);
+   if (err)
+   goto out_cleanup_disk;
 
list_add_tail(>list, _list);
 
return 0;
 
+out_cleanup_disk:
+   blk_cleanup_disk(dev->disk);
 free_dev:
kfree(dev);
 out:
-   return -ENOMEM;
+   return err;
 }
 
 static int __init nfhd_init(void)
-- 
2.30.2




[PATCH 4/9] bcache: add error handling support for add_disk()

2021-10-15 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

This driver doesn't do any unwinding with blk_cleanup_disk()
even on errors after add_disk() and so we follow that
tradition.

Acked-by: Coly Li 
Signed-off-by: Luis Chamberlain 
---
 drivers/md/bcache/super.c | 17 -
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index f2874c77ff79..f0c32cdd6594 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1082,7 +1082,9 @@ int bch_cached_dev_run(struct cached_dev *dc)
closure_sync();
}
 
-   add_disk(d->disk);
+   ret = add_disk(d->disk);
+   if (ret)
+   goto out;
bd_link_disk_holder(dc->bdev, dc->disk.disk);
/*
 * won't show up in the uevent file, use udevadm monitor -e instead
@@ -1534,10 +1536,11 @@ static void flash_dev_flush(struct closure *cl)
 
 static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
 {
+   int err = -ENOMEM;
struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
  GFP_KERNEL);
if (!d)
-   return -ENOMEM;
+   goto err_ret;
 
closure_init(>cl, NULL);
set_closure_fn(>cl, flash_dev_flush, system_wq);
@@ -1551,9 +1554,12 @@ static int flash_dev_run(struct cache_set *c, struct 
uuid_entry *u)
bcache_device_attach(d, c, u - c->uuids);
bch_sectors_dirty_init(d);
bch_flash_dev_request_init(d);
-   add_disk(d->disk);
+   err = add_disk(d->disk);
+   if (err)
+   goto err;
 
-   if (kobject_add(>kobj, _to_dev(d->disk)->kobj, "bcache"))
+   err = kobject_add(>kobj, _to_dev(d->disk)->kobj, "bcache");
+   if (err)
goto err;
 
bcache_device_link(d, c, "volume");
@@ -1567,7 +1573,8 @@ static int flash_dev_run(struct cache_set *c, struct 
uuid_entry *u)
return 0;
 err:
kobject_put(>kobj);
-   return -ENOMEM;
+err_ret:
+   return err;
 }
 
 static int flash_devs_run(struct cache_set *c)
-- 
2.30.2




[PATCH 8/9] rnbd: add error handling support for add_disk()

2021-10-15 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Acked-by: Jack Wang 
Signed-off-by: Luis Chamberlain 
---
 drivers/block/rnbd/rnbd-clt.c | 13 +
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/block/rnbd/rnbd-clt.c b/drivers/block/rnbd/rnbd-clt.c
index 5864c9b46cb9..3b78dc55a9a2 100644
--- a/drivers/block/rnbd/rnbd-clt.c
+++ b/drivers/block/rnbd/rnbd-clt.c
@@ -1384,8 +1384,10 @@ static void setup_request_queue(struct rnbd_clt_dev *dev)
blk_queue_write_cache(dev->queue, dev->wc, dev->fua);
 }
 
-static void rnbd_clt_setup_gen_disk(struct rnbd_clt_dev *dev, int idx)
+static int rnbd_clt_setup_gen_disk(struct rnbd_clt_dev *dev, int idx)
 {
+   int err;
+
dev->gd->major  = rnbd_client_major;
dev->gd->first_minor= idx << RNBD_PART_BITS;
dev->gd->minors = 1 << RNBD_PART_BITS;
@@ -1410,7 +1412,11 @@ static void rnbd_clt_setup_gen_disk(struct rnbd_clt_dev 
*dev, int idx)
 
if (!dev->rotational)
blk_queue_flag_set(QUEUE_FLAG_NONROT, dev->queue);
-   add_disk(dev->gd);
+   err = add_disk(dev->gd);
+   if (err)
+   blk_cleanup_disk(dev->gd);
+
+   return err;
 }
 
 static int rnbd_client_setup_device(struct rnbd_clt_dev *dev)
@@ -1426,8 +1432,7 @@ static int rnbd_client_setup_device(struct rnbd_clt_dev 
*dev)
rnbd_init_mq_hw_queues(dev);
 
setup_request_queue(dev);
-   rnbd_clt_setup_gen_disk(dev, idx);
-   return 0;
+   return rnbd_clt_setup_gen_disk(dev, idx);
 }
 
 static struct rnbd_clt_dev *init_dev(struct rnbd_clt_session *sess,
-- 
2.30.2




[PATCH 9/9] mtd: add add_disk() error handling

2021-10-15 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Acked-by: Miquel Raynal 
Signed-off-by: Luis Chamberlain 
---
 drivers/mtd/mtd_blkdevs.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c
index b8ae1ec14e17..4eaba6f4ec68 100644
--- a/drivers/mtd/mtd_blkdevs.c
+++ b/drivers/mtd/mtd_blkdevs.c
@@ -384,7 +384,9 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
if (new->readonly)
set_disk_ro(gd, 1);
 
-   device_add_disk(>mtd->dev, gd, NULL);
+   ret = device_add_disk(>mtd->dev, gd, NULL);
+   if (ret)
+   goto out_cleanup_disk;
 
if (new->disk_attributes) {
ret = sysfs_create_group(_to_dev(gd)->kobj,
@@ -393,6 +395,8 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
}
return 0;
 
+out_cleanup_disk:
+   blk_cleanup_disk(new->disk);
 out_free_tag_set:
blk_mq_free_tag_set(new->tag_set);
 out_kfree_tag_set:
-- 
2.30.2




[PATCH 2/9] scsi/sr: add error handling support for add_disk()

2021-10-15 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Just put the cdrom kref and have the unwinding be done by
sr_kref_release().

Reviewed-by: Christoph Hellwig 
Signed-off-by: Luis Chamberlain 
---
 drivers/scsi/sr.c | 7 ++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c
index 115f7ef7a5de..4e5515848fa6 100644
--- a/drivers/scsi/sr.c
+++ b/drivers/scsi/sr.c
@@ -728,7 +728,12 @@ static int sr_probe(struct device *dev)
dev_set_drvdata(dev, cd);
disk->flags |= GENHD_FL_REMOVABLE;
sr_revalidate_disk(cd);
-   device_add_disk(>sdev_gendev, disk, NULL);
+
+   error = device_add_disk(>sdev_gendev, disk, NULL);
+   if (error) {
+   kref_put(>kref, sr_kref_release);
+   goto fail;
+   }
 
sdev_printk(KERN_DEBUG, sdev,
"Attached scsi CD-ROM %s\n", cd->cdi.name);
-- 
2.30.2




[PATCH 3/9] dm: add add_disk() error handling

2021-10-15 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

There are two calls to dm_setup_md_queue() which can fail then,
one on dm_early_create() and we can easily see that the error path
there calls dm_destroy in the error path. The other use case is on
the ioctl table_load case. If that fails userspace needs to call
the DM_DEV_REMOVE_CMD to cleanup the state - similar to any other
failure.

Reviewed-by: Hannes Reinecke 
Signed-off-by: Luis Chamberlain 
---
 drivers/md/dm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/md/dm.c b/drivers/md/dm.c
index 245fa4153306..6d3265ed37c0 100644
--- a/drivers/md/dm.c
+++ b/drivers/md/dm.c
@@ -2086,7 +2086,9 @@ int dm_setup_md_queue(struct mapped_device *md, struct 
dm_table *t)
if (r)
return r;
 
-   add_disk(md->disk);
+   r = add_disk(md->disk);
+   if (r)
+   return r;
 
r = dm_sysfs_init(md);
if (r) {
-- 
2.30.2




[PATCH 5/9] xen-blkfront: add error handling support for add_disk()

2021-10-15 Thread Luis Chamberlain
We never checked for errors on device_add_disk() as this function
returned void. Now that this is fixed, use the shiny new error
handling. The function xlvbd_alloc_gendisk() typically does the
unwinding on error on allocating the disk and creating the tag,
but since all that error handling was stuffed inside
xlvbd_alloc_gendisk() we must repeat the tag free'ing as well.

We set the info->rq to NULL to ensure blkif_free() doesn't crash
on blk_mq_stop_hw_queues() on device_add_disk() error as the queue
will be long gone by then.

Reviewed-by: Juergen Gross 
Signed-off-by: Luis Chamberlain 
---
 drivers/block/xen-blkfront.c | 8 +++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c
index df0deb927760..8e3983e456f3 100644
--- a/drivers/block/xen-blkfront.c
+++ b/drivers/block/xen-blkfront.c
@@ -2386,7 +2386,13 @@ static void blkfront_connect(struct blkfront_info *info)
for_each_rinfo(info, rinfo, i)
kick_pending_request_queues(rinfo);
 
-   device_add_disk(>xbdev->dev, info->gd, NULL);
+   err = device_add_disk(>xbdev->dev, info->gd, NULL);
+   if (err) {
+   blk_cleanup_disk(info->gd);
+   blk_mq_free_tag_set(>tag_set);
+   info->rq = NULL;
+   goto fail;
+   }
 
info->is_ready = 1;
return;
-- 
2.30.2




[PATCH 0/9] block: reviewed add_disk() error handling set

2021-10-15 Thread Luis Chamberlain
Jens,

I had last split up patches into 7 groups, but at this point now
most changes are merged except a few more drivers. Instead of creating
a new patch set for each of the 7 groups I'm creating 3 new groups of
patches now:

  * This set, for which we already have an Acked-by or Reviewed-by tag,
it would be nice to get clarification of driver maintainers want
these to go through you or if a the maintainers want to pick these
changes up themselves.

  * A second set will deal with patches which have no reviews done for
them yet 

  * The last set deals with the __register_blkdev() change and the
__must_check change which ensures we don't let in new drivers
which don't deal with error handling.

If you're a maintainer of any of the below patches and wish for them to
go through Jens' tree directly now would be a good time to say so or
you can just pick the patch up yourself.

Luis Chamberlain (9):
  scsi/sd: add error handling support for add_disk()
  scsi/sr: add error handling support for add_disk()
  dm: add add_disk() error handling
  bcache: add error handling support for add_disk()
  xen-blkfront: add error handling support for add_disk()
  m68k/emu/nfblock: add error handling support for add_disk()
  um/drivers/ubd_kern: add error handling support for add_disk()
  rnbd: add error handling support for add_disk()
  mtd: add add_disk() error handling

 arch/m68k/emu/nfblock.c   |  9 +++--
 arch/um/drivers/ubd_kern.c| 13 +
 drivers/block/rnbd/rnbd-clt.c | 13 +
 drivers/block/xen-blkfront.c  |  8 +++-
 drivers/md/bcache/super.c | 17 -
 drivers/md/dm.c   |  4 +++-
 drivers/mtd/mtd_blkdevs.c |  6 +-
 drivers/scsi/sd.c |  8 +++-
 drivers/scsi/sr.c |  7 ++-
 9 files changed, 65 insertions(+), 20 deletions(-)

-- 
2.30.2




[PATCH 7/9] um/drivers/ubd_kern: add error handling support for add_disk()

2021-10-15 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

ubd_disk_register() never returned an error, so just fix
that now and let the caller handle the error condition.

Reviewed-by: Gabriel Krisman Bertazi 
Signed-off-by: Luis Chamberlain 
---
 arch/um/drivers/ubd_kern.c | 13 +
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/arch/um/drivers/ubd_kern.c b/arch/um/drivers/ubd_kern.c
index fefd343412c7..69d2d0049a61 100644
--- a/arch/um/drivers/ubd_kern.c
+++ b/arch/um/drivers/ubd_kern.c
@@ -855,8 +855,8 @@ static const struct attribute_group *ubd_attr_groups[] = {
NULL,
 };
 
-static void ubd_disk_register(int major, u64 size, int unit,
- struct gendisk *disk)
+static int ubd_disk_register(int major, u64 size, int unit,
+struct gendisk *disk)
 {
disk->major = major;
disk->first_minor = unit << UBD_SHIFT;
@@ -873,7 +873,7 @@ static void ubd_disk_register(int major, u64 size, int unit,
 
disk->private_data = _devs[unit];
disk->queue = ubd_devs[unit].queue;
-   device_add_disk(_devs[unit].pdev.dev, disk, ubd_attr_groups);
+   return device_add_disk(_devs[unit].pdev.dev, disk, ubd_attr_groups);
 }
 
 #define ROUND_BLOCK(n) ((n + (SECTOR_SIZE - 1)) & (-SECTOR_SIZE))
@@ -920,10 +920,15 @@ static int ubd_add(int n, char **error_out)
blk_queue_write_cache(ubd_dev->queue, true, false);
blk_queue_max_segments(ubd_dev->queue, MAX_SG);
blk_queue_segment_boundary(ubd_dev->queue, PAGE_SIZE - 1);
-   ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, disk);
+   err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, disk);
+   if (err)
+   goto out_cleanup_disk;
+
ubd_gendisk[n] = disk;
return 0;
 
+out_cleanup_disk:
+   blk_cleanup_disk(disk);
 out_cleanup_tags:
blk_mq_free_tag_set(_dev->tag_set);
 out:
-- 
2.30.2




[PATCH 1/9] scsi/sd: add error handling support for add_disk()

2021-10-15 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

As with the error handling for device_add() we follow the same
logic and just put the device so that cleanup is done via the
scsi_disk_release().

Reviewed-by: Christoph Hellwig 
Signed-off-by: Luis Chamberlain 
---
 drivers/scsi/sd.c | 8 +++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index a646d27df681..d69f2e626e76 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3457,7 +3457,13 @@ static int sd_probe(struct device *dev)
pm_runtime_set_autosuspend_delay(dev,
sdp->host->hostt->rpm_autosuspend_delay);
}
-   device_add_disk(dev, gd, NULL);
+
+   error = device_add_disk(dev, gd, NULL);
+   if (error) {
+   put_device(>dev);
+   goto out;
+   }
+
if (sdkp->capacity)
sd_dif_config_host(sdkp);
 
-- 
2.30.2




[PATCH v2 01/10] block/brd: add error handling support for add_disk()

2021-09-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Signed-off-by: Luis Chamberlain 
---
 drivers/block/brd.c | 10 --
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index 58ec167aa018..c2bf4946f4e3 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -372,6 +372,7 @@ static int brd_alloc(int i)
struct brd_device *brd;
struct gendisk *disk;
char buf[DISK_NAME_LEN];
+   int err = -ENOMEM;
 
brd = kzalloc(sizeof(*brd), GFP_KERNEL);
if (!brd)
@@ -410,14 +411,19 @@ static int brd_alloc(int i)
/* Tell the block layer that this is not a rotational device */
blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
-   add_disk(disk);
+   err = add_disk(disk);
+   if (err)
+   goto out_cleanup_disk;
+
list_add_tail(>brd_list, _devices);
 
return 0;
 
+out_cleanup_disk:
+   blk_cleanup_disk(disk);
 out_free_dev:
kfree(brd);
-   return -ENOMEM;
+   return err;
 }
 
 static void brd_probe(dev_t dev)
-- 
2.30.2




[PATCH v2 06/10] nvdimm/btt: add error handling support for add_disk()

2021-09-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/btt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
index 23ee8c005db5..57b921c5fbb5 100644
--- a/drivers/nvdimm/btt.c
+++ b/drivers/nvdimm/btt.c
@@ -1542,7 +1542,9 @@ static int btt_blk_init(struct btt *btt)
}
 
set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
-   device_add_disk(>nd_btt->dev, btt->btt_disk, NULL);
+   rc = device_add_disk(>nd_btt->dev, btt->btt_disk, NULL);
+   if (rc)
+   goto out_cleanup_disk;
 
btt->nd_btt->size = btt->nlba * (u64)btt->sector_size;
nvdimm_check_and_set_ro(btt->btt_disk);
-- 
2.30.2




[PATCH v2 10/10] zram: add error handling support for add_disk()

2021-09-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Signed-off-by: Luis Chamberlain 
---
 drivers/block/zram/zram_drv.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index f61910c65f0f..59086e178fbd 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -1953,7 +1953,9 @@ static int zram_add(void)
blk_queue_max_write_zeroes_sectors(zram->disk->queue, UINT_MAX);
 
blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, zram->disk->queue);
-   device_add_disk(NULL, zram->disk, zram_disk_attr_groups);
+   ret = device_add_disk(NULL, zram->disk, zram_disk_attr_groups);
+   if (ret)
+   goto out_cleanup_disk;
 
strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
 
@@ -1961,6 +1963,8 @@ static int zram_add(void)
pr_info("Added device: %s\n", zram->disk->disk_name);
return device_id;
 
+out_cleanup_disk:
+   blk_cleanup_disk(zram->disk);
 out_free_idr:
idr_remove(_index_idr, device_id);
 out_free_dev:
-- 
2.30.2




[PATCH v2 00/10] block: second batch of add_disk() error handling conversions

2021-09-27 Thread Luis Chamberlain
This is the second series of driver conversions for add_disk()
error handling. You can find this set and the rest of the 7th set of
driver conversions on my 20210927-for-axboe-add-disk-error-handling
branch [0].

Changes on this v2 since the last first version of this
patch series:

  - rebased onto linux-next tag 20210927
  - nvme-multipath: used test_and_set_bit() as suggested by Keith Busch,
 
and justified this in the code with a comment as this race was not
obvious
  - Added reviewed-by / Acked-by tags where one was provided 

[0] 
https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux-next.git/log/?h=20210927-for-axboe-add-disk-error-handling

Luis Chamberlain (10):
  block/brd: add error handling support for add_disk()
  bcache: add error handling support for add_disk()
  nvme-multipath: add error handling support for add_disk()
  nvdimm/btt: do not call del_gendisk() if not needed
  nvdimm/btt: use goto error labels on btt_blk_init()
  nvdimm/btt: add error handling support for add_disk()
  nvdimm/blk: avoid calling del_gendisk() on early failures
  nvdimm/blk: add error handling support for add_disk()
  xen-blkfront: add error handling support for add_disk()
  zram: add error handling support for add_disk()

 drivers/block/brd.c   | 10 --
 drivers/block/xen-blkfront.c  |  8 +++-
 drivers/block/zram/zram_drv.c |  6 +-
 drivers/md/bcache/super.c | 17 -
 drivers/nvdimm/blk.c  | 21 +++--
 drivers/nvdimm/btt.c  | 24 +++-
 drivers/nvme/host/multipath.c | 13 +++--
 7 files changed, 73 insertions(+), 26 deletions(-)

-- 
2.30.2




[PATCH v2 09/10] xen-blkfront: add error handling support for add_disk()

2021-09-27 Thread Luis Chamberlain
We never checked for errors on device_add_disk() as this function
returned void. Now that this is fixed, use the shiny new error
handling. The function xlvbd_alloc_gendisk() typically does the
unwinding on error on allocating the disk and creating the tag,
but since all that error handling was stuffed inside
xlvbd_alloc_gendisk() we must repeat the tag free'ing as well.

We set the info->rq to NULL to ensure blkif_free() doesn't crash
on blk_mq_stop_hw_queues() on device_add_disk() error as the queue
will be long gone by then.

Signed-off-by: Luis Chamberlain 
---
 drivers/block/xen-blkfront.c | 8 +++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c
index 72902104f111..86440b051766 100644
--- a/drivers/block/xen-blkfront.c
+++ b/drivers/block/xen-blkfront.c
@@ -2385,7 +2385,13 @@ static void blkfront_connect(struct blkfront_info *info)
for_each_rinfo(info, rinfo, i)
kick_pending_request_queues(rinfo);
 
-   device_add_disk(>xbdev->dev, info->gd, NULL);
+   err = device_add_disk(>xbdev->dev, info->gd, NULL);
+   if (err) {
+   blk_cleanup_disk(info->gd);
+   blk_mq_free_tag_set(>tag_set);
+   info->rq = NULL;
+   goto fail;
+   }
 
info->is_ready = 1;
return;
-- 
2.30.2




[PATCH v2 03/10] nvme-multipath: add error handling support for add_disk()

2021-09-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Since we now can tell for sure when a disk was added, move
setting the bit NVME_NSHEAD_DISK_LIVE only when we did
add the disk successfully.

Nothing to do here as the cleanup is done elsewhere. We take
care and use test_and_set_bit() because it is protects against
two nvme paths simultaneously calling device_add_disk() on the
same namespace head.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvme/host/multipath.c | 13 +++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index e8ccdd398f78..35cace4f3f5f 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -496,13 +496,22 @@ int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct 
nvme_ns_head *head)
 static void nvme_mpath_set_live(struct nvme_ns *ns)
 {
struct nvme_ns_head *head = ns->head;
+   int rc;
 
if (!head->disk)
return;
 
+   /*
+* test_and_set_bit() is used because it is protecting against two nvme
+* paths simultaneously calling device_add_disk() on the same namespace
+* head.
+*/
if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, >flags)) {
-   device_add_disk(>subsys->dev, head->disk,
-   nvme_ns_id_attr_groups);
+   rc = device_add_disk(>subsys->dev, head->disk,
+nvme_ns_id_attr_groups);
+   if (rc)
+   return;
+   set_bit(NVME_NSHEAD_DISK_LIVE, >flags);
nvme_add_ns_head_cdev(head);
}
 
-- 
2.30.2




[PATCH v2 04/10] nvdimm/btt: do not call del_gendisk() if not needed

2021-09-27 Thread Luis Chamberlain
We know we don't need del_gendisk() if we haven't added
the disk, so just skip it. This should fix a bug on older
kernels, as del_gendisk() became able to deal with
disks not added only recently, after the patch titled
"block: add flag for add_disk() completion notation".

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/btt.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
index 52de60b7adee..29cc7325e890 100644
--- a/drivers/nvdimm/btt.c
+++ b/drivers/nvdimm/btt.c
@@ -1538,7 +1538,6 @@ static int btt_blk_init(struct btt *btt)
int rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
 
if (rc) {
-   del_gendisk(btt->btt_disk);
blk_cleanup_disk(btt->btt_disk);
return rc;
}
-- 
2.30.2




[PATCH v2 07/10] nvdimm/blk: avoid calling del_gendisk() on early failures

2021-09-27 Thread Luis Chamberlain
If nd_integrity_init() fails we'd get del_gendisk() called,
but that's not correct as we should only call that if we're
done with device_add_disk(). Fix this by providing unwinding
prior to the devm call being registered and moving the devm
registration to the very end.

This should fix calling del_gendisk() if nd_integrity_init()
fails. I only spotted this issue through code inspection. It
does not fix any real world bug.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/blk.c | 17 -
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/nvdimm/blk.c b/drivers/nvdimm/blk.c
index 088d3dd6f6fa..591fa1f86f1e 100644
--- a/drivers/nvdimm/blk.c
+++ b/drivers/nvdimm/blk.c
@@ -240,6 +240,7 @@ static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
resource_size_t available_disk_size;
struct gendisk *disk;
u64 internal_nlba;
+   int rc;
 
internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
@@ -256,20 +257,26 @@ static int nsblk_attach_disk(struct nd_namespace_blk 
*nsblk)
blk_queue_logical_block_size(disk->queue, nsblk_sector_size(nsblk));
blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
 
-   if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
-   return -ENOMEM;
-
if (nsblk_meta_size(nsblk)) {
-   int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
+   rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
 
if (rc)
-   return rc;
+   goto out_before_devm_err;
}
 
set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
device_add_disk(dev, disk, NULL);
+
+   /* nd_blk_release_disk() is called if this fails */
+   if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
+   return -ENOMEM;
+
nvdimm_check_and_set_ro(disk);
return 0;
+
+out_before_devm_err:
+   blk_cleanup_disk(disk);
+   return rc;
 }
 
 static int nd_blk_probe(struct device *dev)
-- 
2.30.2




[PATCH v2 08/10] nvdimm/blk: add error handling support for add_disk()

2021-09-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Since nvdimm/blk uses devm we just need to move the devm
registration towards the end. And in hindsight, that seems
to also provide a fix given del_gendisk() should not be
called unless the disk was already added via add_disk().
The probably of that issue happening is low though, like
OOM while calling devm_add_action(), so the fix is minor.

We manually unwind in case of add_disk() failure prior
to the devm registration.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/blk.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/nvdimm/blk.c b/drivers/nvdimm/blk.c
index 591fa1f86f1e..9f1eb41404ac 100644
--- a/drivers/nvdimm/blk.c
+++ b/drivers/nvdimm/blk.c
@@ -265,7 +265,9 @@ static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
}
 
set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
-   device_add_disk(dev, disk, NULL);
+   rc = device_add_disk(dev, disk, NULL);
+   if (rc)
+   goto out_before_devm_err;
 
/* nd_blk_release_disk() is called if this fails */
if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
-- 
2.30.2




[PATCH v2 02/10] bcache: add error handling support for add_disk()

2021-09-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

This driver doesn't do any unwinding with blk_cleanup_disk()
even on errors after add_disk() and so we follow that
tradition.

Acked-by: Coly Li 
Signed-off-by: Luis Chamberlain 
---
 drivers/md/bcache/super.c | 17 -
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index f2874c77ff79..f0c32cdd6594 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1082,7 +1082,9 @@ int bch_cached_dev_run(struct cached_dev *dc)
closure_sync();
}
 
-   add_disk(d->disk);
+   ret = add_disk(d->disk);
+   if (ret)
+   goto out;
bd_link_disk_holder(dc->bdev, dc->disk.disk);
/*
 * won't show up in the uevent file, use udevadm monitor -e instead
@@ -1534,10 +1536,11 @@ static void flash_dev_flush(struct closure *cl)
 
 static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
 {
+   int err = -ENOMEM;
struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
  GFP_KERNEL);
if (!d)
-   return -ENOMEM;
+   goto err_ret;
 
closure_init(>cl, NULL);
set_closure_fn(>cl, flash_dev_flush, system_wq);
@@ -1551,9 +1554,12 @@ static int flash_dev_run(struct cache_set *c, struct 
uuid_entry *u)
bcache_device_attach(d, c, u - c->uuids);
bch_sectors_dirty_init(d);
bch_flash_dev_request_init(d);
-   add_disk(d->disk);
+   err = add_disk(d->disk);
+   if (err)
+   goto err;
 
-   if (kobject_add(>kobj, _to_dev(d->disk)->kobj, "bcache"))
+   err = kobject_add(>kobj, _to_dev(d->disk)->kobj, "bcache");
+   if (err)
goto err;
 
bcache_device_link(d, c, "volume");
@@ -1567,7 +1573,8 @@ static int flash_dev_run(struct cache_set *c, struct 
uuid_entry *u)
return 0;
 err:
kobject_put(>kobj);
-   return -ENOMEM;
+err_ret:
+   return err;
 }
 
 static int flash_devs_run(struct cache_set *c)
-- 
2.30.2




[PATCH v2 05/10] nvdimm/btt: use goto error labels on btt_blk_init()

2021-09-27 Thread Luis Chamberlain
This will make it easier to share common error paths.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/btt.c | 19 ---
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
index 29cc7325e890..23ee8c005db5 100644
--- a/drivers/nvdimm/btt.c
+++ b/drivers/nvdimm/btt.c
@@ -1520,10 +1520,11 @@ static int btt_blk_init(struct btt *btt)
 {
struct nd_btt *nd_btt = btt->nd_btt;
struct nd_namespace_common *ndns = nd_btt->ndns;
+   int rc = -ENOMEM;
 
btt->btt_disk = blk_alloc_disk(NUMA_NO_NODE);
if (!btt->btt_disk)
-   return -ENOMEM;
+   goto out;
 
nvdimm_namespace_disk_name(ndns, btt->btt_disk->disk_name);
btt->btt_disk->first_minor = 0;
@@ -1535,19 +1536,23 @@ static int btt_blk_init(struct btt *btt)
blk_queue_flag_set(QUEUE_FLAG_NONROT, btt->btt_disk->queue);
 
if (btt_meta_size(btt)) {
-   int rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
-
-   if (rc) {
-   blk_cleanup_disk(btt->btt_disk);
-   return rc;
-   }
+   rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
+   if (rc)
+   goto out_cleanup_disk;
}
+
set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
device_add_disk(>nd_btt->dev, btt->btt_disk, NULL);
+
btt->nd_btt->size = btt->nlba * (u64)btt->sector_size;
nvdimm_check_and_set_ro(btt->btt_disk);
 
return 0;
+
+out_cleanup_disk:
+   blk_cleanup_disk(btt->btt_disk);
+out:
+   return rc;
 }
 
 static void btt_blk_cleanup(struct btt *btt)
-- 
2.30.2




Re: [PATCH 03/10] nvme-multipath: add error handling support for add_disk()

2021-08-30 Thread Luis Chamberlain
On Fri, Aug 27, 2021 at 01:29:32PM -0700, Keith Busch wrote:
> On Fri, Aug 27, 2021 at 12:18:02PM -0700, Luis Chamberlain wrote:
> > @@ -479,13 +479,17 @@ int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, 
> > struct nvme_ns_head *head)
> >  static void nvme_mpath_set_live(struct nvme_ns *ns)
> >  {
> > struct nvme_ns_head *head = ns->head;
> > +   int rc;
> >  
> > if (!head->disk)
> > return;
> >  
> > -   if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, >flags)) {
> > -   device_add_disk(>subsys->dev, head->disk,
> > -   nvme_ns_id_attr_groups);
> > +   if (!test_bit(NVME_NSHEAD_DISK_LIVE, >flags)) {
> 
> This should still be test_and_set_bit() because it is protecting against
> two nvme paths simultaneously calling device_add_disk() on the same
> namespace head.

Interesting, I'll add a comment as well, as this was not clear with the drive
by effort.

  Luis



[PATCH 02/10] bcache: add error handling support for add_disk()

2021-08-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

This driver doesn't do any unwinding with blk_cleanup_disk()
even on errors after add_disk() and so we follow that
tradition.

Signed-off-by: Luis Chamberlain 
---
 drivers/md/bcache/super.c | 17 -
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index f2874c77ff79..f0c32cdd6594 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1082,7 +1082,9 @@ int bch_cached_dev_run(struct cached_dev *dc)
closure_sync();
}
 
-   add_disk(d->disk);
+   ret = add_disk(d->disk);
+   if (ret)
+   goto out;
bd_link_disk_holder(dc->bdev, dc->disk.disk);
/*
 * won't show up in the uevent file, use udevadm monitor -e instead
@@ -1534,10 +1536,11 @@ static void flash_dev_flush(struct closure *cl)
 
 static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
 {
+   int err = -ENOMEM;
struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
  GFP_KERNEL);
if (!d)
-   return -ENOMEM;
+   goto err_ret;
 
closure_init(>cl, NULL);
set_closure_fn(>cl, flash_dev_flush, system_wq);
@@ -1551,9 +1554,12 @@ static int flash_dev_run(struct cache_set *c, struct 
uuid_entry *u)
bcache_device_attach(d, c, u - c->uuids);
bch_sectors_dirty_init(d);
bch_flash_dev_request_init(d);
-   add_disk(d->disk);
+   err = add_disk(d->disk);
+   if (err)
+   goto err;
 
-   if (kobject_add(>kobj, _to_dev(d->disk)->kobj, "bcache"))
+   err = kobject_add(>kobj, _to_dev(d->disk)->kobj, "bcache");
+   if (err)
goto err;
 
bcache_device_link(d, c, "volume");
@@ -1567,7 +1573,8 @@ static int flash_dev_run(struct cache_set *c, struct 
uuid_entry *u)
return 0;
 err:
kobject_put(>kobj);
-   return -ENOMEM;
+err_ret:
+   return err;
 }
 
 static int flash_devs_run(struct cache_set *c)
-- 
2.30.2




[PATCH 09/10] xen-blkfront: add error handling support for add_disk()

2021-08-27 Thread Luis Chamberlain
We never checked for errors on device_add_disk() as this function
returned void. Now that this is fixed, use the shiny new error
handling. The function xlvbd_alloc_gendisk() typically does the
unwinding on error on allocating the disk and creating the tag,
but since all that error handling was stuffed inside
xlvbd_alloc_gendisk() we must repeat the tag free'ing as well.

We set the info->rq to NULL to ensure blkif_free() doesn't crash
on blk_mq_stop_hw_queues() on device_add_disk() error as the queue
will be long gone by then.

Signed-off-by: Luis Chamberlain 
---
 drivers/block/xen-blkfront.c | 8 +++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c
index 715bfa8aca7f..9fe28af5f6d9 100644
--- a/drivers/block/xen-blkfront.c
+++ b/drivers/block/xen-blkfront.c
@@ -2343,7 +2343,13 @@ static void blkfront_connect(struct blkfront_info *info)
for_each_rinfo(info, rinfo, i)
kick_pending_request_queues(rinfo);
 
-   device_add_disk(>xbdev->dev, info->gd, NULL);
+   err = device_add_disk(>xbdev->dev, info->gd, NULL);
+   if (err) {
+   blk_cleanup_disk(info->gd);
+   blk_mq_free_tag_set(>tag_set);
+   info->rq = NULL;
+   goto fail;
+   }
 
info->is_ready = 1;
return;
-- 
2.30.2




[PATCH 01/10] block/brd: add error handling support for add_disk()

2021-08-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Signed-off-by: Luis Chamberlain 
---
 drivers/block/brd.c | 10 --
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index 58ec167aa018..c2bf4946f4e3 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -372,6 +372,7 @@ static int brd_alloc(int i)
struct brd_device *brd;
struct gendisk *disk;
char buf[DISK_NAME_LEN];
+   int err = -ENOMEM;
 
brd = kzalloc(sizeof(*brd), GFP_KERNEL);
if (!brd)
@@ -410,14 +411,19 @@ static int brd_alloc(int i)
/* Tell the block layer that this is not a rotational device */
blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
-   add_disk(disk);
+   err = add_disk(disk);
+   if (err)
+   goto out_cleanup_disk;
+
list_add_tail(>brd_list, _devices);
 
return 0;
 
+out_cleanup_disk:
+   blk_cleanup_disk(disk);
 out_free_dev:
kfree(brd);
-   return -ENOMEM;
+   return err;
 }
 
 static void brd_probe(dev_t dev)
-- 
2.30.2




[PATCH 00/10] block: first batch of add_disk() error handling conversions

2021-08-27 Thread Luis Chamberlain
This is my second batch of driver conversions to use add_disk() error
handling. Please review and let me know if you spot any issues. This is
part of a larger effort to covert all drivers over to use the new
add_disk() error handling. The entire work can be found on my branch
dedicated for this work [0]

[0] 
https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux-next.git/log/?h=20210827-for-axboe-add-disk-error-handling-next-2nd

Luis Chamberlain (10):
  block/brd: add error handling support for add_disk()
  bcache: add error handling support for add_disk()
  nvme-multipath: add error handling support for add_disk()
  nvdimm/btt: do not call del_gendisk() if not needed
  nvdimm/btt: use goto error labels on btt_blk_init()
  nvdimm/btt: add error handling support for add_disk()
  nvdimm/blk: avoid calling del_gendisk() on early failures
  nvdimm/blk: add error handling support for add_disk()
  xen-blkfront: add error handling support for add_disk()
  zram: add error handling support for add_disk()

 drivers/block/brd.c   | 10 --
 drivers/block/xen-blkfront.c  |  8 +++-
 drivers/block/zram/zram_drv.c |  6 +-
 drivers/md/bcache/super.c | 17 -
 drivers/nvdimm/blk.c  | 21 +++--
 drivers/nvdimm/btt.c  | 24 +++-
 drivers/nvme/host/multipath.c | 10 +++---
 7 files changed, 69 insertions(+), 27 deletions(-)

-- 
2.30.2




[PATCH 04/10] nvdimm/btt: do not call del_gendisk() if not needed

2021-08-27 Thread Luis Chamberlain
We know we don't need del_gendisk() if we haven't added
the disk, so just skip it. This should fix a bug on older
kernels, as del_gendisk() became able to deal with
disks not added only recently, after the patch titled
"block: add flag for add_disk() completion notation".

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/btt.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
index 92dec4952297..3fd1bdb9fc05 100644
--- a/drivers/nvdimm/btt.c
+++ b/drivers/nvdimm/btt.c
@@ -1538,7 +1538,6 @@ static int btt_blk_init(struct btt *btt)
int rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
 
if (rc) {
-   del_gendisk(btt->btt_disk);
blk_cleanup_disk(btt->btt_disk);
return rc;
}
-- 
2.30.2




Re: [PATCH 00/10] block: first batch of add_disk() error handling conversions

2021-08-27 Thread Luis Chamberlain


Botched the subject. Sorry. this is the *second* batch :)

  Luis



[PATCH 10/10] zram: add error handling support for add_disk()

2021-08-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Signed-off-by: Luis Chamberlain 
---
 drivers/block/zram/zram_drv.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index fcaf2750f68f..d5b343c2bc96 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -1953,7 +1953,9 @@ static int zram_add(void)
blk_queue_max_write_zeroes_sectors(zram->disk->queue, UINT_MAX);
 
blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, zram->disk->queue);
-   device_add_disk(NULL, zram->disk, zram_disk_attr_groups);
+   ret = device_add_disk(NULL, zram->disk, zram_disk_attr_groups);
+   if (ret)
+   goto out_cleanup_disk;
 
strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
 
@@ -1961,6 +1963,8 @@ static int zram_add(void)
pr_info("Added device: %s\n", zram->disk->disk_name);
return device_id;
 
+out_cleanup_disk:
+   blk_cleanup_disk(zram->disk);
 out_free_idr:
idr_remove(_index_idr, device_id);
 out_free_dev:
-- 
2.30.2




[PATCH 03/10] nvme-multipath: add error handling support for add_disk()

2021-08-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Since we now can tell for sure when a disk was added, move
setting the bit NVME_NSHEAD_DISK_LIVE only when we did
add the disk successfully.

Nothing to do here as the cleanup is done elsewhere.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvme/host/multipath.c | 10 +++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 37ce3e8b1db2..f95643629fdb 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -479,13 +479,17 @@ int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct 
nvme_ns_head *head)
 static void nvme_mpath_set_live(struct nvme_ns *ns)
 {
struct nvme_ns_head *head = ns->head;
+   int rc;
 
if (!head->disk)
return;
 
-   if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, >flags)) {
-   device_add_disk(>subsys->dev, head->disk,
-   nvme_ns_id_attr_groups);
+   if (!test_bit(NVME_NSHEAD_DISK_LIVE, >flags)) {
+   rc = device_add_disk(>subsys->dev, head->disk,
+nvme_ns_id_attr_groups);
+   if (rc)
+   return;
+   set_bit(NVME_NSHEAD_DISK_LIVE, >flags);
nvme_add_ns_head_cdev(head);
}
 
-- 
2.30.2




[PATCH 05/10] nvdimm/btt: use goto error labels on btt_blk_init()

2021-08-27 Thread Luis Chamberlain
This will make it easier to share common error paths.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/btt.c | 19 ---
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
index 3fd1bdb9fc05..275704d80109 100644
--- a/drivers/nvdimm/btt.c
+++ b/drivers/nvdimm/btt.c
@@ -1520,10 +1520,11 @@ static int btt_blk_init(struct btt *btt)
 {
struct nd_btt *nd_btt = btt->nd_btt;
struct nd_namespace_common *ndns = nd_btt->ndns;
+   int rc = -ENOMEM;
 
btt->btt_disk = blk_alloc_disk(NUMA_NO_NODE);
if (!btt->btt_disk)
-   return -ENOMEM;
+   goto out;
 
nvdimm_namespace_disk_name(ndns, btt->btt_disk->disk_name);
btt->btt_disk->first_minor = 0;
@@ -1535,19 +1536,23 @@ static int btt_blk_init(struct btt *btt)
blk_queue_flag_set(QUEUE_FLAG_NONROT, btt->btt_disk->queue);
 
if (btt_meta_size(btt)) {
-   int rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
-
-   if (rc) {
-   blk_cleanup_disk(btt->btt_disk);
-   return rc;
-   }
+   rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
+   if (rc)
+   goto out_cleanup_disk;
}
+
set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
device_add_disk(>nd_btt->dev, btt->btt_disk, NULL);
+
btt->nd_btt->size = btt->nlba * (u64)btt->sector_size;
nvdimm_check_and_set_ro(btt->btt_disk);
 
return 0;
+
+out_cleanup_disk:
+   blk_cleanup_disk(btt->btt_disk);
+out:
+   return rc;
 }
 
 static void btt_blk_cleanup(struct btt *btt)
-- 
2.30.2




[PATCH 07/10] nvdimm/blk: avoid calling del_gendisk() on early failures

2021-08-27 Thread Luis Chamberlain
If nd_integrity_init() fails we'd get del_gendisk() called,
but that's not correct as we should only call that if we're
done with device_add_disk(). Fix this by providing unwinding
prior to the devm call being registered and moving the devm
registration to the very end.

This should fix calling del_gendisk() if nd_integrity_init()
fails. I only spotted this issue through code inspection. It
does not fix any real world bug.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/blk.c | 17 -
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/nvdimm/blk.c b/drivers/nvdimm/blk.c
index 088d3dd6f6fa..591fa1f86f1e 100644
--- a/drivers/nvdimm/blk.c
+++ b/drivers/nvdimm/blk.c
@@ -240,6 +240,7 @@ static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
resource_size_t available_disk_size;
struct gendisk *disk;
u64 internal_nlba;
+   int rc;
 
internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
@@ -256,20 +257,26 @@ static int nsblk_attach_disk(struct nd_namespace_blk 
*nsblk)
blk_queue_logical_block_size(disk->queue, nsblk_sector_size(nsblk));
blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
 
-   if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
-   return -ENOMEM;
-
if (nsblk_meta_size(nsblk)) {
-   int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
+   rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
 
if (rc)
-   return rc;
+   goto out_before_devm_err;
}
 
set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
device_add_disk(dev, disk, NULL);
+
+   /* nd_blk_release_disk() is called if this fails */
+   if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
+   return -ENOMEM;
+
nvdimm_check_and_set_ro(disk);
return 0;
+
+out_before_devm_err:
+   blk_cleanup_disk(disk);
+   return rc;
 }
 
 static int nd_blk_probe(struct device *dev)
-- 
2.30.2




[PATCH 06/10] nvdimm/btt: add error handling support for add_disk()

2021-08-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/btt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
index 275704d80109..abdac4b7769f 100644
--- a/drivers/nvdimm/btt.c
+++ b/drivers/nvdimm/btt.c
@@ -1542,7 +1542,9 @@ static int btt_blk_init(struct btt *btt)
}
 
set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
-   device_add_disk(>nd_btt->dev, btt->btt_disk, NULL);
+   rc = device_add_disk(>nd_btt->dev, btt->btt_disk, NULL);
+   if (rc)
+   goto out_cleanup_disk;
 
btt->nd_btt->size = btt->nlba * (u64)btt->sector_size;
nvdimm_check_and_set_ro(btt->btt_disk);
-- 
2.30.2




[PATCH 08/10] nvdimm/blk: add error handling support for add_disk()

2021-08-27 Thread Luis Chamberlain
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.

Since nvdimm/blk uses devm we just need to move the devm
registration towards the end. And in hindsight, that seems
to also provide a fix given del_gendisk() should not be
called unless the disk was already added via add_disk().
The probably of that issue happening is low though, like
OOM while calling devm_add_action(), so the fix is minor.

We manually unwind in case of add_disk() failure prior
to the devm registration.

Signed-off-by: Luis Chamberlain 
---
 drivers/nvdimm/blk.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/nvdimm/blk.c b/drivers/nvdimm/blk.c
index 591fa1f86f1e..9f1eb41404ac 100644
--- a/drivers/nvdimm/blk.c
+++ b/drivers/nvdimm/blk.c
@@ -265,7 +265,9 @@ static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
}
 
set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
-   device_add_disk(dev, disk, NULL);
+   rc = device_add_disk(dev, disk, NULL);
+   if (rc)
+   goto out_before_devm_err;
 
/* nd_blk_release_disk() is called if this fails */
if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
-- 
2.30.2




Re: [PATCH v2 1/1] kernel.h: Split out panic and oops helpers

2021-04-09 Thread Luis Chamberlain
On Fri, Apr 09, 2021 at 01:02:50PM +0300, Andy Shevchenko wrote:
> kernel.h is being used as a dump for all kinds of stuff for a long time.
> Here is the attempt to start cleaning it up by splitting out panic and
> oops helpers.
> 
> There are several purposes of doing this:
> - dropping dependency in bug.h
> - dropping a loop by moving out panic_notifier.h
> - unload kernel.h from something which has its own domain
> 
> At the same time convert users tree-wide to use new headers, although
> for the time being include new header back to kernel.h to avoid twisted
> indirected includes for existing users.
> 
> Signed-off-by: Andy Shevchenko 
> Reviewed-by: Bjorn Andersson 
> Acked-by: Mike Rapoport 
> Acked-by: Corey Minyard 
> Acked-by: Christian Brauner 
> Acked-by: Arnd Bergmann 
> Acked-by: Kees Cook 
> Acked-by: Wei Liu 
> Acked-by: Rasmus Villemoes 
> Signed-off-by: Andrew Morton 

Acked-by: Luis Chamberlain 

  Luis