2019-12-04 16:00:47

by Bartosz Golaszewski

[permalink] [raw]
Subject: [PATCH v2 09/11] gpiolib: provide a dedicated function for setting lineinfo

From: Bartosz Golaszewski <[email protected]>

We'll soon be filling out the gpioline_info structure in multiple
places. Add a separate function that given a gpio_desc sets all relevant
fields.

Signed-off-by: Bartosz Golaszewski <[email protected]>
---
drivers/gpio/gpiolib.c | 98 ++++++++++++++++++++++++------------------
1 file changed, 55 insertions(+), 43 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index c89d297da270..711963aa9239 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1145,6 +1145,60 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
return ret;
}

+static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
+ struct gpioline_info *info)
+{
+ struct gpio_chip *chip = desc->gdev->chip;
+ unsigned long flags;
+
+ spin_lock_irqsave(&gpio_lock, flags);
+
+ if (desc->name) {
+ strncpy(info->name, desc->name, sizeof(info->name));
+ info->name[sizeof(info->name) - 1] = '\0';
+ } else {
+ info->name[0] = '\0';
+ }
+
+ if (desc->label) {
+ strncpy(info->consumer, desc->label, sizeof(info->consumer));
+ info->consumer[sizeof(info->consumer) - 1] = '\0';
+ } else {
+ info->consumer[0] = '\0';
+ }
+
+ /*
+ * Userspace only need to know that the kernel is using this GPIO so
+ * it can't use it.
+ */
+ info->flags = 0;
+ if (test_bit(FLAG_REQUESTED, &desc->flags) ||
+ test_bit(FLAG_IS_HOGGED, &desc->flags) ||
+ test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
+ test_bit(FLAG_EXPORT, &desc->flags) ||
+ test_bit(FLAG_SYSFS, &desc->flags) ||
+ !pinctrl_gpio_can_use_line(chip->base + info->line_offset))
+ info->flags |= GPIOLINE_FLAG_KERNEL;
+ if (test_bit(FLAG_IS_OUT, &desc->flags))
+ info->flags |= GPIOLINE_FLAG_IS_OUT;
+ if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
+ info->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
+ if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
+ info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
+ GPIOLINE_FLAG_IS_OUT);
+ if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
+ info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
+ GPIOLINE_FLAG_IS_OUT);
+ if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
+ info->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
+ if (test_bit(FLAG_PULL_DOWN, &desc->flags))
+ info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
+ if (test_bit(FLAG_PULL_UP, &desc->flags))
+ info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
+
+ spin_unlock_irqrestore(&gpio_lock, flags);
+}
+
/*
* gpio_ioctl() - ioctl handler for the GPIO chardev
*/
@@ -1185,49 +1239,7 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
if (IS_ERR(desc))
return PTR_ERR(desc);

- if (desc->name) {
- strncpy(lineinfo.name, desc->name,
- sizeof(lineinfo.name));
- lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
- } else {
- lineinfo.name[0] = '\0';
- }
- if (desc->label) {
- strncpy(lineinfo.consumer, desc->label,
- sizeof(lineinfo.consumer));
- lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
- } else {
- lineinfo.consumer[0] = '\0';
- }
-
- /*
- * Userspace only need to know that the kernel is using
- * this GPIO so it can't use it.
- */
- lineinfo.flags = 0;
- if (test_bit(FLAG_REQUESTED, &desc->flags) ||
- test_bit(FLAG_IS_HOGGED, &desc->flags) ||
- test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
- test_bit(FLAG_EXPORT, &desc->flags) ||
- test_bit(FLAG_SYSFS, &desc->flags) ||
- !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
- lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
- if (test_bit(FLAG_IS_OUT, &desc->flags))
- lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
- if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
- lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
- if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
- lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
- GPIOLINE_FLAG_IS_OUT);
- if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
- lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
- GPIOLINE_FLAG_IS_OUT);
- if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
- lineinfo.flags |= GPIOLINE_FLAG_BIAS_DISABLE;
- if (test_bit(FLAG_PULL_DOWN, &desc->flags))
- lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
- if (test_bit(FLAG_PULL_UP, &desc->flags))
- lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
+ gpio_desc_to_lineinfo(desc, &lineinfo);

