Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5416232rdb; Wed, 13 Dec 2023 08:04:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IFtAl/mOzVPKNa+Ii/aN4eZ1ja04lWaqCMf185YhjfklQR9+kbZwD48gaBKqwIgJbR7o8Pl X-Received: by 2002:a05:6a20:7490:b0:18f:97c:8241 with SMTP id p16-20020a056a20749000b0018f097c8241mr4924821pzd.75.1702483498316; Wed, 13 Dec 2023 08:04:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702483498; cv=none; d=google.com; s=arc-20160816; b=L1hOZK2a947BLmriRYJvJ+C2QNci42bDjJiZFiKbTwT7VhViwTDijgDj7TRKZAS89w tO1E05F2gvJhJqegzn3LX3R+yII3SGqYylUnNgkQWSuVKagr0RALydLArZieXdhqOoGP LzgoFkEJg8HTZbliWuyatZhZ+byte/zGcXCe3aeTl4DX4ZFqDLs+DGgplk4THah3o7O+ xiPQYy2eurjTPWBJrkFpzo3KIaZ9ilu6PaPdFSdNSfNcFx+MVFcQnMPzxXjywzLmN3iB 3cCV0QCMxT38RPQ0XkkJ5tvseTfgG+/aVh0/mhbmK+YkveuWzmDEAi/xVRmjNj3plFOD tu2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=QRqwo6sswifF3LFPo684dgIWFUyFasVRNKlEWyiikhI=; fh=kJcXMp4O4T0eOgyy/+VGeOLmnrmVyNT+X7mcHhSI3lc=; b=xiGFtkI9TZIlM3h5WHJ4P1nd2bpOyCnd3tSYxDC5CanwlLdepIeaf+VdtBzKY9xvET TtHelbnEAjmR7lEehOq9O0idqZNoVnGltmQMHaBRTyy3BN1+1MaEEODyrlARGT8UBK3Q hxrLkHpd2GbMIq4Qc861KxlDCJCMQJfQM+FXgURpFssw3h9DkpDubj8aCTHIpUZOiDM5 mCiXN5Beq5bu0MId5mAyu9+TvMDQxywemWqR03bFZwLgEILofg2fn/p7LKxa1AmZEYXs sV3l1lTQN+ZWUOM2C6pqiKJoewWaOc8S6JKuux0yyCwtsdL3eY3L8OqmyUgazhg6ayAt bt3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=R+1MKf1r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id k1-20020a632401000000b005be1ee5be37si9577144pgk.133.2023.12.13.08.04.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 08:04:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=R+1MKf1r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 5F087802B17D; Wed, 13 Dec 2023 08:04:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233137AbjLMQEi (ORCPT + 99 others); Wed, 13 Dec 2023 11:04:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229806AbjLMQEg (ORCPT ); Wed, 13 Dec 2023 11:04:36 -0500 Received: from pandora.armlinux.org.uk (pandora.armlinux.org.uk [IPv6:2001:4d48:ad52:32c8:5054:ff:fe00:142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B58A910B; Wed, 13 Dec 2023 08:04:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2019; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=QRqwo6sswifF3LFPo684dgIWFUyFasVRNKlEWyiikhI=; b=R+1MKf1rzy9ANE955VAmc7MRqm hCWSk4JX137rBfen/P8Nm47Di+4ndSbW7wyKqKAZQfSeKvNqjZOQ7h+ozG1y82K68yoGn+GJmlxTJ HSa3d2+TEnPRtqH2l6FBZBT0QG523tdWrM2gvgIjLfWc+lQ+OwZn8xeMBQZ+p27avi7V6KZ1EIwmE tUOWNpuAZOwuNx7857l8Nppar0NfBSL4jj2fssUdXc1mBs8uG1+Lm5CRVI9LBqGlXLhqKzg21wp1M Pg2rWKekk7nkzy3zfT65bes3ssLwvxH5KjSQJCl0VUaxSTKZcMGrv8l/GBDNAPCaM0ej6RDZvdprY Q6b4hJhQ==; Received: from shell.armlinux.org.uk ([fd8f:7570:feb6:1:5054:ff:fe00:4ec]:55118) by pandora.armlinux.org.uk with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1rDRiY-0000D5-0G; Wed, 13 Dec 2023 16:04:14 +0000 Received: from linux by shell.armlinux.org.uk with local (Exim 4.94.2) (envelope-from ) id 1rDRiW-0001cw-Q5; Wed, 13 Dec 2023 16:04:12 +0000 Date: Wed, 13 Dec 2023 16:04:12 +0000 From: "Russell King (Oracle)" To: Daniel Golle Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chunfeng Yun , Vinod Koul , Kishon Vijay Abraham I , Felix Fietkau , John Crispin , Sean Wang , Mark Lee , Lorenzo Bianconi , Matthias Brugger , AngeloGioacchino Del Regno , Andrew Lunn , Heiner Kallweit , Alexander Couzens , Qingfang Deng , SkyLake Huang , Philipp Zabel , netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-phy@lists.infradead.org Subject: Re: [RFC PATCH net-next v3 3/8] net: pcs: pcs-mtk-lynxi: add platform driver for MT7988 Message-ID: References: <8aa905080bdb6760875d62cb3b2b41258837f80e.1702352117.git.daniel@makrotopia.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8aa905080bdb6760875d62cb3b2b41258837f80e.1702352117.git.daniel@makrotopia.org> Sender: Russell King (Oracle) X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 13 Dec 2023 08:04:55 -0800 (PST) On Tue, Dec 12, 2023 at 03:47:18AM +0000, Daniel Golle wrote: > Introduce a proper platform MFD driver for the LynxI (H)SGMII PCS which > is going to initially be used for the MT7988 SoC. > > Signed-off-by: Daniel Golle I made some specific suggestions about what I wanted to see for "getting" PCS in the previous review, and I'm disappointed that this patch set is still inventing its own solution. > +struct phylink_pcs *mtk_pcs_lynxi_get(struct device *dev, struct device_node *np) > +{ > + struct platform_device *pdev; > + struct mtk_pcs_lynxi *mpcs; > + > + if (!np) > + return NULL; > + > + if (!of_device_is_available(np)) > + return ERR_PTR(-ENODEV); > + > + if (!of_match_node(mtk_pcs_lynxi_of_match, np)) > + return ERR_PTR(-EINVAL); > + > + pdev = of_find_device_by_node(np); > + if (!pdev || !platform_get_drvdata(pdev)) { This is racy - as I thought I described before, userspace can unbind the device in one thread, while another thread is calling this function. With just the right timing, this check succeeds, but... > + if (pdev) > + put_device(&pdev->dev); > + return ERR_PTR(-EPROBE_DEFER); > + } > + > + mpcs = platform_get_drvdata(pdev); mpcs ends up being read as NULL here. Even if you did manage to get a valid pointer, "mpcs" being devm-alloced could be freed from under you at this point... > + device_link_add(dev, mpcs->dev, DL_FLAG_AUTOREMOVE_CONSUMER); resulting in this accessing memory which has been freed. The solution would be either to suppress the bind/unbind attributes (provided the underlying struct device can't go away, which probably also means ensuring the same of the MDIO bus. Aternatively, adding a lock around the remove path and around the checking of platform_get_drvdata() down to adding the device link would probably solve it. However, I come back to my general point - this kind of stuff is hairy. Do we want N different implementations of it in various drivers with subtle bugs, or do we want _one_ implemenatation. If we go with the one implemenation approach, then we need to think about whether we should be using device links or not. The problem could be for network interfaces where one struct device is associated with multiple network interfaces. Using device links has the unfortunate side effect that if the PCS for one of those network interfaces is removed, _all_ network interfaces disappear. My original suggestion was to hook into phylink to cause that to take the link down when an in-use PCS gets removed. > + > + return &mpcs->pcs; > +} > +EXPORT_SYMBOL(mtk_pcs_lynxi_get); > + > +void mtk_pcs_lynxi_put(struct phylink_pcs *pcs) > +{ > + struct mtk_pcs_lynxi *cur, *mpcs = NULL; > + > + if (!pcs) > + return; > + > + mutex_lock(&instance_mutex); > + list_for_each_entry(cur, &mtk_pcs_lynxi_instances, node) > + if (pcs == &cur->pcs) { > + mpcs = cur; > + break; > + } > + mutex_unlock(&instance_mutex); I don't see what this loop gains us, other than checking that the "pcs" is still on the list and hasn't already been removed. If that is all that this is about, then I would suggest: bool found = false; if (!pcs) return; mpcs = pcs_to_mtk_pcs_lynxi(pcs); mutex_lock(&instance_mutex); list_for_each_entry(cur, &mtk_pcs_lynxi_instances, node) if (cur == mpcs) { found = true; break; } mutex_unlock(&instance_mutex); if (WARN_ON(!found)) return; which makes it more obvious why this exists. -- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!