Re: [PATCH v2] usb: raw-gadget: fix raw_event_queue_fetch locking

From: Dan Carpenter
Date: Tue Apr 07 2020 - 10:29:55 EST


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