public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Mukesh Ojha <quic_mojha@quicinc.com>, Pavel Machek <pavel@ucw.cz>,
	linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] leds: class: Protect brightness_show() with led_cdev->led_access mutex
Date: Fri, 1 Nov 2024 17:08:33 +0000	[thread overview]
Message-ID: <20241101170833.GB1807686@google.com> (raw)
In-Reply-To: <62b09eee-553a-a3d1-e2e0-59dee7289019@gmail.com>

On Sun, 27 Oct 2024, Jacek Anaszewski wrote:

> Hi Mukesh,
> 
> On 10/25/24 19:11, Mukesh Ojha wrote:
> > There is NULL pointer issue observed if from Process A where hid device
> > being added which results in adding a led_cdev addition and later a
> > another call to access of led_cdev attribute from Process B can result
> > in NULL pointer issue.
> > 
> > Use mutex led_cdev->led_access to protect access to led->cdev and its
> > attribute inside brightness_show() and max_brightness_show() and also
> > update the comment for mutex that it should be used to protect the led
> > class device fields.

[...]

> > Signed-off-by: Mukesh Ojha <quic_mojha@quicinc.com>
> > ---
> > Changes in v2:
> >   - Updated the comment for led_access mutex lock.
> >   - Also added mutex protection for max_brightness_show().
> > 
> >   drivers/leds/led-class.c | 14 +++++++++++---
> >   include/linux/leds.h     |  2 +-
> >   2 files changed, 12 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
> > index 06b97fd49ad9..f69f4e928d61 100644
> > --- a/drivers/leds/led-class.c
> > +++ b/drivers/leds/led-class.c
> > @@ -29,11 +29,14 @@ static ssize_t brightness_show(struct device *dev,
> >   		struct device_attribute *attr, char *buf)
> >   {
> >   	struct led_classdev *led_cdev = dev_get_drvdata(dev);
> > +	unsigned int brightness;
> > -	/* no lock needed for this */
> > +	mutex_lock(&led_cdev->led_access);
> >   	led_update_brightness(led_cdev);
> > +	brightness = led_cdev->brightness;
> > +	mutex_unlock(&led_cdev->led_access);
> > -	return sprintf(buf, "%u\n", led_cdev->brightness);
> > +	return sprintf(buf, "%u\n", brightness);
> >   }
> >   static ssize_t brightness_store(struct device *dev,
> > @@ -70,8 +73,13 @@ static ssize_t max_brightness_show(struct device *dev,
> >   		struct device_attribute *attr, char *buf)
> >   {
> >   	struct led_classdev *led_cdev = dev_get_drvdata(dev);
> > +	unsigned int max_brightness;
> > +
> > +	mutex_lock(&led_cdev->led_access);
> > +	max_brightness = led_cdev->max_brightness;
> > +	mutex_unlock(&led_cdev->led_access);
> > -	return sprintf(buf, "%u\n", led_cdev->max_brightness);
> > +	return sprintf(buf, "%u\n", max_brightness);
> >   }
> >   static DEVICE_ATTR_RO(max_brightness);
> > diff --git a/include/linux/leds.h b/include/linux/leds.h
> > index e5968c3ed4ae..3524634fcc47 100644
> > --- a/include/linux/leds.h
> > +++ b/include/linux/leds.h
> > @@ -238,7 +238,7 @@ struct led_classdev {
> >   	struct kernfs_node	*brightness_hw_changed_kn;
> >   #endif
> > -	/* Ensures consistent access to the LED Flash Class device */
> > +	/* Ensures consistent access to the LED Class device */
> 
> Nit: It was improper in the original comment as well:
> 
> s/Class/class/

Some things can't be unseen.  Please submit v3.

-- 
Lee Jones [李琼斯]

      reply	other threads:[~2024-11-01 17:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-25 17:11 [PATCH v2] leds: class: Protect brightness_show() with led_cdev->led_access mutex Mukesh Ojha
2024-10-25 17:33 ` anish kumar
2024-10-27 14:20 ` Jacek Anaszewski
2024-11-01 17:08   ` Lee Jones [this message]

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=20241101170833.GB1807686@google.com \
    --to=lee@kernel.org \
    --cc=jacek.anaszewski@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@ucw.cz \
    --cc=quic_mojha@quicinc.com \
    /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