Skip to contentSkip to conversation

devres: provide devm_kstrdup_const()

PATCH Superseded Bartosz Golaszewski 4 participants 13 messages Aug 27, 2018 – Aug 28, 2018

Subsystems (4)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 devres: provide devm_kstrdup_const()

Provide a resource managed version of kstrdup_const(). This variant internally calls devm_kstrdup() on pointers that are outside of .rodata section. Also provide a corresponding version of devm_kfree().

---
 include/linux/device.h |  2 ++
 mm/util.c              | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
include/linux/device.h +2 -0
index 8f882549edee..f8f5982d26b2 100644
@@ -693,7 +693,9 @@ static inline void *devm_kcalloc(struct device *dev,
 	return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
 }
 extern void devm_kfree(struct device *dev, void *p);
+extern void devm_kfree_const(struct device *dev, void *p);
 extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
+extern char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
 extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
 			  gfp_t gfp);
 
mm/util.c +35 -0
index d2890a407332..6d1f41b5775e 100644
@@ -39,6 +39,20 @@ void kfree_const(const void *x)
 }
 EXPORT_SYMBOL(kfree_const);
 
+/**
+ * devm_kfree_const - Resource managed conditional kfree
+ * @dev: device this memory belongs to
+ * @p: memory to free
+ *
+ * Function calls devm_kfree only if @p is not in .rodata section.
+ */
+void devm_kfree_const(struct device *dev, void *p)
+{
+	if (!is_kernel_rodata((unsigned long)p))
+		devm_kfree(dev, p);
+}
+EXPORT_SYMBOL(devm_kfree_const);
+
 /**
  * kstrdup - allocate space for and copy an existing string
  * @s: the string to duplicate
@@ -78,6 +92,27 @@ const char *kstrdup_const(const char *s, gfp_t gfp)
 }
 EXPORT_SYMBOL(kstrdup_const);
 
+/**
+ * devm_kstrdup_const - resource managed conditional string duplication
+ * @dev: device for which to duplicate the string
+ * @s: the string to duplicate
+ * @gfp: the GFP mask used in the kmalloc() call when allocating memory
+ *
+ * Function returns source string if it is in .rodata section otherwise it
+ * fallbacks to devm_kstrdup.
+ *
+ * Strings allocated by devm_kstrdup_const will be automatically freed when
+ * the associated device is detached.
+ */
+char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
+{
+	if (is_kernel_rodata((unsigned long)s))
+		return s;
+
+	return devm_kstrdup(dev, s, gfp);
+}
+EXPORT_SYMBOL(devm_kstrdup_const);
+
 /**
  * kstrndup - allocate space for and copy an existing string
  * @s: the string to duplicate
Signature · 1 line
2.18.0
#1 lore raw

PATCH 2/2 clk: pmc-atom: use devm_kstrdup_const()

Use devm_kstrdup_const() in the pmc-atom driver. This mostly serves as an example of how to use this new routine to shrink driver code.

While we're at it: replace a call to kcalloc() with devm_kcalloc().

---
 drivers/clk/x86/clk-pmc-atom.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
drivers/clk/x86/clk-pmc-atom.c +4 -15
index 08ef69945ffb..daa2192e6568 100644
@@ -253,14 +253,6 @@ static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data *data,
 		plt_clk_unregister_fixed_rate(data->parents[i]);
 }
 
-static void plt_clk_free_parent_names_loop(const char **parent_names,
-					   unsigned int i)
-{
-	while (i--)
-		kfree_const(parent_names[i]);
-	kfree(parent_names);
-}
-
 static void plt_clk_unregister_loop(struct clk_plt_data *data,
 				    unsigned int i)
 {
@@ -286,8 +278,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 	if (!data->parents)
 		return ERR_PTR(-ENOMEM);
 
-	parent_names = kcalloc(nparents, sizeof(*parent_names),
-			       GFP_KERNEL);
+	parent_names = devm_kcalloc(&pdev->dev, nparents,
+				    sizeof(*parent_names), GFP_KERNEL);
 	if (!parent_names)
 		return ERR_PTR(-ENOMEM);
 
@@ -300,7 +292,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 			err = PTR_ERR(data->parents[i]);
 			goto err_unreg;
 		}
-		parent_names[i] = kstrdup_const(clks[i].name, GFP_KERNEL);
+		parent_names[i] = devm_kstrdup_const(&pdev->dev,
+						     clks[i].name, GFP_KERNEL);
 	}
 
 	data->nparents = nparents;
@@ -308,7 +301,6 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 
 err_unreg:
 	plt_clk_unregister_fixed_rate_loop(data, i);
-	plt_clk_free_parent_names_loop(parent_names, i);
 	return ERR_PTR(err);
 }
 
@@ -351,15 +343,12 @@ static int plt_clk_probe(struct platform_device *pdev)
 		goto err_unreg_clk_plt;
 	}
 
-	plt_clk_free_parent_names_loop(parent_names, data->nparents);
-
 	platform_set_drvdata(pdev, data);
 	return 0;
 
 err_unreg_clk_plt:
 	plt_clk_unregister_loop(data, i);
 	plt_clk_unregister_parents(data);
-	plt_clk_free_parent_names_loop(parent_names, data->nparents);
 	return err;
 }
 
Signature · 1 line
2.18.0
#2 lore raw

Re: [PATCH 2/2] clk: pmc-atom: use devm_kstrdup_const()

Quoted 51 lines · Bartosz Golaszewski

Use devm_kstrdup_const() in the pmc-atom driver. This mostly serves as an example of how to use this new routine to shrink driver code.

While we're at it: replace a call to kcalloc() with devm_kcalloc().

---
 drivers/clk/x86/clk-pmc-atom.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
drivers/clk/x86/clk-pmc-atom.c +4 -11
index 08ef69945ffb..daa2192e6568 100644
@@ -253,14 +253,6 @@ static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data *data,
 		plt_clk_unregister_fixed_rate(data->parents[i]);
 }
 
-static void plt_clk_free_parent_names_loop(const char **parent_names,
-					   unsigned int i)
-{
-	while (i--)
-		kfree_const(parent_names[i]);
-	kfree(parent_names);
-}
-
 static void plt_clk_unregister_loop(struct clk_plt_data *data,
 				    unsigned int i)
 {
@@ -286,8 +278,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 	if (!data->parents)
 		return ERR_PTR(-ENOMEM);
 
-	parent_names = kcalloc(nparents, sizeof(*parent_names),
-			       GFP_KERNEL);
+	parent_names = devm_kcalloc(&pdev->dev, nparents,
+				    sizeof(*parent_names), GFP_KERNEL);
 	if (!parent_names)
 		return ERR_PTR(-ENOMEM);
 
@@ -300,7 +292,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 			err = PTR_ERR(data->parents[i]);
 			goto err_unreg;
 		}
-		parent_names[i] = kstrdup_const(clks[i].name, GFP_KERNEL);
+		parent_names[i] = devm_kstrdup_const(&pdev->dev,
+						     clks[i].name, GFP_KERNEL);
 	}
 
 	data->nparents = nparents;

drivers/clk/x86/clk-pmc-atom.c static const char **plt_clk_register_parents(struct platform_device *pdev, · line 311

Quoted 4 lines
drivers/clk/x86/clk-pmc-atom.c +0 -1
@@ -308,7 +301,6 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 
 err_unreg:
 	plt_clk_unregister_fixed_rate_loop(data, i);
-	plt_clk_free_parent_names_loop(parent_names, i);

What happens if clks[i].name is not a part of RO data? The devm_kstrdup_const will allocate memory and nothing will ever free it...

And, please don't drop kfree(parent_names) here.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -0
 	return ERR_PTR(err);
 }
 
@@ -351,15 +343,12 @@ static int plt_clk_probe(struct platform_device *pdev)
 		goto err_unreg_clk_plt;
 	}

drivers/clk/x86/clk-pmc-atom.c static int plt_clk_probe(struct platform_device *pdev) · line 362

Quoted 10 lines
drivers/clk/x86/clk-pmc-atom.c +0 -3
 
-	plt_clk_free_parent_names_loop(parent_names, data->nparents);
-
 	platform_set_drvdata(pdev, data);
 	return 0;
 
 err_unreg_clk_plt:
 	plt_clk_unregister_loop(data, i);
 	plt_clk_unregister_parents(data);
-	plt_clk_free_parent_names_loop(parent_names, data->nparents);

Ditto.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -0
 	return err;
 }
 
Signature · 1 line
2.18.0
Signature · 2 lines
Sincerely yours,
Mike.
#3 lore raw

Re: [PATCH 2/2] clk: pmc-atom: use devm_kstrdup_const()

2018-08-27 12:39 GMT+02:00 Mike Rapoport <[email protected]>:

Quoted 53 lines · Bartosz Golaszewski
Quoted 52 lines

Use devm_kstrdup_const() in the pmc-atom driver. This mostly serves as an example of how to use this new routine to shrink driver code.

While we're at it: replace a call to kcalloc() with devm_kcalloc().

---
 drivers/clk/x86/clk-pmc-atom.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
drivers/clk/x86/clk-pmc-atom.c +4 -11
index 08ef69945ffb..daa2192e6568 100644
@@ -253,14 +253,6 @@ static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data *data,
              plt_clk_unregister_fixed_rate(data->parents[i]);
 }
 
-static void plt_clk_free_parent_names_loop(const char **parent_names,
-                                        unsigned int i)
-{
-     while (i--)
-             kfree_const(parent_names[i]);
-     kfree(parent_names);
-}
-
 static void plt_clk_unregister_loop(struct clk_plt_data *data,
                                  unsigned int i)
 {
@@ -286,8 +278,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
      if (!data->parents)
              return ERR_PTR(-ENOMEM);
 
-     parent_names = kcalloc(nparents, sizeof(*parent_names),
-                            GFP_KERNEL);
+     parent_names = devm_kcalloc(&pdev->dev, nparents,
+                                 sizeof(*parent_names), GFP_KERNEL);
      if (!parent_names)
              return ERR_PTR(-ENOMEM);
 
@@ -300,7 +292,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
                      err = PTR_ERR(data->parents[i]);
                      goto err_unreg;
              }
-             parent_names[i] = kstrdup_const(clks[i].name, GFP_KERNEL);
+             parent_names[i] = devm_kstrdup_const(&pdev->dev,
+                                                  clks[i].name, GFP_KERNEL);
      }
 
      data->nparents = nparents;
@@ -308,7 +301,6 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 
Quoted 7 lines

drivers/clk/x86/clk-pmc-atom.c static const char **plt_clk_register_parents(struct platform_device *pdev, · line 311

Quoted 3 lines
drivers/clk/x86/clk-pmc-atom.c +0 -1
 err_unreg:
      plt_clk_unregister_fixed_rate_loop(data, i);
-     plt_clk_free_parent_names_loop(parent_names, i);

What happens if clks[i].name is not a part of RO data? The devm_kstrdup_const will allocate memory and nothing will ever free it...

I'm looking at it and trying to see if I'm missing something, but AFAIK the whole concept of devm_* is to leave out the resource management part.

devm_kstrdup_const() will internally call devm_kstrdup() for strings that are not in .rodata and once the device is detached, the string will be freed (or not if it's in .rodata).

BR
Bart

Quoted 32 lines

And, please don't drop kfree(parent_names) here.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -0
      return ERR_PTR(err);
 }
 
@@ -351,15 +343,12 @@ static int plt_clk_probe(struct platform_device *pdev)
              goto err_unreg_clk_plt;
      }

drivers/clk/x86/clk-pmc-atom.c static int plt_clk_probe(struct platform_device *pdev) · line 362

Quoted 10 lines
drivers/clk/x86/clk-pmc-atom.c +0 -3
 
-     plt_clk_free_parent_names_loop(parent_names, data->nparents);
-
      platform_set_drvdata(pdev, data);
      return 0;
 
 err_unreg_clk_plt:
      plt_clk_unregister_loop(data, i);
      plt_clk_unregister_parents(data);
-     plt_clk_free_parent_names_loop(parent_names, data->nparents);

Ditto.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -1
      return err;
 }
 
--

2.18.0

--
Sincerely yours,
Mike.

#4 lore raw

Re: [PATCH 2/2] clk: pmc-atom: use devm_kstrdup_const()

Quoted 62 lines · Bartosz Golaszewski

2018-08-27 12:39 GMT+02:00 Mike Rapoport <[email protected]>:

Quoted 53 lines · Bartosz Golaszewski
Quoted 52 lines

Use devm_kstrdup_const() in the pmc-atom driver. This mostly serves as an example of how to use this new routine to shrink driver code.

While we're at it: replace a call to kcalloc() with devm_kcalloc().

---
 drivers/clk/x86/clk-pmc-atom.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
drivers/clk/x86/clk-pmc-atom.c +4 -11
index 08ef69945ffb..daa2192e6568 100644
@@ -253,14 +253,6 @@ static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data *data,
              plt_clk_unregister_fixed_rate(data->parents[i]);
 }
 
-static void plt_clk_free_parent_names_loop(const char **parent_names,
-                                        unsigned int i)
-{
-     while (i--)
-             kfree_const(parent_names[i]);
-     kfree(parent_names);
-}
-
 static void plt_clk_unregister_loop(struct clk_plt_data *data,
                                  unsigned int i)
 {
@@ -286,8 +278,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
      if (!data->parents)
              return ERR_PTR(-ENOMEM);
 
-     parent_names = kcalloc(nparents, sizeof(*parent_names),
-                            GFP_KERNEL);
+     parent_names = devm_kcalloc(&pdev->dev, nparents,
+                                 sizeof(*parent_names), GFP_KERNEL);
      if (!parent_names)
              return ERR_PTR(-ENOMEM);
 
@@ -300,7 +292,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
                      err = PTR_ERR(data->parents[i]);
                      goto err_unreg;
              }
-             parent_names[i] = kstrdup_const(clks[i].name, GFP_KERNEL);
+             parent_names[i] = devm_kstrdup_const(&pdev->dev,
+                                                  clks[i].name, GFP_KERNEL);
      }
 
      data->nparents = nparents;
@@ -308,7 +301,6 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 
Quoted 7 lines

drivers/clk/x86/clk-pmc-atom.c static const char **plt_clk_register_parents(struct platform_device *pdev, · line 311

Quoted 3 lines
drivers/clk/x86/clk-pmc-atom.c +0 -1
 err_unreg:
      plt_clk_unregister_fixed_rate_loop(data, i);
-     plt_clk_free_parent_names_loop(parent_names, i);

What happens if clks[i].name is not a part of RO data? The devm_kstrdup_const will allocate memory and nothing will ever free it...

Quoted 7 lines

I'm looking at it and trying to see if I'm missing something, but AFAIK the whole concept of devm_* is to leave out the resource management part.

devm_kstrdup_const() will internally call devm_kstrdup() for strings that are not in .rodata and once the device is detached, the string will be freed (or not if it's in .rodata).

And when it's going to be freed, how the resource management will know whether it's .rodata or not?

Quoted 36 lines

BR
Bart

Quoted 32 lines

And, please don't drop kfree(parent_names) here.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -0
      return ERR_PTR(err);
 }
 
@@ -351,15 +343,12 @@ static int plt_clk_probe(struct platform_device *pdev)
              goto err_unreg_clk_plt;
      }

drivers/clk/x86/clk-pmc-atom.c static int plt_clk_probe(struct platform_device *pdev) · line 362

Quoted 10 lines
drivers/clk/x86/clk-pmc-atom.c +0 -3
 
-     plt_clk_free_parent_names_loop(parent_names, data->nparents);
-
      platform_set_drvdata(pdev, data);
      return 0;
 
 err_unreg_clk_plt:
      plt_clk_unregister_loop(data, i);
      plt_clk_unregister_parents(data);
-     plt_clk_free_parent_names_loop(parent_names, data->nparents);

Ditto.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -1
      return err;
 }
 
--

2.18.0

--
Sincerely yours,
Mike.

Signature · 2 lines
Sincerely yours,
Mike.
#5 lore raw

Re: [PATCH 2/2] clk: pmc-atom: use devm_kstrdup_const()

2018-08-27 14:52 GMT+02:00 Mike Rapoport <[email protected]>:

Quoted 67 lines · Bartosz Golaszewski
Quoted 66 lines

2018-08-27 12:39 GMT+02:00 Mike Rapoport <[email protected]>:

Quoted 53 lines · Bartosz Golaszewski
Quoted 52 lines

Use devm_kstrdup_const() in the pmc-atom driver. This mostly serves as an example of how to use this new routine to shrink driver code.

While we're at it: replace a call to kcalloc() with devm_kcalloc().

---
 drivers/clk/x86/clk-pmc-atom.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
drivers/clk/x86/clk-pmc-atom.c +4 -11
index 08ef69945ffb..daa2192e6568 100644
@@ -253,14 +253,6 @@ static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data *data,
              plt_clk_unregister_fixed_rate(data->parents[i]);
 }
 
-static void plt_clk_free_parent_names_loop(const char **parent_names,
-                                        unsigned int i)
-{
-     while (i--)
-             kfree_const(parent_names[i]);
-     kfree(parent_names);
-}
-
 static void plt_clk_unregister_loop(struct clk_plt_data *data,
                                  unsigned int i)
 {
@@ -286,8 +278,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
      if (!data->parents)
              return ERR_PTR(-ENOMEM);
 
-     parent_names = kcalloc(nparents, sizeof(*parent_names),
-                            GFP_KERNEL);
+     parent_names = devm_kcalloc(&pdev->dev, nparents,
+                                 sizeof(*parent_names), GFP_KERNEL);
      if (!parent_names)
              return ERR_PTR(-ENOMEM);
 
@@ -300,7 +292,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
                      err = PTR_ERR(data->parents[i]);
                      goto err_unreg;
              }
-             parent_names[i] = kstrdup_const(clks[i].name, GFP_KERNEL);
+             parent_names[i] = devm_kstrdup_const(&pdev->dev,
+                                                  clks[i].name, GFP_KERNEL);
      }
 
      data->nparents = nparents;
@@ -308,7 +301,6 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 
Quoted 7 lines

drivers/clk/x86/clk-pmc-atom.c static const char **plt_clk_register_parents(struct platform_device *pdev, · line 311

Quoted 3 lines
drivers/clk/x86/clk-pmc-atom.c +0 -1
 err_unreg:
      plt_clk_unregister_fixed_rate_loop(data, i);
-     plt_clk_free_parent_names_loop(parent_names, i);

What happens if clks[i].name is not a part of RO data? The devm_kstrdup_const will allocate memory and nothing will ever free it...

I'm looking at it and trying to see if I'm missing something, but AFAIK the whole concept of devm_* is to leave out the resource management part.

Quoted 7 lines
Quoted 3 lines

devm_kstrdup_const() will internally call devm_kstrdup() for strings that are not in .rodata and once the device is detached, the string will be freed (or not if it's in .rodata).

And when it's going to be freed, how the resource management will know whether it's .rodata or not?

If the string to be duplicated is in .rodata, it's returned as is from devm_kstrdup_const(). Never gets added to the devres list, never get's freed. When the string to be duplicated is not in .rodata, devm_kstrdup() is called from devm_kstrdup_const(). Now the string is in the devres list of this device and it will get freed on driver detach. I really don't see what else could be a problem here.

BR
Bart

Quoted 41 lines
Quoted 36 lines

BR
Bart

Quoted 32 lines

And, please don't drop kfree(parent_names) here.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -0
      return ERR_PTR(err);
 }
 
@@ -351,15 +343,12 @@ static int plt_clk_probe(struct platform_device *pdev)
              goto err_unreg_clk_plt;
      }

drivers/clk/x86/clk-pmc-atom.c static int plt_clk_probe(struct platform_device *pdev) · line 362

Quoted 10 lines
drivers/clk/x86/clk-pmc-atom.c +0 -3
 
-     plt_clk_free_parent_names_loop(parent_names, data->nparents);
-
      platform_set_drvdata(pdev, data);
      return 0;
 
 err_unreg_clk_plt:
      plt_clk_unregister_loop(data, i);
      plt_clk_unregister_parents(data);
-     plt_clk_free_parent_names_loop(parent_names, data->nparents);

Ditto.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -1
      return err;
 }
 
--

2.18.0

--
Sincerely yours,
Mike.

--
Sincerely yours,
Mike.

#6 lore raw

Re: [PATCH 2/2] clk: pmc-atom: use devm_kstrdup_const()

Quoted 76 lines · Bartosz Golaszewski

2018-08-27 14:52 GMT+02:00 Mike Rapoport <[email protected]>:

Quoted 67 lines · Bartosz Golaszewski
Quoted 66 lines

2018-08-27 12:39 GMT+02:00 Mike Rapoport <[email protected]>:

Quoted 53 lines · Bartosz Golaszewski
Quoted 52 lines

Use devm_kstrdup_const() in the pmc-atom driver. This mostly serves as an example of how to use this new routine to shrink driver code.

While we're at it: replace a call to kcalloc() with devm_kcalloc().

---
 drivers/clk/x86/clk-pmc-atom.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
drivers/clk/x86/clk-pmc-atom.c +4 -11
index 08ef69945ffb..daa2192e6568 100644
@@ -253,14 +253,6 @@ static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data *data,
              plt_clk_unregister_fixed_rate(data->parents[i]);
 }
 
-static void plt_clk_free_parent_names_loop(const char **parent_names,
-                                        unsigned int i)
-{
-     while (i--)
-             kfree_const(parent_names[i]);
-     kfree(parent_names);
-}
-
 static void plt_clk_unregister_loop(struct clk_plt_data *data,
                                  unsigned int i)
 {
@@ -286,8 +278,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
      if (!data->parents)
              return ERR_PTR(-ENOMEM);
 
-     parent_names = kcalloc(nparents, sizeof(*parent_names),
-                            GFP_KERNEL);
+     parent_names = devm_kcalloc(&pdev->dev, nparents,
+                                 sizeof(*parent_names), GFP_KERNEL);
      if (!parent_names)
              return ERR_PTR(-ENOMEM);
 
@@ -300,7 +292,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
                      err = PTR_ERR(data->parents[i]);
                      goto err_unreg;
              }
-             parent_names[i] = kstrdup_const(clks[i].name, GFP_KERNEL);
+             parent_names[i] = devm_kstrdup_const(&pdev->dev,
+                                                  clks[i].name, GFP_KERNEL);
      }
 
      data->nparents = nparents;
@@ -308,7 +301,6 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,
 
Quoted 7 lines

drivers/clk/x86/clk-pmc-atom.c static const char **plt_clk_register_parents(struct platform_device *pdev, · line 311

Quoted 3 lines
drivers/clk/x86/clk-pmc-atom.c +0 -1
 err_unreg:
      plt_clk_unregister_fixed_rate_loop(data, i);
-     plt_clk_free_parent_names_loop(parent_names, i);

What happens if clks[i].name is not a part of RO data? The devm_kstrdup_const will allocate memory and nothing will ever free it...

I'm looking at it and trying to see if I'm missing something, but AFAIK the whole concept of devm_* is to leave out the resource management part.

Quoted 7 lines
Quoted 3 lines

devm_kstrdup_const() will internally call devm_kstrdup() for strings that are not in .rodata and once the device is detached, the string will be freed (or not if it's in .rodata).

And when it's going to be freed, how the resource management will know whether it's .rodata or not?

Quoted 6 lines

If the string to be duplicated is in .rodata, it's returned as is from devm_kstrdup_const(). Never gets added to the devres list, never get's freed. When the string to be duplicated is not in .rodata, devm_kstrdup() is called from devm_kstrdup_const(). Now the string is in the devres list of this device and it will get freed on driver detach. I really don't see what else could be a problem here.

Thanks for the clarification.
I think that it's worth adding a similar explanation to the changelog of the first patch.

Quoted 45 lines

BR
Bart

Quoted 41 lines
Quoted 36 lines

BR
Bart

Quoted 32 lines

And, please don't drop kfree(parent_names) here.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -0
      return ERR_PTR(err);
 }
 
@@ -351,15 +343,12 @@ static int plt_clk_probe(struct platform_device *pdev)
              goto err_unreg_clk_plt;
      }

drivers/clk/x86/clk-pmc-atom.c static int plt_clk_probe(struct platform_device *pdev) · line 362

Quoted 10 lines
drivers/clk/x86/clk-pmc-atom.c +0 -3
 
-     plt_clk_free_parent_names_loop(parent_names, data->nparents);
-
      platform_set_drvdata(pdev, data);
      return 0;
 
 err_unreg_clk_plt:
      plt_clk_unregister_loop(data, i);
      plt_clk_unregister_parents(data);
-     plt_clk_free_parent_names_loop(parent_names, data->nparents);

Ditto.

Quoted 6 lines
drivers/clk/x86/clk-pmc-atom.c +0 -1
      return err;
 }
 
--

2.18.0

--
Sincerely yours,
Mike.

--
Sincerely yours,
Mike.

Signature · 2 lines
Sincerely yours,
Mike.
#7 lore raw
In reply to Bartosz Golaszewski
Quoted 3 lines · Bartosz Golaszewski

Provide a resource managed version of kstrdup_const(). This variant internally calls devm_kstrdup() on pointers that are outside of .rodata section. Also provide a corresponding version of devm_kfree().

[]

mm/util.c

Quoted 1 line
mm/util.c +0 -0

[]

Quoted 17 lines

/**
* kstrdup - allocate space for and copy an existing string
* @s: the string to duplicate

mm/util.c +10 -0
@@ -78,6 +92,27 @@ const char *kstrdup_const(const char *s, gfp_t gfp)
 }
 EXPORT_SYMBOL(kstrdup_const);
 
+/**
+ * devm_kstrdup_const - resource managed conditional string duplication
+ * @dev: device for which to duplicate the string
+ * @s: the string to duplicate
+ * @gfp: the GFP mask used in the kmalloc() call when allocating memory
+ *
+ * Function returns source string if it is in .rodata section otherwise it
+ * fallbacks to devm_kstrdup.
+ *
+ * Strings allocated by devm_kstrdup_const will be automatically freed when

mm/util.c const char *kstrdup_const(const char *s, gfp_t gfp) · line 114

Quoted 10 lines
mm/util.c +10 -0
+ * the associated device is detached.
+ */
+char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
+{
+	if (is_kernel_rodata((unsigned long)s))
+		return s;
+
+	return devm_kstrdup(dev, s, gfp);
+}
+EXPORT_SYMBOL(devm_kstrdup_const);

