public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Andrey Konovalov <andreyknvl@google.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Felipe Balbi <balbi@kernel.org>, Jonathan Corbet <corbet@lwn.net>,
	Alan Stern <stern@rowland.harvard.edu>
Subject: Re: [PATCH v2] usb: raw-gadget: fix raw_event_queue_fetch locking
Date: Tue, 7 Apr 2020 17:29:36 +0300	[thread overview]
Message-ID: <20200407142936.GL2066@kadam> (raw)
In-Reply-To: <c4cedb13ee6159857ed7d9884e55718e4b1dede4.1586268809.git.andreyknvl@google.com>

On Tue, Apr 07, 2020 at 04:14:50PM +0200, Andrey Konovalov wrote:
> @@ -89,11 +90,18 @@ static struct usb_raw_event *raw_event_queue_fetch(
>  	 * there's at least one event queued by decrementing the semaphore,
>  	 * and then take the lock to protect queue struct fields.
>  	 */
> -	if (down_interruptible(&queue->sema))
> -		return NULL;
> +	ret = down_interruptible(&queue->sema);
> +	if (ret)
> +		return ERR_PTR(ret);
>  	spin_lock_irqsave(&queue->lock, flags);
> -	if (WARN_ON(!queue->size))
> +	/*
> +	 * queue->size must have the same value as queue->sema counter (before
> +	 * the down_interruptible() call above), so this check is a fail-safe.
> +	 */
> +	if (WARN_ON(!queue->size)) {
> +		spin_unlock_irqrestore(&queue->lock, flags);
>  		return NULL;

I'm sorry for not noticing this earlier.  When a function returns both
error pointers and NULL then NULL is supposed to a special case of
success.  For example:

	my_struct_pointer = get_optional_feature();

If there is a memory allocation failure then my_struct_pointer is
-ENOMEM and we fail.  But say the optional feature is disabled, then
we can't return a valid pointer, but it's also working as designed so
it's not an error.  In that case we return NULL.  The surrounding code
should be written to allow NULL pointers.

So I don't think returning NULL here is correct.

regards,
dan carpenter


  reply	other threads:[~2020-04-07 14:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-07 14:14 [PATCH v2] usb: raw-gadget: fix raw_event_queue_fetch locking Andrey Konovalov
2020-04-07 14:29 ` Dan Carpenter [this message]
2020-04-07 14:48   ` Andrey Konovalov

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=20200407142936.GL2066@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=andreyknvl@google.com \
    --cc=balbi@kernel.org \
    --cc=corbet@lwn.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /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