* [PATCH] mtd: core: Fix ERROR of refcount due to del_mtd_device()
@ 2022-11-19 6:39 Shang XiaoJing
2022-11-24 11:36 ` Miquel Raynal
0 siblings, 1 reply; 2+ messages in thread
From: Shang XiaoJing @ 2022-11-19 6:39 UTC (permalink / raw)
To: miquel.raynal, richard, vigneshr, zev, linux-mtd; +Cc: shangxiaojing
del_mtd_device() will call of_node_put() to mtd_get_of_node(mtd), which
is mtd->dev.of_node. However, memset(&mtd->dev, 0) is called before
of_node_put(). As the result, of_node_put() won't do anything in
del_mtd_device(), and causes the refcount leak.
del_mtd_device()
memset(&mtd->dev, 0, sizeof(mtd->dev) # clear mtd->dev
of_node_put()
mtd_get_of_node(mtd) # mtd->dev is cleared, can't locate of_node
# of_node_put(NULL) won't do anything
Fix the ERROR by caching the pointer of the device_node.
OF: ERROR: memory leak, expected refcount 1 instead of 2,
of_node_get()/of_node_put() unbalanced - destroy cset entry: attach
overlay node /spi/spi-sram@0
CPU: 3 PID: 275 Comm: python3 Tainted: G N 6.1.0-rc3+ #54
0d8a1edddf51f172ff5226989a7565c6313b08e2
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
rel-1.15.0-0-g2dd4b9b3f840-prebuilt.qemu.org 04/01/2014
Call Trace:
<TASK>
dump_stack_lvl+0x67/0x83
kobject_get+0x155/0x160
of_node_get+0x1f/0x30
of_fwnode_get+0x43/0x70
fwnode_handle_get+0x54/0x80
fwnode_get_nth_parent+0xc9/0xe0
fwnode_full_name_string+0x3f/0xa0
device_node_string+0x30f/0x750
pointer+0x598/0x7a0
vsnprintf+0x62d/0x9b0
...
cfs_overlay_release+0x30/0x90
config_item_release+0xbe/0x1a0
config_item_put+0x5e/0x80
configfs_rmdir+0x3bd/0x540
vfs_rmdir+0x18c/0x320
do_rmdir+0x198/0x330
__x64_sys_rmdir+0x2c/0x40
do_syscall_64+0x37/0x90
entry_SYSCALL_64_after_hwframe+0x63/0xcd
Fixes: 00596576a051 ("mtd: core: clear out unregistered devices a bit more")
Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
---
drivers/mtd/mtdcore.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 18aa54460d36..74b1a1c61b39 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -774,6 +774,7 @@ int del_mtd_device(struct mtd_info *mtd)
{
int ret;
struct mtd_notifier *not;
+ struct device_node *mtd_of_node;
mutex_lock(&mtd_table_mutex);
@@ -792,6 +793,7 @@ int del_mtd_device(struct mtd_info *mtd)
mtd->index, mtd->name, mtd->usecount);
ret = -EBUSY;
} else {
+ mtd_of_node = mtd_get_of_node(mtd);
debugfs_remove_recursive(mtd->dbg.dfs_dir);
/* Try to remove the NVMEM provider */
@@ -803,7 +805,7 @@ int del_mtd_device(struct mtd_info *mtd)
memset(&mtd->dev, 0, sizeof(mtd->dev));
idr_remove(&mtd_idr, mtd->index);
- of_node_put(mtd_get_of_node(mtd));
+ of_node_put(mtd_of_node);
module_put(THIS_MODULE);
ret = 0;
--
2.17.1
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] mtd: core: Fix ERROR of refcount due to del_mtd_device()
2022-11-19 6:39 [PATCH] mtd: core: Fix ERROR of refcount due to del_mtd_device() Shang XiaoJing
@ 2022-11-24 11:36 ` Miquel Raynal
0 siblings, 0 replies; 2+ messages in thread
From: Miquel Raynal @ 2022-11-24 11:36 UTC (permalink / raw)
To: Shang XiaoJing, miquel.raynal, richard, vigneshr, zev, linux-mtd
On Sat, 2022-11-19 at 06:39:15 UTC, Shang XiaoJing wrote:
> del_mtd_device() will call of_node_put() to mtd_get_of_node(mtd), which
> is mtd->dev.of_node. However, memset(&mtd->dev, 0) is called before
> of_node_put(). As the result, of_node_put() won't do anything in
> del_mtd_device(), and causes the refcount leak.
>
> del_mtd_device()
> memset(&mtd->dev, 0, sizeof(mtd->dev) # clear mtd->dev
> of_node_put()
> mtd_get_of_node(mtd) # mtd->dev is cleared, can't locate of_node
> # of_node_put(NULL) won't do anything
>
> Fix the ERROR by caching the pointer of the device_node.
>
> OF: ERROR: memory leak, expected refcount 1 instead of 2,
> of_node_get()/of_node_put() unbalanced - destroy cset entry: attach
> overlay node /spi/spi-sram@0
> CPU: 3 PID: 275 Comm: python3 Tainted: G N 6.1.0-rc3+ #54
> 0d8a1edddf51f172ff5226989a7565c6313b08e2
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
> rel-1.15.0-0-g2dd4b9b3f840-prebuilt.qemu.org 04/01/2014
> Call Trace:
> <TASK>
> dump_stack_lvl+0x67/0x83
> kobject_get+0x155/0x160
> of_node_get+0x1f/0x30
> of_fwnode_get+0x43/0x70
> fwnode_handle_get+0x54/0x80
> fwnode_get_nth_parent+0xc9/0xe0
> fwnode_full_name_string+0x3f/0xa0
> device_node_string+0x30f/0x750
> pointer+0x598/0x7a0
> vsnprintf+0x62d/0x9b0
> ...
> cfs_overlay_release+0x30/0x90
> config_item_release+0xbe/0x1a0
> config_item_put+0x5e/0x80
> configfs_rmdir+0x3bd/0x540
> vfs_rmdir+0x18c/0x320
> do_rmdir+0x198/0x330
> __x64_sys_rmdir+0x2c/0x40
> do_syscall_64+0x37/0x90
> entry_SYSCALL_64_after_hwframe+0x63/0xcd
>
> Fixes: 00596576a051 ("mtd: core: clear out unregistered devices a bit more")
> Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.
Miquel
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-11-24 11:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-19 6:39 [PATCH] mtd: core: Fix ERROR of refcount due to del_mtd_device() Shang XiaoJing
2022-11-24 11:36 ` Miquel Raynal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).