[PATCH v2] soc: ti: knav_dma: fix device node leak in of_channel_match_helper()

zhaofeng2@cmss.chinamobile.com posted 1 patch 3 hours ago
drivers/soc/ti/knav_dma.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
[PATCH v2] soc: ti: knav_dma: fix device node leak in of_channel_match_helper()
Posted by zhaofeng2@cmss.chinamobile.com 3 hours ago
From: Zhao Feng <zhaofeng2@cmss.chinamobile.com>

of_channel_match_helper() takes two OF references: one via
of_parse_phandle() into dma_node (for the ->name string handed out
through *dma_instance) and one via of_parse_phandle_with_fixed_args()
into args.np (for the channel number).  Both are leaked:

- The three error paths (property match failure, fixed-args parse
  failure, missing args) drop neither dma_node nor args.np.

- The successful path leaked both prior to the initial fix.

Drop dma_node on the two error paths present in upstream, drop
args.np on the one error path where it is obtained, and on success
transfer dma_node to the caller so that knav_dma_open_channel(),
which only uses the ->name string to locate the matching DMA instance
within the function, can release the reference once the lookup is
complete.

Fixes: 88139ed03058 ("soc: ti: add Keystone Navigator DMA support")
Signed-off-by: Zhao Feng <zhaofeng2@cmss.chinamobile.com>
---
v2: also release dma_node on error paths and transfer it to
    knav_dma_open_channel() for release after last use, per review
    from Nishanth Menon.

 drivers/soc/ti/knav_dma.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

--- a/drivers/soc/ti/knav_dma.c
+++ b/drivers/soc/ti/knav_dma.c
@@ -364,7 +364,8 @@
 DEFINE_SHOW_ATTRIBUTE(knav_dma_debug);
 
 static int of_channel_match_helper(struct device_node *np, const char *name,
-					const char **dma_instance)
+					const char **dma_instance,
+					struct device_node **dma_node_out)
 {
 	struct of_phandle_args args;
 	struct device_node *dma_node;
@@ -378,15 +379,20 @@
 	index = of_property_match_string(np, "ti,navigator-dma-names", name);
 	if (index < 0) {
 		dev_err(kdev->dev, "No 'ti,navigator-dma-names' property\n");
+		of_node_put(dma_node);
 		return -ENODEV;
 	}
 
 	if (of_parse_phandle_with_fixed_args(np, "ti,navigator-dmas",
 					1, index, &args)) {
 		dev_err(kdev->dev, "Missing the phandle args name %s\n", name);
+		of_node_put(dma_node);
 		return -ENODEV;
 	}
 
+	of_node_put(args.np);
+
+	*dma_node_out = dma_node;
 	return args.args[0];
 }
 
@@ -403,6 +409,7 @@
 {
 	struct knav_dma_device *dma = NULL, *iter1;
 	struct knav_dma_chan *chan = NULL, *iter2;
+	struct device_node *dma_node;
 	int chan_num = -1;
 	const char *instance;
 
@@ -411,7 +418,7 @@
 		return NULL;
 	}
 
-	chan_num = of_channel_match_helper(dev->of_node, name, &instance);
+	chan_num = of_channel_match_helper(dev->of_node, name, &instance, &dma_node);
 	if (chan_num < 0) {
 		dev_err(kdev->dev, "No DMA instance with name %s\n", name);
 		return NULL;
@@ -425,6 +432,7 @@
 	if (config->direction != DMA_MEM_TO_DEV &&
 	    config->direction != DMA_DEV_TO_MEM) {
 		dev_err(kdev->dev, "bad direction\n");
+		of_node_put(dma_node);
 		return NULL;
 	}
 
@@ -437,6 +445,7 @@
 	}
 	if (!dma) {
 		dev_err(kdev->dev, "No DMA instance with name %s\n", instance);
+		of_node_put(dma_node);
 		return NULL;
 	}
 
@@ -457,6 +466,7 @@
 	if (!chan) {
 		dev_err(kdev->dev, "channel %d is not in DMA %s\n",
 				chan_num, instance);
+		of_node_put(dma_node);
 		return NULL;
 	}
 
@@ -464,6 +474,7 @@
 		if (!check_config(chan, config)) {
 			dev_err(kdev->dev, "channel %d config miss-match\n",
 				chan_num);
+			of_node_put(dma_node);
 			return NULL;
 		}
 	}
@@ -476,6 +487,8 @@
 
 	dev_dbg(kdev->dev, "channel %d opened from DMA %s\n",
 				chan_num, instance);
+
+	of_node_put(dma_node);
 
 	return chan;
 }