From nobody Wed Oct 8 20:02:01 2025 Received: from mail-oa1-f73.google.com (mail-oa1-f73.google.com [209.85.160.73]) (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 52E6B246BD5 for ; Tue, 24 Jun 2025 19:03:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791814; cv=none; b=EJGHWdsu1KdJ1d35odnPywa+tYp8cmtw1yGlt3RslBBJoUcgTt6gfbYSzk+z8LXV92IZmCI1JGoZnnWT6HG9sDyxPrWRTbzvggk/leWhyWpjXqR6AfY8G57HZrtadVR1tSoqpjr5aFEGEvt8dYh6NNRxK8Dqz1s8IiKgUPLm3p8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791814; c=relaxed/simple; bh=/d23ywHfcUan7UtbzKVQ/S1GTCmQbW3G9hmSJrAOtco=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=DZkNrxlQgcI5ebfJxDLWIIEINX4nf2uooqLedutaPK6cplTv+CIXLmhxXaZyDsBI/zm7qfcNF3VK8dp1JSvt8H7QN6zDoc1hLM/l3/i30b0sAhsYYLX9IDBZbCRAErZmAd5cI96WXhfn/OtABVxuiqDsCUe+OwCBXl2fEU2dWQE= 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=gbiCcc8P; arc=none smtp.client-ip=209.85.160.73 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gbiCcc8P" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-2e8e969090aso718102fac.0 for ; Tue, 24 Jun 2025 12:03:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750791812; x=1751396612; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=q2qPXmUwMcTn/QFsidbTHkUm8odPwzO3HSooJaMxC2I=; b=gbiCcc8PiENj8syFi+4YhegHzBmCkyua9uTvYEbmCoj+vNsComc8r6TUlMvK3UMe/u r00EOEokerq/hhptF0lEZ1tcpAKiIbodMzAWnSf1CmgGmK13ktKLMl68xVYkG92tBZQf h8ACe8tqQG6F8bm130S6r5/W+YBC9u024swJ72o2z8ur64Hj2jwwDP4iSDWc6EHFNavo iK+gOQBofG+yZkXTj5eNt+L52OPU0ZoV7bpDlFydqHiTMctP8pCFbDO3as1MacClHXhS EXV5GuxY2ePnaMmLAII7X8Ryf7GRDqtCMGoj1NWWLXZF2rGruR2DSq8imoRbL7U7O3VN VVxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750791812; x=1751396612; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=q2qPXmUwMcTn/QFsidbTHkUm8odPwzO3HSooJaMxC2I=; b=JdwOsopygPy1aUz1eJ9pF63f2g4Saa3zF19D7RulNRFcPPBXFx6gW7AuQX7hVBXYFy 3kATRDlfHTv0QaySiE9At7w1OXwKwEoaS6P7nKdDmIw3AFSPEl69I52crixzmq/AA0gZ Qkgte86+J1rmNi4eGULIJuELpvFPhm8r7CdIITRjJF+Tye0g8XLI9ULdtedEv74LlkGh bzXY31aWVYMklvqcOlKKK0JfyXRc1SHvTa7Pw+4MEcay8mUbCp+xEEPWPoOVqIkm3V8X C/+GT56uVQ0ZC1iy30kgn96U+v/a0jDxuuIMBSxc1xieqCzX80IkXJj0YzrnK9jIgHNG Ac3g== X-Forwarded-Encrypted: i=1; AJvYcCW2gimITNLXYNdPURNSheLErhMtpEIhWLopZD42wvzWOuACa3AmJu9+poayKRQJlQeS0IuuIDm0LaogtiE=@vger.kernel.org X-Gm-Message-State: AOJu0YwbWsYsGhbBSwSZA7FyZVd/q4MVDFkcr7gMLESfjeeL6DOYhHTa ctWJsrEONyQO9lCbDzl+fSYF1Kcxy7KTzsuFjn73V2UarBPZ4+xXpmooEIEeZgHpbgNrY9jsbRI gk70LPYnRkw== X-Google-Smtp-Source: AGHT+IHutS72xAleEuv7XRMMzqYDSDcNU9hXAz/588k8XeDd+ZaGQViK5p9pUQLMVyc5wpzkiwAmNl3xdRYZ X-Received: from oabwc17.prod.google.com ([2002:a05:6871:a511:b0:2e9:54dd:50d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:71d6:b0:2d6:49df:a649 with SMTP id 586e51a60fabf-2efb28b6dcdmr343091fac.31.1750791812310; Tue, 24 Jun 2025 12:03:32 -0700 (PDT) Date: Tue, 24 Jun 2025 12:03:21 -0700 In-Reply-To: <20250624190326.2038704-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250624190326.2038704-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.714.g196bf9f422-goog Message-ID: <20250624190326.2038704-2-irogers@google.com> Subject: [PATCH v1 1/5] perf dso: Add missed dso__put to dso__load_kcore From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Howard Chu , Charlie Jenkins , Thomas Richter , Athira Rajeev , Stephen Brennan , Jean-Philippe Romain , Junhao He , "Dr. David Alan Gilbert" , Dmitry Vyukov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The kcore loading creates a set of list nodes that have reference counted references to maps of the kcore. The list node freeing in the success path wasn't releasing the maps, add the missing puts. It is unclear why this leak was being missed by leak sanitizer. Fixes: 83720209961f ("perf map: Move map list node into symbol") Signed-off-by: Ian Rogers --- tools/perf/util/symbol.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 8b30c6f16a9e..fd4583718eab 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1422,6 +1422,7 @@ static int dso__load_kcore(struct dso *dso, struct ma= p *map, goto out_err; } } + map__zput(new_node->map); free(new_node); } =20 --=20 2.50.0.714.g196bf9f422-goog From nobody Wed Oct 8 20:02:01 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 F32F82E7640 for ; Tue, 24 Jun 2025 19:03:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791816; cv=none; b=VNNQmjAi4GZgeL3wRCSfCOC8wHlB45JKRgX8WImuzZBEmR7QZh5oChs8uQTr+0EOoaJuNISpfWAESo7vEp73WfezUeo59WP6GEKYFzQPG+sI5/pkD6Tksji5TVAyqpDPSRCDvUXJCGpyeJRQBMFXi5dm4iGzR/uAwbPERAJzZ3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791816; c=relaxed/simple; bh=+VqdNyAU7xmLjbwR33lOaXTxN2lkq0tJ1SdzqhwGtgc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=Oz6BnIgVXgINozX7IuDLA7VgNw4eWY/RJA+bZ+aOnfOzAqe5pPWXAx/gmW9+S5tVjcKWnUrY7e1sghD3BEIh3mKC9JmBf02ToOENKE4IRis00lc9MaacsV6nOW9LAlkYXF2h4Q1ka3r9NgpO91VGZ0tQabI37ef7k/8FRgK7bks= 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=L2A7sj02; arc=none smtp.client-ip=209.85.210.201 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="L2A7sj02" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-740270e168aso415685b3a.1 for ; Tue, 24 Jun 2025 12:03:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750791814; x=1751396614; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=1Lm/IbEx6R0SSnBj56mL+ciOEH3EHGJ1ssPuhOnqIVE=; b=L2A7sj02fx9yKQXZCifUELgMtMoxvAyPn1AWDioNJt5NN9jiQfCem4FZw6etqHcnKl gGCfw5r3LdLvIhrEFp9ZeYarEpEe8+S9YAEP60dRnmTi0J0zB+GIfNTqe4tCrwqqNXv2 hPAE1q5SwLAiL0Ofsf4YWnl7WJ9HOXuaXybu7YiY5JMcRBraeujcUX2rZgcGmmHdRabu MXtWqvcqywpYSXoPec6VW9/qkbOzeN577H4he7l55sf5Qy8FNWL6+Perowqm7pwHki59 cmJNfwT7IJe5L3Ni9F/uYjY1YvpydXnzNjRz52hZ+/LhIxoovjv8YDQaQV2eKnhDZrfw omXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750791814; x=1751396614; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1Lm/IbEx6R0SSnBj56mL+ciOEH3EHGJ1ssPuhOnqIVE=; b=mBTp50x1K9VYTMd1YKKkc53NM1w6Vgrb1ACbbwGYh+B5EAcAU50Z/pH/FGxyJK8U87 CnMvCOpBNLc5n57AqGskcQ94h9aNv5xeKUTQCESXSzjexSy20ze9Ev6scJAQ00f8oYFX WB8UKxBA+g5y2KVtJr+fbo+yix4fQQHMqhn019FR95DPcgbteAuVTOZx5zf6boEc5egm 5Ux1rdPbidxe2G/LE3ZPMFpXvkNK/YkrdKW1hz4XDV6XmLBy/v+rwbz1Lys6x2bTCyol om38mPOzGwafOhXySfnXkyIGEUm3MHxtkIJmZpDO5mW1N1nV0QWpmfGY6apNIlMSgobC Rmyg== X-Forwarded-Encrypted: i=1; AJvYcCUjOoLm/WhLqs1j0fxRhUYoU45Y2Cgsm5WMyRgroA2zfdy57Hk1171DpIouHGIExhVQYXl0yD+FUWExIIY=@vger.kernel.org X-Gm-Message-State: AOJu0YwXcMJK88CX68LYUQ9gUzAclz9WAhyOHBklPljo985EZT7+spWL fc40bzgfmKQ5OTFv08OBXU/HpQWgdZx7aqsMcQQPYiZ6Km8ZvWu91lLerFJ//lFQ7UD6p86iC4g cL3FAD7/cXg== X-Google-Smtp-Source: AGHT+IGJ9yEUYLjTSRdiHE/QX5GvHiGdASKkqLhg/pDe3gFNZXNMK0+DtFMCh4mraC9S1u01oH1oQTEht+uf X-Received: from pfhx37.prod.google.com ([2002:a05:6a00:18a5:b0:746:223d:ebdc]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1146:b0:748:3964:6177 with SMTP id d2e1a72fcca58-74ad44f267dmr327221b3a.19.1750791814219; Tue, 24 Jun 2025 12:03:34 -0700 (PDT) Date: Tue, 24 Jun 2025 12:03:22 -0700 In-Reply-To: <20250624190326.2038704-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250624190326.2038704-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.714.g196bf9f422-goog Message-ID: <20250624190326.2038704-3-irogers@google.com> Subject: [PATCH v1 2/5] perf test code-reading: Avoid a leak of cpus and threads From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Howard Chu , Charlie Jenkins , Thomas Richter , Athira Rajeev , Stephen Brennan , Jean-Philippe Romain , Junhao He , "Dr. David Alan Gilbert" , Dmitry Vyukov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The perf_evlist__set_maps does the necessary gets on the arguments passed, so the reference count bumping isn't necessary and creates a memory leak. Signed-off-by: Ian Rogers --- tools/perf/tests/code-reading.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-readin= g.c index cf6edbe697b2..6efb6b4bbcce 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -749,13 +749,6 @@ static int do_test_code_reading(bool try_kcore) pr_debug("perf_evlist__open() failed!\n%s\n", errbuf); } =20 - /* - * Both cpus and threads are now owned by evlist - * and will be freed by following perf_evlist__set_maps - * call. Getting reference to keep them alive. - */ - perf_cpu_map__get(cpus); - perf_thread_map__get(threads); perf_evlist__set_maps(&evlist->core, NULL, NULL); evlist__delete(evlist); evlist =3D NULL; --=20 2.50.0.714.g196bf9f422-goog From nobody Wed Oct 8 20:02:01 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 5C7702E9ECC for ; Tue, 24 Jun 2025 19:03:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791819; cv=none; b=gbfr3kSVtj+3B8MtzoF5iQESfPutALEHgloG5O4GQgM/RwnvlTbW86QWEUPFgxGyY4Nzr1jyQKOBl5IlbGqef0/GFngUH+2LKDzHRo6RWekYsQQ6LpfUYdHFKO36jMQI/Hty8Wb0/E51WewKcfCXAp5SbjZ/ifA6bFMj0TACpGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791819; c=relaxed/simple; bh=ktXu6VcAdkGJ2v1xOhOgufhv3qxhgg3zbylT0vutCvY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=X8Y9syza4HPkVfjeeyUTx2QlJQdxoCs5afr+BDzLvUgjSK47UehWi+ufZejSintmFWH3flJCnbbOon71U9wQ6d728kDG9Q4VWu8P4PTUNyf/N/oUhl7V5qsx4efUEeumrzNJcFLtX9+RX6vTXyjMApSj8tEmzjwUIrgFQMVQuXE= 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=xKmvhRRa; arc=none smtp.client-ip=209.85.210.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xKmvhRRa" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-74928291bc3so439326b3a.0 for ; Tue, 24 Jun 2025 12:03:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750791816; x=1751396616; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=afW80ZcEEJPE1ltr0EmfHWGXUQF1oWc8ptVOF2S/Uts=; b=xKmvhRRa66vrJMPgD8+9E/ld2Ij4Inbzc4+fHidIpoHpwaJS7k7AgorQvIsBSC2X6x PVADmIyQpHz8oAaPPF1keB3fF38j1c6b0gsKaMm5DS35TTHr8syuhzISGZnSQk90r2Js esh6bPYZK1krSzNizhYi7Z+IN9KpX1DERReG1oiYEUq5pmo+fLW5BusFf76q8LhDqyZX WilrGyumzDLExLzUKXp2Zv2cZ8RvOQ3z58ad5V6miAbEsKMLKrWxqdzlTLS29KC0Iah8 lL5IHNnPBB9zgsz10zhf6Bwib47RfdhrEY2lJPurEp2+xT13wjuW5OMDlZN90XLV3qT5 Z7tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750791816; x=1751396616; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=afW80ZcEEJPE1ltr0EmfHWGXUQF1oWc8ptVOF2S/Uts=; b=CtbgtlZZgV7dfHhMYJ4j273iIYNKL5Lm5Ljh4Kvcb5lbmXzLSdF771gsu+xRfqV7pW 38dG2SoW6DqdaOsvcttVxT3sawnJ/QnLnUou+Xv2tgVtOwypfTMXb/vVqyMywVQ5E8w4 PJMhPgtIaZbm/JwpLOYaY6PxYj6TXRoh6cSy8zc6jNYkQ9bXbtQ88+T38iFto1Rwx5JY cYlopCZKE1LbjERMdpD65laCIm2R1RsCEy2B9wyok7NNtYR04VTFNIsRgCUflgGV66k3 YseDR+nYwPBi1Va0v/JWxUst3Has738O+GBtSVNp/d1No9Blp3MjINq0Vn/6eLER+gG7 d1ZA== X-Forwarded-Encrypted: i=1; AJvYcCXuARAjaT4wN6FG5I/Ldzh6nIZYTaePiiNwVe5sORoCXVMM6luHDId4ew/UP8lj//GPIVEwmT06S/dRiFk=@vger.kernel.org X-Gm-Message-State: AOJu0Yzp0tfX2sVzibRSJqac9AjJ/JaZwTXgBvP7ajnZarDq+Y28sI4g 4eQ4TkXq8KjW1D2ewwSLZkltX+tiA1FUZbboCmn2RpgXhN3CzC5aak7fFOrOo2lMgBjBHVxbC/q NGAUZrltOvA== X-Google-Smtp-Source: AGHT+IGO41J2m+KRVvAkZqtaf/fDEU02WT3KsOZvnS1GIfQ/BtppdtfGOoQgWLRf6EMpLFJFzoEGCm7POlSL X-Received: from pfbha5.prod.google.com ([2002:a05:6a00:8505:b0:748:df52:fdc5]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:391a:b0:737:9b:582a with SMTP id d2e1a72fcca58-74ad4546a08mr383375b3a.24.1750791816273; Tue, 24 Jun 2025 12:03:36 -0700 (PDT) Date: Tue, 24 Jun 2025 12:03:23 -0700 In-Reply-To: <20250624190326.2038704-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250624190326.2038704-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.714.g196bf9f422-goog Message-ID: <20250624190326.2038704-4-irogers@google.com> Subject: [PATCH v1 3/5] perf hwmon_pmu: Hold path rather than fd From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Howard Chu , Charlie Jenkins , Thomas Richter , Athira Rajeev , Stephen Brennan , Jean-Philippe Romain , Junhao He , "Dr. David Alan Gilbert" , Dmitry Vyukov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hold the path to the hwmon_pmu rather than the file descriptor. The file descriptor is somewhat problematic in that it reflects the directory state when opened, something that may vary in testing. Using a path simplifies testing and to some extent cleanup as the hwmon_pmu is owned by the pmus list and intentionally global and leaked when perf terminates, the file descriptor being left open looks like a leak. Signed-off-by: Ian Rogers --- tools/perf/tests/hwmon_pmu.c | 11 ++++++----- tools/perf/util/hwmon_pmu.c | 38 ++++++++++++++++++++++++++---------- tools/perf/util/hwmon_pmu.h | 4 ++-- tools/perf/util/pmus.c | 2 +- tools/perf/util/pmus.h | 2 +- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/tools/perf/tests/hwmon_pmu.c b/tools/perf/tests/hwmon_pmu.c index 0837aca1cdfa..151f02701c8c 100644 --- a/tools/perf/tests/hwmon_pmu.c +++ b/tools/perf/tests/hwmon_pmu.c @@ -93,9 +93,10 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t s= z) pr_err("Failed to mkdir hwmon directory\n"); goto err_out; } - hwmon_dirfd =3D openat(test_dirfd, "hwmon1234", O_DIRECTORY); + strncat(dir, "/hwmon1234", sz - strlen(dir)); + hwmon_dirfd =3D open(dir, O_PATH|O_DIRECTORY); if (hwmon_dirfd < 0) { - pr_err("Failed to open test hwmon directory \"%s/hwmon1234\"\n", dir); + pr_err("Failed to open test hwmon directory \"%s\"\n", dir); goto err_out; } file =3D openat(hwmon_dirfd, "name", O_WRONLY | O_CREAT, 0600); @@ -130,18 +131,18 @@ static struct perf_pmu *test_pmu_get(char *dir, size_= t sz) } =20 /* Make the PMU reading the files created above. */ - hwm =3D perf_pmus__add_test_hwmon_pmu(hwmon_dirfd, "hwmon1234", test_hwmo= n_name); + hwm =3D perf_pmus__add_test_hwmon_pmu(dir, "hwmon1234", test_hwmon_name); if (!hwm) pr_err("Test hwmon creation failed\n"); =20 err_out: if (!hwm) { test_pmu_put(dir, hwm); - if (hwmon_dirfd >=3D 0) - close(hwmon_dirfd); } if (test_dirfd >=3D 0) close(test_dirfd); + if (hwmon_dirfd >=3D 0) + close(hwmon_dirfd); return hwm; } =20 diff --git a/tools/perf/util/hwmon_pmu.c b/tools/perf/util/hwmon_pmu.c index c25e7296f1c1..7edda010ba27 100644 --- a/tools/perf/util/hwmon_pmu.c +++ b/tools/perf/util/hwmon_pmu.c @@ -104,7 +104,7 @@ static const char *const hwmon_units[HWMON_TYPE_MAX] = =3D { struct hwmon_pmu { struct perf_pmu pmu; struct hashmap events; - int hwmon_dir_fd; + char *hwmon_dir; }; =20 /** @@ -245,7 +245,7 @@ static int hwmon_pmu__read_events(struct hwmon_pmu *pmu) return 0; =20 /* Use openat so that the directory contents are refreshed. */ - io_dir__init(&dir, openat(pmu->hwmon_dir_fd, ".", O_CLOEXEC | O_DIRECTORY= | O_RDONLY)); + io_dir__init(&dir, open(pmu->hwmon_dir, O_CLOEXEC | O_DIRECTORY | O_RDONL= Y)); =20 if (dir.dirfd < 0) return -ENOENT; @@ -283,7 +283,7 @@ static int hwmon_pmu__read_events(struct hwmon_pmu *pmu) __set_bit(item, alarm ? value->alarm_items : value->items); if (item =3D=3D HWMON_ITEM_LABEL) { char buf[128]; - int fd =3D openat(pmu->hwmon_dir_fd, ent->d_name, O_RDONLY); + int fd =3D openat(dir.dirfd, ent->d_name, O_RDONLY); ssize_t read_len; =20 if (fd < 0) @@ -342,7 +342,8 @@ static int hwmon_pmu__read_events(struct hwmon_pmu *pmu) return err; } =20 -struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, int hwmon_dir, con= st char *sysfs_name, const char *name) +struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, const char *hwmon_= dir, + const char *sysfs_name, const char *name) { char buf[32]; struct hwmon_pmu *hwm; @@ -365,7 +366,11 @@ struct perf_pmu *hwmon_pmu__new(struct list_head *pmus= , int hwmon_dir, const cha return NULL; } =20 - hwm->hwmon_dir_fd =3D hwmon_dir; + hwm->hwmon_dir =3D strdup(hwmon_dir); + if (!hwm->hwmon_dir) { + perf_pmu__delete(&hwm->pmu); + return NULL; + } hwm->pmu.alias_name =3D strdup(sysfs_name); if (!hwm->pmu.alias_name) { perf_pmu__delete(&hwm->pmu); @@ -399,7 +404,7 @@ void hwmon_pmu__exit(struct perf_pmu *pmu) free(value); } hashmap__clear(&hwm->events); - close(hwm->hwmon_dir_fd); + zfree(&hwm->hwmon_dir); } =20 static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_b= uf, size_t out_buf_len, @@ -409,6 +414,10 @@ static size_t hwmon_pmu__describe_items(struct hwmon_p= mu *hwm, char *out_buf, si size_t bit; char buf[64]; size_t len =3D 0; + int dir =3D open(hwm->hwmon_dir, O_CLOEXEC | O_DIRECTORY | O_RDONLY); + + if (dir < 0) + return 0; =20 for_each_set_bit(bit, items, HWMON_ITEM__MAX) { int fd; @@ -421,7 +430,7 @@ static size_t hwmon_pmu__describe_items(struct hwmon_pm= u *hwm, char *out_buf, si key.num, hwmon_item_strs[bit], is_alarm ? "_alarm" : ""); - fd =3D openat(hwm->hwmon_dir_fd, buf, O_RDONLY); + fd =3D openat(dir, buf, O_RDONLY); if (fd > 0) { ssize_t read_len =3D read(fd, buf, sizeof(buf)); =20 @@ -443,6 +452,7 @@ static size_t hwmon_pmu__describe_items(struct hwmon_pm= u *hwm, char *out_buf, si close(fd); } } + close(dir); return len; } =20 @@ -712,6 +722,7 @@ int perf_pmus__read_hwmon_pmus(struct list_head *pmus) size_t line_len; int hwmon_dir, name_fd; struct io io; + char buf2[128]; =20 if (class_hwmon_ent->d_type !=3D DT_LNK) continue; @@ -730,12 +741,13 @@ int perf_pmus__read_hwmon_pmus(struct list_head *pmus) close(hwmon_dir); continue; } - io__init(&io, name_fd, buf, sizeof(buf)); + io__init(&io, name_fd, buf2, sizeof(buf2)); io__getline(&io, &line, &line_len); if (line_len > 0 && line[line_len - 1] =3D=3D '\n') line[line_len - 1] =3D '\0'; - hwmon_pmu__new(pmus, hwmon_dir, class_hwmon_ent->d_name, line); + hwmon_pmu__new(pmus, buf, class_hwmon_ent->d_name, line); close(name_fd); + close(hwmon_dir); } free(line); close(class_hwmon_dir.dirfd); @@ -753,6 +765,10 @@ int evsel__hwmon_pmu_open(struct evsel *evsel, .type_and_num =3D evsel->core.attr.config, }; int idx =3D 0, thread =3D 0, nthreads, err =3D 0; + int dir =3D open(hwm->hwmon_dir, O_CLOEXEC | O_DIRECTORY | O_RDONLY); + + if (dir < 0) + return -errno; =20 nthreads =3D perf_thread_map__nr(threads); for (idx =3D start_cpu_map_idx; idx < end_cpu_map_idx; idx++) { @@ -763,7 +779,7 @@ int evsel__hwmon_pmu_open(struct evsel *evsel, snprintf(buf, sizeof(buf), "%s%d_input", hwmon_type_strs[key.type], key.num); =20 - fd =3D openat(hwm->hwmon_dir_fd, buf, O_RDONLY); + fd =3D openat(dir, buf, O_RDONLY); FD(evsel, idx, thread) =3D fd; if (fd < 0) { err =3D -errno; @@ -771,6 +787,7 @@ int evsel__hwmon_pmu_open(struct evsel *evsel, } } } + close(dir); return 0; out_close: if (err) @@ -784,6 +801,7 @@ int evsel__hwmon_pmu_open(struct evsel *evsel, } thread =3D nthreads; } while (--idx >=3D 0); + close(dir); return err; } =20 diff --git a/tools/perf/util/hwmon_pmu.h b/tools/perf/util/hwmon_pmu.h index b3329774d2b2..dc711b289ff5 100644 --- a/tools/perf/util/hwmon_pmu.h +++ b/tools/perf/util/hwmon_pmu.h @@ -135,14 +135,14 @@ bool parse_hwmon_filename(const char *filename, * hwmon_pmu__new() - Allocate and construct a hwmon PMU. * * @pmus: The list of PMUs to be added to. - * @hwmon_dir: An O_DIRECTORY file descriptor for a hwmon directory. + * @hwmon_dir: The path to a hwmon directory. * @sysfs_name: Name of the hwmon sysfs directory like hwmon0. * @name: The contents of the "name" file in the hwmon directory. * * Exposed for testing. Regular construction should happen via * perf_pmus__read_hwmon_pmus. */ -struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, int hwmon_dir, +struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, const char *hwmon_= dir, const char *sysfs_name, const char *name); void hwmon_pmu__exit(struct perf_pmu *pmu); =20 diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 3bbd26fec78a..eed8d8005cff 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -762,7 +762,7 @@ struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs= _dirfd, const char *name) return perf_pmu__lookup(&other_pmus, test_sysfs_dirfd, name, /*eager_load= =3D*/true); } =20 -struct perf_pmu *perf_pmus__add_test_hwmon_pmu(int hwmon_dir, +struct perf_pmu *perf_pmus__add_test_hwmon_pmu(const char *hwmon_dir, const char *sysfs_name, const char *name) { diff --git a/tools/perf/util/pmus.h b/tools/perf/util/pmus.h index 8def20e615ad..d5140804a630 100644 --- a/tools/perf/util/pmus.h +++ b/tools/perf/util/pmus.h @@ -29,7 +29,7 @@ int perf_pmus__num_core_pmus(void); bool perf_pmus__supports_extended_type(void); =20 struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs_dirfd, const char = *name); -struct perf_pmu *perf_pmus__add_test_hwmon_pmu(int hwmon_dir, +struct perf_pmu *perf_pmus__add_test_hwmon_pmu(const char *hwmon_dir, const char *sysfs_name, const char *name); struct perf_pmu *perf_pmus__fake_pmu(void); --=20 2.50.0.714.g196bf9f422-goog From nobody Wed Oct 8 20:02:01 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 E5A742EA490 for ; Tue, 24 Jun 2025 19:03:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791820; cv=none; b=Qxayc2cYxTFAzYY6ONbxD5Zs7jiyZwxTchZR0s5gupft/uyeTuHhyvVuGtWmnJrsQMK/IwIpT1kTnKRP/8E3r6E7Z2vuQyZHKmRYzXDWGba77JI9cADqijC7HvR9amnSq1SBQuceGm8G/l//t/oren5MoyDfs2Givobt8A9kuOY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791820; c=relaxed/simple; bh=3tgl5xNUFacWr4ywDmbYp2bm4KueiDTCnUjsw04koYg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=a311zzs9Jhl2N8ggIP27kt6CBjKdp24uyW7/E+bakTatWp1q8QeQXeXc2k+4rr6WEb9UhW4IKggOtQep4Sj+In11/S6mRH4TVfNfWdF5axrfc+PY79sKl5L/Ob1LVap1olv+etCq5gcg3IUsX0JLZxvtbgQBBn895YVfHLr06+4= 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=FHFFCUfK; arc=none smtp.client-ip=209.85.215.201 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FHFFCUfK" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b2fa1a84566so3522542a12.1 for ; Tue, 24 Jun 2025 12:03:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750791818; x=1751396618; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=03XNXmHms+qeob4wdysyvHMz+S3X0yXNsMANvIhS/98=; b=FHFFCUfKqk98KhFheiM16jdT5nDSegYhqqkeTXDTyY4eNZEfclClbyncLGYZwCbFpf aQhLdRWos+fs0Kj11BxgUBD/086t7/g+xwV7wCGx1FjLS4FHybn2rQg6ocPXhIZI/Lv5 2QBVU1ErnVvJxmHRI2YyWN+wSJ4tGJirUo+ohgD3xhjDVFrnpHINkPTqL5t+hDtQq7rX XWuiIwqzyu1IAajvM2DgSjCdNr+fe5Z2yTiw8M/rVhIDPDSUiFpKHk1fo9jAu8ewMXFz sh9Tz1CWkEGsVReanoJO5nCZQUZajcrp14sEwkNhQnyn1VB1rRixhZZ1RGTdl8G3yrHf f2xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750791818; x=1751396618; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=03XNXmHms+qeob4wdysyvHMz+S3X0yXNsMANvIhS/98=; b=osN/5QplRGlX2jYJ5w91MgdoEnGcsbX3TIIY/OML1wJucBdwFIYXKgKgXfQuNcvp6x nDZdtx1te3jc3Bs09UcN168Y33TMgob6l3Fq+R36meZxwc2EqT+L9GoCY1sArDfJp+hi fkRGst3TIxrLp10ruwWpwMjaqOo0PHebcnJ9to0peN/U3AxLSTRLXVgNcaePirfeqpdA 5XE7ur+mOc1WQfphNZmeCDmpN/2ofkjrjVYkVnsLI6Dpysh1b3Vu7PxtOhLlyjHEjK/+ okaaf6UzRDB8KtEYt0wsoQQ/w5FSFGbNVoC1DhbcRXdU6IC0mjQWGRAY9u/9oP49g6nU caZQ== X-Forwarded-Encrypted: i=1; AJvYcCXKqgAMwnTW/s54ZbZElq5EWzl4xQxOStIFrx0uNkXLNVSbpZWsuSggEi0Pg54noMHXI84YZyrm4qjddAg=@vger.kernel.org X-Gm-Message-State: AOJu0YxB/GRqpkG9DTMm2vZjoeLZW1EGMsUcElS8yBtShc25+C6NvFiV T4pPVEN51IwD1QjSeZKPf7WxErkJPrW6b6rF7mq3wdFefUEZuoLtuaedkVMdKq+IaRaOvfAzXX6 xxHeoR5QNZw== X-Google-Smtp-Source: AGHT+IHPdOePcyO4aRvWDhy29wCjuehy2KtmqTe6eCZKwMPh45qJNuyJpcPz7B7VpggSzv/oBYMA2XI9jPSd X-Received: from pjbqn8.prod.google.com ([2002:a17:90b:3d48:b0:312:2b3:7143]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2e41:b0:313:33ca:3b8b with SMTP id 98e67ed59e1d1-315f26189bcmr19867a91.9.1750791818291; Tue, 24 Jun 2025 12:03:38 -0700 (PDT) Date: Tue, 24 Jun 2025 12:03:24 -0700 In-Reply-To: <20250624190326.2038704-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250624190326.2038704-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.714.g196bf9f422-goog Message-ID: <20250624190326.2038704-5-irogers@google.com> Subject: [PATCH v1 4/5] perf dso: With ref count checking, avoid dso_data holding dso live From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Howard Chu , Charlie Jenkins , Thomas Richter , Athira Rajeev , Stephen Brennan , Jean-Philippe Romain , Junhao He , "Dr. David Alan Gilbert" , Dmitry Vyukov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With the dso_data embedded in a dso there is a reference counted pointer to the dso rather than using container_of with reference count checking. This data can hold the dso live meaning that no dso__put ever deletes it. Add a check for this case and close the dso_data when it happens. There isn't an infinite loop as the dso_data clears the file descriptor prior to putting on the dso. Signed-off-by: Ian Rogers --- tools/perf/util/dso.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 057fcf4225ac..c6c1637e098c 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1612,6 +1612,10 @@ struct dso *dso__get(struct dso *dso) =20 void dso__put(struct dso *dso) { +#ifdef REFCNT_CHECKING + if (dso && dso__data(dso) && refcount_read(&RC_CHK_ACCESS(dso)->refcnt) = =3D=3D 2) + dso__data_close(dso); +#endif if (dso && refcount_dec_and_test(&RC_CHK_ACCESS(dso)->refcnt)) dso__delete(dso); else --=20 2.50.0.714.g196bf9f422-goog From nobody Wed Oct 8 20:02:01 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 5FAF52EB5D6 for ; Tue, 24 Jun 2025 19:03:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791822; cv=none; b=pYm+DWcqHa7zFpYixP2DW2yEOPB77PdLMczdV0L1Vk+kw2kKCH5NMNOWTxMgAawWH12CLgO9QpjBvkamYtM5BGNW17MaqSWsPkusPo8FaH4Z6UDc2FjBtquXKxQpmTw159WBKqbXgSyJh4W3N7Nh5My17/71qVycyoCSZNJrnHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750791822; c=relaxed/simple; bh=Wblp0ohl6IRm7lIeNpyX4alondQblEImOX1Lca1Vayc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=PXlONfVQ+/RuK2fhBATAV2RJUF8kaynQClc6ke1ZYdjzilw/x78GNSoDRP01pk+GukmRyNkMTSKGzl4pU2qRHkP95JnfwQl3ok61TvcCVzIJ8dKGSImxhCo8xLRB4RegjXBK5j+6nRY4saE5s2JMx+zgb7kQky9Oo5LrI3VXspU= 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=iWlf1Ghi; arc=none smtp.client-ip=209.85.210.201 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iWlf1Ghi" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-748b4d5c045so4450051b3a.1 for ; Tue, 24 Jun 2025 12:03:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750791820; x=1751396620; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=OvhUhEpt8Wtn+apdV1pw7/FGDr2NOUshwP9osdCowcg=; b=iWlf1GhikkN/gqhpMG42XhbqD5VdiEqwvCiIlCDzjt4zMOIp7SeE++xYptoefqX0VI mKHC/JzgJs9XyyZY5O5eu1GtwaQRigiD6yKXx/t7+gszNLNzDsSFwpHLEqzBKQPpO3Mj 62GnZmJzWHIeY6uFudeem9pOil99BXJXlTxrlF6WYYt50mdjOH6M8RE9FXjHXDF3tL7z yi6g1hxYkYeDrvWurQ86QeEIjvFCkClAq412RSwB1tx4wvNksRQukM1YSci9I5aza9vI 3IxqiqUd0fjQ91DrRoiAA8j2X7HhVXr4xyH0aKjzz3awSss/x/aQvr3CSR9nTt2M9AjC U9Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750791820; x=1751396620; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OvhUhEpt8Wtn+apdV1pw7/FGDr2NOUshwP9osdCowcg=; b=ZRqbN/BBEVn0nxAnMZb+laEd2FLA9NP0112kOMvNAru69vp+8ekghdhfuQVV5K5jp8 OKvBxvu+o7ycSRLpPTNB1AWInkfXjVQQQGfuR/YIG9nw0zZQV/ehyu+nXVOjaRpp1keM WtEvh4lE4HHdPlcdNtrk4S7WzCulOezwft4bSQ59FMCPyCSFPDyrn2D18lndr4qKN+tZ S6ADV1pamAcTv/CGYZPbjWRIxfrKA3N7cF3dK/UHUFcfM6PxyCueEO2q+Kmvw+O+5+xN ROG9irlpXODb9TS7SQlMipP3cgmdKAuludaUEBJn4bDAlu0sV8+FE7ZR1hnICTCZMzMY Halw== X-Forwarded-Encrypted: i=1; AJvYcCVPL40BslOIfcl/njJAoL5wcRohrjKjw11eDGHK0bt9Sta9j3VOoWQnh/UL3+C2waveSghW2X6Ovm0TiTw=@vger.kernel.org X-Gm-Message-State: AOJu0Ywyw0u1yOtWVL2biK1H/EFxEOuik9yMVD8K3ZpcwduMEA6/MB/X E6QSb5ngyGs3Qqjfayq8YCOnSCVPsdprgpWL2p7hxo2yGW1AVRB8bl+voR/09xOZCziaf6e8T+3 +z7zWCQiMgg== X-Google-Smtp-Source: AGHT+IHXRzxwI/vxMtdvnFS97/XuZM4ViICM7CB3vtBrkVTBTbh4ijqkM8zAUt69HZBcWf2I334trVSs07qB X-Received: from pfuv1.prod.google.com ([2002:a05:6a00:1481:b0:746:2862:2a2f]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:170b:b0:748:f74f:6d27 with SMTP id d2e1a72fcca58-74ad45e0092mr388115b3a.24.1750791820499; Tue, 24 Jun 2025 12:03:40 -0700 (PDT) Date: Tue, 24 Jun 2025 12:03:25 -0700 In-Reply-To: <20250624190326.2038704-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250624190326.2038704-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.714.g196bf9f422-goog Message-ID: <20250624190326.2038704-6-irogers@google.com> Subject: [PATCH v1 5/5] perf test: In forked mode add check that fds aren't leaked From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Howard Chu , Charlie Jenkins , Thomas Richter , Athira Rajeev , Stephen Brennan , Jean-Philippe Romain , Junhao He , "Dr. David Alan Gilbert" , Dmitry Vyukov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a test is forked no file descriptors should be open, however, parent ones may have been inherited - in particular those of the pipes of other forked child test processes. Add a loop to clean-up/close those file descriptors prior to running the test. At the end of the test assert that no additional file descriptors are present as this would indicate a file descriptor leak. Signed-off-by: Ian Rogers --- tools/perf/tests/builtin-test.c | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index 45d3d8b3317a..4061d5d969aa 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -4,6 +4,7 @@ * * Builtin regression testing command: ever growing number of sanity tests */ +#include #include #include #include @@ -155,6 +156,71 @@ static struct test_workload *workloads[] =3D { #define test_suite__for_each_test_case(suite, idx) \ for (idx =3D 0; (suite)->test_cases && (suite)->test_cases[idx].name !=3D= NULL; idx++) =20 +static void close_parent_fds(void) +{ + DIR *dir =3D opendir("/proc/self/fd"); + struct dirent *ent; + + while ((ent =3D readdir(dir))) { + char *end; + long fd; + + if (ent->d_type !=3D DT_LNK) + continue; + + if (!isdigit(ent->d_name[0])) + continue; + + fd =3D strtol(ent->d_name, &end, 10); + if (*end) + continue; + + if (fd <=3D 3 || fd =3D=3D dirfd(dir)) + continue; + + close(fd); + } + closedir(dir); +} + +static void check_leaks(void) +{ + DIR *dir =3D opendir("/proc/self/fd"); + struct dirent *ent; + int leaks =3D 0; + + while ((ent =3D readdir(dir))) { + char path[PATH_MAX]; + char *end; + long fd; + ssize_t len; + + if (ent->d_type !=3D DT_LNK) + continue; + + if (!isdigit(ent->d_name[0])) + continue; + + fd =3D strtol(ent->d_name, &end, 10); + if (*end) + continue; + + if (fd <=3D 3 || fd =3D=3D dirfd(dir)) + continue; + + leaks++; + len =3D readlinkat(dirfd(dir), ent->d_name, path, sizeof(path)); + if (len > 0 && (size_t)len < sizeof(path)) + path[len] =3D '\0'; + else + strncpy(path, ent->d_name, sizeof(path)); + pr_err("Leak of file descriptor %s that opened: '%s'\n", ent->d_name, pa= th); + } + closedir(dir); + if (leaks) + abort(); +} + static int test_suite__num_test_cases(const struct test_suite *t) { int num; @@ -242,6 +308,8 @@ static int run_test_child(struct child_process *process) struct child_test *child =3D container_of(process, struct child_test, pro= cess); int err; =20 + close_parent_fds(); + err =3D sigsetjmp(run_test_jmp_buf, 1); if (err) { fprintf(stderr, "\n---- unexpected signal (%d) ----\n", err); @@ -257,6 +325,7 @@ static int run_test_child(struct child_process *process) err =3D test_function(child->test, child->test_case_num)(child->test, chi= ld->test_case_num); pr_debug("---- end(%d) ----\n", err); =20 + check_leaks(); err_out: fflush(NULL); for (size_t i =3D 0; i < ARRAY_SIZE(signals); i++) --=20 2.50.0.714.g196bf9f422-goog