Doesn't this lose constness and don't you get
a compiler warning here?

The kstrdup_const function returns a const char *, why shouldn't this?

#8 lore raw

2018-08-27 10:42 GMT+02:00 Joe Perches <[email protected]>:

Quoted 29 lines · Bartosz Golaszewski
Quoted 3 lines

Provide a resource managed version of kstrdup_const(). This variant internally calls devm_kstrdup() on pointers that are outside of .rodata section. Also provide a corresponding version of devm_kfree().

[]

mm/util.c

Quoted 1 line
mm/util.c +0 -0

[]

Quoted 22 lines

/**
* kstrdup - allocate space for and copy an existing string
* @s: the string to duplicate

mm/util.c +15 -0
@@ -78,6 +92,27 @@ const char *kstrdup_const(const char *s, gfp_t gfp)
 }
 EXPORT_SYMBOL(kstrdup_const);
 
+/**
+ * devm_kstrdup_const - resource managed conditional string duplication
+ * @dev: device for which to duplicate the string
+ * @s: the string to duplicate
+ * @gfp: the GFP mask used in the kmalloc() call when allocating memory
+ *
+ * Function returns source string if it is in .rodata section otherwise it
+ * fallbacks to devm_kstrdup.
+ *
+ * Strings allocated by devm_kstrdup_const will be automatically freed when
+ * the associated device is detached.
+ */
+char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
+{
+     if (is_kernel_rodata((unsigned long)s))
Quoted 9 lines

mm/util.c const char *kstrdup_const(const char *s, gfp_t gfp) · line 114

Quoted 5 lines
mm/util.c +5 -0
+             return s;
+
+     return devm_kstrdup(dev, s, gfp);
+}
+EXPORT_SYMBOL(devm_kstrdup_const);

