Re: [PATCH] thermal: sysfs: fall back to vzalloc for cooling device's statistics

From: Yue Hu
Date: Wed Aug 19 2020 - 22:57:41 EST


On Wed, 19 Aug 2020 16:47:01 +0530
Amit Kucheria <amitk@xxxxxxxxxx> wrote:

> On Tue, Aug 18, 2020 at 12:00 PM Yue Hu <zbestahu@xxxxxxxxx> wrote:
> >
> > From: Yue Hu <huyue2@xxxxxxxxxx>
> >
> > We observed warning about kzalloc() when register thermal cooling device
> > in backlight_device_register(). backlight display can be a cooling device
> > since reducing screen brightness will can help reduce temperature.
> >
> > However, ->get_max_state of backlight will assign max brightness of 1024
> > to states. The memory size can be getting 1MB+ due to states * states.
> > That is so large to trigger kmalloc() warning.
> >
> > So, let's remove it and try vzalloc() if kzalloc() fails.
> >
> > Signed-off-by: Yue Hu <huyue2@xxxxxxxxxx>
> > ---
> > drivers/thermal/thermal_sysfs.c | 8 ++++++--
> > 1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
> > index aa99edb..9bae0b6 100644
> > --- a/drivers/thermal/thermal_sysfs.c
> > +++ b/drivers/thermal/thermal_sysfs.c
> > @@ -16,6 +16,8 @@
> > #include <linux/device.h>
> > #include <linux/err.h>
> > #include <linux/slab.h>
> > +#include <linux/vmalloc.h>
> > +#include <linux/mm.h>
> > #include <linux/string.h>
> > #include <linux/jiffies.h>
> >
> > @@ -919,7 +921,9 @@ static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
> > var += sizeof(*stats->time_in_state) * states;
> > var += sizeof(*stats->trans_table) * states * states;
> >
> > - stats = kzalloc(var, GFP_KERNEL);
> > + stats = kzalloc(var, GFP_KERNEL | __GFP_NOWARN);
> > + if (!stats)
> > + stats = vzalloc(var);
>
> Couldn't this be replaced by kvzalloc()?

Yes, it should be more better as kvzalloc has a vmalloc fallback.

Thx.

>
> > if (!stats)
> > return;
> >
> > @@ -938,7 +942,7 @@ static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
> >
> > static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
> > {
> > - kfree(cdev->stats);
> > + kvfree(cdev->stats);
> > cdev->stats = NULL;
> > }
> >
> > --
> > 1.9.1
> >