Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp13068747rwb; Sat, 26 Nov 2022 22:15:32 -0800 (PST) X-Google-Smtp-Source: AA0mqf4VwpLFxIYdqtTBe1qgyuS2opVfDvg9iEaHqkralOfP5DlsDP6/daxaBWDkXLhHCdbXki+P X-Received: by 2002:a17:902:ce90:b0:187:19c4:373a with SMTP id f16-20020a170902ce9000b0018719c4373amr38912178plg.163.1669529731841; Sat, 26 Nov 2022 22:15:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669529731; cv=none; d=google.com; s=arc-20160816; b=MPkTkle1NTbohCey8oP2Qj6UZHAFPyi42Huk70354EnpAHlLzsXphCzd5z5bnmKNPl 0dnY85gdbpbWBiIgr4v5v1JT3KvkmkOv9qVBZRD9fHqkC/OhQdGTTsNOL2EJ2LOUrpe+ 74VSRj3IsYKVKvmAhgo1L1JioP0AVPhnR+jsdmf/0cxzvN/KSFJqN3oKFY2HfbTk1Wkj TCxWPqf387Gdnk/AxP9ABnkEOtEmvD26HaYhX46b/wJzO+gqOcjiglmniNi166Zoudsl vZR/5tZ3U+oeWvMXwCvfDRlLW2VwQDPB6rVPlRrgx+05Tu1KUWWjday4Ra6/h4hFYv2Y rkWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=jX4FlIDKRZ6HtC7tR+0qLxHARsOw0Qkz+IpKaPjHkjU=; b=QyNztE5PYW5UcZyjOxsvKkkkO3ZoGH725mnpaDPemh1vKEWcJ80xq8tRA29ddLXvbU R+zv5KmEVLT7Ln87rn6qIgrLXZ7rghbsJpLfk9BtiPNFiZWntVIix7EaMeMZg+vUoCuH NdywqlA3owUeISiv5KzIo/T2C50L0CHUK+fBdf1eCLJBzo0KNcmG3SzgdKMoCLaeYQTV ulxiYvUkRkKoC00utfOZZBIvglV5BTpvCng/gtgIKOeI66ybF5wJtLw+6I9U6k/ddPtf YXg07B1j/sk727wjPl4xGa3Z1gObbvoC0Zu05zbw/k6C5ZYKdZyq8O6VKlK/hGtTxf4l GZwQ== 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 w13-20020a170902e88d00b00188ef2314b2si9809778plg.79.2022.11.26.22.15.10; Sat, 26 Nov 2022 22:15:31 -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 S229504AbiK0FKt (ORCPT + 85 others); Sun, 27 Nov 2022 00:10:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229453AbiK0FKp (ORCPT ); Sun, 27 Nov 2022 00:10:45 -0500 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53C9613F6F; Sat, 26 Nov 2022 21:10:44 -0800 (PST) Received: by mail-pf1-f181.google.com with SMTP id a9so4092121pfr.0; Sat, 26 Nov 2022 21:10:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jX4FlIDKRZ6HtC7tR+0qLxHARsOw0Qkz+IpKaPjHkjU=; b=mNalp5d0XlgnowPUC0RWR8s3zax7wmDc3l9ttGYSVLZbbh7f1px7/vU4mY9XlWwx0t l6QkvMnEL+/X1btFNvQCztsXWE5pCWQrA79frkG7qUO0nOFIbU2ybCkcCGmgMkXpnUDo P/CKdre7nA0CS16+ESFGlqKSctZ3C3DyOTrnyht7j8fB8Q4gapjyEmFPSVV/1YvGEBP9 cz55DOQtSHOIriVhXQrsQxrHNpoab/1nIeTSKCeEQtAq6zfCongG+DF8+LcMkrQET1FU STAWD7K4y9BJI3egnoRuxmZvH/5uW9keq55FPRwZFGmUKhrgUO78QCymqhVrVi67C/ZL XUeA== X-Gm-Message-State: ANoB5pniHarzF5ogHzG7HV6SyOpj8EMgyYw3RHA+nsB7DHT7pCjzNnsv 2vbRGMq8CKtKRuAAETCLYatXd1X4xkJAvdSewLp+gFJ66R9PuA== X-Received: by 2002:a05:6a00:194a:b0:56b:a795:e99c with SMTP id s10-20020a056a00194a00b0056ba795e99cmr35289001pfk.14.1669525843741; Sat, 26 Nov 2022 21:10:43 -0800 (PST) MIME-Version: 1.0 References: <20221104073659.414147-1-mailhol.vincent@wanadoo.fr> <20221126162211.93322-1-mailhol.vincent@wanadoo.fr> <20221126162211.93322-3-mailhol.vincent@wanadoo.fr> In-Reply-To: From: Vincent MAILHOL Date: Sun, 27 Nov 2022 14:10:32 +0900 Message-ID: Subject: Re: [PATCH v4 2/6] can: etas_es58x: add devlink support To: Andrew Lunn Cc: linux-can@vger.kernel.org, Marc Kleine-Budde , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , netdev@vger.kernel.org, linux-usb@vger.kernel.org, Saeed Mahameed , Jiri Pirko , Lukas Magel Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, 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 Tue. 27 Nov. 2022 at 01:51, Andrew Lunn wrote: > > @@ -2196,11 +2198,12 @@ static struct es58x_device *es58x_init_es58x_dev(struct usb_interface *intf, > > ops = &es581_4_ops; > > } > > > > - es58x_dev = devm_kzalloc(dev, es58x_sizeof_es58x_device(param), > > - GFP_KERNEL); > > - if (!es58x_dev) > > + devlink = devlink_alloc(&es58x_dl_ops, es58x_sizeof_es58x_device(param), > > + dev); > > + if (!devlink) > > return ERR_PTR(-ENOMEM); > > > > + es58x_dev = devlink_priv(devlink); > > That is 'interesting'. Another interesting thing I found is: https://elixir.bootlin.com/linux/v6.1-rc6/source/drivers/net/ethernet/intel/ice/ice_devlink.c#L866 Because devlink does not have an equivalent to devm_kzalloc(), that driver uses devm_add_action_or_reset() instead. But any other drivers will call devlink_free() in their disconnect function. So here, I just followed the trend. > Makes me wonder about lifetimes of different > objects. Previously your es58x_dev structure would disappear when the > driver is released, or an explicit call to devm_kfree(). Now it > disappears when devlink_free() is called. Even before that, this driver used to release es58x_dev in its disconnect() function. I changed it to use devm_kzalloc() last year after discovering its existence. https://git.kernel.org/torvalds/linux/c/6bde4c7fd845 >Any danger of use after free here? devlink_alloc() allocates one continuous block for both the devlink and the device priv (struct es58x_dev here): https://elixir.bootlin.com/linux/v6.1-rc6/source/net/core/devlink.c#L9629 So calling devlink_free() also releases struct es58x_dev. > USB devices always make me wonder about life times rules since they > are probably the mode dynamic sort of device the kernel has the > handle, them just abruptly disappearing. > > > es58x_dev->param = param; > > es58x_dev->ops = ops; > > es58x_dev->dev = dev; > > @@ -2247,6 +2250,8 @@ static int es58x_probe(struct usb_interface *intf, > > if (ret) > > return ret; > > > > + devlink_register(priv_to_devlink(es58x_dev)); > > + > > for (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++) { > > ret = es58x_init_netdev(es58x_dev, ch_idx); > > if (ret) { > > @@ -2272,8 +2277,10 @@ static void es58x_disconnect(struct usb_interface *intf) > > dev_info(&intf->dev, "Disconnecting %s %s\n", > > es58x_dev->udev->manufacturer, es58x_dev->udev->product); > > > > + devlink_unregister(priv_to_devlink(es58x_dev)); > > es58x_free_netdevs(es58x_dev); > > es58x_free_urbs(es58x_dev); > > + devlink_free(priv_to_devlink(es58x_dev)); > > usb_set_intfdata(intf, NULL); > > Should devlink_free() be after usb_set_inftdata()? A look at $ git grep -W "usb_set_intfdata(.*NULL)" shows that the two patterns (freeing before or after usb_set_intfdata()) coexist. You are raising an important question here. usb_set_intfdata() does not have documentation that freeing before it is risky. And the documentation of usb_driver::disconnect says that: "@disconnect: Called when the interface is no longer accessible, usually because its device has been (or is being) disconnected or the driver module is being unloaded." Ref: https://elixir.bootlin.com/linux/v6.1-rc6/source/include/linux/usb.h#L1130 So the interface no longer being accessible makes me assume that the order does not matter. If it indeed matters, then this is a foot gun and there is some clean-up work waiting for us on many drivers. @Greg, any thoughts on whether or not the order of usb_set_intfdata() and resource freeing matters or not? Yours sincerely, Vincent Mailhol