099f0d2879683367ea6b4277c022adb6e6c6f91b
[openwrt/openwrt.git] / target / linux / generic / backport-5.15 / 424-v6.4-0003-mtd-core-fix-error-path-for-nvmem-provider.patch
1 From e0489f6e221f5ddee6cb3bd51b992b790c5fa4b9 Mon Sep 17 00:00:00 2001
2 From: Michael Walle <michael@walle.cc>
3 Date: Wed, 8 Mar 2023 09:20:20 +0100
4 Subject: [PATCH] mtd: core: fix error path for nvmem provider
5
6 If mtd_otp_nvmem_add() fails, the partitions won't be removed
7 because there is simply no call to del_mtd_partitions().
8 Unfortunately, add_mtd_partitions() will print all partitions to
9 the kernel console. If mtd_otp_nvmem_add() returns -EPROBE_DEFER
10 this would print the partitions multiple times to the kernel
11 console. Instead move mtd_otp_nvmem_add() to the beginning of the
12 function.
13
14 Fixes: 4b361cfa8624 ("mtd: core: add OTP nvmem provider support")
15 Cc: stable@vger.kernel.org
16 Signed-off-by: Michael Walle <michael@walle.cc>
17 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
18 Link: https://lore.kernel.org/linux-mtd/20230308082021.870459-3-michael@walle.cc
19 ---
20 drivers/mtd/mtdcore.c | 13 ++++++++++---
21 1 file changed, 10 insertions(+), 3 deletions(-)
22
23 --- a/drivers/mtd/mtdcore.c
24 +++ b/drivers/mtd/mtdcore.c
25 @@ -1031,10 +1031,14 @@ int mtd_device_parse_register(struct mtd
26
27 mtd_set_dev_defaults(mtd);
28
29 + ret = mtd_otp_nvmem_add(mtd);
30 + if (ret)
31 + goto out;
32 +
33 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
34 ret = add_mtd_device(mtd);
35 if (ret)
36 - return ret;
37 + goto out;
38 }
39
40 /* Prefer parsed partitions over driver-provided fallback */
41 @@ -1069,9 +1073,12 @@ int mtd_device_parse_register(struct mtd
42 register_reboot_notifier(&mtd->reboot_notifier);
43 }
44
45 - ret = mtd_otp_nvmem_add(mtd);
46 -
47 out:
48 + if (ret) {
49 + nvmem_unregister(mtd->otp_user_nvmem);
50 + nvmem_unregister(mtd->otp_factory_nvmem);
51 + }
52 +
53 if (ret && device_is_registered(&mtd->dev))
54 del_mtd_device(mtd);
55