From 35667b9983a2a89e504377e89388982dc398773d Mon Sep 17 00:00:00 2001 From: Johannes Thumshirn Date: Wed, 8 Jul 2015 17:15:34 +0200 Subject: mtd: Destroy mtd_idr on module_exit Destroy mtd_idr on module_exit, reclaiming the allocated memory. This was detected by the following semantic patch (written by Luis Rodriguez ) @ defines_module_init @ declarer name module_init, module_exit; declarer name DEFINE_IDR; identifier init; @@ module_init(init); @ defines_module_exit @ identifier exit; @@ module_exit(exit); @ declares_idr depends on defines_module_init && defines_module_exit @ identifier idr; @@ DEFINE_IDR(idr); @ on_exit_calls_destroy depends on declares_idr && defines_module_exit @ identifier declares_idr.idr, defines_module_exit.exit; @@ exit(void) { ... idr_destroy(&idr); ... } @ missing_module_idr_destroy depends on declares_idr && defines_module_exit && !on_exit_calls_destroy @ identifier declares_idr.idr, defines_module_exit.exit; @@ exit(void) { ... +idr_destroy(&idr); } Signed-off-by: Johannes Thumshirn Signed-off-by: Brian Norris --- drivers/mtd/mtdcore.c | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers/mtd/mtdcore.c') diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 8bbbb751bf45..8598e3b783fe 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -1301,6 +1301,7 @@ static void __exit cleanup_mtd(void) remove_proc_entry("mtd", NULL); class_unregister(&mtd_class); bdi_destroy(&mtd_bdi); + idr_destroy(&mtd_idr); } module_init(init_mtd); -- cgit v1.2.3 From 260e89a6e0d6dcaccd484cf13a69285c3d22268f Mon Sep 17 00:00:00 2001 From: Frans Klaver Date: Wed, 10 Jun 2015 22:38:15 +0200 Subject: mtd: core: tone down suggestion that dev.parent should be set add_mtd_device() has a comment suggesting that the caller should have set dev.parent. This is required to have the parent device symlink show up in sysfs, but not for proper operation of the mtd device itself. Currently we have five drivers registering mtd devices during module initialization, so they don't actually provide a parent device to link to. That means we cannot WARN_ON() here, as it would trigger false positives. Make the comment a bit less firm in its assertion that dev.parent should be set. Signed-off-by: Frans Klaver Signed-off-by: Brian Norris --- drivers/mtd/mtdcore.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/mtd/mtdcore.c') diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 8598e3b783fe..ac280eef5ae8 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -430,7 +430,7 @@ int add_mtd_device(struct mtd_info *mtd) } /* Caller should have set dev.parent to match the - * physical device. + * physical device, if appropriate. */ mtd->dev.type = &mtd_devtype; mtd->dev.class = &mtd_class; -- cgit v1.2.3 From 807f16d4db956b364ae852a63ad7d79460838866 Mon Sep 17 00:00:00 2001 From: Frans Klaver Date: Wed, 10 Jun 2015 22:38:16 +0200 Subject: mtd: core: set some defaults when dev.parent is set If a parent device is set, add_mtd_device() has enough knowledge to fill in some sane default values for the module name and owner. Do so if they aren't already set. Signed-off-by: Frans Klaver Signed-off-by: Brian Norris --- drivers/mtd/mtdcore.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'drivers/mtd/mtdcore.c') diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index ac280eef5ae8..bbcba0dbd5c3 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -418,6 +418,15 @@ int add_mtd_device(struct mtd_info *mtd) mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; + if (mtd->dev.parent) { + if (!mtd->owner && mtd->dev.parent->driver) + mtd->owner = mtd->dev.parent->driver->owner; + if (!mtd->name) + mtd->name = dev_name(mtd->dev.parent); + } else { + pr_debug("mtd device won't show a device symlink in sysfs\n"); + } + /* Some chips always power up locked. Unlock them now */ if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { error = mtd_unlock(mtd, 0, mtd->size); -- cgit v1.2.3 From 3e00ed0e984bbec47f5e531bad3cf36885aa5d83 Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Mon, 1 Jun 2015 16:17:19 -0700 Subject: mtd: fixup corner case error handling in mtd_device_parse_register() Since commit 3efe41be224c ("mtd: implement common reboot notifier boilerplate"), we might try to register a reboot notifier for an MTD that failed to register. Let's avoid this by making the error path clearer. Signed-off-by: Brian Norris Reviewed-by: Richard Weinberger --- drivers/mtd/mtdcore.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'drivers/mtd/mtdcore.c') diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index bbcba0dbd5c3..a2e76ac9f1dc 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -588,9 +588,15 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, else ret = nr_parts; } + /* Didn't come up with either parsed OR fallback partitions */ + if (ret < 0) { + pr_info("mtd: failed to find partitions\n"); + goto out; + } - if (ret >= 0) - ret = mtd_add_device_partitions(mtd, real_parts, ret); + ret = mtd_add_device_partitions(mtd, real_parts, ret); + if (ret) + goto out; /* * FIXME: some drivers unfortunately call this function more than once. @@ -605,6 +611,7 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, register_reboot_notifier(&mtd->reboot_notifier); } +out: kfree(real_parts); return ret; } -- cgit v1.2.3 From be0dbff8b46d69bd738f63b4fe0cf64417f776b0 Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Mon, 1 Jun 2015 16:17:20 -0700 Subject: mtd: warn when registering the same master many times When CONFIG_MTD_PARTITIONED_MASTER=y, it is fatal to call mtd_device_parse_register() twice on the same MTD, as we try to register the same device/kobject multipile times. When CONFIG_MTD_PARTITIONED_MASTER=n, calling mtd_device_parse_register() is more of just a nuisance, as we can mostly navigate around any conflicting actions. But anyway, doing so is a Bad Thing (TM), and we should complain loudly for any drivers that try to do this. Signed-off-by: Brian Norris Reviewed-by: Richard Weinberger --- drivers/mtd/mtdcore.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'drivers/mtd/mtdcore.c') diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index a2e76ac9f1dc..41dc501cb49a 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -387,6 +387,14 @@ int add_mtd_device(struct mtd_info *mtd) struct mtd_notifier *not; int i, error; + /* + * May occur, for instance, on buggy drivers which call + * mtd_device_parse_register() multiple times on the same master MTD, + * especially with CONFIG_MTD_PARTITIONED_MASTER=y. + */ + if (WARN_ONCE(mtd->backing_dev_info, "MTD already registered\n")) + return -EEXIST; + mtd->backing_dev_info = &mtd_bdi; BUG_ON(mtd->writesize == 0); @@ -606,6 +614,7 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, * does cause problems with parse_mtd_partitions() above (e.g., * cmdlineparts will register partitions more than once). */ + WARN_ONCE(mtd->reboot_notifier.notifier_call, "MTD already registered\n"); if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; register_reboot_notifier(&mtd->reboot_notifier); -- cgit v1.2.3 From 5a2415b0769233194f20d3906c3ffc6a2033317c Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Sun, 11 Oct 2015 13:03:47 -0700 Subject: mtd: mtdpart: Do not fail mtd probe when parsing partitions fails Due to wrong assumption in ofpart ofpart fails on Exynos on SPI chips with no partitions because the subnode containing controller data confuses the ofpart parser. Thus compiling in ofpart support automatically fails probing any SPI NOR flash without partitions on Exynos. Compiling in a partitioning scheme should not cause probe of otherwise valid device to fail. Instead, let's do the following: * try parsers until one succeeds * if no parser succeeds, report the first error we saw * even in the failure case, allow MTD to probe, with fallback partitions or no partitions at all -- the master device will still be registered Issue report and comments initially by Michal Suchanek. Reported-by: Michal Suchanek Signed-off-by: Brian Norris --- drivers/mtd/mtdcore.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'drivers/mtd/mtdcore.c') diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 41dc501cb49a..b1eea48c501d 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -598,8 +598,10 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, } /* Didn't come up with either parsed OR fallback partitions */ if (ret < 0) { - pr_info("mtd: failed to find partitions\n"); - goto out; + pr_info("mtd: failed to find partitions; one or more parsers reports errors (%d)\n", + ret); + /* Don't abort on errors; we can still use unpartitioned MTD */ + ret = 0; } ret = mtd_add_device_partitions(mtd, real_parts, ret); -- cgit v1.2.3 From f8479dd6a03cfdc3b14e742045bfd8748cd86bd7 Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Tue, 3 Nov 2015 17:01:53 -0800 Subject: mtd: don't WARN about overloaded users of mtd->reboot_notifier.notifier_call There are multiple types of users of mtd->reboot_notifier.notifier_call: (1) A while back, the cfi_cmdset_000{1,2} chip drivers implemented a reboot notifier to (on a best effort basis) attempt to reset their flash chips before rebooting. (2) More recently, we implemented a common _reboot() hook so that MTD drivers (particularly, NAND flash) could better halt I/O operations without having to reimplement the same notifier boilerplate. Currently, the WARN_ONCE() condition here was written to handle (2), but at the same time it mis-diagnosed case (1) as an already-registered MTD. Let's fix this by having the WARN_ONCE() condition better imitate the condition that immediately follows it. (Wow, I don't know how I missed that one.) (Side note: Unfortunately, we can't yet combine the reboot notifier code for (1) and (2) with a patch like [1], because some users of (1) also use mtdconcat, and so the mtd_info struct from cfi_cmdset_000{1,2} won't actually get registered with mtdcore, and therefore their reboot notifier won't get registered.) [1] http://patchwork.ozlabs.org/patch/417981/ Suggested-by: Guenter Roeck Signed-off-by: Brian Norris Cc: Jesper Nilsson Cc: linux-cris-kernel@axis.com Tested-by: Ezequiel Garcia Tested-by: Guenter Roeck Signed-off-by: Brian Norris --- drivers/mtd/mtdcore.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'drivers/mtd/mtdcore.c') diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index b1eea48c501d..a91cee90aef9 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -616,7 +616,8 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, * does cause problems with parse_mtd_partitions() above (e.g., * cmdlineparts will register partitions more than once). */ - WARN_ONCE(mtd->reboot_notifier.notifier_call, "MTD already registered\n"); + WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call, + "MTD already registered\n"); if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; register_reboot_notifier(&mtd->reboot_notifier); -- cgit v1.2.3