Doesn't this lose constness and don't you get
a compiler warning here?

Yes it does but for some reason gcc 6.3 didn't complain...

Quoted 3 lines

The kstrdup_const function returns a const char *, why shouldn't this?

It probably should, I'll fix it for v2.

Bart

#9 lore raw
In reply to Bartosz Golaszewski
Quoted 51 lines · Bartosz Golaszewski

Provide a resource managed version of kstrdup_const(). This variant internally calls devm_kstrdup() on pointers that are outside of .rodata section. Also provide a corresponding version of devm_kfree().

---
 include/linux/device.h |  2 ++
 mm/util.c              | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
include/linux/device.h +2 -0
index 8f882549edee..f8f5982d26b2 100644
@@ -693,7 +693,9 @@ static inline void *devm_kcalloc(struct device *dev,
 	return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
 }
 extern void devm_kfree(struct device *dev, void *p);
+extern void devm_kfree_const(struct device *dev, void *p);
 extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
+extern char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
 extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
 			  gfp_t gfp);
 
mm/util.c +14 -0
index d2890a407332..6d1f41b5775e 100644
@@ -39,6 +39,20 @@ void kfree_const(const void *x)
 }
 EXPORT_SYMBOL(kfree_const);
 
+/**
+ * devm_kfree_const - Resource managed conditional kfree
+ * @dev: device this memory belongs to
+ * @p: memory to free
+ *
+ * Function calls devm_kfree only if @p is not in .rodata section.
+ */
+void devm_kfree_const(struct device *dev, void *p)
+{
+	if (!is_kernel_rodata((unsigned long)p))
+		devm_kfree(dev, p);
+}
+EXPORT_SYMBOL(devm_kfree_const);
+
 /**
  * kstrdup - allocate space for and copy an existing string
  * @s: the string to duplicate
@@ -78,6 +92,27 @@ const char *kstrdup_const(const char *s, gfp_t gfp)
 }

mm/util.c const char *kstrdup_const(const char *s, gfp_t gfp) · line 102

Quoted 10 lines
mm/util.c +8 -0
 EXPORT_SYMBOL(kstrdup_const);
 
+/**
+ * devm_kstrdup_const - resource managed conditional string duplication
+ * @dev: device for which to duplicate the string
+ * @s: the string to duplicate
+ * @gfp: the GFP mask used in the kmalloc() call when allocating memory
+ *
+ * Function returns source string if it is in .rodata section otherwise it
+ * fallbacks to devm_kstrdup.

Please make it proper "Returns:" description and move to the end of the comment. See Documentation/doc-guide/kernel-doc.rst.

mm/util.c const char *kstrdup_const(const char *s, gfp_t gfp) · line 115

Quoted 12 lines
mm/util.c +12 -0
+ * Strings allocated by devm_kstrdup_const will be automatically freed when
+ * the associated device is detached.
+ */
+char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
+{
+	if (is_kernel_rodata((unsigned long)s))
+		return s;
+
+	return devm_kstrdup(dev, s, gfp);
+}
+EXPORT_SYMBOL(devm_kstrdup_const);
+

