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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A1EB5ECAAA1 for ; Tue, 6 Sep 2022 12:27:53 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4MMPmR67X4z3c18 for ; Tue, 6 Sep 2022 22:27:51 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=o0FOon/0; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=kernel.org (client-ip=139.178.84.217; helo=dfw.source.kernel.org; envelope-from=acme@kernel.org; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=o0FOon/0; dkim-atps=neutral Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4MMPll3SQKz2xHM for ; Tue, 6 Sep 2022 22:27:15 +1000 (AEST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 73F396150B; Tue, 6 Sep 2022 12:27:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACB90C433D6; Tue, 6 Sep 2022 12:27:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662467230; bh=ROd6gADFJ3cfF/PWA6JaxGXAfhNHmr3DVsYT1UhA9Uc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=o0FOon/0ilITbYQDLBQOk3fCwYw2o3M4ivW8tzeoQI3sS57RJoSaaa8y8r3/1ldxk XXpPvsamz5GA+/c3i635FuaQ69QQGU9BxL5n4HRzr4WyEWYwJ5cHmIAdAZ2x4fJ2Mk 9ObkEC/r/5dz/7ilZ0Av4kD17wVNldTPN961IrUqNjrn/0HLdv6/9x1EVHi4kggUFX lFsZFzkXwiKCMkP8LbiQBsSMfTgK/E2BuHpUc0MuGRsDAJC6RNMwobAfyzncmqtXfo ALmTE4Hdv6o9V8r1eE88CSv/qAkHoqltLiTZykr2769c9DcEgKm69G91Ihxyu4feir 4GzdJNox39+Pg== Received: by quaco.ghostprotocols.net (Postfix, from userid 1000) id 72FDB404A1; Tue, 6 Sep 2022 09:27:08 -0300 (-03) Date: Tue, 6 Sep 2022 09:27:08 -0300 From: Arnaldo Carvalho de Melo To: Athira Rajeev Subject: Re: [PATCH V2 1/2] tools/perf: Fix out of bound access to affinity "sched_cpus" Message-ID: References: <20220905141929.7171-1-atrajeev@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220905141929.7171-1-atrajeev@linux.vnet.ibm.com> X-Url: http://acmel.wordpress.com X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: maddy@linux.vnet.ibm.com, rnsastry@linux.ibm.com, linux-perf-users@vger.kernel.org, jolsa@kernel.org, kjain@linux.ibm.com, linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" Em Mon, Sep 05, 2022 at 07:49:28PM +0530, Athira Rajeev escreveu: > The affinity code in "affinity_set" function access array > named "sched_cpus". The size for this array is allocated in > affinity_setup function which is nothing but value from > get_cpu_set_size. This is used to contain the cpumask value > for each cpu. While setting bit for each cpu, it calls > "set_bit" function which access index in sched_cpus array. > If we provide a command-line option to -C which is more than > the number of CPU's present in the system, the set_bit could > access an array member which is out-of the array size. This > is because currently, there is no boundary check for the CPU. > This will result in seg fault: Thanks, tested, reproduced the problem before, and the fix after, applied. - Arnaldo > <<>> > ./perf stat -C 12323431 ls > Perf can support 2048 CPUs. Consider raising MAX_NR_CPUS > Segmentation fault (core dumped) > <<>> > > Fix this by adding boundary check for the array. > > After the fix from powerpc system: > > <<>> > ./perf stat -C 12323431 ls 1>out > Perf can support 2048 CPUs. Consider raising MAX_NR_CPUS > > Performance counter stats for 'CPU(s) 12323431': > > msec cpu-clock > context-switches > cpu-migrations > page-faults > cycles > instructions > branches > branch-misses > > 0.001192373 seconds time elapsed > <<>> > > Reported-by: Nageswara Sastry > Tested-by: Nageswara Sastry > Signed-off-by: Athira Rajeev > --- > Changelog: > From v1 -> v2: > Addressed review comment from Jiri Olsa by changing condition > check to directly use "cpu_set_size * 8" for comparing with the > cpu number. > > tools/perf/util/affinity.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/tools/perf/util/affinity.c b/tools/perf/util/affinity.c > index 4d216c0dc425..4ee96b3c755b 100644 > --- a/tools/perf/util/affinity.c > +++ b/tools/perf/util/affinity.c > @@ -49,8 +49,14 @@ void affinity__set(struct affinity *a, int cpu) > { > int cpu_set_size = get_cpu_set_size(); > > - if (cpu == -1) > + /* > + * Return: > + * - if cpu is -1 > + * - restrict out of bound access to sched_cpus > + */ > + if (cpu == -1 || ((cpu >= (cpu_set_size * 8)))) > return; > + > a->changed = true; > set_bit(cpu, a->sched_cpus); > /* > -- > 2.35.1 -- - Arnaldo