LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()
@ 2018-06-01  8:31 Andy Shevchenko
  2018-06-01  8:31 ` [PATCH v1 2/3] Input: gpio-keys - Switch to bitmap_zalloc() Andy Shevchenko
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Andy Shevchenko @ 2018-06-01  8:31 UTC (permalink / raw)
  To: Dmitry Torokhov, Jeffy Chen, linux-input, linux-kernel; +Cc: Andy Shevchenko

A lot of code become ugly because of open coding allocations for bitmaps.

Introduce three helpers to allow users be more clear of intention
and keep their code neat.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 include/linux/bitmap.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 1ee46f492267..845822425393 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -6,6 +6,7 @@
 
 #include <linux/types.h>
 #include <linux/bitops.h>
+#include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/kernel.h>
 
@@ -104,6 +105,21 @@
  * contain all bit positions from 0 to 'bits' - 1.
  */
 
+static inline unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
+{
+	return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long), flags);
+}
+
+static inline unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
+{
+	return kcalloc(BITS_TO_LONGS(nbits), sizeof(unsigned long), flags);
+}
+
+static inline void bitmap_free(const unsigned long *bitmap)
+{
+	kfree(bitmap);
+}
+
 /*
  * lib/bitmap.c provides these functions:
  */
-- 
2.17.0

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v1 2/3] Input: gpio-keys - Switch to bitmap_zalloc()
  2018-06-01  8:31 [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free() Andy Shevchenko
@ 2018-06-01  8:31 ` Andy Shevchenko
  2018-06-01 18:34   ` Dmitry Torokhov
  2018-06-01  8:31 ` [PATCH v1 3/3] Input: evdev " Andy Shevchenko
  2018-06-01 18:33 ` [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free() Dmitry Torokhov
  2 siblings, 1 reply; 9+ messages in thread
From: Andy Shevchenko @ 2018-06-01  8:31 UTC (permalink / raw)
  To: Dmitry Torokhov, Jeffy Chen, linux-input, linux-kernel; +Cc: Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating.
Besides that it returns pointer of bitmap type instead of opaque void *.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/input/keyboard/gpio_keys.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index 052e37675086..492a971b95b5 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -196,7 +196,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
 	ssize_t ret;
 	int i;
 
-	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
+	bits = bitmap_zalloc(n_events, GFP_KERNEL);
 	if (!bits)
 		return -ENOMEM;
 
@@ -216,7 +216,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
 	buf[ret++] = '\n';
 	buf[ret] = '\0';
 
-	kfree(bits);
+	bitmap_free(bits);
 
 	return ret;
 }
@@ -240,7 +240,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
 	ssize_t error;
 	int i;
 
-	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
+	bits = bitmap_zalloc(n_events, GFP_KERNEL);
 	if (!bits)
 		return -ENOMEM;
 
@@ -284,7 +284,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
 	mutex_unlock(&ddata->disable_lock);
 
 out:
-	kfree(bits);
+	bitmap_free(bits);
 	return error;
 }
 
-- 
2.17.0

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v1 3/3] Input: evdev - Switch to bitmap_zalloc()
  2018-06-01  8:31 [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free() Andy Shevchenko
  2018-06-01  8:31 ` [PATCH v1 2/3] Input: gpio-keys - Switch to bitmap_zalloc() Andy Shevchenko
