Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp2373114rwn; Fri, 9 Sep 2022 12:39:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR6taOguRHTghnQHFsujRoUh+yekYlhIb/6PLPWn1qYcptpw4pAtUhWpFI8PYJQdZIWuoyxE X-Received: by 2002:a17:907:3e27:b0:774:3e36:f00e with SMTP id hp39-20020a1709073e2700b007743e36f00emr6998933ejc.83.1662752377461; Fri, 09 Sep 2022 12:39:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662752377; cv=none; d=google.com; s=arc-20160816; b=nF1VuRNEm+zM7nLP518FiG95Y0hodQwdZpkYJ7x8+dC3lFJ/IguFVqknWq+FCLuvU+ FAQhQa9Vy6NdXMPFsy3R2XESC3BP/VWp1pxzsTLM4YYmuD0V38+SV0RanvJcnMsPBJJS xQP+VK+KhlXNSi4rwdwgNC7JjSiq4lbfT4As47UvTn3iFlcsgxP9Hk9n1UGPxUcE6+KE AjfgOhcc+BSLvjVKkJZGb13fiLB0dt0DGUjrIjVzZ28r9zz1EhZu6T+9IxMn+GGY1Tny Pn2welTqatRrslaOb6SOgYPoDEqpgVoVaRFUhbNHI5TzJXo5PJZoj0J8MhiTa8A6TLlv f27A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=MxJlAieoI0TgAMq3MnhprrteEcvbjMxPRTOKIFXhakY=; b=jBKUABWKlwQZG0ERtoJ70ZqnWwzYZ2L6owLtx7JlBQ/9dMwgZ/IGjr7vwJCbASPy88 tXKR2bCXayYwXkD9CRvkLogVO66daP/Rpow2RO/Br3TfvXwbHX02h9U8Fvv85M1jVHjG ir1tcgDuLchVLVqWCMLJivZ4BSzEq6puQFnk+QqINRX9sSLZmQYExyuYJjc02TD0Xupn kAuW7cstFO83sFCxoG59c9HN17v1YqTbKE83itPz8QfUxMgdGm1nMbSPnLD1rIIHD9Xt vOpMDEUnsDmvOpc/5lqE7YpzHgPc12X/aBKhvWd6B3ROYlwyYlC78du7Wif1CweUat+7 IPwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baikalelectronics.ru header.s=mail header.b=YhISV66I; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=baikalelectronics.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g8-20020a056402428800b00451489ff6a6si24581edc.203.2022.09.09.12.39.11; Fri, 09 Sep 2022 12:39:37 -0700 (PDT) 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; dkim=pass header.i=@baikalelectronics.ru header.s=mail header.b=YhISV66I; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=baikalelectronics.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231759AbiIIThy (ORCPT + 99 others); Fri, 9 Sep 2022 15:37:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231484AbiIIThF (ORCPT ); Fri, 9 Sep 2022 15:37:05 -0400 Received: from mail.baikalelectronics.com (mail.baikalelectronics.com [87.245.175.230]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E4BC8112130; Fri, 9 Sep 2022 12:36:39 -0700 (PDT) Received: from mail (mail.baikal.int [192.168.51.25]) by mail.baikalelectronics.com (Postfix) with ESMTP id 0FC8ADBA; Fri, 9 Sep 2022 22:40:24 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.baikalelectronics.com 0FC8ADBA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baikalelectronics.ru; s=mail; t=1662752424; bh=MxJlAieoI0TgAMq3MnhprrteEcvbjMxPRTOKIFXhakY=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=YhISV66IKakezX9NkxCY8cApL5mYwmnlSg+h2QstlTvJow+OlTfgQrOeIQycNK5h0 5ZLJQZVqTLRy73Gef4BsZa2f7kXpYs5D7/IvGjhD6f5gyWjQgRK6DGaliJvLjWzyZo YJPfU6X3uWiA3s+0Hot5D6C3Jdng3GPOPhWcejos= Received: from localhost (192.168.168.10) by mail (192.168.51.25) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Fri, 9 Sep 2022 22:36:35 +0300 From: Serge Semin To: Damien Le Moal , Hans de Goede , Jens Axboe , Hannes Reinecke CC: Serge Semin , Serge Semin , Alexey Malahov , Pavel Parkhomenko , Rob Herring , Krzysztof Kozlowski , , , Subject: [PATCH v8 08/23] ata: libahci_platform: Sanity check the DT child nodes number Date: Fri, 9 Sep 2022 22:36:06 +0300 Message-ID: <20220909193621.17380-9-Sergey.Semin@baikalelectronics.ru> In-Reply-To: <20220909193621.17380-1-Sergey.Semin@baikalelectronics.ru> References: <20220909193621.17380-1-Sergey.Semin@baikalelectronics.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: MAIL.baikal.int (192.168.51.25) To mail (192.168.51.25) X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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 Having greater than AHCI_MAX_PORTS (32) ports detected isn't that critical from the further AHCI-platform initialization point of view since exceeding the ports upper limit will cause allocating more resources than will be used afterwards. But detecting too many child DT-nodes doesn't seem right since it's very unlikely to have it on an ordinary platform. In accordance with the AHCI specification there can't be more than 32 ports implemented at least due to having the CAP.NP field of 5 bits wide and the PI register of dword size. Thus if such situation is found the DTB must have been corrupted and the data read from it shouldn't be reliable. Let's consider that as an erroneous situation and halt further resources allocation. Note it's logically more correct to have the nports set only after the initialization value is checked for being sane. So while at it let's make sure nports is assigned with a correct value. Signed-off-by: Serge Semin Reviewed-by: Hannes Reinecke --- Changelog v2: - Drop the else word from the child_nodes value checking if-else-if statement (@Damien) and convert the after-else part into the ternary operator-based statement. Changelog v4: - Fix some logical mistakes in the patch log. (@Sergei Shtylyov) - Replace "?:" operator with if-else statement. (@Damien) --- drivers/ata/libahci_platform.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/ata/libahci_platform.c b/drivers/ata/libahci_platform.c index 7366eb0adf41..bacb974c1b16 100644 --- a/drivers/ata/libahci_platform.c +++ b/drivers/ata/libahci_platform.c @@ -450,14 +450,24 @@ struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev, } } - hpriv->nports = child_nodes = of_get_child_count(dev->of_node); + /* + * Too many sub-nodes most likely means having something wrong with + * the firmware. + */ + child_nodes = of_get_child_count(dev->of_node); + if (child_nodes > AHCI_MAX_PORTS) { + rc = -EINVAL; + goto err_out; + } /* * If no sub-node was found, we still need to set nports to * one in order to be able to use the * ahci_platform_[en|dis]able_[phys|regulators] functions. */ - if (!child_nodes) + if (child_nodes) + hpriv->nports = child_nodes; + else hpriv->nports = 1; hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL); -- 2.37.2