The devm_ variants seem to belong to drivers/base/devres.c rather than mm/util.c

Quoted 6 lines
mm/util.c +0 -0
 /**
  * kstrndup - allocate space for and copy an existing string
  * @s: the string to duplicate
Signature · 1 line
2.18.0
Signature · 2 lines
Sincerely yours,
Mike.
#10 lore raw

2018-08-27 12:33 GMT+02:00 Mike Rapoport <[email protected]>:

Quoted 54 lines · Bartosz Golaszewski
Quoted 53 lines

Provide a resource managed version of kstrdup_const(). This variant internally calls devm_kstrdup() on pointers that are outside of .rodata section. Also provide a corresponding version of devm_kfree().

---
 include/linux/device.h |  2 ++
 mm/util.c              | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
include/linux/device.h +2 -0
index 8f882549edee..f8f5982d26b2 100644
@@ -693,7 +693,9 @@ static inline void *devm_kcalloc(struct device *dev,
      return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
 }
 extern void devm_kfree(struct device *dev, void *p);
+extern void devm_kfree_const(struct device *dev, void *p);
 extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
+extern char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
 extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
                        gfp_t gfp);
 
mm/util.c +14 -0
index d2890a407332..6d1f41b5775e 100644
@@ -39,6 +39,20 @@ void kfree_const(const void *x)
 }
 EXPORT_SYMBOL(kfree_const);
 
+/**
+ * devm_kfree_const - Resource managed conditional kfree
+ * @dev: device this memory belongs to
+ * @p: memory to free
+ *
+ * Function calls devm_kfree only if @p is not in .rodata section.
+ */
+void devm_kfree_const(struct device *dev, void *p)
+{
+     if (!is_kernel_rodata((unsigned long)p))
+             devm_kfree(dev, p);
+}
+EXPORT_SYMBOL(devm_kfree_const);
+
 /**
  * kstrdup - allocate space for and copy an existing string
  * @s: the string to duplicate
@@ -78,6 +92,27 @@ const char *kstrdup_const(const char *s, gfp_t gfp)
 }
 EXPORT_SYMBOL(kstrdup_const);
 
Quoted 12 lines

mm/util.c const char *kstrdup_const(const char *s, gfp_t gfp) · line 102

Quoted 8 lines
mm/util.c +8 -0
+/**
+ * devm_kstrdup_const - resource managed conditional string duplication
+ * @dev: device for which to duplicate the string
+ * @s: the string to duplicate
+ * @gfp: the GFP mask used in the kmalloc() call when allocating memory
+ *
+ * Function returns source string if it is in .rodata section otherwise it
+ * fallbacks to devm_kstrdup.

Please make it proper "Returns:" description and move to the end of the comment. See Documentation/doc-guide/kernel-doc.rst.

Sure.

Quoted 7 lines
Quoted 7 lines
mm/util.c +7 -0
+ * Strings allocated by devm_kstrdup_const will be automatically freed when
+ * the associated device is detached.
+ */
+char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
+{
+     if (is_kernel_rodata((unsigned long)s))
+             return s;
Quoted 9 lines

mm/util.c const char *kstrdup_const(const char *s, gfp_t gfp) · line 115

Quoted 5 lines
mm/util.c +5 -0
+
+     return devm_kstrdup(dev, s, gfp);
+}
+EXPORT_SYMBOL(devm_kstrdup_const);
+

