Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp400367imn; Thu, 28 Jul 2022 04:21:56 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sT4NHzwDZ7BXIDupWfSu2eziCIzzVrhBqvDoKzHb/8j6p2gCUTQxp3lJf5TipSAOWU/r2n X-Received: by 2002:a17:906:6a29:b0:72e:e716:d220 with SMTP id qw41-20020a1709066a2900b0072ee716d220mr20246352ejc.82.1659007316151; Thu, 28 Jul 2022 04:21:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659007316; cv=none; d=google.com; s=arc-20160816; b=nPH5rW6I1iaIrpmGtcHPj49kaJdFIJ5BR2R7zg898lNM/XtrJ1TPKhTNy0y/xQCP4L 3dpgsOiMW4EvYDKpYXg01StauLStblVX2zm/mSD1YPbWH+xl1+foQakxPR0xHDmAct7e vPnApjHZ+5TPArtwaj7X6JvWbW4rWiUk+JtR5IR7mfkcFT2KnpKrUPnErIKD6/tN0USy tsvr5kdohnPE8dZrWRRTR23DKInd6JLvHnzberhN19Oz1IlOXj8Y1Q/hiOV1Vl+ynfW/ fD+lC2vRgrfJpW0PLcJXDzkUAgeBj5O80KguB3BuQ9EJEKwxCF+7Y24VN9oAayIVlxMZ FJkw== 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=1w09WlHzsLJJ1Iix44RngaZfNGmBziRoxLwbJ26Cixk=; b=Xe+5iANxlEoMvnx+FBd8yjIU9GRIv7f2zovAGmsIYOCkbFzzR6hq81jXpmk8dBfyUS A4EeJCu2jywdmTWcNfXzA9nwikG7XDA1B+DIQli3Wviood0UXG/WQH0c7PIl4iMLNbEn LjTbEaxfFA+KYxLr9lucnGzNSk76rzp6tpRMLEB0ShVwz8cTqmb78ANsryevAGvl+Gab wkomtC+oOG9mje5IsxvCIUZV+dSd8pidBRKi+6qiNURTxvsfc2KzoaJ8iqivaq2to0l5 DahXBjGo6L6h/TJGG+tkSb+4ioHKlS4tIAKZrslCDNZSAM9126+70EA4SDerc1Os3xKs 0Ldg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baikalelectronics.ru header.s=mail header.b=kLSAmc+y; 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 ho40-20020a1709070ea800b00726b8cf8defsi477043ejc.949.2022.07.28.04.21.30; Thu, 28 Jul 2022 04:21:56 -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=kLSAmc+y; 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 S235732AbiG1LTu (ORCPT + 99 others); Thu, 28 Jul 2022 07:19:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236320AbiG1LT1 (ORCPT ); Thu, 28 Jul 2022 07:19:27 -0400 Received: from mail.baikalelectronics.com (unknown [87.245.175.230]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BD56167147; Thu, 28 Jul 2022 04:19:25 -0700 (PDT) Received: from mail (mail.baikal.int [192.168.51.25]) by mail.baikalelectronics.com (Postfix) with ESMTP id E155016D6; Thu, 28 Jul 2022 14:21:44 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.baikalelectronics.com E155016D6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baikalelectronics.ru; s=mail; t=1659007304; bh=1w09WlHzsLJJ1Iix44RngaZfNGmBziRoxLwbJ26Cixk=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=kLSAmc+yjNXSwxWpbEN0Nr1dX7jm2RcuY+N2ZzlXcO6EvuPY/HIDzcqWHWVyPiQLQ d64BvrIfZzZvR6v8TAovI4QlKH4McnGeqFfvVzZ4wPFHHUw8Ui74xV10qx/b1ss+Cj kaYbRqrAkSjQOIor0WpfnrXc1t9laQw8GW22DAN4= Received: from localhost (192.168.53.207) by mail (192.168.51.25) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Thu, 28 Jul 2022 14:19:19 +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 , , , Subject: [PATCH RESEND v6 08/23] ata: libahci_platform: Sanity check the DT child nodes number Date: Thu, 28 Jul 2022 14:18:49 +0300 Message-ID: <20220728111905.12427-9-Sergey.Semin@baikalelectronics.ru> In-Reply-To: <20220728111905.12427-1-Sergey.Semin@baikalelectronics.ru> References: <20220728111905.12427-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,T_SPF_PERMERROR 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 814804582d1d..579039956917 100644 --- a/drivers/ata/libahci_platform.c +++ b/drivers/ata/libahci_platform.c @@ -451,14 +451,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.35.1