@ 2018-06-01  8:31 ` Andy Shevchenko
  2018-06-01 18:34   ` Dmitry Torokhov
  2018-06-01 18:33 ` [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free() Dmitry Torokhov
  2 siblings, 1 reply; 9+ messages in thread
From: Andy Shevchenko @ 2018-06-01  8:31 UTC (permalink / raw)
  To: Dmitry Torokhov, Jeffy Chen, linux-input, linux-kernel; +Cc: Andy Shevchenko

Switch to bitmap_zalloc() to show clearly what we are allocating.
Besides that it returns pointer of bitmap type instead of opaque void *.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/input/evdev.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
index c81c79d01d93..3f87ef973bc7 100644
--- a/drivers/input/evdev.c
+++ b/drivers/input/evdev.c
@@ -481,7 +481,7 @@ static int evdev_release(struct inode *inode, struct file *file)
 	evdev_detach_client(evdev, client);
 
 	for (i = 0; i < EV_CNT; ++i)
-		kfree(client->evmasks[i]);
+		bitmap_free(client->evmasks[i]);
 
 	kvfree(client);
 
@@ -925,17 +925,15 @@ static int evdev_handle_get_val(struct evdev_client *client,
 {
 	int ret;
 	unsigned long *mem;
-	size_t len;
 
-	len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
-	mem = kmalloc(len, GFP_KERNEL);
+	mem = bitmap_malloc(maxbit, GFP_KERNEL);
 	if (!mem)
 		return -ENOMEM;
 
 	spin_lock_irq(&dev->event_lock);
 	spin_lock(&client->buffer_lock);
 
-	memcpy(mem, bits, len);
+	bitmap_copy(mem, bits, maxbit);
 
 	spin_unlock(&dev->event_lock);
 
@@ -947,7 +945,7 @@ static int evdev_handle_get_val(struct evdev_client *client,
 	if (ret < 0)
 		evdev_queue_syn_dropped(client);
 
-	kfree(mem);
+	bitmap_free(mem);
 
 	return ret;
 }
@@ -1003,13 +1001,13 @@ static int evdev_set_mask(struct evdev_client *client,
 	if (!cnt)
 		return 0;
 
-	mask = kcalloc(sizeof(unsigned long), BITS_TO_LONGS(cnt), GFP_KERNEL);
+	mask = bitmap_zalloc(cnt, GFP_KERNEL);
 	if (!mask)
 		return -ENOMEM;
 
 	error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);
 	if (error < 0) {
-		kfree(mask);
+		bitmap_free(mask);
 		return error;
 	}
 
@@ -1018,7 +1016,7 @@ static int evdev_set_mask(struct evdev_client *client,
 	client->evmasks[type] = mask;
 	spin_unlock_irqrestore(&client->buffer_lock, flags);
 
-	kfree(oldmask);
+	bitmap_free(oldmask);
 
 	return 0;
 }
-- 
2.17.0

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()
  2018-06-01  8:31 [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free() Andy Shevchenko
  2018-06-01  8:31 ` [PATCH v1 2/3] Input: gpio-keys - Switch to bitmap_zalloc() Andy Shevchenko
  2018-06-01  8:31 ` [PATCH v1 3/3] Input: evdev " Andy Shevchenko
@ 2018-06-01 18:33 ` Dmitry Torokhov
  2018-06-04  9:59   ` Andy Shevchenko
  2 siblings, 1 reply; 9+ messages in thread
From: Dmitry Torokhov @ 2018-06-01 18:33 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Jeffy Chen, linux-input, linux-kernel

Hi Andy,

On Fri, Jun 01, 2018 at 11:31:18AM +0300, Andy Shevchenko wrote:
> A lot of code become ugly because of open coding allocations for bitmaps.
> 
> Introduce three helpers to allow users be more clear of intention
> and keep their code neat.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

This looks nice and I like how it simplifies drivers. How do we merge
this? 

> ---
>  include/linux/bitmap.h | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
> index 1ee46f492267..845822425393 100644
> --- a/include/linux/bitmap.h
> +++ b/include/linux/bitmap.h
> @@ -6,6 +6,7 @@
>  
>  #include <linux/types.h>
>  #include <linux/bitops.h>
> +#include <linux/slab.h>
>  #include <linux/string.h>
>  #include <linux/kernel.h>
>  
> @@ -104,6 +105,21 @@
>   * contain all bit positions from 0 to 'bits' - 1.
>   */
>  
> +static inline unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
> +{
> +	return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long), flags);
> +}
> +
> +static inline unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
> +{
> +	return kcalloc(BITS_TO_LONGS(nbits), sizeof(unsigned long), flags);

	retrun bitmap_alloc(nbits, flags | __GFP_ZERO);

?

> +}
> +
> +static inline void bitmap_free(const unsigned long *bitmap)
> +{
> +	kfree(bitmap);
> +}
> +
>  /*
>   * lib/bitmap.c provides these functions:
>   */
> -- 
> 2.17.0
> 

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v1 2/3] Input: gpio-keys - Switch to bitmap_zalloc()
  2018-06-01  8:31 ` [PATCH v1 2/3] Input: gpio-keys - Switch to bitmap_zalloc() Andy Shevchenko
