2022-05-20 00:39:15

by Kent Overstreet

[permalink] [raw]
Subject: [PATCH v2 22/28] Input/joystick/analog: Convert from seq_buf -> printbuf

seq_buf is being deprecated, this converts to printbuf which is similar
but heap allocates the string buffer.

This means we have to consider memory allocation context & failure: Here
we're in device initialization so GFP_KERNEL should be fine, and also as
we're in device initialization returning -ENOMEM is fine.

Signed-off-by: Kent Overstreet <[email protected]>
---
drivers/input/joystick/analog.c | 37 ++++++++++++++++++++-------------
1 file changed, 23 insertions(+), 14 deletions(-)

diff --git a/drivers/input/joystick/analog.c b/drivers/input/joystick/analog.c
index 3088c5b829..72e1e30d19 100644
--- a/drivers/input/joystick/analog.c
+++ b/drivers/input/joystick/analog.c
@@ -19,7 +19,7 @@
#include <linux/input.h>
#include <linux/gameport.h>
#include <linux/jiffies.h>
-#include <linux/seq_buf.h>
+#include <linux/printbuf.h>
#include <linux/timex.h>
#include <linux/timekeeping.h>

@@ -337,26 +337,32 @@ static void analog_calibrate_timer(struct analog_port *port)
* analog_name() constructs a name for an analog joystick.
*/

