Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp517218pxf; Thu, 25 Mar 2021 08:35:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy73XRqHPVGWNgwJ1r4GFb2pKI/oeUC8qPrzrMNA7PpE3R6rO4OmvVFauvLj0qR0O1Ty4Px X-Received: by 2002:a17:906:7e12:: with SMTP id e18mr10498279ejr.316.1616686504093; Thu, 25 Mar 2021 08:35:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616686504; cv=none; d=google.com; s=arc-20160816; b=XJC+v9NsuC8AP4eHZDSL8pGRk8xAjm9JE7Gpf1+gwioZIknu3gMBHMQPSpfQBVB/g2 kwMn0C1jzkEyA/Pyq0B2GaQEONtP+WLgwLvq9RvzeUVGHvCmPROItP9i50favVQcXprD oiRe9sM27Yjw4HfOVE5VfgAgaLDmshPx09xR1eL8kiua/i2Fep6nTGsB4bWD8UT0sose 2Bmk3wbFsEfQcSssB4JrqiD09wSmcGcEZ/KXExhx1gtzqDfQS1fUvaYYa7qOlKG0QmMB QR+up7eDIenvc6hl//pAACLRRqSi06jD2vlvOK1DADEGzxAxSjPM0J5A8J4PoApTImwA zevg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:ironport-sdr :ironport-sdr; bh=t4V9eUqTVcXc/QHECTO8UxNMhDvE9dFNXZ2PVAINwEw=; b=FowqabmT6KJ4S0p/bwLkY52GpjFgIsv+A5l21KCA5QYRWRinX8EJdmW86Z8y28UBhJ O6zan4perRLFrCKEIwBf5kLawJEYJ61jgRwGzNZG/yc4WDGlU6wBWJW295Wd0DtN9okZ /wB+o0KdfnILJ8ER9GGDPG5nBqTzNewlO+7ZgR3NLAnvaWm+XMtXGgYcotKEOiS4TrPQ Bh2R6K6yDgRCqUprV2ESGkuKFCoTw7kyYOad/W1RhDN1NJWOZ20bMUxMUwj4rg6wYk3U hHYizD2VE0PF+5TxbqDRK6nLwXCJHi+EjZLLuIGPPp6x1uPGhSCL+46dNKOfXN/FxqdL sRhQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v25si4555573ejw.423.2021.03.25.08.34.41; Thu, 25 Mar 2021 08:35:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231359AbhCYPdp (ORCPT + 99 others); Thu, 25 Mar 2021 11:33:45 -0400 Received: from mga01.intel.com ([192.55.52.88]:40679 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231373AbhCYPdW (ORCPT ); Thu, 25 Mar 2021 11:33:22 -0400 IronPort-SDR: PieRAsMXPgg6QGNOK/91OdFxOWMKr6PDlO0CfJbDnx8yjAePH1OKZ+bg8KrRStSjzqLHzk7dxN mRwjHHirVmuA== X-IronPort-AV: E=McAfee;i="6000,8403,9934"; a="211088220" X-IronPort-AV: E=Sophos;i="5.81,277,1610438400"; d="scan'208";a="211088220" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2021 08:33:22 -0700 IronPort-SDR: 54Ba1dtwnmKdN/mMr3Vx1g/BYfG6rbdVuoJ7VEyO8+zykORvZXm6eKdYWmivDIcfE9NH7u1bj5 MtkVPnlkPX6Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.81,277,1610438400"; d="scan'208";a="514681763" Received: from kuha.fi.intel.com ([10.237.72.162]) by fmsmga001.fm.intel.com with SMTP; 25 Mar 2021 08:33:19 -0700 Received: by kuha.fi.intel.com (sSMTP sendmail emulation); Thu, 25 Mar 2021 17:33:18 +0200 Date: Thu, 25 Mar 2021 17:33:18 +0200 From: Heikki Krogerus To: Greg Kroah-Hartman Cc: Alan Stern , Benson Leung , Prashant Malani , Guenter Roeck , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/6] usb: Iterator for ports Message-ID: References: <20210325122926.58392-1-heikki.krogerus@linux.intel.com> <20210325122926.58392-2-heikki.krogerus@linux.intel.com> <20210325144109.GB785961@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 25, 2021 at 04:20:15PM +0100, Greg Kroah-Hartman wrote: > On Thu, Mar 25, 2021 at 05:14:42PM +0200, Heikki Krogerus wrote: > > On Thu, Mar 25, 2021 at 10:41:09AM -0400, Alan Stern wrote: > > > On Thu, Mar 25, 2021 at 03:29:21PM +0300, Heikki Krogerus wrote: > > > > Introducing usb_for_each_port(). It works the same way as > > > > usb_for_each_dev(), but instead of going through every USB > > > > device in the system, it walks through the USB ports in the > > > > system. > > > > > > > > Signed-off-by: Heikki Krogerus > > > > > > This has a couple of nasty errors. > > > > > > > --- > > > > drivers/usb/core/usb.c | 43 ++++++++++++++++++++++++++++++++++++++++++ > > > > include/linux/usb.h | 1 + > > > > 2 files changed, 44 insertions(+) > > > > > > > > diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c > > > > index 2ce3667ec6fae..6d49db9a1b208 100644 > > > > --- a/drivers/usb/core/usb.c > > > > +++ b/drivers/usb/core/usb.c > > > > @@ -398,6 +398,49 @@ int usb_for_each_dev(void *data, int (*fn)(struct usb_device *, void *)) > > > > } > > > > EXPORT_SYMBOL_GPL(usb_for_each_dev); > > > > > > > > +struct each_hub_arg { > > > > + void *data; > > > > + int (*fn)(struct device *, void *); > > > > +}; > > > > + > > > > +static int __each_hub(struct device *dev, void *data) > > > > +{ > > > > + struct each_hub_arg *arg = (struct each_hub_arg *)data; > > > > + struct usb_device *hdev = to_usb_device(dev); > > > > > > to_usb_device() won't work properly if the struct device isn't embedded > > > in an actual usb_device structure. And that will happen, since the USB > > > bus type holds usb_interface structures as well as usb_devices. > > > > OK, so I need to filter them out. > > > > > In fact, you should use usb_for_each_dev here; it already does what you > > > want. > > > > Unfortunately I can't use usb_for_each_dev here, because it deals with > > struct usb_device while I need to deal with struct device in the > > callback. > > Why do you need 'struct device' in the callback? All you really want is > the hub devices, right? I need the ports, not the hubs. > > > > + struct usb_hub *hub; > > > > + int ret; > > > > + int i; > > > > + > > > > + hub = usb_hub_to_struct_hub(hdev); > > > > + if (!hub) > > > > + return 0; > > > > + > > > > + for (i = 0; i < hdev->maxchild; i++) { > > > > + ret = arg->fn(&hub->ports[i]->dev, arg->data); > > > > + if (ret) > > > > + return ret; > > > > + } > > > > + > > > > + return 0; > > > > +} > > > > > > Don't you need some sort of locking or refcounting here? What would > > > happen if this hub got removed while the routine was running? > > > > I'll use a lock then. > > That's not going to work to be held over a callback. Just properly > increment the reference count. I though we have done that already. Does bus_for_each_dev() do that for the device that it passes to the callback until that callback returns? thanks, -- heikki