@ 2018-06-01 18:34   ` Dmitry Torokhov
  0 siblings, 0 replies; 9+ messages in thread
From: Dmitry Torokhov @ 2018-06-01 18:34 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Jeffy Chen, linux-input, linux-kernel

On Fri, Jun 01, 2018 at 11:31:19AM +0300, Andy Shevchenko wrote:
> Switch to bitmap_zalloc() to show clearly what we are allocating.
> Besides that it returns pointer of bitmap type instead of opaque void *.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

> ---
>  drivers/input/keyboard/gpio_keys.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
> index 052e37675086..492a971b95b5 100644
> --- a/drivers/input/keyboard/gpio_keys.c
> +++ b/drivers/input/keyboard/gpio_keys.c
> @@ -196,7 +196,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
>  	ssize_t ret;
>  	int i;
>  
> -	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
> +	bits = bitmap_zalloc(n_events, GFP_KERNEL);
>  	if (!bits)
>  		return -ENOMEM;
>  
> @@ -216,7 +216,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
>  	buf[ret++] = '\n';
>  	buf[ret] = '\0';
>  
> -	kfree(bits);
> +	bitmap_free(bits);
>  
>  	return ret;
>  }
> @@ -240,7 +240,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
>  	ssize_t error;
>  	int i;
>  
> -	bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
> +	bits = bitmap_zalloc(n_events, GFP_KERNEL);
>  	if (!bits)
>  		return -ENOMEM;
>  
> @@ -284,7 +284,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
>  	mutex_unlock(&ddata->disable_lock);
>  
>  out:
> -	kfree(bits);
> +	bitmap_free(bits);
>  	return error;
>  }
>  
> -- 
> 2.17.0
> 

-- 
Dmitry

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v1 3/3] Input: evdev - Switch to bitmap_zalloc()
  2018-06-01  8:31 ` [PATCH v1 3/3] Input: evdev " Andy Shevchenko