if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
return -EFAULT;
--
2.23.0


2019-12-04 22:32:26

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 09/11] gpiolib: provide a dedicated function for setting lineinfo

On Wed, Dec 4, 2019 at 6:02 PM Bartosz Golaszewski <[email protected]> wrote:
>
> From: Bartosz Golaszewski <[email protected]>
>
> We'll soon be filling out the gpioline_info structure in multiple
> places. Add a separate function that given a gpio_desc sets all relevant
> fields.

> + if (desc->name) {
> + strncpy(info->name, desc->name, sizeof(info->name));
> + info->name[sizeof(info->name) - 1] = '\0';
> + } else {
> + info->name[0] = '\0';
> + }
> +
> + if (desc->label) {
> + strncpy(info->consumer, desc->label, sizeof(info->consumer));
> + info->consumer[sizeof(info->consumer) - 1] = '\0';
> + } else {
> + info->consumer[0] = '\0';
> + }

I think we have to fix GCC warnings first and then do whatever this patch does.

--
With Best Regards,
Andy Shevchenko

2019-12-05 09:29:43

by Bartosz Golaszewski

[permalink] [raw]
Subject: Re: [PATCH v2 09/11] gpiolib: provide a dedicated function for setting lineinfo

śr., 4 gru 2019 o 23:30 Andy Shevchenko <[email protected]> napisał(a):
>
> On Wed, Dec 4, 2019 at 6:02 PM Bartosz Golaszewski <[email protected]> wrote:
> >
> > From: Bartosz Golaszewski <[email protected]>
> >
> > We'll soon be filling out the gpioline_info structure in multiple
> > places. Add a separate function that given a gpio_desc sets all relevant
> > fields.
>
> > + if (desc->name) {
> > + strncpy(info->name, desc->name, sizeof(info->name));
> > + info->name[sizeof(info->name) - 1] = '\0';
> > + } else {
> > + info->name[0] = '\0';
> > + }
> > +
> > + if (desc->label) {
> > + strncpy(info->consumer, desc->label, sizeof(info->consumer));
> > + info->consumer[sizeof(info->consumer) - 1] = '\0';
> > + } else {
> > + info->consumer[0] = '\0';
> > + }
>
> I think we have to fix GCC warnings first and then do whatever this patch does.
>

What GCC warnings are you referring to exactly?

Bart

> --
> With Best Regards,
> Andy Shevchenko

2019-12-05 10:23:04

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 09/11] gpiolib: provide a dedicated function for setting lineinfo

On Thu, Dec 5, 2019 at 11:28 AM Bartosz Golaszewski
<[email protected]> wrote:
> śr., 4 gru 2019 o 23:30 Andy Shevchenko <[email protected]> napisał(a):
> > On Wed, Dec 4, 2019 at 6:02 PM Bartosz Golaszewski <[email protected]> wrote:

> > > + if (desc->name) {
> > > + strncpy(info->name, desc->name, sizeof(info->name));
> > > + info->name[sizeof(info->name) - 1] = '\0';
> > > + } else {
> > > + info->name[0] = '\0';
> > > + }
> > > +
> > > + if (desc->label) {
> > > + strncpy(info->consumer, desc->label, sizeof(info->consumer));
> > > + info->consumer[sizeof(info->consumer) - 1] = '\0';
> > > + } else {
> > > + info->consumer[0] = '\0';
> > > + }
> >
> > I think we have to fix GCC warnings first and then do whatever this patch does.
> >
>
> What GCC warnings are you referring to exactly?

stncpy() against partial string without NUL-terminator.

So, if desc->label is longer than info->consumer, it will be copied
partially. I don't check if the modern GCC clever enough to see the
next operation which does the termination.

--
With Best Regards,
Andy Shevchenko

