linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Lechner <david@lechnology.com>
To: William Breathitt Gray <vilhelm.gray@gmail.com>
Cc: jic23@kernel.org, kamel.bouhara@bootlin.com,
	gwendal@chromium.org, alexandre.belloni@bootlin.com,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-stm32@st-md-mailman.stormreply.com,
	linux-arm-kernel@lists.infradead.org, syednwaris@gmail.com,
	patrick.havelange@essensium.com, fabrice.gasnier@st.com,
	mcoquelin.stm32@gmail.com, alexandre.torgue@st.com
Subject: Re: [PATCH v5 3/5] counter: Add character device interface
Date: Sun, 25 Oct 2020 11:34:43 -0500	[thread overview]
Message-ID: <e0b7989f-6a99-0fae-471c-8d06c8e951b0@lechnology.com> (raw)
In-Reply-To: <20201025131809.GB3458@shinobu>

On 10/25/20 8:18 AM, William Breathitt Gray wrote:
> On Tue, Oct 20, 2020 at 11:06:42AM -0500, David Lechner wrote:
>> On 10/18/20 11:58 AM, William Breathitt Gray wrote:
>>> On Wed, Oct 14, 2020 at 05:40:44PM -0500, David Lechner wrote:
>>>> On 9/26/20 9:18 PM, William Breathitt Gray wrote:
>>>>> +static ssize_t counter_chrdev_read(struct file *filp, char __user *buf,
>>>>> +				   size_t len, loff_t *f_ps)
>>>>> +{
>>>>> +	struct counter_device *const counter = filp->private_data;
>>>>> +	int err;
>>>>> +	unsigned long flags;
>>>>> +	unsigned int copied;
>>>>> +
>>>>> +	if (len < sizeof(struct counter_event))
>>>>> +		return -EINVAL;
>>>>> +
>>>>> +	do {
>>>>> +		if (kfifo_is_empty(&counter->events)) {
>>>>> +			if (filp->f_flags & O_NONBLOCK)
>>>>> +				return -EAGAIN;
>>>>> +
>>>>> +			err = wait_event_interruptible(counter->events_wait,
>>>>> +					!kfifo_is_empty(&counter->events));
>>>>> +			if (err)
>>>>> +				return err;
>>>>> +		}
>>>>> +
>>>>> +		raw_spin_lock_irqsave(&counter->events_lock, flags);
>>>>> +		err = kfifo_to_user(&counter->events, buf, len, &copied);
>>>>> +		raw_spin_unlock_irqrestore(&counter->events_lock, flags);
>>>>> +		if (err)
>>>>> +			return err;
>>>>> +	} while (!copied);
>>>>> +
>>>>> +	return copied;
>>>>> +}
>>>>
>>>> All other uses of kfifo_to_user() I saw use a mutex instead of spin
>>>> lock. I don't see a reason for disabling interrupts here.
>>>
>>> The Counter character device interface is special in this case because
>>> counter->events could be accessed from an interrupt context. This is
>>> possible if counter_push_event() is called for an interrupt (as is the
>>> case for the 104_quad_8 driver). In this case, we can't use mutex
>>> because we can't sleep in an interrupt context, so our only option is to
>>> use spin lock.
>>>
>>
>>
>> The way I understand it, locking is only needed for concurrent readers
>> and locking between reader and writer is not needed.
> 
> You're right, it does say in the kfifo.h comments that with only one
> concurrent reader and one current write, we don't need extra locking to
> use these macros. Because we only have one kfifo_to_user() operating on
> counter->events, does that mean we don't need locking at all here for
> the counter_chrdev_read() function?
> 
> William Breathitt Gray
> 

Even if we have the policy that only one file handle to the chrdev
can be open at a time, it is still possible that the it could be
read from multiple threads. So it I think it makes sense to keep
it just to be safe.

  reply	other threads:[~2020-10-25 16:35 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-27  2:18 [PATCH v5 0/5] Introduce the Counter character device interface William Breathitt Gray
2020-09-27  2:18 ` [PATCH v5 1/5] counter: Internalize sysfs interface code William Breathitt Gray
2020-10-13  2:15   ` David Lechner
2020-10-18 14:49     ` William Breathitt Gray
2020-10-20 15:38       ` David Lechner
2020-10-23 13:12         ` William Breathitt Gray
2020-10-15  1:38   ` David Lechner
2020-10-18 17:00     ` William Breathitt Gray
2020-09-27  2:18 ` [PATCH v5 2/5] docs: counter: Update to reflect sysfs internalization William Breathitt Gray
2020-09-27  2:18 ` [PATCH v5 3/5] counter: Add character device interface William Breathitt Gray
2020-10-14  1:40   ` David Lechner
2020-10-18 16:49     ` William Breathitt Gray
2020-10-20 15:53       ` David Lechner
2020-10-25 12:55         ` William Breathitt Gray
2020-10-25 16:36           ` David Lechner
2020-10-14 17:43   ` David Lechner
2020-10-14 19:05     ` William Breathitt Gray
2020-10-14 22:32       ` David Lechner
2020-10-14 22:40   ` David Lechner
2020-10-18 16:58     ` William Breathitt Gray
2020-10-20 16:06       ` David Lechner
2020-10-25 13:18         ` William Breathitt Gray
2020-10-25 16:34           ` David Lechner [this message]
2020-10-25 17:53             ` William Breathitt Gray
2020-09-27  2:18 ` [PATCH v5 4/5] docs: counter: Document " William Breathitt Gray
2020-10-08  8:09   ` Pavel Machek
2020-10-08 12:28     ` William Breathitt Gray
2020-10-12 17:04       ` David Lechner
2020-10-13 18:58         ` William Breathitt Gray
2020-10-13 19:08           ` David Lechner
2020-10-13 19:27             ` William Breathitt Gray
2020-09-27  2:18 ` [PATCH v5 5/5] counter: 104-quad-8: Add IRQ support for the ACCES 104-QUAD-8 William Breathitt Gray
2020-10-14  0:13   ` David Lechner
2020-10-18 14:50     ` William Breathitt Gray
2020-10-13  0:35 ` [PATCH v5 0/5] Introduce the Counter character device interface David Lechner
2020-10-18 14:14   ` William Breathitt Gray

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=e0b7989f-6a99-0fae-471c-8d06c8e951b0@lechnology.com \
    --to=david@lechnology.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=alexandre.torgue@st.com \
    --cc=fabrice.gasnier@st.com \
    --cc=gwendal@chromium.org \
    --cc=jic23@kernel.org \
    --cc=kamel.bouhara@bootlin.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=patrick.havelange@essensium.com \
    --cc=syednwaris@gmail.com \
    --cc=vilhelm.gray@gmail.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;
as well as URLs for NNTP newsgroup(s).