@ 2018-06-01 18:34   ` Dmitry Torokhov
  0 siblings, 0 replies; 9+ messages in thread
From: Dmitry Torokhov @ 2018-06-01 18:34 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Jeffy Chen, linux-input, linux-kernel

On Fri, Jun 01, 2018 at 11:31:20AM +0300, Andy Shevchenko wrote:
> Switch to bitmap_zalloc() to show clearly what we are allocating.
> Besides that it returns pointer of bitmap type instead of opaque void *.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

> ---
>  drivers/input/evdev.c | 16 +++++++---------
>  1 file changed, 7 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
> index c81c79d01d93..3f87ef973bc7 100644
> --- a/drivers/input/evdev.c
> +++ b/drivers/input/evdev.c
> @@ -481,7 +481,7 @@ static int evdev_release(struct inode *inode, struct file *file)
>  	evdev_detach_client(evdev, client);
>  
>  	for (i = 0; i < EV_CNT; ++i)
> -		kfree(client->evmasks[i]);
> +		bitmap_free(client->evmasks[i]);
>  
>  	kvfree(client);
>  
> @@ -925,17 +925,15 @@ static int evdev_handle_get_val(struct evdev_client *client,
>  {
>  	int ret;
>  	unsigned long *mem;
> -	size_t len;
>  
> -	len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
> -	mem = kmalloc(len, GFP_KERNEL);
> +	mem = bitmap_malloc(maxbit, GFP_KERNEL);
>  	if (!mem)
>  		return -ENOMEM;
>  
>  	spin_lock_irq(&dev->event_lock);
>  	spin_lock(&client->buffer_lock);
>  
> -	memcpy(mem, bits, len);
> +	bitmap_copy(mem, bits, maxbit);
>  
>  	spin_unlock(&dev->event_lock);
>  
> @@ -947,7 +945,7 @@ static int evdev_handle_get_val(struct evdev_client *client,
>  	if (ret < 0)
>  		evdev_queue_syn_dropped(client);
>  
> -	kfree(mem);
> +	bitmap_free(mem);
>  
>  	return ret;
>  }
> @@ -1003,13 +1001,13 @@ static int evdev_set_mask(struct evdev_client *client,
>  	if (!cnt)
>  		return 0;
>  
> -	mask = kcalloc(sizeof(unsigned long), BITS_TO_LONGS(cnt), GFP_KERNEL);
> +	mask = bitmap_zalloc(cnt, GFP_KERNEL);
>  	if (!mask)
>  		return -ENOMEM;
>  
>  	error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);
>  	if (error < 0) {
> -		kfree(mask);
> +		bitmap_free(mask);
>  		return error;
>  	}
>  
> @@ -1018,7 +1016,7 @@ static int evdev_set_mask(struct evdev_client *client,
>  	client->evmasks[type] = mask;
>  	spin_unlock_irqrestore(&client->buffer_lock, flags);
>  
> -	kfree(oldmask);
> +	bitmap_free(oldmask);
>  
>  	return 0;
>  }
> -- 
> 2.17.0
> 

-- 
Dmitry

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()
  2018-06-01 18:33 ` [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free() Dmitry Torokhov
@ 2018-06-04  9:59   ` Andy Shevchenko
  2018-06-04 21:57     ` Dmitry Torokhov
  0 siblings, 1 reply; 9+ messages in thread
From: Andy Shevchenko @ 2018-06-04  9:59 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: Jeffy Chen, linux-input, linux-kernel

On Fri, 2018-06-01 at 11:33 -0700, Dmitry Torokhov wrote:
> Hi Andy,
> 
> On Fri, Jun 01, 2018 at 11:31:18AM +0300, Andy Shevchenko wrote:
> > A lot of code become ugly because of open coding allocations for
> > bitmaps.
> > 
> > Introduce three helpers to allow users be more clear of intention
> > and keep their code neat.
> > 
> > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> 
> This looks nice and I like how it simplifies drivers.

Thanks!

>  How do we merge
> this? 

I suppose through 'input' tree if there is no objections.

> > ---
> >  include/linux/bitmap.h | 16 ++++++++++++++++
> >  1 file changed, 16 insertions(+)
> > 
> > diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
> > index 1ee46f492267..845822425393 100644
> > --- a/include/linux/bitmap.h
> > +++ b/include/linux/bitmap.h
> > @@ -6,6 +6,7 @@
> >  
> >  #include <linux/types.h>
> >  #include <linux/bitops.h>
> > +#include <linux/slab.h>
> >  #include <linux/string.h>
> >  #include <linux/kernel.h>
> >  
> > @@ -104,6 +105,21 @@
> >   * contain all bit positions from 0 to 'bits' - 1.
> >   */
> >  
> > +static inline unsigned long *bitmap_alloc(unsigned int nbits, gfp_t
> > flags)
> > +{
> > +	return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned
> > long), flags);
> > +}
> > +
> > +static inline unsigned long *bitmap_zalloc(unsigned int nbits,
> > gfp_t flags)
> > +{
> > +	return kcalloc(BITS_TO_LONGS(nbits), sizeof(unsigned long),
> > flags);
> 
> 	retrun bitmap_alloc(nbits, flags | __GFP_ZERO);
> 
> ?

I though about this, but decide not to rely on linux/gfp.h.
If you think explicit __GFP_ZERO is better, I can replace in v2, or if
you have a chance to do that when applying it would be appreciated.

> 
> > +}
> > +
> > +static inline void bitmap_free(const unsigned long *bitmap)
> > +{
> > +	kfree(bitmap);
> > +}
> > +
> >  /*
> >   * lib/bitmap.c provides these functions:
> >   */
> > -- 
> > 2.17.0
> > 
> 
> Thanks.
> 

