Re: [PATCH V3 1/2] devres: introduce API "devm_kstrdup"

2014-01-29 Thread Manish Badarkhe
Hi Mark,

On Wed, Jan 29, 2014 at 5:10 PM, Mark Brown  wrote:
> On Wed, Jan 29, 2014 at 04:38:20PM +0530, Manish Badarkhe wrote:
>> On Wed, Jan 29, 2014 at 9:48 AM, Joe Perches  wrote:
>
>> >> + buf = devm_kzalloc(dev, size, gfp);
>
>> > If this is really necessary, please use devm_kmalloc
>
>> devm_kzalloc is always better giving zeroed memory locations.
>> Is there any reason not to go for it?
>
> If the allocated memory is going to be immediately overwritten with the
> string then it shouldn't make any difference if it was zeroed when
> allocated.

Thank you for clarification. I will go ahead to make it devm_kmalloc
and post a new version of patch.

Regards
Manish Badarkhe
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH V3 1/2] devres: introduce API "devm_kstrdup"

2014-01-29 Thread Mark Brown
On Wed, Jan 29, 2014 at 04:38:20PM +0530, Manish Badarkhe wrote:
> On Wed, Jan 29, 2014 at 9:48 AM, Joe Perches  wrote:

> >> + buf = devm_kzalloc(dev, size, gfp);

> > If this is really necessary, please use devm_kmalloc

> devm_kzalloc is always better giving zeroed memory locations.
> Is there any reason not to go for it?

If the allocated memory is going to be immediately overwritten with the
string then it shouldn't make any difference if it was zeroed when
allocated.


signature.asc
Description: Digital signature


Re: [PATCH V3 1/2] devres: introduce API "devm_kstrdup"

2014-01-29 Thread Manish Badarkhe
Hi Joe,

Thank you for your review.

