Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp749861pxb; Fri, 22 Apr 2022 10:20:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx16ZDSQ7+DFrhtoNTYskFCl3MC2VhwxQSZLF/3P+OYj2NhRpPERBEBQ9vF66mc0aN/QmSO X-Received: by 2002:a17:902:7610:b0:158:89a5:fc5a with SMTP id k16-20020a170902761000b0015889a5fc5amr5574175pll.116.1650648035315; Fri, 22 Apr 2022 10:20:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650648035; cv=none; d=google.com; s=arc-20160816; b=sslrm9naNrP+e2UzSDm6jB1olGnKRGdgDfGLstOl8FUoGy1ezvi5/D3gT1ou54XDB1 fgtUGjBO/TunVpzMO01+PjznDVXGM5fED34VlevoM4kO32vSPvlKGu+R97jbuWhG+JRb B4tzOn9HHEk4vw7/PGQkD0U8EVOKi5QvIMzVpW4P4eqS8CpprAcoV0z7DGenGGjaWAzs e/BKnMw+y0n5T+Zj0Z111cZrcMMl4F9HXfbLFLIZrLbTM+H+nrH1wiwJ3QWOUnJHLKaa LlcarHOq39CpjZRgheoKh63L/KDA5uJSUSkmiC7qNDKCcSj6pd7+4bXrZewB2mtM8L8r H0mA== 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 :message-id:to:from:cc:in-reply-to:subject:date:dkim-signature; bh=1s4gUhN00hvXjHQF/+x0N4gKH6WVZc/YWM+Yr6Op0YU=; b=XC8mFJQVwYD6o3pHn6y6sysWDmauVI5UaqFG3MtRHraUlsldATulARbz3hcw19BVWT ihbRBNsCo/N3NigeqU8DNqNEHqZORCC8RKdDLrfmXwZd405jgf+DHrdqGcGiyyFZOy2j g1nwN9LR06IIG/oo17VsTp/3/PbaS0NkeZ2Vrvm60rTnOxD8SeNldZ0fIxrtDFThrC4x fVCpHDBg20bMUQXnB9iMc8/3fQFu75V3pNoGsKOF6IMFcc1NquF8ypDXbeAHL5guFoS2 uiDcoozT2zaDqCZbmKCxXRWyQ/6cGhfR0Je3iDyryqE9Ifxt9cMX4SzrnAcoZAiJlr7w Ho8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=5JyuAuOT; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id h15-20020a170902f70f00b00153b2d16593si8617404plo.411.2022.04.22.10.20.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 10:20:35 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=5JyuAuOT; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CC65185944; Fri, 22 Apr 2022 10:15:54 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354778AbiDTWlC (ORCPT + 99 others); Wed, 20 Apr 2022 18:41:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233746AbiDTWlA (ORCPT ); Wed, 20 Apr 2022 18:41:00 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37FF2BE35 for ; Wed, 20 Apr 2022 15:38:13 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id e62-20020a17090a6fc400b001d2cd8e9b0aso3396850pjk.5 for ; Wed, 20 Apr 2022 15:38:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=date:subject:in-reply-to:cc:from:to:message-id:mime-version :content-transfer-encoding; bh=1s4gUhN00hvXjHQF/+x0N4gKH6WVZc/YWM+Yr6Op0YU=; b=5JyuAuOTwJTohS87I/3Ve6D+H+T+O15/pe3TEl7x+PM1CTgjSUT+VHl9QLbTz0UTzm bBfxrM3EKNJ0RHy16fHhCIkbZBdlEYQWS2vNRsIXmMAPIX1Z+qRdpwc0T5gx4YdmKPeO ZbZZ9uGY2P3cFXqY9YQ/cYQ9RPUD+8+69VH5IRZndVPhYrI7D+UzoqFqK3ykS+cV37nC xG+J+U1m4g0uZMZrpBQBR1AuYACU2My0O0pN/iF8HjnUOylf3ymuKiAThdvcQ3xqplTu wQ1VUKC4Dpvb5s1voLQPVwNjCdVKtmb8c85k7k2Lr8fwnm9URqgfykxQHZIg1WkymCcB Bt3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:subject:in-reply-to:cc:from:to:message-id :mime-version:content-transfer-encoding; bh=1s4gUhN00hvXjHQF/+x0N4gKH6WVZc/YWM+Yr6Op0YU=; b=oxoyrNqMVnZiBekXfjFWfsQ9Y3cx+8ZlUjxtxLwV5xNGPtj43tWIou7u4VwvTfBx7k ihmlET9k19DI+K8HjE2quhigyxvBs3As9cQ37Tx+0AM4tdNeZ6+xKPtjwH5Ss80cb9mO UuyQIcthJPqoG+zh3uyzrdC+rxTztG4Ult+hXXdkhLc3+BjIX9YlrTq4wMLaoTmUavKo WwWxzEOtAha5c7iURBguYollUtfEPqpX3UI7vgtOHNrNOAkGdULiHYFB8Ew+wUc4EyWd 5UCPGPWmcOUT9TFqavS4vly7flZ4CvBPXLf8uTLP/zU3rVglf+4fmfyvWjFnQHdhHzEV RpcQ== X-Gm-Message-State: AOAM533wLvugndBfreT6+ECixm2Y3qPUX0iE0la2Cvuvpq8nh0sITGJx e7Fj+EhNb6A2+S/K7TY72O86Zw== X-Received: by 2002:a17:90b:4a8a:b0:1d2:bff4:b44f with SMTP id lp10-20020a17090b4a8a00b001d2bff4b44fmr6872379pjb.118.1650494292709; Wed, 20 Apr 2022 15:38:12 -0700 (PDT) Received: from localhost ([12.3.194.138]) by smtp.gmail.com with ESMTPSA id x69-20020a628648000000b0050ad2fe3b18sm589106pfd.60.2022.04.20.15.38.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 15:38:12 -0700 (PDT) Date: Wed, 20 Apr 2022 15:38:12 -0700 (PDT) X-Google-Original-Date: Wed, 20 Apr 2022 15:38:10 PDT (-0700) Subject: Re: drivers/perf/riscv_pmu_sbi.c:464 pmu_sbi_get_ctrinfo() warn: potentially one past the end of array 'pmu_ctr_list[i]' In-Reply-To: <202204161940.BrRZvzdD-lkp@intel.com> CC: kbuild@lists.01.org, lkp@intel.com, kbuild-all@lists.01.org, linux-kernel@vger.kernel.org From: Palmer Dabbelt To: dan.carpenter@oracle.com, Atish Patra Message-ID: Mime-Version: 1.0 (MHng) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE autolearn=no 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 On Wed, 20 Apr 2022 02:31:33 PDT (-0700), dan.carpenter@oracle.com wrote: > tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master > head: 59250f8a7f3a60a2661b84cbafc1e0eb5d05ec9b > commit: e9991434596f5373dfd75857b445eb92a9253c56 RISC-V: Add perf platform driver based on SBI PMU extension > config: riscv-randconfig-m031-20220416 (https://download.01.org/0day-ci/archive/20220416/202204161940.BrRZvzdD-lkp@intel.com/config) > compiler: riscv32-linux-gcc (GCC) 11.2.0 > > If you fix the issue, kindly add following tag as appropriate > Reported-by: kernel test robot > Reported-by: Dan Carpenter > > smatch warnings: > drivers/perf/riscv_pmu_sbi.c:464 pmu_sbi_get_ctrinfo() warn: potentially one past the end of array 'pmu_ctr_list[i]' > drivers/perf/riscv_pmu_sbi.c:464 pmu_sbi_get_ctrinfo() warn: potentially one past the end of array 'pmu_ctr_list[i]' > > vim +464 drivers/perf/riscv_pmu_sbi.c > > e9991434596f53 Atish Patra 2022-02-18 444 static int pmu_sbi_get_ctrinfo(int nctr) > e9991434596f53 Atish Patra 2022-02-18 445 { > e9991434596f53 Atish Patra 2022-02-18 446 struct sbiret ret; > e9991434596f53 Atish Patra 2022-02-18 447 int i, num_hw_ctr = 0, num_fw_ctr = 0; > e9991434596f53 Atish Patra 2022-02-18 448 union sbi_pmu_ctr_info cinfo; > e9991434596f53 Atish Patra 2022-02-18 449 > e9991434596f53 Atish Patra 2022-02-18 450 pmu_ctr_list = kcalloc(nctr, sizeof(*pmu_ctr_list), GFP_KERNEL); > ^^^^ > > e9991434596f53 Atish Patra 2022-02-18 451 if (!pmu_ctr_list) > e9991434596f53 Atish Patra 2022-02-18 452 return -ENOMEM; > e9991434596f53 Atish Patra 2022-02-18 453 > e9991434596f53 Atish Patra 2022-02-18 454 for (i = 0; i <= nctr; i++) { > ^^^^^^^^^ > The <= should be < > > e9991434596f53 Atish Patra 2022-02-18 455 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0); > e9991434596f53 Atish Patra 2022-02-18 456 if (ret.error) > e9991434596f53 Atish Patra 2022-02-18 457 /* The logical counter ids are not expected to be contiguous */ > e9991434596f53 Atish Patra 2022-02-18 458 continue; > e9991434596f53 Atish Patra 2022-02-18 459 cinfo.value = ret.value; > e9991434596f53 Atish Patra 2022-02-18 460 if (cinfo.type == SBI_PMU_CTR_TYPE_FW) > e9991434596f53 Atish Patra 2022-02-18 461 num_fw_ctr++; > e9991434596f53 Atish Patra 2022-02-18 462 else > e9991434596f53 Atish Patra 2022-02-18 463 num_hw_ctr++; > e9991434596f53 Atish Patra 2022-02-18 @464 pmu_ctr_list[i].value = cinfo.value; > ^^^^^^^^^^^^^^^ > Off by one > > e9991434596f53 Atish Patra 2022-02-18 465 } > e9991434596f53 Atish Patra 2022-02-18 466 > e9991434596f53 Atish Patra 2022-02-18 467 pr_info("%d firmware and %d hardware counters\n", num_fw_ctr, num_hw_ctr); > e9991434596f53 Atish Patra 2022-02-18 468 > e9991434596f53 Atish Patra 2022-02-18 469 return 0; > e9991434596f53 Atish Patra 2022-02-18 470 } I think this should do it diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index a1317a483512..50394ef1adef 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -457,7 +457,7 @@ static int pmu_sbi_get_ctrinfo(int nctr) if (!pmu_ctr_list) return -ENOMEM; - for (i = 0; i <= nctr; i++) { + for (i = 0; i < nctr; i++) { ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0); if (ret.error) /* The logical counter ids are not expected to be contiguous */ but I'm not super familiar with the perf code and there's frequently this pattern of "0 is reserved as a special value" in the RISC-V specs (interrupt numbers, for example) so I may be wrong here. IIUC none of that is going on here, as these are all indirect/non-contiguous, but I'll let Atish take a look. Thanks!