Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1283777rwb; Thu, 8 Dec 2022 08:45:26 -0800 (PST) X-Google-Smtp-Source: AA0mqf7YITOWphPiicdA1MfFc3q/Gh9/h+vhl41TVEn2eB5ehEMh85UE99J9fBuNQdmRIjOvH50a X-Received: by 2002:a17:903:228a:b0:189:d82e:79d1 with SMTP id b10-20020a170903228a00b00189d82e79d1mr17628763plh.70.1670517926325; Thu, 08 Dec 2022 08:45:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670517926; cv=none; d=google.com; s=arc-20160816; b=nQtqKuu19jDZHCqCymOFSQJyyoy3v5fTQ9PwOtZXjjDVBC/BiWl5iMfXw/Qiy/yWtM QRe1U5Fr5boA+5EgsqW0y7x3Ms3qRb4o1AMh8sqmjWmJSe9fjp8ngZpnUd/Gp8LZ+Rtj JyOzQlGqQ7Uvr7mBFsyQ2s422Cpd2dFgurnBjm1KCC1U82rGuJO9LGPnjRg4oqRd/8qi wB9yScnoEVlXF9EPjULp9S1Q03Enet2KKGf+eyNkriwaFDzcToO4FVHRz/o0fCl9E1Fo fAoTjc5AVcKxNmFAaB7Aa+HbOWlTzcXpDHdHWCX2dcYKlWqOhSBX++Ll0z0xjC3syyn1 dvGA== 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; bh=TPN1rgakJvH0LQDcE1o1tzrSHx2BFrkee3kBiH5/Np8=; b=A3Aid3RZABjRiEvAn6QadiDqrQw4zG7GfZMI6+T7UDqEl4YW/XKxtPIJW1U1e7zW2C 2oM8ppMv4Z8TLSpwY5SjrQNwlv5oZaOneVpgwKT7BBCJ4PnHwdTgQ0LUBfAuR1gKpDl1 sTmCKkyomASCpltamJSvfyUyDaNE+FBcOQukS59Tf059VQSG19vLiUopWG+ZYpkp08Ih 237Pw2FiG9OsIysglIKsTUCa5Q2hq1tianjxHRpF4fPmzsMNST4UwPE+mPY/IC6Cm1k6 jWauyj5u1ZioJ3aPIySYsF572p9cbKcjFK9L0sZ1jmIg/AYop3EpPicIf+np9d0saeBI xPFw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p16-20020a170902e75000b0018996404dd9si18594157plf.267.2022.12.08.08.45.13; Thu, 08 Dec 2022 08:45:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230037AbiLHQ2y (ORCPT + 72 others); Thu, 8 Dec 2022 11:28:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230136AbiLHQ23 (ORCPT ); Thu, 8 Dec 2022 11:28:29 -0500 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 2BC9B1005F for ; Thu, 8 Dec 2022 08:28:28 -0800 (PST) Received: (qmail 731998 invoked by uid 1000); 8 Dec 2022 11:28:24 -0500 Date: Thu, 8 Dec 2022 11:28:24 -0500 From: Alan Stern To: Vincent MAILHOL Cc: Oliver Neukum , Marc Kleine-Budde , linux-can@vger.kernel.org, Wolfgang Grandegger , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Frank Jungclaus , socketcan@esd.eu, Yasushi SHOJI , Stefan =?iso-8859-1?Q?M=E4tje?= , Hangyu Hua , Oliver Hartkopp , Peter Fink , Jeroen Hofstee , Christoph =?iso-8859-1?Q?M=F6hring?= , John Whittington , Vasanth Sadhasivan , Jimmy Assarsson , Anssi Hannula , Pavel Skripkin , Stephane Grosjean , Wolfram Sang , "Gustavo A . R . Silva" , Julia Lawall , Dongliang Mu , Sebastian Haas , Maximilian Schneider , Daniel Berglund , Olivier Sobrie , Remigiusz =?utf-8?B?S2/FgsWCxIV0YWo=?= , Jakob Unterwurzacher , Martin Elshuber , Bernd Krumboeck , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Subject: Re: [PATCH 0/8] can: usb: remove all usb_set_intfdata(intf, NULL) in drivers' disconnect() Message-ID: References: <20221203133159.94414-1-mailhol.vincent@wanadoo.fr> <9493232b-c8fa-5612-fb13-fccf58b01942@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Dec 09, 2022 at 12:44:51AM +0900, Vincent MAILHOL wrote: > On Thu. 8 Dec. 2022 at 20:04, Oliver Neukum wrote: > > >> which is likely, then please also remove checks like this: > > >> > > >> struct ems_usb *dev = usb_get_intfdata(intf); > > >> > > >> usb_set_intfdata(intf, NULL); > > >> > > >> if (dev) { > > > > Here. If you have a driver that uses usb_claim_interface(). > > You need this check or you unregister an already unregistered > > netdev. > > Sorry, but with all my best intentions, I still do not get it. During > the second iteration, inft is NULL and: No, intf is never NULL. Rather, the driver-specific pointer stored in intfdata may be NULL. You seem to be confusing intf with intfdata(intf). > /* equivalent to dev = intf->dev.data. Because intf is NULL, > * this is a NULL pointer dereference */ > struct ems_usb *dev = usb_get_intfdata(intf); So here dev will be NULL when the second interface's disconnect routine runs, because the first time through the routine sets the intfdata to NULL for both interfaces: USB core calls ->disconnect(intf1) disconnect routine sets intfdata(intf1) and intfdata(intf2) both to NULL and handles the disconnection USB core calls ->disconnect(intf2) disconnect routine sees that intfdata(intf2) is already NULL, so it knows that it doesn't need to do anything more. As you can see in this scenario, neither intf1 nor intf2 is ever NULL. > /* OK, intf is already NULL */ > usb_set_intfdata(intf, NULL); > > /* follows a NULL pointer dereference so this is undefined > * behaviour */ > if (dev) { > > How is this a valid check that you entered the function for the second > time? If intf is the flag, you should check intf, not dev? Something > like this: intf is not a flag; it is the argument to the function and is never NULL. The flag is the intfdata. > struct ems_usb *dev; > > if (!intf) > return; > > dev = usb_get_intfdata(intf); > /* ... */ > > I just can not see the connection between intf being NULL and the if > (dev) check. All I see is some undefined behaviour, sorry. Once you get it straightened out in your head, you will understand. Alan Stern