Skip to contentSkip to conversation

input: mtk-pmic-keys: Fix probe when no DT node present

PATCH In review Matthias Brugger 3 participants 3 messages Jun 22, 2018 – Jul 3, 2018

Subsystems (4)
lore
Share & tools
lore

Matthias Brugger

author maintainer
#0 lore raw

PATCH 1/1 input: mtk-pmic-keys: Fix probe when no DT node present

The drivers gets probed from a mfd devices. So the driver runs probe although no DT node exists. This leads to a NULL pointer dereference in the probe function. Check if a node exists and error out in case none is present.

Fixes 3e9f0b3e2b27 ("input: Add MediaTek PMIC keys support")
---
 drivers/input/keyboard/mtk-pmic-keys.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/input/keyboard/mtk-pmic-keys.c +3 -0
index 02c67a1749fc..388043e1939c 100644
@@ -257,6 +257,9 @@ static int mtk_pmic_keys_probe(struct platform_device *pdev)
 	const struct of_device_id *of_id =
 		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
 
+	if (of_id == NULL)
+		return -ENODEV;
+
 	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
 	if (!keys)
 		return -ENOMEM;
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 4 lines · Matthias Brugger

The drivers gets probed from a mfd devices. So the driver runs probe although no DT node exists. This leads to a NULL pointer dereference in the probe function. Check if a node exists and error out in case none is present.

Hmm, if a cell specifies of_compatible and no such node is present in DT maybe we should bail out in mfd_add_device() instead of pushing the check into individual drivers? Something like:

drivers/mfd/mfd-core.c +17 -6
index 94e3f32ce935..36a5fd98000d 100644
@@ -137,6 +137,18 @@ static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
 }
 #endif
 
+static struct device_node *mfd_get_node_for_cell(struct device *parent,
+						 const struct mfd_cell *cell)
+{
+	struct device_node *np;
+
+	for_each_child_of_node(parent->of_node, np)
+		if (of_device_is_compatible(np, cell->of_compatible))
+			return np;
+
+	return NULL;
+}
+
 static int mfd_add_device(struct device *parent, int id,
 			  const struct mfd_cell *cell, atomic_t *usage_count,
 			  struct resource *mem_base,
@@ -144,7 +156,6 @@ static int mfd_add_device(struct device *parent, int id,
 {
 	struct resource *res;
 	struct platform_device *pdev;
-	struct device_node *np = NULL;
 	int ret = -ENOMEM;
 	int platform_id;
 	int r;
@@ -176,11 +187,11 @@ static int mfd_add_device(struct device *parent, int id,
 		goto fail_res;
 
 	if (parent->of_node && cell->of_compatible) {
-		for_each_child_of_node(parent->of_node, np) {
-			if (of_device_is_compatible(np, cell->of_compatible)) {
-				pdev->dev.of_node = np;
-				break;
-			}
+		pdev->dev.of_node = mfd_get_node_for_cell(parent, cell);
+		if (!pdev->dev.of_node) {
+			dev_err(parent, "unable to locate node for %s (%s)\n",
+				cell->name, cell->of_compatible);
+			goto fail_res;
 		}
 	}
 

Lee?

Quoted 24 lines
Fixes 3e9f0b3e2b27 ("input: Add MediaTek PMIC keys support")
---
 drivers/input/keyboard/mtk-pmic-keys.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/input/keyboard/mtk-pmic-keys.c +3 -0
index 02c67a1749fc..388043e1939c 100644
@@ -257,6 +257,9 @@ static int mtk_pmic_keys_probe(struct platform_device *pdev)
 	const struct of_device_id *of_id =
 		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
 
+	if (of_id == NULL)
+		return -ENODEV;
+
 	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
 	if (!keys)
 		return -ENOMEM;
Signature · 1 line
2.17.1

Thanks.

Signature · 1 line
Dmitry
#2 lore raw
Quoted 58 lines · Dmitry Torokhov
Quoted 4 lines · Matthias Brugger

The drivers gets probed from a mfd devices. So the driver runs probe although no DT node exists. This leads to a NULL pointer dereference in the probe function. Check if a node exists and error out in case none is present.

Hmm, if a cell specifies of_compatible and no such node is present in DT maybe we should bail out in mfd_add_device() instead of pushing the check into individual drivers? Something like:

drivers/mfd/mfd-core.c +17 -6
index 94e3f32ce935..36a5fd98000d 100644
@@ -137,6 +137,18 @@ static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
 }
 #endif
 
+static struct device_node *mfd_get_node_for_cell(struct device *parent,
+						 const struct mfd_cell *cell)
+{
+	struct device_node *np;
+
+	for_each_child_of_node(parent->of_node, np)
+		if (of_device_is_compatible(np, cell->of_compatible))
+			return np;
+
+	return NULL;
+}
+
 static int mfd_add_device(struct device *parent, int id,
 			  const struct mfd_cell *cell, atomic_t *usage_count,
 			  struct resource *mem_base,
@@ -144,7 +156,6 @@ static int mfd_add_device(struct device *parent, int id,
 {
 	struct resource *res;
 	struct platform_device *pdev;
-	struct device_node *np = NULL;
 	int ret = -ENOMEM;
 	int platform_id;
 	int r;
@@ -176,11 +187,11 @@ static int mfd_add_device(struct device *parent, int id,
 		goto fail_res;
 
 	if (parent->of_node && cell->of_compatible) {
-		for_each_child_of_node(parent->of_node, np) {
-			if (of_device_is_compatible(np, cell->of_compatible)) {
-				pdev->dev.of_node = np;
-				break;
-			}
+		pdev->dev.of_node = mfd_get_node_for_cell(parent, cell);
+		if (!pdev->dev.of_node) {
+			dev_err(parent, "unable to locate node for %s (%s)\n",
+				cell->name, cell->of_compatible);
+			goto fail_res;
 		}
 	}
 
Quoted 1 line

Lee?

This change results the wrong semantics. MFD overrides Device Tree, such that if a device is supplied and registered using mfd_add_device(s) it should be probed. The only reason we provide a pointer to the of_node is for the child device to pull properties from.

If a device should only be probed if an OF node exists, then it should not be registered at the MFD level.

Quoted 23 lines
Quoted 11 lines
Fixes 3e9f0b3e2b27 ("input: Add MediaTek PMIC keys support")
---
 drivers/input/keyboard/mtk-pmic-keys.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/input/keyboard/mtk-pmic-keys.c +0 -0
index 02c67a1749fc..388043e1939c 100644

drivers/input/keyboard/mtk-pmic-keys.c static int mtk_pmic_keys_probe(struct platform_device *pdev) · line 265

Quoted 9 lines
drivers/input/keyboard/mtk-pmic-keys.c +3 -0
@@ -257,6 +257,9 @@ static int mtk_pmic_keys_probe(struct platform_device *pdev)
 	const struct of_device_id *of_id =
 		of_match_device(of_mtk_pmic_keys_match_tbl, &pdev->dev);
 
+	if (of_id == NULL)
+		return -ENODEV;
+
 	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
 	if (!keys)
 		return -ENOMEM;

Thanks.

Signature · 4 lines
Lee Jones [???]
Linaro Services Technical Lead
Linaro.org ? Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog