Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1434483rdb; Fri, 16 Feb 2024 16:31:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW/b7l/edq0RAxkn8GUsoBgwzgwxB7klGemEk2wHBPKCeVOQlqGkrX7B6nR8Qmj1H7zDHrm0Xv6b0eDVPsmUQgZoa+uXOsX4iUnHex7Zg== X-Google-Smtp-Source: AGHT+IHnMtiFYkWLJvWHTtUUzt6Z66utH4jOT+PZZUU/5O97zhH3CN7c5FHVD6KohTbTFnDR3Trx X-Received: by 2002:a05:6a20:e608:b0:19c:8a24:82e1 with SMTP id my8-20020a056a20e60800b0019c8a2482e1mr6856316pzb.17.1708129893661; Fri, 16 Feb 2024 16:31:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708129893; cv=pass; d=google.com; s=arc-20160816; b=DHMHIkHFTn8WgnD+uEdjlLIH2hwqRU9Lcv0Naig6SJ/LHdho/GzkTZXhecc8zKsRVP O0qJEh7C/JYk3WUMoy3zbj65RZpYp9xhDvCArCTxJYnReiDYBVak8ZyPd9rxlY37iygR /nzaAEDBz4rQv12BI6IDAl8WGGJonxj+gVHO1O32qH07nrQ520pJVaObDd3h5lgMdJkB 83MnWu+1pOeWjyg1sOEijIqzX8x9Ss/G4mqLKzzNiKJnU4cevaaUYJL7letZFYFguu1p zp81JX50tYbyojgZ5hVd6G9B9JzFeWg1L8xRbtR1hGGXkSGQJz+35qTcRCp0umAwRdBj Mr1A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence; bh=gV3LRreLb1kZqKq2G0q5vBMXGxsObp5N7fG6tA3C6K8=; fh=rOW/ePQk4HwWCL8DvtGNWifx3dz4QSyLaMVOpbC+KTA=; b=ygtfzXrd1LIUAbC6l7C0/DKo6R3ZHBDd0Pq0iicFMSvxEKjb7yDAjgL9xvF8nv5z19 hKzPhtiRFOQhkkueqmR2BJnbU4h1LQu3YgnD2wI5HtOpmJdFoeqJBY/UIbXXxThjRnGf mHqKE2R5p7EN0ESAf/WiMfcjElm1wm3htB/mWY+0sWolHAV4gn1aVmejOvU8d+po9INA tXL1Pxk6gIdSCuuvu3iL/oOPeJXG+T5hSEHXVqBlhJAagF/DukNMTZdkc9O2lgXmrIoD nnm16oLlx2cDRhkfIcBFK049OJ38AmziB650l720wyuzfnG8Oh4R3LB371etMo8Onf6o geog==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-69579-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69579-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id cq18-20020a056a00331200b006e13dd0e775si690970pfb.80.2024.02.16.16.31.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 16:31:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69579-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-69579-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69579-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BF66328487E for ; Sat, 17 Feb 2024 00:29:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 04C501DFF2; Sat, 17 Feb 2024 00:25:24 +0000 (UTC) Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E43975C8B; Sat, 17 Feb 2024 00:25:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708129523; cv=none; b=LGwNf2IBYNlrC2JNDVAcvBD3zfCfQEu1o407kxAi9I6jkKWTVQCJbzSFXccur6cNibqlqb586zbGvX00d3FWXoED+XQD6mnbjZxThCanFYdlsMej7muaznQHe1SvBxx37WelKDRCusdRmq/QuE7vARY3TAayyriWNLSp5VplRTI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708129523; c=relaxed/simple; bh=TB3RsJQOiyTR8tEs8xp9ZVxkFZri+g+5FS8M54/Voa0=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=f3YRCYAahlryNjFtsBpOqpCg9bdVXQgRsqWgEpsrPoR5gMIM+8luAYOpeC2XaUPmX9Yqm6XYCHhcR/HQpy0FbnSZVqv8MtBwPdeD1NHKXQeGS0BcYRLm15co/KhgKvOZvkKgFROxspS6jnMjpWq7NdRG91oWYni4ceh8Ew2ck9E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.215.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-5cddfe0cb64so2087790a12.0; Fri, 16 Feb 2024 16:25:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708129521; x=1708734321; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gV3LRreLb1kZqKq2G0q5vBMXGxsObp5N7fG6tA3C6K8=; b=KOGv5NFOj4lVtbUwt6FmRVE099GCSoVBvp5B47tiZ8sbfP+u/GBhnuOSmjNGPhOC00 WBAzX8LC+Nii9s0Y339H0K8MURKLZS3lGLBsi2gC0mj/PaDEBYzIlo6zzwtXdml/gPIp fJO0e6BW1Vlx8AiYe5Tn7VPSmA5Gf3olK3lMYwxjQyfIhuetAZucCv1ipIdNy1X7Db16 FpsJ1czLwH0pcp0uihfMuD9y1OCjpMP/W1BMiHAWylLs7uAgoIfsAi6l/MtLG7LEAAEm OdfcczUKqUphdm4eabjE2VliQHEbexdHXQA0U729RsOP6htb/W3uRJx143kWQfXEPy2G AOrA== X-Forwarded-Encrypted: i=1; AJvYcCU6cMKPk9gOhyOg8gxRfJBiye+tRXsh7z/ZheAslzddFcrZmgccVtAkF7wa22ZNAoyK51g1fy2OZtx8ptqT9m/kXFswLnPHdEznOywrSaccehpcWXmhUfZIkI5OTlwprI5XVHaaEfy2jVFRh33YBHSD8u2YtN0ApbiC0hd+0xM+gexRuA== X-Gm-Message-State: AOJu0YzlAinW5BC3uba+kVeS2PYJ81cDBAy3FCWrPYXdtoy2lKhtMzsA S15MfAFavF2CoJfSGqtsJ1n5Ynljam8wghJHIU+1VpPHdSZZWQ8xfAySmITdgs4p9U5RB9Mv8Jq gP5ryyJNT98j6vZG0LoCaPt433Ws= X-Received: by 2002:a05:6a21:1706:b0:19c:6a60:b433 with SMTP id nv6-20020a056a21170600b0019c6a60b433mr6463168pzb.3.1708129520906; Fri, 16 Feb 2024 16:25:20 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240202234057.2085863-1-irogers@google.com> <20240202234057.2085863-3-irogers@google.com> In-Reply-To: <20240202234057.2085863-3-irogers@google.com> From: Namhyung Kim Date: Fri, 16 Feb 2024 16:25:09 -0800 Message-ID: Subject: Re: [PATCH v3 2/8] libperf cpumap: Ensure empty cpumap is NULL from alloc To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Suzuki K Poulose , Mike Leach , James Clark , Leo Yan , John Garry , Will Deacon , Thomas Gleixner , Darren Hart , Davidlohr Bueso , =?UTF-8?Q?Andr=C3=A9_Almeida?= , Kan Liang , K Prateek Nayak , Sean Christopherson , Paolo Bonzini , Kajol Jain , Athira Rajeev , Andrew Jones , Alexandre Ghiti , Atish Patra , "Steinar H. Gunderson" , Yang Jihong , Yang Li , Changbin Du , Sandipan Das , Ravi Bangoria , Paran Lee , Nick Desaulniers , Huacai Chen , Yanteng Si , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Feb 2, 2024 at 3:41=E2=80=AFPM Ian Rogers wrot= e: > > Potential corner cases could cause a cpumap to be allocated with size > 0, but an empty cpumap should be represented as NULL. Add a path in > perf_cpu_map__alloc to ensure this. > > Suggested-by: James Clark > Closes: https://lore.kernel.org/lkml/2cd09e7c-eb88-6726-6169-647dcd0a8101= @arm.com/ > Signed-off-by: Ian Rogers > --- > tools/lib/perf/cpumap.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c > index ba49552952c5..cae799ad44e1 100644 > --- a/tools/lib/perf/cpumap.c > +++ b/tools/lib/perf/cpumap.c > @@ -18,9 +18,13 @@ void perf_cpu_map__set_nr(struct perf_cpu_map *map, in= t nr_cpus) > > struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus) > { > - RC_STRUCT(perf_cpu_map) *cpus =3D malloc(sizeof(*cpus) + sizeof(s= truct perf_cpu) * nr_cpus); > + RC_STRUCT(perf_cpu_map) *cpus; > struct perf_cpu_map *result; > > + if (nr_cpus =3D=3D 0) > + return NULL; But allocation failure also returns NULL. Then callers should check what's the expected result. Thanks, Namhyung > + > + cpus =3D malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus= ); > if (ADD_RC_CHK(result, cpus)) { > cpus->nr =3D nr_cpus; > refcount_set(&cpus->refcnt, 1); > -- > 2.43.0.594.gd9cf4e227d-goog >