2019-12-05 13:46:48

by Bartosz Golaszewski

[permalink] [raw]
Subject: Re: [PATCH v2 09/11] gpiolib: provide a dedicated function for setting lineinfo

czw., 5 gru 2019 o 11:21 Andy Shevchenko <[email protected]> napisał(a):
>
> On Thu, Dec 5, 2019 at 11:28 AM Bartosz Golaszewski
> <[email protected]> wrote:
> > śr., 4 gru 2019 o 23:30 Andy Shevchenko <[email protected]> napisał(a):
> > > On Wed, Dec 4, 2019 at 6:02 PM Bartosz Golaszewski <[email protected]> wrote:
>
> > > > + if (desc->name) {
> > > > + strncpy(info->name, desc->name, sizeof(info->name));
> > > > + info->name[sizeof(info->name) - 1] = '\0';
> > > > + } else {
> > > > + info->name[0] = '\0';
> > > > + }
> > > > +
> > > > + if (desc->label) {
> > > > + strncpy(info->consumer, desc->label, sizeof(info->consumer));
> > > > + info->consumer[sizeof(info->consumer) - 1] = '\0';
> > > > + } else {
> > > > + info->consumer[0] = '\0';
> > > > + }
> > >
> > > I think we have to fix GCC warnings first and then do whatever this patch does.
> > >
> >
> > What GCC warnings are you referring to exactly?
>
> stncpy() against partial string without NUL-terminator.
>
> So, if desc->label is longer than info->consumer, it will be copied
> partially. I don't check if the modern GCC clever enough to see the
> next operation which does the termination.
>

I'm not sure I get it. What warnings does it produce and in what
environment? I don't see any.

If you want it simpler - we can do `snprintf(info->consumer,
sizeof(info->consumer), desc->label ?: "")`.

Bart

2019-12-05 16:50:32

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 09/11] gpiolib: provide a dedicated function for setting lineinfo

On Thu, Dec 5, 2019 at 3:45 PM Bartosz Golaszewski <[email protected]> wrote:
> czw., 5 gru 2019 o 11:21 Andy Shevchenko <[email protected]> napisał(a):
> > On Thu, Dec 5, 2019 at 11:28 AM Bartosz Golaszewski
> > <[email protected]> wrote:
> > > śr., 4 gru 2019 o 23:30 Andy Shevchenko <[email protected]> napisał(a):
> > > > On Wed, Dec 4, 2019 at 6:02 PM Bartosz Golaszewski <[email protected]> wrote:

> > > > > + if (desc->name) {
> > > > > + strncpy(info->name, desc->name, sizeof(info->name));
> > > > > + info->name[sizeof(info->name) - 1] = '\0';
> > > > > + } else {
> > > > > + info->name[0] = '\0';
> > > > > + }
> > > > > +
> > > > > + if (desc->label) {
> > > > > + strncpy(info->consumer, desc->label, sizeof(info->consumer));
> > > > > + info->consumer[sizeof(info->consumer) - 1] = '\0';
> > > > > + } else {
> > > > > + info->consumer[0] = '\0';
> > > > > + }
> > > >
> > > > I think we have to fix GCC warnings first and then do whatever this patch does.
> > > >
> > >
> > > What GCC warnings are you referring to exactly?
> >
> > stncpy() against partial string without NUL-terminator.
> >
> > So, if desc->label is longer than info->consumer, it will be copied
> > partially. I don't check if the modern GCC clever enough to see the
> > next operation which does the termination.
> >
>
> I'm not sure I get it. What warnings does it produce and in what
> environment?

Some kind of
warning: ‘strncpy’ specified bound 16 equals destination size
[-Wstringop-truncation]

> I don't see any.

Good, I just checked and see none as well. It means GCC understands
that strncpy() is followed by guaranteed NUL-termination.

> If you want it simpler - we can do `snprintf(info->consumer,
> sizeof(info->consumer), desc->label ?: "")`.

It makes sense only in above context.

--
With Best Regards,
Andy Shevchenko