-- 
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Intel Finland Oy

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()
  2018-06-04  9:59   ` Andy Shevchenko
@ 2018-06-04 21:57     ` Dmitry Torokhov
  2018-06-04 22:14       ` Dmitry Torokhov
  0 siblings, 1 reply; 9+ messages in thread
From: Dmitry Torokhov @ 2018-06-04 21:57 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Jeffy Chen, linux-input, linux-kernel

On Mon, Jun 04, 2018 at 12:59:54PM +0300, Andy Shevchenko wrote:
> On Fri, 2018-06-01 at 11:33 -0700, Dmitry Torokhov wrote:
> > Hi Andy,
> > 
> > On Fri, Jun 01, 2018 at 11:31:18AM +0300, Andy Shevchenko wrote:
> > > A lot of code become ugly because of open coding allocations for
> > > bitmaps.
> > > 
> > > Introduce three helpers to allow users be more clear of intention
> > > and keep their code neat.
> > > 
> > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > 
> > This looks nice and I like how it simplifies drivers.
> 
> Thanks!
> 
> >  How do we merge
> > this? 
> 
> I suppose through 'input' tree if there is no objections.

OK, let's wait for objections for a few days.

> 
> > > ---
> > >  include/linux/bitmap.h | 16 ++++++++++++++++
> > >  1 file changed, 16 insertions(+)
> > > 
> > > diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
> > > index 1ee46f492267..845822425393 100644
> > > --- a/include/linux/bitmap.h
> > > +++ b/include/linux/bitmap.h
> > > @@ -6,6 +6,7 @@
> > >  
> > >  #include <linux/types.h>
> > >  #include <linux/bitops.h>
> > > +#include <linux/slab.h>
> > >  #include <linux/string.h>
> > >  #include <linux/kernel.h>
> > >  
> > > @@ -104,6 +105,21 @@
> > >   * contain all bit positions from 0 to 'bits' - 1.
> > >   */
> > >  
> > > +static inline unsigned long *bitmap_alloc(unsigned int nbits, gfp_t
> > > flags)
> > > +{
> > > +	return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned
> > > long), flags);
> > > +}
> > > +
> > > +static inline unsigned long *bitmap_zalloc(unsigned int nbits,
> > > gfp_t flags)
> > > +{
> > > +	return kcalloc(BITS_TO_LONGS(nbits), sizeof(unsigned long),
> > > flags);
> > 
> > 	retrun bitmap_alloc(nbits, flags | __GFP_ZERO);
> > 
> > ?
> 
> I though about this, but decide not to rely on linux/gfp.h.
> If you think explicit __GFP_ZERO is better, I can replace in v2, or if

I like it ;) and we already do it for kcalloc done via kmalloc_array
with __GFP_ZERO or kzalloc (kmalloc with __GFP_ZERO).

> you have a chance to do that when applying it would be appreciated.

Yeah, I can do that, no problem.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()
  2018-06-04 21:57     ` Dmitry Torokhov
