* [PATCH 1/2] gpio: sim: fix an invalid __free() usage
@ 2023-09-15 12:34 Bartosz Golaszewski
2023-09-15 12:34 ` [PATCH 2/2] gpio: sim: include a missing header Bartosz Golaszewski
2023-09-16 11:19 ` [PATCH 1/2] gpio: sim: fix an invalid __free() usage Andy Shevchenko
0 siblings, 2 replies; 3+ messages in thread
From: Bartosz Golaszewski @ 2023-09-15 12:34 UTC (permalink / raw)
To: Linus Walleij, Andy Shevchenko, Kent Gibson, Alexey Dobriyan,
Peter Zijlstra
Cc: linux-gpio, linux-kernel, Bartosz Golaszewski
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
gpio_sim_make_line_names() returns NULL or ERR_PTR() so we must not use
__free(kfree) on the returned address. Let's rework the function so that
it returns the size of the gpio-line-names array or a negative error
code on failure. This way we know that the string array will either stay
NULL or be set to a correct, kcalloc()'ed address.
Fixes: 3faf89f27aab ("gpio: sim: simplify code with cleanup helpers")
Reported-by: Alexey Dobriyan <adobriyan@gmail.com>
Closes: https://lore.kernel.org/all/07c32bf1-6c1a-49d9-b97d-f0ae4a2b42ab@p183/
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
drivers/gpio/gpio-sim.c | 37 ++++++++++++++++++++-----------------
1 file changed, 20 insertions(+), 17 deletions(-)
diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
index 2b9d9e172d5d..b4e6d06d08a2 100644
--- a/drivers/gpio/gpio-sim.c
+++ b/drivers/gpio/gpio-sim.c
@@ -717,13 +717,14 @@ gpio_sim_device_config_live_show(struct config_item *item, char *page)
return sprintf(page, "%c\n", live ? '1' : '0');
}
-static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank,
- unsigned int *line_names_size)
+static int gpio_sim_make_line_names(struct gpio_sim_bank *bank,
+ char ***line_names)
{
unsigned int max_offset = 0;
bool has_line_names = false;
struct gpio_sim_line *line;
- char **line_names;
+ int line_names_size;
+ char **names;
list_for_each_entry(line, &bank->line_list, siblings) {
if (line->offset >= bank->num_lines)
@@ -743,26 +744,27 @@ static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank,
if (!has_line_names)
/*
- * This is not an error - NULL means, there are no line
- * names configured.
+ * This is not an error - 0 means, there are no line names
+ * configured.
*/
- return NULL;
+ return 0;
- *line_names_size = max_offset + 1;
+ line_names_size = max_offset + 1;
- line_names = kcalloc(*line_names_size, sizeof(*line_names), GFP_KERNEL);
- if (!line_names)
- return ERR_PTR(-ENOMEM);
+ names = kcalloc(line_names_size, sizeof(*line_names), GFP_KERNEL);
+ if (!names)
+ return -ENOMEM;
list_for_each_entry(line, &bank->line_list, siblings) {
if (line->offset >= bank->num_lines)
continue;
if (line->name && (line->offset <= max_offset))
- line_names[line->offset] = line->name;
+ names[line->offset] = line->name;
}
- return line_names;
+ *line_names = names;
+ return line_names_size;
}
static void gpio_sim_remove_hogs(struct gpio_sim_device *dev)
@@ -866,8 +868,9 @@ gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
struct fwnode_handle *parent)
{
struct property_entry properties[GPIO_SIM_PROP_MAX];
- unsigned int prop_idx = 0, line_names_size = 0;
char **line_names __free(kfree) = NULL;
+ unsigned int prop_idx = 0;
+ int line_names_size;
memset(properties, 0, sizeof(properties));
@@ -877,11 +880,11 @@ gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label",
bank->label);
- line_names = gpio_sim_make_line_names(bank, &line_names_size);
- if (IS_ERR(line_names))
- return ERR_CAST(line_names);
+ line_names_size = gpio_sim_make_line_names(bank, &line_names);
+ if (line_names_size < 0)
+ return ERR_PTR(line_names_size);
- if (line_names)
+ if (line_names_size > 0)
properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
"gpio-line-names",
line_names, line_names_size);
--
2.39.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] gpio: sim: include a missing header
2023-09-15 12:34 [PATCH 1/2] gpio: sim: fix an invalid __free() usage Bartosz Golaszewski
@ 2023-09-15 12:34 ` Bartosz Golaszewski
2023-09-16 11:19 ` [PATCH 1/2] gpio: sim: fix an invalid __free() usage Andy Shevchenko
1 sibling, 0 replies; 3+ messages in thread
From: Bartosz Golaszewski @ 2023-09-15 12:34 UTC (permalink / raw)
To: Linus Walleij, Andy Shevchenko, Kent Gibson, Alexey Dobriyan,
Peter Zijlstra
Cc: linux-gpio, linux-kernel, Bartosz Golaszewski
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
We're using various ERR macros from linux/err.h but the include is
missing. Add it.
Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
drivers/gpio/gpio-sim.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
index b4e6d06d08a2..d9d7df7bd387 100644
--- a/drivers/gpio/gpio-sim.c
+++ b/drivers/gpio/gpio-sim.c
@@ -12,6 +12,7 @@
#include <linux/completion.h>
#include <linux/configfs.h>
#include <linux/device.h>
+#include <linux/err.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio/driver.h>
#include <linux/gpio/machine.h>
--
2.39.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] gpio: sim: fix an invalid __free() usage
2023-09-15 12:34 [PATCH 1/2] gpio: sim: fix an invalid __free() usage Bartosz Golaszewski
2023-09-15 12:34 ` [PATCH 2/2] gpio: sim: include a missing header Bartosz Golaszewski
@ 2023-09-16 11:19 ` Andy Shevchenko
1 sibling, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2023-09-16 11:19 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Linus Walleij, Kent Gibson, Alexey Dobriyan, Peter Zijlstra,
linux-gpio, linux-kernel, Bartosz Golaszewski
On Fri, Sep 15, 2023 at 02:34:22PM +0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> gpio_sim_make_line_names() returns NULL or ERR_PTR() so we must not use
> __free(kfree) on the returned address. Let's rework the function so that
> it returns the size of the gpio-line-names array or a negative error
> code on failure. This way we know that the string array will either stay
> NULL or be set to a correct, kcalloc()'ed address.
...
> + names = kcalloc(line_names_size, sizeof(*line_names), GFP_KERNEL);
sizeof(*line_names) is a bit unusual here. names is typed as double pointer,
while line_names as a tripple one.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-09-16 11:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-15 12:34 [PATCH 1/2] gpio: sim: fix an invalid __free() usage Bartosz Golaszewski
2023-09-15 12:34 ` [PATCH 2/2] gpio: sim: include a missing header Bartosz Golaszewski
2023-09-16 11:19 ` [PATCH 1/2] gpio: sim: fix an invalid __free() usage Andy Shevchenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®