From patchwork Tue Jan 3 17:18:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Pelletier X-Patchwork-Id: 89689 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp8106866qgi; Tue, 3 Jan 2017 09:18:34 -0800 (PST) X-Received: by 10.84.173.195 with SMTP id p61mr136593072plb.68.1483463914588; Tue, 03 Jan 2017 09:18:34 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e72si69595073pfl.77.2017.01.03.09.18.34; Tue, 03 Jan 2017 09:18:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965156AbdACRSN (ORCPT + 4 others); Tue, 3 Jan 2017 12:18:13 -0500 Received: from mail-qk0-f171.google.com ([209.85.220.171]:34491 "EHLO mail-qk0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759919AbdACRSE (ORCPT ); Tue, 3 Jan 2017 12:18:04 -0500 Received: by mail-qk0-f171.google.com with SMTP id h201so241978131qke.1 for ; Tue, 03 Jan 2017 09:18:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=DYpnRBqyRHTJStMqpKQzSZLq1ziRYN5GZi6oDi7FuZ4=; b=L8sI8ObWzc+Mrw8OCAekmbM5ExkySZ4cu7Q1WCZXRJY+FNU6CgcBtPryLLG50NOghK yDhXU5SYGxKMP6zSuaLRud5JYBvl9TwiukUvxXMX/TWhB/XzjV5ftIO5bsnrpb3d1kr+ QJDZNU935BJ76Dq+wbwVULwGfn++OnRSYCjXZK83byti8R0eInXPQphzjKaRnoGA0iYR UFcuqrH5HUkBx8eBQUihvBD2gfk/V9OQaY+tLYloRNKwSNWkmi28YwlsiH7XVztjYXJs 2nhddu/VSy5ZlHKkFMi+iPJNVKKx3+SrzxGOPRTz+QRuQwb4GI3b+m97JzH9fvG6rGqK 9OJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=DYpnRBqyRHTJStMqpKQzSZLq1ziRYN5GZi6oDi7FuZ4=; b=EKU4JdUM/zAt/0XgQlOL1SmzO00MP8bIG9aYsTv1jDlxdvHq5ztoTTCIeO6HggRVYU iLJVFJZh7J34mNORfeoXLwU16MOqOQKBDSc1/bJ5GsuJZYNvwEQ7zQxT5dBWpJTuvzBS gIDcrpFXsrVhCV6jdWeIYcdN5YQU9OhoufJL9p9xu2zYTFCDQMzGfbf9/H2tv0QrIqDt ddZ8dkPMdJduh4gG7bDnWlHJCsoFObCSxMAQxNSSF3XyYNtXaefQTsc6JZXQry5m10Xp J502jYjlH9yd9A5WxDb41/JxDnuKyi8srbEXflOwwwiCbb/I4w7fQJ31dl4+kYIlD0sv IkUQ== X-Gm-Message-State: AIkVDXKmJ4fxBWuVRqLKlgxUNOGUzs5kBb4520Ig7Ty+FKO6EyupHLmalDetL+CbrAZz2ydHOBI+T5xcJ3e4pw== X-Received: by 10.55.17.223 with SMTP id 92mr60480937qkr.30.1483463883412; Tue, 03 Jan 2017 09:18:03 -0800 (PST) MIME-Version: 1.0 Received: by 10.237.32.235 with HTTP; Tue, 3 Jan 2017 09:18:02 -0800 (PST) In-Reply-To: References: From: Vincent Pelletier Date: Tue, 3 Jan 2017 18:18:02 +0100 Message-ID: Subject: Re: Oops with dwc3 in device mode and functionfs To: Felipe Balbi , USB list Cc: Andy Shevchenko Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Checking more, I think the 3 "do {...} while(--count)" loops in f_fs.c are wrong: they should be "while(count--){...}" ("git format-patch" attached, sorry, I'm on a dev-hostile mail client at the moment): - count covers function-specific endpoint, so it is 0 when no endpoint is declared, which leads to an integer underflow and exceeding eps array end. - a function enabling/disabling endpoint 0 does not make sense to me (they are only supposed to care about their own descriptor, don't they ?) On Tue, Jan 3, 2017 at 3:49 PM, Vincent Pelletier wrote: > The functionfs side of things is a bit complicated, as I'm writing a > python module[2] for it, and I am still very new to functionfs. It > does not crash on 4.9-rc7 though. As per the above, this was not the whole truth: a device declared with 2 endpoints does not crash on 4.9-rc7, crashes on 4.10-rc2, including with the upcomming rc3 fixes. A zero endpoint version crashes *and* made me realise the loop error. Sadly, this fix alone is not enough to get a functional device. I did not investigate much yet, and need to get some sleep. -- Vincent Pelletier >From 85815ce5c82022ccfbb2c0184589c4766325b517 Mon Sep 17 00:00:00 2001 From: Vincent Pelletier Date: Tue, 3 Jan 2017 16:47:23 +0100 Subject: [RFC] usb: gadget: f_fs: Fix iterations on endpoints. When zero endpoints are declared for a function, there is no endpoint to disable, enable or free, so replace do...while loops with while loops. Change pre-decrement to post-decrement to iterate the same number of times when there are endpoints to process. Signed-off-by: Vincent Pelletier --- drivers/usb/gadget/function/f_fs.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c index 5e746adc8a2d..5490fc51638e 100644 --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -1806,7 +1806,7 @@ static void ffs_func_eps_disable(struct ffs_function *func) unsigned long flags; spin_lock_irqsave(&func->ffs->eps_lock, flags); - do { + while (count--) { /* pending requests get nuked */ if (likely(ep->ep)) usb_ep_disable(ep->ep); @@ -1817,7 +1817,7 @@ static void ffs_func_eps_disable(struct ffs_function *func) __ffs_epfile_read_buffer_free(epfile); ++epfile; } - } while (--count); + } spin_unlock_irqrestore(&func->ffs->eps_lock, flags); } @@ -1831,7 +1831,7 @@ static int ffs_func_eps_enable(struct ffs_function *func) int ret = 0; spin_lock_irqsave(&func->ffs->eps_lock, flags); - do { + while(count--) { struct usb_endpoint_descriptor *ds; int desc_idx; @@ -1867,7 +1867,7 @@ static int ffs_func_eps_enable(struct ffs_function *func) ++ep; ++epfile; - } while (--count); + } spin_unlock_irqrestore(&func->ffs->eps_lock, flags); return ret; @@ -3448,12 +3448,12 @@ static void ffs_func_unbind(struct usb_configuration *c, /* cleanup after autoconfig */ spin_lock_irqsave(&func->ffs->eps_lock, flags); - do { + while (count--) { if (ep->ep && ep->req) usb_ep_free_request(ep->ep, ep->req); ep->req = NULL; ++ep; - } while (--count); + } spin_unlock_irqrestore(&func->ffs->eps_lock, flags); kfree(func->eps); func->eps = NULL; -- 2.11.0