-static void analog_name(struct analog *analog)
+static int analog_name(struct analog *analog)
{
- struct seq_buf s;
+ struct printbuf buf = PRINTBUF;
+ int ret = 0;

- seq_buf_init(&s, analog->name, sizeof(analog->name));
- seq_buf_printf(&s, "Analog %d-axis %d-button",
- hweight8(analog->mask & ANALOG_AXES_STD),
- hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
- hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
+ pr_buf(&buf, "Analog %d-axis %d-button",
+ hweight8(analog->mask & ANALOG_AXES_STD),
+ hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
+ hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);

if (analog->mask & ANALOG_HATS_ALL)
- seq_buf_printf(&s, " %d-hat",
- hweight16(analog->mask & ANALOG_HATS_ALL));
+ pr_buf(&buf, " %d-hat",
+ hweight16(analog->mask & ANALOG_HATS_ALL));

if (analog->mask & ANALOG_HAT_FCS)
- seq_buf_printf(&s, " FCS");
+ pr_buf(&buf, " FCS");
if (analog->mask & ANALOG_ANY_CHF)
- seq_buf_printf(&s, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
+ pr_buf(&buf, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");

- seq_buf_printf(&s, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick");
+ pr_buf(&buf, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick");
+
+ ret = buf.allocation_failure ? -ENOMEM : 0;
+ if (!ret)
+ strlcpy(analog->name, buf.buf, sizeof(analog->name));
+ printbuf_exit(&buf);
+ return ret;
}

/*
@@ -369,7 +375,10 @@ static int analog_init_device(struct analog_port *port, struct analog *analog, i
int i, j, t, v, w, x, y, z;
int error;

- analog_name(analog);
+ error = analog_name(analog);
+ if (error)
+ return error;
+
snprintf(analog->phys, sizeof(analog->phys),
"%s/input%d", port->gameport->phys, index);
analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
--
2.36.0



2022-05-20 08:42:50

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 22/28] Input/joystick/analog: Convert from seq_buf -> printbuf

On Thu, May 19, 2022 at 04:19:27PM -0400, Kent Overstreet wrote:
> On Thu, May 19, 2022 at 11:04:27PM +0300, Andy Shevchenko wrote:
> > On Thu, May 19, 2022 at 01:24:15PM -0400, Kent Overstreet wrote:

..

> This conversion predated my adding external buffers to printbufs - now the patch
> is more of a direct conversion:
>
> -- >8 --
> Subject: [PATCH] Input/joystick/analog: Convert from seq_buf -> printbuf

This looks much better.

--
With Best Regards,
Andy Shevchenko



2022-05-20 23:02:53

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 22/28] Input/joystick/analog: Convert from seq_buf -> printbuf

On Thu, May 19, 2022 at 01:24:15PM -0400, Kent Overstreet wrote:
> seq_buf is being deprecated, this converts to printbuf which is similar
> but heap allocates the string buffer.
>
> This means we have to consider memory allocation context & failure: Here
> we're in device initialization so GFP_KERNEL should be fine, and also as
> we're in device initialization returning -ENOMEM is fine.

...

> + int ret = 0;

Redundant assignment.

...

> - seq_buf_printf(&s, " %d-hat",
> - hweight16(analog->mask & ANALOG_HATS_ALL));
> + pr_buf(&buf, " %d-hat",
> + hweight16(analog->mask & ANALOG_HATS_ALL));

Now you may put it on one line here and in similar cases.

...

> + ret = buf.allocation_failure ? -ENOMEM : 0;
> + if (!ret)
> + strlcpy(analog->name, buf.buf, sizeof(analog->name));
> + printbuf_exit(&buf);
> + return ret;

Looks like anti-pattern. On top a bit twisted error code manipulation before
checking for error codes, but what about

static int printbuf_export(*buf, *out, size)
{
...
}

ret = printbuf_export(&buf, analog->name, sizeof(analog->name));
printbuf_exit(&buf);
return ret;

?

--
With Best Regards,
Andy Shevchenko



2022-05-21 08:08:28

by Kent Overstreet

[permalink] [raw]
Subject: Re: [PATCH v2 22/28] Input/joystick/analog: Convert from seq_buf -> printbuf

On Thu, May 19, 2022 at 11:04:27PM +0300, Andy Shevchenko wrote:
> On Thu, May 19, 2022 at 01:24:15PM -0400, Kent Overstreet wrote:
> > seq_buf is being deprecated, this converts to printbuf which is similar
> > but heap allocates the string buffer.
> >
> > This means we have to consider memory allocation context & failure: Here
> > we're in device initialization so GFP_KERNEL should be fine, and also as
> > we're in device initialization returning -ENOMEM is fine.
>
> ...
>
> > + int ret = 0;
>
> Redundant assignment.
>
> ...
>
> > - seq_buf_printf(&s, " %d-hat",
> > - hweight16(analog->mask & ANALOG_HATS_ALL));
> > + pr_buf(&buf, " %d-hat",
> > + hweight16(analog->mask & ANALOG_HATS_ALL));
>
> Now you may put it on one line here and in similar cases.
>
> ...
>
> > + ret = buf.allocation_failure ? -ENOMEM : 0;
> > + if (!ret)
> > + strlcpy(analog->name, buf.buf, sizeof(analog->name));
> > + printbuf_exit(&buf);
> > + return ret;
>
> Looks like anti-pattern. On top a bit twisted error code manipulation before
> checking for error codes, but what about

This conversion predated my adding external buffers to printbufs - now the patch
is more of a direct conversion:

-- >8 --
Subject: [PATCH] Input/joystick/analog: Convert from seq_buf -> printbuf

seq_buf is being deprecated, this converts to printbuf.

Signed-off-by: Kent Overstreet <[email protected]>
---
drivers/input/joystick/analog.c | 23 ++++++++++-------------
1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/drivers/input/joystick/analog.c b/drivers/input/joystick/analog.c
index 3088c5b829..1dde969b29 100644
--- a/drivers/input/joystick/analog.c
+++ b/drivers/input/joystick/analog.c
@@ -19,7 +19,7 @@
#include <linux/input.h>
#include <linux/gameport.h>
#include <linux/jiffies.h>
-#include <linux/seq_buf.h>
+#include <linux/printbuf.h>
#include <linux/timex.h>
#include <linux/timekeeping.h>

@@ -339,24 +339,21 @@ static void analog_calibrate_timer(struct analog_port *port)

static void analog_name(struct analog *analog)
{
- struct seq_buf s;
+ struct printbuf buf = PRINTBUF_EXTERN(analog->name, sizeof(analog->name));

- seq_buf_init(&s, analog->name, sizeof(analog->name));
- seq_buf_printf(&s, "Analog %d-axis %d-button",
- hweight8(analog->mask & ANALOG_AXES_STD),
- hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
- hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
+ pr_buf(&buf, "Analog %d-axis %d-button",
+ hweight8(analog->mask & ANALOG_AXES_STD),
+ hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
+ hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);

if (analog->mask & ANALOG_HATS_ALL)
- seq_buf_printf(&s, " %d-hat",
- hweight16(analog->mask & ANALOG_HATS_ALL));
-
+ pr_buf(&buf, " %d-hat", hweight16(analog->mask & ANALOG_HATS_ALL));
if (analog->mask & ANALOG_HAT_FCS)
- seq_buf_printf(&s, " FCS");
+ pr_buf(&buf, " FCS");
if (analog->mask & ANALOG_ANY_CHF)
- seq_buf_printf(&s, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
+ pr_buf(&buf, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");

- seq_buf_printf(&s, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick");
+ pr_buf(&buf, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick");
}

/*
--
2.36.0