generic: fix Macronix SPI-NAND driver
[openwrt/openwrt.git] / target / linux / generic / backport-5.15 / 808-v6.2-0013-nvmem-core-fix-device-node-refcounting.patch
1 From edcf2fb660526b5ed29f93bd17328a2b4835c8b2 Mon Sep 17 00:00:00 2001
2 From: Michael Walle <michael@walle.cc>
3 Date: Fri, 27 Jan 2023 10:40:12 +0000
4 Subject: [PATCH] nvmem: core: fix device node refcounting
5
6 In of_nvmem_cell_get(), of_get_next_parent() is used on cell_np. This
7 will decrement the refcount on cell_np, but cell_np is still used later
8 in the code. Use of_get_parent() instead and of_node_put() in the
9 appropriate places.
10
11 Fixes: 69aba7948cbe ("nvmem: Add a simple NVMEM framework for consumers")
12 Fixes: 7ae6478b304b ("nvmem: core: rework nvmem cell instance creation")
13 Cc: stable@vger.kernel.org
14 Signed-off-by: Michael Walle <michael@walle.cc>
15 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
16 Link: https://lore.kernel.org/r/20230127104015.23839-8-srinivas.kandagatla@linaro.org
17 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
18 ---
19 drivers/nvmem/core.c | 11 ++++++++---
20 1 file changed, 8 insertions(+), 3 deletions(-)
21
22 --- a/drivers/nvmem/core.c
23 +++ b/drivers/nvmem/core.c
24 @@ -1237,16 +1237,21 @@ struct nvmem_cell *of_nvmem_cell_get(str
25 if (!cell_np)
26 return ERR_PTR(-ENOENT);
27
28 - nvmem_np = of_get_next_parent(cell_np);
29 - if (!nvmem_np)
30 + nvmem_np = of_get_parent(cell_np);
31 + if (!nvmem_np) {
32 + of_node_put(cell_np);
33 return ERR_PTR(-EINVAL);
34 + }
35
36 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
37 of_node_put(nvmem_np);
38 - if (IS_ERR(nvmem))
39 + if (IS_ERR(nvmem)) {
40 + of_node_put(cell_np);
41 return ERR_CAST(nvmem);
42 + }
43
44 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
45 + of_node_put(cell_np);
46 if (!cell_entry) {
47 __nvmem_device_put(nvmem);
48 return ERR_PTR(-ENOENT);