@ 2018-06-04 22:14       ` Dmitry Torokhov
  0 siblings, 0 replies; 9+ messages in thread
From: Dmitry Torokhov @ 2018-06-04 22:14 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Jeffy Chen, linux-input, linux-kernel

On Mon, Jun 04, 2018 at 02:57:23PM -0700, Dmitry Torokhov wrote:
> On Mon, Jun 04, 2018 at 12:59:54PM +0300, Andy Shevchenko wrote:
> > On Fri, 2018-06-01 at 11:33 -0700, Dmitry Torokhov wrote:
> > > Hi Andy,
> > > 
> > > On Fri, Jun 01, 2018 at 11:31:18AM +0300, Andy Shevchenko wrote:
> > > > A lot of code become ugly because of open coding allocations for
> > > > bitmaps.
> > > > 
> > > > Introduce three helpers to allow users be more clear of intention
> > > > and keep their code neat.
> > > > 
> > > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > > 
> > > This looks nice and I like how it simplifies drivers.
> > 
> > Thanks!
> > 
> > >  How do we merge
> > > this? 
> > 
> > I suppose through 'input' tree if there is no objections.
> 
> OK, let's wait for objections for a few days.
> 
> > 
> > > > ---
> > > >  include/linux/bitmap.h | 16 ++++++++++++++++
> > > >  1 file changed, 16 insertions(+)
> > > > 
> > > > diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
> > > > index 1ee46f492267..845822425393 100644
> > > > --- a/include/linux/bitmap.h
> > > > +++ b/include/linux/bitmap.h
> > > > @@ -6,6 +6,7 @@
> > > >  
> > > >  #include <linux/types.h>
> > > >  #include <linux/bitops.h>
> > > > +#include <linux/slab.h>
> > > >  #include <linux/string.h>
> > > >  #include <linux/kernel.h>
> > > >  
> > > > @@ -104,6 +105,21 @@
> > > >   * contain all bit positions from 0 to 'bits' - 1.
> > > >   */
> > > >  
> > > > +static inline unsigned long *bitmap_alloc(unsigned int nbits, gfp_t
> > > > flags)
> > > > +{
> > > > +	return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned
> > > > long), flags);
> > > > +}
> > > > +
> > > > +static inline unsigned long *bitmap_zalloc(unsigned int nbits,
> > > > gfp_t flags)
> > > > +{
> > > > +	return kcalloc(BITS_TO_LONGS(nbits), sizeof(unsigned long),
> > > > flags);
> > > 
> > > 	retrun bitmap_alloc(nbits, flags | __GFP_ZERO);
> > > 
> > > ?
> > 
> > I though about this, but decide not to rely on linux/gfp.h.
> > If you think explicit __GFP_ZERO is better, I can replace in v2, or if
> 
> I like it ;) and we already do it for kcalloc done via kmalloc_array
> with __GFP_ZERO or kzalloc (kmalloc with __GFP_ZERO).
> 
> > you have a chance to do that when applying it would be appreciated.
> 
> Yeah, I can do that, no problem.

Ugh, there is circular dependency:

  CC      arch/x86/kernel/asm-offsets.s
In file included from ./include/linux/cpumask.h:12:0,
                 from ./arch/x86/include/asm/cpumask.h:5,
                 from ./arch/x86/include/asm/msr.h:11,
                 from ./arch/x86/include/asm/processor.h:21,
                 from ./arch/x86/include/asm/cpufeature.h:5,
                 from ./arch/x86/include/asm/thread_info.h:53,
                 from ./include/linux/thread_info.h:38,
                 from ./arch/x86/include/asm/preempt.h:7,
                 from ./include/linux/preempt.h:81,
                 from ./include/linux/spinlock.h:51,
                 from ./include/linux/mmzone.h:8,
                 from ./include/linux/gfp.h:6,
                 from ./include/linux/slab.h:15,
                 from ./include/linux/crypto.h:24,
                 from arch/x86/kernel/asm-offsets.c:9:
./include/linux/bitmap.h: In function ‘bitmap_alloc’:
./include/linux/bitmap.h:110:9: error: implicit declaration of function ‘kmalloc_array’ [-Werror=implicit-function-declaration]
  return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long), flags);
         ^~~~~~~~~~~~~
./include/linux/bitmap.h:110:9: warning: return makes pointer from integer without a cast [-Wint-conversion]
  return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long), flags);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So we have slab.h ... -> cpumask.h -> bitmap.h -> slab.h -> BOOM

We will probably have to move implementation into lib/bitmap.h. I think
that should be OK. Can you make the change?

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2018-06-04 22:14 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-01  8:31 [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free() Andy Shevchenko
2018-06-01  8:31 ` [PATCH v1 2/3] Input: gpio-keys - Switch to bitmap_zalloc() Andy Shevchenko
2018-06-01 18:34   ` Dmitry Torokhov
2018-06-01  8:31 ` [PATCH v1 3/3] Input: evdev " Andy Shevchenko
2018-06-01 18:34   ` Dmitry Torokhov
2018-06-01 18:33 ` [PATCH v1 1/3] bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free() Dmitry Torokhov
2018-06-04  9:59   ` Andy Shevchenko
2018-06-04 21:57     ` Dmitry Torokhov
2018-06-04 22:14       ` Dmitry Torokhov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).