The devm_ variants seem to belong to drivers/base/devres.c rather than mm/util.c

Not all devm_ variants live in drivers/base/devres.c, many subsystems implement them locally. In this case we need to choose between exporting is_kernel_rodata() and putting devm_kstrdup_const() in mm/util.c. I chose the latter, since it's cleaner.

Bart

Quoted 11 lines
Quoted 6 lines
mm/util.c +0 -1
 /**
  * kstrndup - allocate space for and copy an existing string
  * @s: the string to duplicate
--

2.18.0

--
Sincerely yours,
Mike.

#11 lore raw
Quoted 30 lines · Bartosz Golaszewski

2018-08-27 12:33 GMT+02:00 Mike Rapoport <[email protected]>:

Quoted 29 lines · Bartosz Golaszewski
Quoted 28 lines

Provide a resource managed version of kstrdup_const(). This variant internally calls devm_kstrdup() on pointers that are outside of .rodata section. Also provide a corresponding version of devm_kfree().

---
 include/linux/device.h |  2 ++
 mm/util.c              | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
include/linux/device.h +2 -0
index 8f882549edee..f8f5982d26b2 100644
@@ -693,7 +693,9 @@ static inline void *devm_kcalloc(struct device *dev,
      return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
 }
 extern void devm_kfree(struct device *dev, void *p);
+extern void devm_kfree_const(struct device *dev, void *p);
 extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
+extern char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
 extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
                        gfp_t gfp);
 
mm/util.c +0 -0
index d2890a407332..6d1f41b5775e 100644

[ ... ]

Quoted 17 lines
Quoted 7 lines
Quoted 7 lines

+ * Strings allocated by devm_kstrdup_const will be automatically freed when
+ * the associated device is detached.
+ */

