From patchwork Fri Feb 2 23:40:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 196084 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp759225dyc; Fri, 2 Feb 2024 15:42:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IHaixNPbRPgM732MEq/6B9l8iR5HD/AnQvP0TtBZ685X/n/ebF9g/TnIF9w08BLXSEdRP5u X-Received: by 2002:a05:622a:593:b0:42b:f794:d46 with SMTP id c19-20020a05622a059300b0042bf7940d46mr6444540qtb.60.1706917327904; Fri, 02 Feb 2024 15:42:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706917327; cv=pass; d=google.com; s=arc-20160816; b=pZbDjV37+VIcZAgN7fIgja10VLlns1p3KhR7cEctdG0/VvGgl6WoFr4Q8ZBaCa7eGl nzFw/IXpgENfAqBV1QYQy6EgQgqA1u2wCVYWHxcJaFwt7NZLt1SsyJVqN3q6a9Km/y06 GLKPQsXOJIOXVFutbdwAaq4aAUDccsFiETOJD8OcbPDfoOeP7j8/3Mxgojq0o739dh8x NH3FuAxTqAdmw0uJ7/1WtsuI+1P185bYBUEKhxwTC2BXnJgpUmLIyHMAVE0v+FyurYVP knjJ1EbixMfusvQvg7kxeVYIHpq3HVC9oBT5BWTpdw6Vz84lc91aIZxpumWFOvF2tQuv 1G2A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=wOc7/ot5clrlqkd/XVVApb41eylij+dgoGLfSlWPOp0=; fh=Nlhf4X30j6heizD8GMG6198g4c7lxJN77yU2/5LPjsY=; b=ktUcMoyr8HVP5YLkDjQLZPwOWr797kiszJXfA3gi+dxx3AQq0IlVa6AjRrvQrQIvzD +1cW87DauHCtHL2GqCFlwZeVVt3FVdzTcB7DqSzGecdMF6X+wKTG43wLEeLWqNojFqR7 D0oYh0XuiEd4A1ALQB6hkhMnFCNgweLrlXzXBy6D2hR7vPqM6v0jYCtzq1OcixBAX9tG Agpfd4QoRQ+koh2Vq0iRTU+jFc4HDYnq+6jbFce0moJf2x/mycPds+rvCwQa7/ZMmqkD VhBSWP4rdTO43c+Y0gT+m1zJcwYG3w/Pilc1i21xMp4kAcxncjJq2hzl3GPGFDFthrcu Ohbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AyrvHKJj; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50720-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50720-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCUWpIWrlP2StCEaSjc0pQRI8/BzOI3E5e8VOvqqhiAMMSmxse8BH4Pt1kLDM86ZB6QuMj2g7P3Aog+ANYcaI3kicJeVcg== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id n9-20020a05622a040900b0042ab228f5dcsi3120156qtx.779.2024.02.02.15.42.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 15:42:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50720-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AyrvHKJj; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50720-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50720-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B04DD1C20CB4 for ; Fri, 2 Feb 2024 23:42:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 97DC812C7E5; Fri, 2 Feb 2024 23:41:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AyrvHKJj" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 3CA9212D779 for ; Fri, 2 Feb 2024 23:41:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706917277; cv=none; b=q8i2BwUoUuwPQdVM4q+GQygAFM+SPdcudjqE/70YYDPd/Dg5nRKaqLSwf91Xmfu/5G2rSkHYfupsIHc/0woUyx8qbUG6ViNVUaU3BW4/cGB6rz4cPgGaXsNdGxhWH+fsNzfKJw0b+UpxeEIIWsQiPFaz03bZdxlaMtBLclZPCLQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706917277; c=relaxed/simple; bh=R7HDLt+WXCKcw0UAraBcZnWD54QthkXyaQu13uOf35M=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=JH+fbjxihsSQWLtf2SaB2TxVc/f1s4Ezf/vcNnocDD+ojPf/md3AXWjAl4ThngaKwdLOyuiNDy7GvS2+P/wGvsCr8bNF4pagm48Dm7vHaJ1SPe/afZrVkiRXwCeQWE7yPrAMZfz5+ee4Pn220g3/srRwHV9pgvWxsJL9yBbtRnA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AyrvHKJj; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b9f4a513so4065541276.3 for ; Fri, 02 Feb 2024 15:41:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706917274; x=1707522074; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=wOc7/ot5clrlqkd/XVVApb41eylij+dgoGLfSlWPOp0=; b=AyrvHKJjjXcpTnTw4Q1Oh72rOi5vJrbZ3NSKSgFRcmHrdQ9urAxqzaDeeAWfqmw1CL V2OIprdiY9MJ0htwTWHjbjukLdw7GmjOkyqarwZWfqn2/8Nov6Ocp/QbJOAyaHPZqEhv hziwunHjYpud1MfPnxCijqL4At/8TCLcXP3ITG/sHjJf5dLMZBDCWVnSsVuJpt8WizXl wZDeqrqXusMSV10naS2Ovb/WYhlAqQRr2Xtbkc0txIShOu29QlZQm42MK7Ezu4CBW1H2 OxpzDQzCpQwQLEealrpbXlNFI9Pleu4GOdpHUI3AnJSzmnPwCCL/ker674WkFBkQGxwz OW2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706917274; x=1707522074; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wOc7/ot5clrlqkd/XVVApb41eylij+dgoGLfSlWPOp0=; b=s0CHHmfWSUx/vLlekIpxwyw2nm3WhU1a0InuFyvicCZnvj0QCfxNoJ9Zmz/6HTFEbS q8cBTXxMK+uigQp6fohlY7xUVvDETdQ0OBGy9ZZIOmHdrY8fYnMh4jArljW0Xb36mVgA BbT/5ck7YKsHhEo8F6FzZrjk2XL/yvvnoQt2I7EbyNjX6m7O6BjNPCLGSNP2seTB7UpF s5PPNANemFd2qD3nHld0OM47BKaR9M/XOTJjlhlwWNjJBPlyq5Unx7m8wTBVxh9vm9sh XAyMAe9Ki4vnjqN0wkcWw83QmLQJ997BSIp8qXKtqxsNt4AHAaeSVddjWfAReX/wrwK/ hKYg== X-Gm-Message-State: AOJu0Yw1veCrCusjYhRQDA8GBA2mm66YBIHSRPXT+KcAEJz5IB9+CZSx uJnxMWkqqMV6hLMQOpGCvyhzsA/79kBOxlcpbsFZ8qS+K0vXsdRssycsuSnMyWaLux4UWGOKf/S R1hQlDA== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7732:d863:503:f53d]) (user=irogers job=sendgmr) by 2002:a05:6902:1b08:b0:dc2:398d:a671 with SMTP id eh8-20020a0569021b0800b00dc2398da671mr1024554ybb.10.1706917274174; Fri, 02 Feb 2024 15:41:14 -0800 (PST) Date: Fri, 2 Feb 2024 15:40:51 -0800 In-Reply-To: <20240202234057.2085863-1-irogers@google.com> Message-Id: <20240202234057.2085863-3-irogers@google.com> 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> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Subject: [PATCH v3 2/8] libperf cpumap: Ensure empty cpumap is NULL from alloc From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789832544332552779 X-GMAIL-MSGID: 1789832544332552779 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, int nr_cpus) struct perf_cpu_map *perf_cpu_map__alloc(int nr_cpus) { - RC_STRUCT(perf_cpu_map) *cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus); + RC_STRUCT(perf_cpu_map) *cpus; struct perf_cpu_map *result; + if (nr_cpus == 0) + return NULL; + + cpus = malloc(sizeof(*cpus) + sizeof(struct perf_cpu) * nr_cpus); if (ADD_RC_CHK(result, cpus)) { cpus->nr = nr_cpus; refcount_set(&cpus->refcnt, 1);