All of lore.kernel.org
 help / color / mirror / Atom feed
From: Robert Hodaszi <robert.hodaszi@digi.com>
To: "Oliver Neukum" <oneukum@suse.com>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
	"Bjørn Mork" <bjorn@mork.no>
Subject: Re: Handling incoming ZLP in cdc-wdm
Date: Thu, 3 Apr 2025 14:25:19 +0200	[thread overview]
Message-ID: <a4052845-1903-48b8-8607-5bf0ac8dfbef@digi.com> (raw)
In-Reply-To: <3296aaff-a591-4fcd-b421-9cfcc6291d2b@suse.com>

> Then we have a problem. If we are servicing a read() syscall,
> we have to either return data, or, if we cannot do that
> we either sleep or return -EAGAIN depending on O_NONBLOCK.
>
> There is nothing we can do about that. This makes me think
> that the issue here is poll() rather than in wdm_read()
(Resending the mail, because Thunderbird changed format to HTML...)

Yes, exactly, that's what I'm saying. :) It's not a problem with 
wdm_read(). The problem is that wdm_poll() confuses users with returning 
available data, although there's nothing there to read, and wdm_read() 
returns with EAGAIN (correctly). That's why we don't want to set 
WDM_READ when the buffer is empty, and there's no error neither (so we 
just received a ZLP).

Returning with EAGAIN in wdm_read() is totally OK, except if cdc-wdm 
told the caller through poll that there's available data.

>
>> So what about modifying the service_interrupt_work to no simply set 
>> WDM_READ if resp_count is 0, but instead to check if there's any real 
>> message in the buffer, to not confuse consumers. Something like this:
>
> That specific proposal will not work because the issue
> is in service_interrupt_work() which can already be scheduled.
> We cannot prevent that.
>
>>     diff --git a/drivers/usb/class/cdc-wdm.c 
>> b/drivers/usb/class/cdc-wdm.c
>>     index 37873acd18f4..9037379f3603 100644
>>     --- a/drivers/usb/class/cdc-wdm.c
>>     +++ b/drivers/usb/class/cdc-wdm.c
>>     @@ -1010,7 +1010,7 @@ static void service_interrupt_work(struct
>>     work_struct *work)
>>
>>              spin_lock_irq(&desc->iuspin);
>>              service_outstanding_interrupt(desc);
>>     -       if (!desc->resp_count) {
>>     +       if (!desc->resp_count && (desc->length || desc->rerr)) {
>>                      set_bit(WDM_READ, &desc->flags);
>>                      wake_up(&desc->wait);
>
> And what happens if wdm_read() wakes up because a signal is delivered?
>
OK, I might not see the obvious, but I don't understand this now. Let me 
copy here the complete patch, not just the chunks of it. That might make 
more sense. This can be completely wrong though.

    diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c
    index 86ee39db013f..9164e311a3e8 100644
    --- a/drivers/usb/class/cdc-wdm.c
    +++ b/drivers/usb/class/cdc-wdm.c
    @@ -92,7 +92,6 @@ struct wdm_device {
          u16            wMaxCommand;
          u16            wMaxPacketSize;
          __le16            inum;
    -    int            reslength;
          int            length;
          int            read;
          int            count;
    @@ -214,6 +213,11 @@ static void wdm_in_callback(struct urb *urb)
          if (desc->rerr == 0 && status != -EPIPE)
              desc->rerr = status;

    +    if (length == 0) {
    +        dev_dbg(&desc->intf->dev, "received ZLP\n");
    +        goto skip_error;
    +    }
    +
          if (length + desc->length > desc->wMaxCommand) {
              /* The buffer would overflow */
              set_bit(WDM_OVERFLOW, &desc->flags);
    @@ -222,15 +226,14 @@ static void wdm_in_callback(struct urb *urb)
              if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
                  memmove(desc->ubuf + desc->length, desc->inbuf, length);
                  desc->length += length;
    -            desc->reslength = length;
              }
          }
      skip_error:

    -    if (desc->rerr) {
    +    if (desc->rerr || length == 0) {
              /*
    -         * Since there was an error, userspace may decide to not read
    -         * any data after poll'ing.
    +         * If there was a ZLP or an error, userspace may decide to not
    +         * read any data after poll'ing.
               * We should respond to further attempts from the device
    to send
               * data, so that we can get unstuck.
               */
    @@ -585,15 +588,6 @@ static ssize_t wdm_read
                  goto retry;
              }

    -        if (!desc->reslength) { /* zero length read */
    -            dev_dbg(&desc->intf->dev, "zero length - clearing
    WDM_READ\n");
    -            clear_bit(WDM_READ, &desc->flags);
    -            rv = service_outstanding_interrupt(desc);
    -            spin_unlock_irq(&desc->iuspin);
    -            if (rv < 0)
    -                goto err;
    -            goto retry;
    -        }
              cntr = desc->length;
              spin_unlock_irq(&desc->iuspin);
          }
    @@ -1005,7 +999,7 @@ static void service_interrupt_work(struct
    work_struct *work)

          spin_lock_irq(&desc->iuspin);
          service_outstanding_interrupt(desc);
    -    if (!desc->resp_count) {
    +    if (!desc->resp_count && (desc->length || desc->rerr)) {
              set_bit(WDM_READ, &desc->flags);
              wake_up(&desc->wait);
          }


So with this:

    1. not setting WDM_READ on ZLP
    2. on ZLP, we just submitting another URB to receive the remaining
    responses, if resp_count != 0, so keeping the receiving alive
    3. in service_interrupt_work(), just setting the WDM_READ if we
    finished response receiving, and there's data in the buffer for the
    user to read out, or we received and error
    4. reslength is no longer needed, as that checked for ZLP-s, but we
    are handling them wdm_in_callback()


Best regards,
Robert



  reply	other threads:[~2025-04-03 12:25 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-26 16:03 Handling incoming ZLP in cdc-wdm Hodaszi, Robert
2025-03-27 13:01 ` Robert Hodaszi
2025-03-27 13:24   ` Oliver Neukum
2025-03-27 15:27     ` Robert Hodaszi
2025-03-31  9:59       ` Oliver Neukum
2025-04-02 11:57         ` Robert Hodaszi
2025-04-02 14:01           ` Oliver Neukum
2025-04-02 15:01             ` Robert Hodaszi
2025-04-02 19:13               ` Oliver Neukum
2025-04-03 12:25                 ` Robert Hodaszi [this message]
     [not found]                 ` <898977f7-3882-4ffe-8833-c44f06914337@digi.com>
2025-04-03 12:58                   ` Oliver Neukum
2025-04-03 14:42                     ` Robert Hodaszi
  -- strict thread matches above, loose matches on Subject: below --
2025-03-26 15:50 Robert Hodaszi
2025-03-27 13:21 ` Oliver Neukum
2025-03-27 15:23   ` Robert Hodaszi

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=a4052845-1903-48b8-8607-5bf0ac8dfbef@digi.com \
    --to=robert.hodaszi@digi.com \
    --cc=bjorn@mork.no \
    --cc=linux-usb@vger.kernel.org \
    --cc=oneukum@suse.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.