mm/util.c +4 -0
+char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
+{
+     if (is_kernel_rodata((unsigned long)s))
+             return s;
Quoted 9 lines

mm/util.c const char *kstrdup_const(const char *s, gfp_t gfp) · line 115

Quoted 5 lines
mm/util.c +5 -0
+
+     return devm_kstrdup(dev, s, gfp);
+}
+EXPORT_SYMBOL(devm_kstrdup_const);
+

The devm_ variants seem to belong to drivers/base/devres.c rather than mm/util.c

Quoted 4 lines

Not all devm_ variants live in drivers/base/devres.c, many subsystems implement them locally. In this case we need to choose between exporting is_kernel_rodata() and putting devm_kstrdup_const() in mm/util.c. I chose the latter, since it's cleaner.

I rather think that moving is_kernel_rodata() to include/asm-generic/sections.h and having devm_kstrdup_const() next to devm_kstrdup() would be cleaner.

Quoted 2 lines

Bart

Signature · 2 lines
Sincerely yours,
Mike.
#12 lore raw
In reply to Bartosz Golaszewski
I love your patch! Perhaps something to improve:

Hi Bartosz,

I love your patch! Perhaps something to improve:

[auto build test WARNING on clk/clk-next]
[also build test WARNING on v4.19-rc1 next-20180827]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Bartosz-Golaszewski/devres-provide-devm_kstrdup_const/20180827-162336
base:   https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
config: powerpc-mpc836x_mds_defconfig (attached as .config)
compiler: powerpc-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.2.0 make.cross ARCH=powerpc

All warnings (new ones prefixed by >>):

mm/util.c: In function 'devm_kstrdup_const':

>> mm/util.c:110:10: warning: return discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
      return s;
             ^

vim +/const +110 mm/util.c

94
95 /**
96 * devm_kstrdup_const - resource managed conditional string duplication
97 * @dev: device for which to duplicate the string
98 * @s: the string to duplicate
99 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
100 *
101 * Function returns source string if it is in .rodata section otherwise it
102 * fallbacks to devm_kstrdup.
103 *
104 * Strings allocated by devm_kstrdup_const will be automatically freed when
105 * the associated device is detached.
106 */
107 char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
108 {
109 if (is_kernel_rodata((unsigned long)s))

 > 110			return s;

111
112 return devm_kstrdup(dev, s, gfp);
113 }
114 EXPORT_SYMBOL(devm_kstrdup_const);
115

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation