From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Mirsad Todorovac <mirsad.todorovac@alu.unizg.hr>
Cc: Bartosz Golaszewski <brgl@bgdev.pl>,
linux-gpio@vger.kernel.org,
Linus Walleij <linus.walleij@linaro.org>,
linux-kernel@vger.kernel.org,
Thorsten Leemhuis <regressions@leemhuis.info>
Subject: Re: INFO: REPRODUCED: memory leak in gpio device in 6.2-rc6
Date: Mon, 20 Feb 2023 15:43:42 +0200 [thread overview]
Message-ID: <Y/N5Dt6G397rkfBd@smile.fi.intel.com> (raw)
In-Reply-To: <4b001ce6-b35d-3ad1-b757-f5f6baca7b51@alu.unizg.hr>
On Mon, Feb 20, 2023 at 02:10:00PM +0100, Mirsad Todorovac wrote:
> On 2/16/23 15:16, Bartosz Golaszewski wrote:
...
> As Mr. McKenney once said, a bunch of monkeys with keyboard could
> have done it in a considerable number of trials and errors ;-)
>
> But here I have something that could potentially leak as well. I could not devise a
> reproducer due to the leak being lightly triggered only in extreme memory contention.
>
> See it for yourself:
>
> drivers/gpio/gpio-sim.c:
> 301 static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip)
> 302 {
> 303 struct device_attribute *val_dev_attr, *pull_dev_attr;
> 304 struct gpio_sim_attribute *val_attr, *pull_attr;
> 305 unsigned int num_lines = chip->gc.ngpio;
> 306 struct device *dev = chip->gc.parent;
> 307 struct attribute_group *attr_group;
> 308 struct attribute **attrs;
> 309 int i, ret;
> 310
> 311 chip->attr_groups = devm_kcalloc(dev, sizeof(*chip->attr_groups),
> 312 num_lines + 1, GFP_KERNEL);
> 313 if (!chip->attr_groups)
> 314 return -ENOMEM;
> 315
> 316 for (i = 0; i < num_lines; i++) {
> 317 attr_group = devm_kzalloc(dev, sizeof(*attr_group), GFP_KERNEL);
> 318 attrs = devm_kcalloc(dev, GPIO_SIM_NUM_ATTRS, sizeof(*attrs),
> 319 GFP_KERNEL);
> 320 val_attr = devm_kzalloc(dev, sizeof(*val_attr), GFP_KERNEL);
> 321 pull_attr = devm_kzalloc(dev, sizeof(*pull_attr), GFP_KERNEL);
> 322 if (!attr_group || !attrs || !val_attr || !pull_attr)
> 323 return -ENOMEM;
> 324
> 325 attr_group->name = devm_kasprintf(dev, GFP_KERNEL,
> 326 "sim_gpio%u", i);
> 327 if (!attr_group->name)
> 328 return -ENOMEM;
>
> Apparently, if the memory allocation only partially succeeds, in the theoretical case
> that the system is close to its kernel memory exhaustion, `return -ENOMEM` would not
> free the partially succeeded allocs, would it?
>
> To explain it better, I tried a version that is not yet full doing "all or nothing"
> memory allocation for the gpio-sim driver, because I am not that familiar with the
> driver internals.
devm_*() mean that the resource allocation is made in a managed manner, so when
it's done, it will be freed automatically.
The question is: is the lifetime of the attr_groups should be lesser or the
same as chip->gc.parent? Maybe it's incorrect to call devm_*() in the first place?
Or maybe the chip->gc.parent should be changed to something else (actual GPIO
device, but then it's unclear how to provide the attributes in non-racy way.
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2023-02-20 13:43 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-31 9:36 INFO: memory leak in gpio device in 6.2-rc6 Mirsad Goran Todorovac
2023-02-08 19:55 ` INFO: REPRODUCED: " Mirsad Goran Todorovac
2023-02-12 14:19 ` Andy Shevchenko
2023-02-14 18:19 ` Mirsad Goran Todorovac
2023-02-15 10:53 ` Andy Shevchenko
2023-02-15 14:45 ` Mirsad Goran Todorovac
2023-02-16 14:16 ` Bartosz Golaszewski
2023-02-20 13:10 ` Mirsad Todorovac
2023-02-20 13:43 ` Andy Shevchenko [this message]
2023-02-21 13:52 ` Mirsad Goran Todorovac
2023-02-21 14:20 ` Mirsad Goran Todorovac
2023-02-21 14:32 ` Mirsad Goran Todorovac
2023-02-21 15:39 ` Andy Shevchenko
2023-02-22 10:53 ` Bartosz Golaszewski
2023-02-22 21:27 ` Mirsad Goran Todorovac
2023-02-24 15:12 ` Mirsad Todorovac
2023-02-24 17:40 ` Andy Shevchenko
2023-02-27 18:38 ` Mirsad Todorovac
2023-02-27 23:13 ` Andy Shevchenko
2023-03-08 13:11 ` Mirsad Todorovac
2023-03-08 15:20 ` Andy Shevchenko
2023-03-08 23:17 ` Mirsad Goran Todorovac
2023-02-14 20:54 ` INFO: BISECTED: " Mirsad Goran Todorovac
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y/N5Dt6G397rkfBd@smile.fi.intel.com \
--to=andriy.shevchenko@intel.com \
--cc=brgl@bgdev.pl \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mirsad.todorovac@alu.unizg.hr \
--cc=regressions@leemhuis.info \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox