From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6682FC4332F for ; Sat, 19 Nov 2022 05:41:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231543AbiKSFlN (ORCPT ); Sat, 19 Nov 2022 00:41:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229470AbiKSFlK (ORCPT ); Sat, 19 Nov 2022 00:41:10 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2586C56565; Fri, 18 Nov 2022 21:41:08 -0800 (PST) Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NDjDW2V6fzRpGg; Sat, 19 Nov 2022 13:40:43 +0800 (CST) Received: from kwepemm600005.china.huawei.com (7.193.23.191) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 19 Nov 2022 13:41:07 +0800 Received: from [10.67.109.54] (10.67.109.54) by kwepemm600005.china.huawei.com (7.193.23.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 19 Nov 2022 13:41:06 +0800 Subject: Re: [PATCH net v2] net: mdio-ipq4019: fix possible invalid pointer dereference To: Andrew Lunn References: <20221117090514.118296-1-tanghui20@huawei.com> <6cad3105-0e70-d890-162b-513855885fde@huawei.com> CC: , , , , , , , , From: Hui Tang Message-ID: <3cb5a576-8eb7-54fc-4f4b-9db360b6713d@huawei.com> Date: Sat, 19 Nov 2022 13:41:05 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.109.54] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm600005.china.huawei.com (7.193.23.191) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/11/18 21:44, Andrew Lunn wrote: >> So, the code should be as follows, is that right? >> >> + void __iomem *devm_ioremap_resource_optional(struct device *dev, >> + const struct resource *res) >> + { >> + void __iomem *base; >> + >> + base = __devm_ioremap_resource(dev, res, DEVM_IOREMAP); >> + if (IS_ERR(base) && PTR_ERR(base) == -ENOMEM) >> + return NULL; >> + >> + return base; >> + } >> >> >> [...] >> res = platform_get_resource(pdev, IORESOURCE_MEM, 1); >> - if (res) >> + if (res) { >> + priv->eth_ldo_rdy = devm_ioremap_resource_optional(&pdev->dev, res) >> + if (IS_ERR(priv->eth_ldo_rdy)) >> + return PTR_ERR(priv->eth_ldo_rdy); >> + } >> [...] > > Yes, that is the basic concept. > > The only thing i might change is the double meaning of -ENOMEM. > __devm_ioremap_resource() allocates memory, and if that memory > allocation fails, it returns -ENOMEM. If the resource does not exist, > it also returns -ENOMEM. So you cannot tell these two error conditions > apart. Most of the other get_foo() calls return -ENODEV if the > gpio/regulator/clock does not exist, so you can tell if you are out of > memory. But ioremap is specifically about memory so -ENOMEM actually > makes sense. > > If you are out of memory, it seems likely the problem is not going to > go away quickly, so the next allocation will also fail, and hopefully > the error handling will then work. So i don't think it is major > issue. So yes, go with the code above. > Hi, Andrew My new patchset is ready, but I just found out that another patch has been applied to netdev/net.git. Can I solve the problem in present way? And I will add devm_ioremap_resource_optional() helper later to optimize related drivers. How about this? Thanks.