On Wed, Jan 29, 2014 at 9:48 AM, Joe Perches  wrote:
> On Wed, 2014-01-29 at 09:33 +0530, Manish Badarkhe wrote:
>> This patch introduces "devm_kstrdup" API so that the
>> device's driver can allocate memory and copy string.
> []
>> diff --git a/drivers/base/devres.c b/drivers/base/devres.c
> []
>> @@ -791,6 +791,32 @@ void * devm_kmalloc(struct device *dev, size_t size, 
>> gfp_t gfp)
>>  EXPORT_SYMBOL_GPL(devm_kmalloc);
>>
>>  /**
>> + * devm_kstrdup - Allocate resource managed space and
>> + *and copy an existing string
>> + * @dev: Device to allocate memory for
>> + * @s: the string to duplicate
>> + * @size: Allocation size
>
> Why is size necessary at all?
> I think it should be calculated by strlen

I thought of avoiding string length calculation in function.
But,yes its better to do it in function to avoid extra parsing
of argument to function.Will update code and
post a patch.

>> +char *devm_kstrdup(struct device *dev,
>> +const char *s, size_t size, gfp_t gfp)
>> +{
>> + char *buf;
>> +
>> + if (!s)
>> + return NULL;
>> +
>> + buf = devm_kzalloc(dev, size, gfp);
>
> If this is really necessary, please use devm_kmalloc

devm_kzalloc is always better giving zeroed memory locations.
Is there any reason not to go for it?

Regards
Manish Badarkhe
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH V3 1/2] devres: introduce API devm_kstrdup

2014-01-29 Thread Manish Badarkhe
Hi Joe,

Thank you for your review.

On Wed, Jan 29, 2014 at 9:48 AM, Joe Perches j...@perches.com wrote:
 On Wed, 2014-01-29 at 09:33 +0530, Manish Badarkhe wrote:
 This patch introduces devm_kstrdup API so that the
 device's driver can allocate memory and copy string.
 []
 diff --git a/drivers/base/devres.c b/drivers/base/devres.c
 []
 @@ -791,6 +791,32 @@ void * devm_kmalloc(struct device *dev, size_t size, 
 gfp_t gfp)
  EXPORT_SYMBOL_GPL(devm_kmalloc);

  /**
 + * devm_kstrdup - Allocate resource managed space and
 + *and copy an existing string
 + * @dev: Device to allocate memory for
 + * @s: the string to duplicate
 + * @size: Allocation size

 Why is size necessary at all?
 I think it should be calculated by strlen

I thought of avoiding string length calculation in function.
But,yes its better to do it in function to avoid extra parsing
of argument to function.Will update code and
post a patch.

 +char *devm_kstrdup(struct device *dev,
 +const char *s, size_t size, gfp_t gfp)
 +{
 + char *buf;
 +
 + if (!s)
 + return NULL;
 +
 + buf = devm_kzalloc(dev, size, gfp);

 If this is really necessary, please use devm_kmalloc

devm_kzalloc is always better giving zeroed memory locations.
Is there any reason not to go for it?

Regards
Manish Badarkhe
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH V3 1/2] devres: introduce API devm_kstrdup

2014-01-29 Thread Mark Brown
On Wed, Jan 29, 2014 at 04:38:20PM +0530, Manish Badarkhe wrote:
 On Wed, Jan 29, 2014 at 9:48 AM, Joe Perches j...@perches.com wrote:

  + buf = devm_kzalloc(dev, size, gfp);

  If this is really necessary, please use devm_kmalloc

 devm_kzalloc is always better giving zeroed memory locations.
 Is there any reason not to go for it?

If the allocated memory is going to be immediately overwritten with the
string then it shouldn't make any difference if it was zeroed when
allocated.


signature.asc
Description: Digital signature


Re: [PATCH V3 1/2] devres: introduce API devm_kstrdup

2014-01-29 Thread Manish Badarkhe
Hi Mark,

On Wed, Jan 29, 2014 at 5:10 PM, Mark Brown broo...@kernel.org wrote:
 On Wed, Jan 29, 2014 at 04:38:20PM +0530, Manish Badarkhe wrote:
 On Wed, Jan 29, 2014 at 9:48 AM, Joe Perches j...@perches.com wrote:

  + buf = devm_kzalloc(dev, size, gfp);

  If this is really necessary, please use devm_kmalloc

 devm_kzalloc is always better giving zeroed memory locations.
 Is there any reason not to go for it?

 If the allocated memory is going to be immediately overwritten with the
 string then it shouldn't make any difference if it was zeroed when
 allocated.

Thank you for clarification. I will go ahead to make it devm_kmalloc
and post a new version of patch.

Regards
Manish Badarkhe
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH V3 1/2] devres: introduce API "devm_kstrdup"

2014-01-28 Thread Joe Perches
On Wed, 2014-01-29 at 09:33 +0530, Manish Badarkhe wrote:
> This patch introduces "devm_kstrdup" API so that the
> device's driver can allocate memory and copy string.
[]
> diff --git a/drivers/base/devres.c b/drivers/base/devres.c
[]
> @@ -791,6 +791,32 @@ void * devm_kmalloc(struct device *dev, size_t size, 
> gfp_t gfp)
>  EXPORT_SYMBOL_GPL(devm_kmalloc);
>  
>  /**
> + * devm_kstrdup - Allocate resource managed space and
> + *and copy an existing string
> + * @dev: Device to allocate memory for
> + * @s: the string to duplicate
> + * @size: Allocation size

Why is size necessary at all?
I think it should be calculated by strlen

> +char *devm_kstrdup(struct device *dev,
> +const char *s, size_t size, gfp_t gfp)
> +{
> + char *buf;
> +
> + if (!s)
> + return NULL;
> +
> + buf = devm_kzalloc(dev, size, gfp);

If this is really necessary, please use devm_kmalloc


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH V3 1/2] devres: introduce API "devm_kstrdup"

2014-01-28 Thread Manish Badarkhe
This patch introduces "devm_kstrdup" API so that the
device's driver can allocate memory and copy string.

Signed-off-by: Manish Badarkhe 
---
:100644 100644 545c4de... 6e88a3d... M  drivers/base/devres.c
:100644 100644 952b010... 8fee649... M  include/linux/device.h
 drivers/base/devres.c  |   26 ++
 include/linux/device.h |2 ++
 2 files changed, 28 insertions(+)

diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 545c4de..6e88a3d 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -791,6 +791,32 @@ void * devm_kmalloc(struct device *dev, size_t size, gfp_t 
gfp)
 EXPORT_SYMBOL_GPL(devm_kmalloc);
 
 /**
+ * devm_kstrdup - Allocate resource managed space and
+ *and copy an existing string
+ * @dev: Device to allocate memory for
+ * @s: the string to duplicate
+ * @size: Allocation size
+ * @gfp: the GFP mask used in the devm_kzalloc() call when
+ *   allocating memory
+ * RETURNS:
+ * Pointer to allocated string on success, NULL on failure.
+ */
+char *devm_kstrdup(struct device *dev,
+  const char *s, size_t size, gfp_t gfp)
+{
+   char *buf;
+
+   if (!s)
+   return NULL;
+
+   buf = devm_kzalloc(dev, size, gfp);
+   if (buf)
+   memcpy(buf, s, size);
+   return buf;
+}
+EXPORT_SYMBOL_GPL(devm_kstrdup);
+
+/**
  * devm_kfree - Resource-managed kfree
  * @dev: Device this memory belongs to
  * @p: Memory to free
diff --git a/include/linux/device.h b/include/linux/device.h
index 952b010..8fee649 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -626,6 +626,8 @@ 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 char *devm_kstrdup(struct device *dev, const char *s, size_t size,
+   gfp_t gfp);
 
 void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
 void __iomem *devm_request_and_ioremap(struct device *dev,
-- 
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH V3 1/2] devres: introduce API devm_kstrdup

2014-01-28 Thread Manish Badarkhe
This patch introduces devm_kstrdup API so that the
device's driver can allocate memory and copy string.

Signed-off-by: Manish Badarkhe badarkhe.man...@gmail.com
---
:100644 100644 545c4de... 6e88a3d... M  drivers/base/devres.c
:100644 100644 952b010... 8fee649... M  include/linux/device.h
 drivers/base/devres.c  |   26 ++
 include/linux/device.h |2 ++
 2 files changed, 28 insertions(+)

diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 545c4de..6e88a3d 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -791,6 +791,32 @@ void * devm_kmalloc(struct device *dev, size_t size, gfp_t 
gfp)
 EXPORT_SYMBOL_GPL(devm_kmalloc);
 
 /**
+ * devm_kstrdup - Allocate resource managed space and
+ *and copy an existing string
+ * @dev: Device to allocate memory for
+ * @s: the string to duplicate
+ * @size: Allocation size
+ * @gfp: the GFP mask used in the devm_kzalloc() call when
+ *   allocating memory
+ * RETURNS:
+ * Pointer to allocated string on success, NULL on failure.
+ */
+char *devm_kstrdup(struct device *dev,
+  const char *s, size_t size, gfp_t gfp)
+{
+   char *buf;
+
+   if (!s)
+   return NULL;
+
+   buf = devm_kzalloc(dev, size, gfp);
+   if (buf)
+   memcpy(buf, s, size);
+   return buf;
+}
+EXPORT_SYMBOL_GPL(devm_kstrdup);
+
+/**
  * devm_kfree - Resource-managed kfree
  * @dev: Device this memory belongs to
  * @p: Memory to free
diff --git a/include/linux/device.h b/include/linux/device.h
index 952b010..8fee649 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -626,6 +626,8 @@ 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 char *devm_kstrdup(struct device *dev, const char *s, size_t size,
+   gfp_t gfp);
 
 void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
 void __iomem *devm_request_and_ioremap(struct device *dev,
-- 
1.7.10.4

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH V3 1/2] devres: introduce API devm_kstrdup

2014-01-28 Thread Joe Perches
On Wed, 2014-01-29 at 09:33 +0530, Manish Badarkhe wrote:
 This patch introduces devm_kstrdup API so that the
 device's driver can allocate memory and copy string.
[]
 diff --git a/drivers/base/devres.c b/drivers/base/devres.c
[]
 @@ -791,6 +791,32 @@ void * devm_kmalloc(struct device *dev, size_t size, 
 gfp_t gfp)
  EXPORT_SYMBOL_GPL(devm_kmalloc);
  
  /**
 + * devm_kstrdup - Allocate resource managed space and
 + *and copy an existing string
 + * @dev: Device to allocate memory for
 + * @s: the string to duplicate
 + * @size: Allocation size

Why is size necessary at all?
I think it should be calculated by strlen

 +char *devm_kstrdup(struct device *dev,
 +const char *s, size_t size, gfp_t gfp)
 +{
 + char *buf;
 +
 + if (!s)
 + return NULL;
 +
 + buf = devm_kzalloc(dev, size, gfp);

If this is really necessary, please use devm_kmalloc


--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/