Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp13646110rwl; Wed, 4 Jan 2023 10:57:30 -0800 (PST) X-Google-Smtp-Source: AMrXdXsG5eSxcI0ue5HL5mn3LGru76gVbzgwNK1eR6eMiIwDPNjrzj18WMaFQPMzLFm/hp5KDUdl X-Received: by 2002:aa7:8292:0:b0:576:7fb9:85cc with SMTP id s18-20020aa78292000000b005767fb985ccmr44738285pfm.14.1672858650163; Wed, 04 Jan 2023 10:57:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672858650; cv=none; d=google.com; s=arc-20160816; b=qYyMFw0CWCZF67Ns9Q6/rmqbxqBxtxbHYYnFYkjpahU8uT3dquWklYb/pUFwq92bnI HZzdMdWI1NzHfN04koXHOeWK7HyzZbK5G3o5NSwS1dclSZhAoKz/LdlDZL2aXfmClR13 uf+OUskPWQSv6CkX7mAA2Abh4uOx25uiR/9SoHXIXAlgRcwgJ/pv4TgK4cVgmy6JfZC4 xjUpnUvoK28miElf03vOnYgZs0rqERpXqDOE8OhpDIzSsLcBQV4WjI4nZci3RWzCtahE cNrTn6NgVsMPnHk1AXtBUj3grj1trv6MTjmOA9XWXZ+ruaRdFRpDASQrB9yiZVVturqD XK3g== 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; bh=jncNtnTtdjcVqBVUO6HXdmIL8PwTDk+Kmt7OG5mUFFk=; b=NzJBPzMgMegAmI/uni+6aLMkLpsk6dNRiBEK6kwb8Vy9aOE4bisxiROFL51JNxOnB1 16RWGyFpJTWpQOUdpO14TCZG4gq88+hUlwAzR8269JeNcEZriYtsekfDnNfOFNR57YmY OJN/Igfr5yACuqKgH0ntvLaZeRI+RF4GrkTtNFPLyi/b8wURML5CukmABWLxksQFMEUR mB2JvyWjqoNQKUuQYBOJ1gfgkCMk98p8KjRntWSHde4LRdWfT/RQE/m23x8MEfwzB9DO Ggvjpbxty5Ek67fkM+UrqhGTRWfbaD+1LUmeOZYa/Qnd3r2SDhM91nze2lGp6BJpdwcY iPaA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b22-20020a056a000cd600b005772295cb2csi20404599pfv.271.2023.01.04.10.57.23; Wed, 04 Jan 2023 10:57:30 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239832AbjADScR (ORCPT + 56 others); Wed, 4 Jan 2023 13:32:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239814AbjADSbj (ORCPT ); Wed, 4 Jan 2023 13:31:39 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 276CADEA2; Wed, 4 Jan 2023 10:31:38 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A5FD41063; Wed, 4 Jan 2023 10:32:19 -0800 (PST) Received: from pierre123.arm.com (unknown [10.57.44.105]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 071103F663; Wed, 4 Jan 2023 10:31:33 -0800 (PST) From: Pierre Gondois To: linux-kernel@vger.kernel.org Cc: Pierre Gondois , Krzysztof Kozlowski , Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , "Rafael J. Wysocki" , Len Brown , Sudeep Holla , Greg Kroah-Hartman , Jeremy Linton , Conor Dooley , Gavin Shan , linux-arm-kernel@lists.infradead.org, linux-riscv@lists.infradead.org, linux-acpi@vger.kernel.org Subject: [PATCH v4 3/6] cacheinfo: Check 'cache-unified' property to count cache leaves Date: Wed, 4 Jan 2023 19:30:26 +0100 Message-Id: <20230104183033.755668-4-pierre.gondois@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230104183033.755668-1-pierre.gondois@arm.com> References: <20230104183033.755668-1-pierre.gondois@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 The DeviceTree Specification v0.3 specifies that the cache node '[d-|i-|]cache-size' property is required. The 'cache-unified' property is specifies whether the cache level is separate or unified. If the cache-size property is missing, no cache leaves is accounted. This can lead to a 'BUG: KASAN: slab-out-of-bounds' [1] bug. Check 'cache-unified' property and always account for at least one cache leave when parsing the device tree. [1] https://lore.kernel.org/all/0f19cb3f-d6cf-4032-66d2-dedc9d09a0e3@linaro.org/ Reported-by: Krzysztof Kozlowski Signed-off-by: Pierre Gondois --- drivers/base/cacheinfo.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/drivers/base/cacheinfo.c b/drivers/base/cacheinfo.c index 3e965ef6ee42..339cd1e3d5af 100644 --- a/drivers/base/cacheinfo.c +++ b/drivers/base/cacheinfo.c @@ -225,12 +225,9 @@ static int cache_setup_of_node(unsigned int cpu) return 0; } -int init_of_cache_level(unsigned int cpu) +static int of_count_cache_leaves(struct device_node *np) { - struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); - struct device_node *np = of_cpu_device_node_get(cpu); - struct device_node *prev = NULL; - unsigned int levels = 0, leaves = 0, level; + unsigned int leaves = 0; if (of_property_read_bool(np, "cache-size")) ++leaves; @@ -238,6 +235,28 @@ int init_of_cache_level(unsigned int cpu) ++leaves; if (of_property_read_bool(np, "d-cache-size")) ++leaves; + + if (!leaves) { + /* The '[i-|d-|]cache-size' property is required, but + * if absent, fallback on the 'cache-unified' property. + */ + if (of_property_read_bool(np, "cache-unified")) + return 1; + else + return 2; + } + + return leaves; +} + +int init_of_cache_level(unsigned int cpu) +{ + struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); + struct device_node *np = of_cpu_device_node_get(cpu); + struct device_node *prev = NULL; + unsigned int levels = 0, leaves, level; + + leaves = of_count_cache_leaves(np); if (leaves > 0) levels = 1; @@ -251,12 +270,8 @@ int init_of_cache_level(unsigned int cpu) goto err_out; if (level <= levels) goto err_out; - if (of_property_read_bool(np, "cache-size")) - ++leaves; - if (of_property_read_bool(np, "i-cache-size")) - ++leaves; - if (of_property_read_bool(np, "d-cache-size")) - ++leaves; + + leaves += of_count_cache_leaves(np); levels = level; } -- 2.25.1