Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1901438pxy; Mon, 2 Aug 2021 13:20:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyuGe3VsJZl8gl6C9sQWhEwm43WSwYGvpYJkh3W/i2iNRdvzyRrGaZHHt3JwEumOGN86DcN X-Received: by 2002:a17:906:3983:: with SMTP id h3mr16677051eje.249.1627935634695; Mon, 02 Aug 2021 13:20:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627935634; cv=none; d=google.com; s=arc-20160816; b=RkQwutqOo6sQIanyXeAICG/k8i4TPqVcanYHh0tdLvtWJCHI9kfAtyku6uaZbvxAin xvaoK4UgYjw+K0CRxAqcJPY3G6Z+3DMl+/IfeW6FWLZ7OEW7YW2YtHetTHyv2OmGshHn GwEVw+0ENEf8n3QNExTuKCskPhXtf1c6cMhrx42gGpmScaupR+wjqlp0KeX5spVAeDh/ 9Cbea+owvS3fH51QZgD6OIV8zJ8JboWylCB90U1nJkDu/IYXnnmdRmW8PeaZDVa1BQdX FzCdmvTiRUDLpx12yHnNBFT4jXgf6sgwj+uvy2oiOWubN0WweKNzt0WIeffRlmGge3F/ XrHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=5z65MU1ZlNhZZG+kuphYs+pzOMsk6nOdcY8xws3IzNU=; b=KefTjG1LC8q8WrQndnDEP+YY6ZklibeWtzfeOt9LwSe5V2WpgY4ZKX7Jhsk7+Hz4h0 gK2JcVmDVTuE7oathKDc719IYQqQWcaO1P0OCl++QOkWsrUlCd5fAZ2/sq4eTfvxyAiY Io6Ud4j4SLCcZDRr30O7qwcxx1fbboHbbO51CSIvZEe694jiL3M1R1+3npMmkBAN25aB 4QL9iF8TzLFpP+GEwDMdU9NeH0vdw19gQ8PEw+CtgA3cJkEfl+PmlSc4q1GE3/tTcDKa +U8uwpPYS6mD48nU7inSy3SWlcpcH61rEweWve4Ur/G7Cu6Ov+RGprdh04hY+NDCPwW+ tHdQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i14si13157370eje.573.2021.08.02.13.20.10; Mon, 02 Aug 2021 13:20:34 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230095AbhHBUQc (ORCPT + 99 others); Mon, 2 Aug 2021 16:16:32 -0400 Received: from netrider.rowland.org ([192.131.102.5]:42781 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S229729AbhHBUQb (ORCPT ); Mon, 2 Aug 2021 16:16:31 -0400 Received: (qmail 344224 invoked by uid 1000); 2 Aug 2021 16:16:20 -0400 Date: Mon, 2 Aug 2021 16:16:20 -0400 From: Alan Stern To: Salah Triki Cc: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , Greg Kroah-Hartman , open list , Oliver Neukum Subject: Re: [PATCH v2] bluetooth: bcm203x: update the reference count of udev Message-ID: <20210802201620.GA344022@rowland.harvard.edu> References: <20210731154102.GA908767@pc> <9B2209B6-D371-432B-A3F5-F1CD7C7967A3@holtmann.org> <20210802193411.GA1006176@pc> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210802193411.GA1006176@pc> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 02, 2021 at 08:34:11PM +0100, Salah Triki wrote: > On Sun, Aug 01, 2021 at 08:01:06PM +0200, Marcel Holtmann wrote: > > Hi Salah, > > > > > Use usb_get_dev() to increment the reference count of the usb device > > > structure in order to avoid releasing the structure while it is still in > > > use. And use usb_put_dev() to decrement the reference count and thus, > > > when it will be equal to 0 the structure will be released. > > > > > > Signed-off-by: Salah Triki > > > --- > > > Change since v1: > > > Modification of the description > > > > > > drivers/bluetooth/bcm203x.c | 4 +++- > > > 1 file changed, 3 insertions(+), 1 deletion(-) > > > > > > diff --git a/drivers/bluetooth/bcm203x.c b/drivers/bluetooth/bcm203x.c > > > index e667933c3d70..547d35425d70 100644 > > > --- a/drivers/bluetooth/bcm203x.c > > > +++ b/drivers/bluetooth/bcm203x.c > > > @@ -166,7 +166,7 @@ static int bcm203x_probe(struct usb_interface *intf, const struct usb_device_id > > > if (!data) > > > return -ENOMEM; > > > > > > - data->udev = udev; > > > + data->udev = usb_get_dev(udev); > > > data->state = BCM203X_LOAD_MINIDRV; > > > > > > data->urb = usb_alloc_urb(0, GFP_KERNEL); > > > @@ -244,6 +244,8 @@ static void bcm203x_disconnect(struct usb_interface *intf) > > > > > > usb_set_intfdata(intf, NULL); > > > > > > + usb_put_dev(data->udev); > > > + > > > usb_free_urb(data->urb); > > > kfree(data->fw_data); > > > kfree(data->buffer); > > > > I do not understand this. If this is something broken, then it is broken in > > btusb.c as well and that driver is heavily used by all sorts of devices. So > > we should have seen bug reports about this. > > > > Regards > > > > Marcel > > > Hi Marcel, > > The patch is based on the following documentation of usb_get_dev(): > > [quote] > Each live reference to a device should be refcounted. > > Drivers for USB interfaces should normally record such references in their > probe() methods, when they bind to an interface, and release them by calling > usb_put_dev(), in their disconnect() methods. > [/quote] That documentation is incorrect. It is not necessary for drivers to take a reference to the devices they are bound to. Properly written subsystems will guarantee that the driver is unbound from the device before the device is released. To put it another way, if failure to take such a reference leads to an invalid memory access then there is a bug in the subsystem, not in the driver. Rather than changing the bcm203x driver, you should consider getting rid of the unnecessary advice in the documentation of usb_get_dev. Alan Stern