History

Revert "mtd: core: always create master device"

The idea behind this patch was to always let a "master" mtd device available to anchor runtime PM. Historically, there was no mtd device representing the whole storage as soon as partitions were coming into play. The introduction of CONFIG_MTD_PARTITIONED_MASTER allowed to keep this "master" device, but was not enabled by default to avoid breaking existing users (otherwise the mtd device numbering would be totally messed up with an off by 1, at least). The approach of adding an mtd_master class on top of partitioned mtd devices is breaking the mtd core in many creative ways, so better think again this approach and revert the faulty changes for now. This reverts commit 0aa7b390fc40a871267a2328bbbefca8b37ad307.

Miquel Raynal committed 1 year ago

Commit 635e1181 parent 19272b35 files · +51 −123Browse files

Unified · Split

5 files
  1. drivers/mtd/mtdchar.c +1 −1
  2. drivers/mtd/mtdcore.c +40 −112
  3. drivers/mtd/mtdcore.h +1 −1
  4. drivers/mtd/mtdpart.c +8 −8
  5. include/linux/mtd/partitions.h +1 −1
drivers/mtd/mtdchar.c+1−1
@@ -559,7 +559,7 @@ static int mtdchar_blkpg_ioctl(struct mtd_info *mtd, · 558 unmodified lines
559559 /* Sanitize user input */
560560 p.devname[BLKPG_DEVNAMELTH - 1] = '\0';
561561
562− return mtd_add_partition(mtd, p.devname, p.start, p.length, NULL);
562+ return mtd_add_partition(mtd, p.devname, p.start, p.length);
563563
564564 case BLKPG_DEL_PARTITION:
565565
drivers/mtd/mtdcore.c+40−112
@@ -68,13 +68,7 @@ static struct class mtd_class = { · 67 unmodified lines
6868 .pm = MTD_CLS_PM_OPS,
6969 };
7070
71−static struct class mtd_master_class = {
72− .name = "mtd_master",
73− .pm = MTD_CLS_PM_OPS,
74−};
75−
7671 static DEFINE_IDR(mtd_idr);
77−static DEFINE_IDR(mtd_master_idr);
7872
7973 /* These are exported solely for the purpose of mtd_blkdevs.c. You
8074 should not use them for _anything_ else */
@@ -89,9 +83,8 @@ EXPORT_SYMBOL_GPL(__mtd_next_device); · 8 unmodified lines
8983
9084 static LIST_HEAD(mtd_notifiers);
9185
92−#define MTD_MASTER_DEVS 255
86+
9387 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
94−static dev_t mtd_master_devt;
9588
9689 /* REVISIT once MTD uses the driver model better, whoever allocates
9790 * the mtd_info will probably want to use the release() hook...
@@ -111,17 +104,6 @@ static void mtd_release(struct device *dev) · 13 unmodified lines
111104 device_destroy(&mtd_class, index + 1);
112105 }
113106
114−static void mtd_master_release(struct device *dev)
115−{
116− struct mtd_info *mtd = dev_get_drvdata(dev);
117−
118− idr_remove(&mtd_master_idr, mtd->index);
119− of_node_put(mtd_get_of_node(mtd));
120−
121− if (mtd_is_partition(mtd))
122− release_mtd_partition(mtd);
123−}
124−
125107 static void mtd_device_release(struct kref *kref)
126108 {
127109 struct mtd_info *mtd = container_of(kref, struct mtd_info, refcnt);
@@ -385,11 +367,6 @@ static const struct device_type mtd_devtype = { · 257 unmodified lines
385367 .release = mtd_release,
386368 };
387369
388−static const struct device_type mtd_master_devtype = {
389− .name = "mtd_master",
390− .release = mtd_master_release,
391−};
392−
393370 static bool mtd_expert_analysis_mode;
394371
395372 #ifdef CONFIG_DEBUG_FS
@@ -657,13 +634,13 @@ exit_parent: · 261 unmodified lines
657634 /**
658635 * add_mtd_device - register an MTD device
659636 * @mtd: pointer to new MTD device info structure
660− * @partitioned: create partitioned device
661637 *
662638 * Add a device to the list of MTD devices present in the system, and
663639 * notify each currently active MTD 'user' of its arrival. Returns
664640 * zero on success or non-zero on failure.
665641 */
666−int add_mtd_device(struct mtd_info *mtd, bool partitioned)
642+
643+int add_mtd_device(struct mtd_info *mtd)
667644 {
668645 struct device_node *np = mtd_get_of_node(mtd);
669646 struct mtd_info *master = mtd_get_master(mtd);
@@ -710,17 +687,10 @@ int add_mtd_device(struct mtd_info *mtd, bool partitioned) · 40 unmodified lines
710687 ofidx = -1;
711688 if (np)
712689 ofidx = of_alias_get_id(np, "mtd");
713− if (partitioned) {
714− if (ofidx >= 0)
715− i = idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL);
716− else
717− i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
718− } else {
719− if (ofidx >= 0)
720− i = idr_alloc(&mtd_master_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL);
721− else
722− i = idr_alloc(&mtd_master_idr, mtd, 0, 0, GFP_KERNEL);
723− }
690+ if (ofidx >= 0)
691+ i = idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL);
692+ else
693+ i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
724694 if (i < 0) {
725695 error = i;
726696 goto fail_locked;
@@ -768,18 +738,10 @@ int add_mtd_device(struct mtd_info *mtd, bool partitioned) · 41 unmodified lines
768738 /* Caller should have set dev.parent to match the
769739 * physical device, if appropriate.
770740 */
771− if (partitioned) {
772− mtd->dev.type = &mtd_devtype;
773− mtd->dev.class = &mtd_class;
774− mtd->dev.devt = MTD_DEVT(i);
775− dev_set_name(&mtd->dev, "mtd%d", i);
776− error = dev_set_name(&mtd->dev, "mtd%d", i);
777− } else {
778− mtd->dev.type = &mtd_master_devtype;
779− mtd->dev.class = &mtd_master_class;
780− mtd->dev.devt = MKDEV(MAJOR(mtd_master_devt), i);
781− error = dev_set_name(&mtd->dev, "mtd_master%d", i);
782− }
741+ mtd->dev.type = &mtd_devtype;
742+ mtd->dev.class = &mtd_class;
743+ mtd->dev.devt = MTD_DEVT(i);
744+ error = dev_set_name(&mtd->dev, "mtd%d", i);
783745 if (error)
784746 goto fail_devname;
785747 dev_set_drvdata(&mtd->dev, mtd);
@@ -787,7 +749,6 @@ int add_mtd_device(struct mtd_info *mtd, bool partitioned) · 1 unmodified line
787749 of_node_get(mtd_get_of_node(mtd));
788750 error = device_register(&mtd->dev);
789751 if (error) {
790− pr_err("mtd: %s device_register fail %d\n", mtd->name, error);
791752 put_device(&mtd->dev);
792753 goto fail_added;
793754 }
@@ -799,13 +760,10 @@ int add_mtd_device(struct mtd_info *mtd, bool partitioned) · 5 unmodified lines
799760
800761 mtd_debugfs_populate(mtd);
801762
802− if (partitioned) {
803− device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
804− "mtd%dro", i);
805− }
763+ device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
764+ "mtd%dro", i);
806765
807− pr_debug("mtd: Giving out %spartitioned device %d to %s\n",
808− partitioned ? "" : "un-", i, mtd->name);
766+ pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
809767 /* No need to get a refcount on the module containing
810768 the notifier, since we hold the mtd_table_mutex */
811769 list_for_each_entry(not, &mtd_notifiers, list)
@@ -813,16 +771,13 @@ int add_mtd_device(struct mtd_info *mtd, bool partitioned) · 1 unmodified line
813771
814772 mutex_unlock(&mtd_table_mutex);
815773
816− if (partitioned) {
817− if (of_property_read_bool(mtd_get_of_node(mtd), "linux,rootfs")) {
818− if (IS_BUILTIN(CONFIG_MTD)) {
819− pr_info("mtd: setting mtd%d (%s) as root device\n",
820− mtd->index, mtd->name);
821− ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, mtd->index);
822− } else {
823− pr_warn("mtd: can't set mtd%d (%s) as root device - mtd must be builtin\n",
824− mtd->index, mtd->name);
825− }
774+ if (of_property_read_bool(mtd_get_of_node(mtd), "linux,rootfs")) {
775+ if (IS_BUILTIN(CONFIG_MTD)) {
776+ pr_info("mtd: setting mtd%d (%s) as root device\n", mtd->index, mtd->name);
777+ ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, mtd->index);
778+ } else {
779+ pr_warn("mtd: can't set mtd%d (%s) as root device - mtd must be builtin\n",
780+ mtd->index, mtd->name);
826781 }
827782 }
828783
@@ -838,10 +793,7 @@ fail_nvmem_add: · 9 unmodified lines
838793 fail_added:
839794 of_node_put(mtd_get_of_node(mtd));
840795 fail_devname:
841− if (partitioned)
842− idr_remove(&mtd_idr, i);
843− else
844− idr_remove(&mtd_master_idr, i);
796+ idr_remove(&mtd_idr, i);
845797 fail_locked:
846798 mutex_unlock(&mtd_table_mutex);
847799 return error;
@@ -859,14 +811,12 @@ fail_locked: · 11 unmodified lines
859811
860812 int del_mtd_device(struct mtd_info *mtd)
861813 {
862− struct mtd_notifier *not;
863− struct idr *idr;
864814 int ret;
815+ struct mtd_notifier *not;
865816
866817 mutex_lock(&mtd_table_mutex);
867818
868− idr = mtd->dev.class == &mtd_class ? &mtd_idr : &mtd_master_idr;
869− if (idr_find(idr, mtd->index) != mtd) {
819+ if (idr_find(&mtd_idr, mtd->index) != mtd) {
870820 ret = -ENODEV;
871821 goto out_error;
872822 }
@@ -1106,7 +1056,6 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, · 233 unmodified lines
11061056 const struct mtd_partition *parts,
11071057 int nr_parts)
11081058 {
1109− struct mtd_info *parent;
11101059 int ret, err;
11111060
11121061 mtd_set_dev_defaults(mtd);
@@ -1115,30 +1064,25 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, · 2 unmodified lines
11151064 if (ret)
11161065 goto out;
11171066
1118− ret = add_mtd_device(mtd, false);
1119− if (ret)
1120− goto out;
1121−
11221067 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
1123− ret = mtd_add_partition(mtd, mtd->name, 0, MTDPART_SIZ_FULL, &parent);
1068+ ret = add_mtd_device(mtd);
11241069 if (ret)
11251070 goto out;
1126−
1127− } else {
1128− parent = mtd;
11291071 }
11301072
11311073 /* Prefer parsed partitions over driver-provided fallback */
1132− ret = parse_mtd_partitions(parent, types, parser_data);
1074+ ret = parse_mtd_partitions(mtd, types, parser_data);
11331075 if (ret == -EPROBE_DEFER)
11341076 goto out;
11351077
11361078 if (ret > 0)
11371079 ret = 0;
11381080 else if (nr_parts)
1139− ret = add_mtd_partitions(parent, parts, nr_parts);
1140− else if (!IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
1141− ret = mtd_add_partition(parent, mtd->name, 0, MTDPART_SIZ_FULL, NULL);
1081+ ret = add_mtd_partitions(mtd, parts, nr_parts);
1082+ else if (!device_is_registered(&mtd->dev))
1083+ ret = add_mtd_device(mtd);
1084+ else
1085+ ret = 0;
11421086
11431087 if (ret)
11441088 goto out;
@@ -1158,14 +1102,13 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, · 13 unmodified lines
11581102 register_reboot_notifier(&mtd->reboot_notifier);
11591103 }
11601104
1161− return 0;
11621105 out:
1163− nvmem_unregister(mtd->otp_user_nvmem);
1164− nvmem_unregister(mtd->otp_factory_nvmem);
1165−
1166− del_mtd_partitions(mtd);
1106+ if (ret) {
1107+ nvmem_unregister(mtd->otp_user_nvmem);
1108+ nvmem_unregister(mtd->otp_factory_nvmem);
1109+ }
11671110
1168− if (device_is_registered(&mtd->dev)) {
1111+ if (ret && device_is_registered(&mtd->dev)) {
11691112 err = del_mtd_device(mtd);
11701113 if (err)
11711114 pr_err("Error when deleting MTD device (%d)\n", err);
@@ -1324,7 +1267,8 @@ int __get_mtd_device(struct mtd_info *mtd) · 152 unmodified lines
13241267 mtd = mtd->parent;
13251268 }
13261269
1327− kref_get(&master->refcnt);
1270+ if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
1271+ kref_get(&master->refcnt);
13281272
13291273 return 0;
13301274 }
@@ -1418,7 +1362,8 @@ void __put_mtd_device(struct mtd_info *mtd) · 87 unmodified lines
14181362 mtd = parent;
14191363 }
14201364
1421− kref_put(&master->refcnt, mtd_device_release);
1365+ if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
1366+ kref_put(&master->refcnt, mtd_device_release);
14221367
14231368 module_put(master->owner);
14241369
@@ -2585,16 +2530,6 @@ static int __init init_mtd(void) · 1160 unmodified lines
25852530 if (ret)
25862531 goto err_reg;
25872532
2588− ret = class_register(&mtd_master_class);
2589− if (ret)
2590− goto err_reg2;
2591−
2592− ret = alloc_chrdev_region(&mtd_master_devt, 0, MTD_MASTER_DEVS, "mtd_master");
2593− if (ret < 0) {
2594− pr_err("unable to allocate char dev region\n");
2595− goto err_chrdev;
2596− }
2597−
25982533 mtd_bdi = mtd_bdi_init("mtd");
25992534 if (IS_ERR(mtd_bdi)) {
26002535 ret = PTR_ERR(mtd_bdi);
@@ -2619,10 +2554,6 @@ out_procfs: · 18 unmodified lines
26192554 bdi_unregister(mtd_bdi);
26202555 bdi_put(mtd_bdi);
26212556 err_bdi:
2622− unregister_chrdev_region(mtd_master_devt, MTD_MASTER_DEVS);
2623−err_chrdev:
2624− class_unregister(&mtd_master_class);
2625−err_reg2:
26262557 class_unregister(&mtd_class);
26272558 err_reg:
26282559 pr_err("Error registering mtd class or bdi: %d\n", ret);
@@ -2636,12 +2567,9 @@ static void __exit cleanup_mtd(void) · 7 unmodified lines
26362567 if (proc_mtd)
26372568 remove_proc_entry("mtd", NULL);
26382569 class_unregister(&mtd_class);
2639− class_unregister(&mtd_master_class);
2640− unregister_chrdev_region(mtd_master_devt, MTD_MASTER_DEVS);
26412570 bdi_unregister(mtd_bdi);
26422571 bdi_put(mtd_bdi);
26432572 idr_destroy(&mtd_idr);
2644− idr_destroy(&mtd_master_idr);
26452573 }
26462574
26472575 module_init(init_mtd);
drivers/mtd/mtdcore.h+1−1
@@ -8,7 +8,7 @@ extern struct mutex mtd_table_mutex; · 7 unmodified lines
88 extern struct backing_dev_info *mtd_bdi;
99
1010 struct mtd_info *__mtd_next_device(int i);
11−int __must_check add_mtd_device(struct mtd_info *mtd, bool partitioned);
11+int __must_check add_mtd_device(struct mtd_info *mtd);
1212 int del_mtd_device(struct mtd_info *mtd);
1313 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
1414 int del_mtd_partitions(struct mtd_info *);
drivers/mtd/mtdpart.c+8−8
@@ -86,7 +86,8 @@ static struct mtd_info *allocate_partition(struct mtd_info *parent, · 85 unmodified lines
8686 * parent conditional on that option. Note, this is a way to
8787 * distinguish between the parent and its partitions in sysfs.
8888 */
89− child->dev.parent = &parent->dev;
89+ child->dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
90+ &parent->dev : parent->dev.parent;
9091 child->dev.of_node = part->of_node;
9192 child->parent = parent;
9293 child->part.offset = part->offset;
@@ -242,7 +243,7 @@ static int mtd_add_partition_attrs(struct mtd_info *new) · 149 unmodified lines
242243 }
243244
244245 int mtd_add_partition(struct mtd_info *parent, const char *name,
245− long long offset, long long length, struct mtd_info **out)
246+ long long offset, long long length)
246247 {
247248 struct mtd_info *master = mtd_get_master(parent);
248249 u64 parent_size = mtd_is_partition(parent) ?
@@ -275,15 +276,12 @@ int mtd_add_partition(struct mtd_info *parent, const char *name, · 26 unmodified lines
275276 list_add_tail(&child->part.node, &parent->partitions);
276277 mutex_unlock(&master->master.partitions_lock);
277278
278− ret = add_mtd_device(child, true);
279+ ret = add_mtd_device(child);
279280 if (ret)
280281 goto err_remove_part;
281282
282283 mtd_add_partition_attrs(child);
283284
284− if (out)
285− *out = child;
286−
287285 return 0;
288286
289287 err_remove_part:
@@ -415,7 +413,7 @@ int add_mtd_partitions(struct mtd_info *parent, · 125 unmodified lines
415413 list_add_tail(&child->part.node, &parent->partitions);
416414 mutex_unlock(&master->master.partitions_lock);
417415
418− ret = add_mtd_device(child, true);
416+ ret = add_mtd_device(child);
419417 if (ret) {
420418 mutex_lock(&master->master.partitions_lock);
421419 list_del(&child->part.node);
@@ -592,6 +590,9 @@ static int mtd_part_of_parse(struct mtd_info *master, · 170 unmodified lines
592590 int ret, err = 0;
593591
594592 dev = &master->dev;
593+ /* Use parent device (controller) if the top level MTD is not registered */
594+ if (!IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) && !mtd_is_partition(master))
595+ dev = master->dev.parent;
595596
596597 np = mtd_get_of_node(master);
597598 if (mtd_is_partition(master))
@@ -710,7 +711,6 @@ int parse_mtd_partitions(struct mtd_info *master, const char *const *types, · 112 unmodified lines
710711 if (ret < 0 && !err)
711712 err = ret;
712713 }
713−
714714 return err;
715715 }
716716
include/linux/mtd/partitions.h+1−1
@@ -108,7 +108,7 @@ extern void deregister_mtd_parser(struct mtd_part_parser *parser); · 107 unmodified lines
108108 deregister_mtd_parser)
109109
110110 int mtd_add_partition(struct mtd_info *master, const char *name,
111− long long offset, long long length, struct mtd_info **part);
111+ long long offset, long long length);
112112 int mtd_del_partition(struct mtd_info *master, int partno);
113113 uint64_t mtd_get_device_size(const struct mtd_info *mtd);
114114