From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796078621; b=AaS03qefltuZzu13XnR4LHkcey1bgwCEFa7FrmQn8aA6zscvfNlamde5jo8NOulAB/gW FHmwgpdNscLAZqpw82eDDotw9BnCR9pe9JRReXJmpM3H40FVYabXe5bMS10levcCXlDej xIWwJD/BzAzOHCQ9tgupQR03Yq00rK9dY5p2ZD/wJwrFYA1cEtSP3VYZVmVbWED+5tSYV u+CkOIO/0gbgz36sQ+DOn/Qomz8MTHoPY2ZqjDc/0nTKAg3Jws1fXCHdlHLjo9OD+TcVp 3v/K6D6M2c8by0zexQKeeTxzG6872n0FK5iDP+hDxL0JS9xofnRBsxpCQxfMw6tF20A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796078621; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=s2gD8FLlx/YF/9rtFC74z1YKVTWYBQ1R8TTbhb93q40=; b=PaBpRM6bqQ2o4seY8JTvCDYbEE+JjYV9Ut2C3PmaPmCsMwqZR4GQRl3d7v0DrL6B0F3o FPep4ZmMZ31cLlL7W8vbH4dNSupykPHXiQpD0exWOLY2c0lXrvOhC4CWym3NndjUf5v04 XDAYTKvANTvTOM50YhswHNo35zstiWNUuNdKY4A8skKJC2/5y1v/Dyq5uVDtF0vu/U5vb GAP1z1cTNAzkzVKgCQb9Ou/jpbIC3mBA8bVGs/nB9h+Hq2iTCtPitndrkULITqGzGSj89 Pv6bgIG0/RpM4T+tA8fXw5jtklC8pui4QqvYKl/PofhnhJl95ljV1TYW/dtX8OfQrRg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=OxzSbCKb; arc=none smtp.remote-ip=74.125.82.70 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5739606A334 for ; Mon, 30 Nov 2026 22:43:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A5739606A334 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.a=rsa-sha256 header.s=20260707 header.b=OxzSbCKb Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-139b62317d0so14003874c88.0 for ; Mon, 30 Nov 2026 14:43:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796078620; x=1796683420; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=s2gD8FLlx/YF/9rtFC74z1YKVTWYBQ1R8TTbhb93q40=; b=OxzSbCKbne+XUaYse+UUI/mIF9AoY5tku3U7PG9oR8v3yigR5lgxAtmTsYJ8EB8cur v/ReGNFp002/v83IeN+lHUBy+l4OrE6sOoTJikSUdWY1uniBWZQVvGJ0HTZzrvcrUNHW BuER0trnMQlWRJPFV8IbEBoGHbCu0/5q7AoH+cjxK3v8IrJOdC8vye7c++yaL/6/YVIl +F8RdlfLAJeMSmL4/phwig8TmmnHbHirkPa/6vbu+Ob6uudjadxkqmNN/dnYU4OEixz/ v2T8dLytarnedd2ZR5c9zVXsbwlY66qkvsLmGoJOABF2+SYNkSSIHlrDag8jVpAgoUL6 E4Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796078620; x=1796683420; h=content-type:cc: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:content-type; bh=s2gD8FLlx/YF/9rtFC74z1YKVTWYBQ1R8TTbhb93q40=; b=Cst8vMwLfBHF94uioJVVKBbafXcQhiySLyWvkuWPS6KHdvZkXIBIYJSGq/lDxqsxyR NUQme/V5YTrgXQijrATh2gAnk0BEY4hmfvnWkZuAu7ffQXuki1U/wYttTvWQfhs1TGM/ FdMpfY1+FGc6BcnzYqJGyZ5S5UUbb1dT5Puwm0QWRrKSUab38sfhuF7KEgyWO+erL/fA lU/04blZEEco90dtW2nom+H6++NaLsXwa+Yu+MxGNEq2r8YsbChYnLq1Fkzb9Yil8O61 78D/AknthTQ1ix7Gavlr/j64DnFi0LjUs9GCCAIwGK7MWcI4D80MwJL/fS+HFlh14Svl MNew== X-Forwarded-Encrypted: i=1; AL+hErHzxLr6oRS3nLL113+DFui1y8xj/50nNmZjROYbJWO+OFrdveCbWyblY0nn8YLib3Q6zPUrd6/IjnCm//tNbk+c@vger.kernel.org X-Gm-Message-State: APuU4Z+GCfWtTQBRN5P1r9tawdXJVJPg8EFKSZVngLq8RUlXyt3O5Q8R AC5Yv0xLUFugXIZUYd/TjGnDSn2q6Eqtbhs2a3a7yMuQrwy23nrssC0HGZ6W5Op1Ocnd+60GFB4 BXgqsuL15oA== X-Received: from dlbqe9.prod.google.com ([2002:a05:7023:b89:b0:1ac:2245:a27b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:2706:b0:154:e9d2:d741 with SMTP id a92af1059eb24-1b5568a12f4mr34493529c88.8.1796078619372; Mon, 30 Nov 2026 14:43:39 -0800 (PST) Date: Mon, 30 Nov 2026 14:43:29 -0800 In-Reply-To: <20261130224332.79575-1-irogers@google.com> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261130224332.79575-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261130224332.79575-2-irogers@google.com> Subject: [PATCH v2 1/4] perf threads: Don't resurrect removed threads in last_match From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim Cc: Jiri Olsa , Adrian Hunter , James Clark , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" threads__find() looks up the thread under down_read(&table->lock), drops the read lock, and then acquires down_write(&table->lock) inside threads_table_entry__set_last_match() to update table->last_match. If threads__remove() (or threads__remove_all_threads()) runs in the window between up_read(&table->lock) and down_write(&table->lock), it clears table->last_match and deletes the thread from table->shard, only for threads__find() to overwrite table->last_match with the removed thread. Subsequent lookups for that tid then return the removed thread from table->last_match, causing stale event attribution and keeping a cached reference until another lookup evicts it. In addition, threads__find() unnecessarily took down_write(&table->lock) to re-set table->last_match even when last_match already hit. Only update table->last_match on a cache miss, and recheck under down_write(&table->lock) that tid still maps to the same struct thread in table->shard before updating table->last_match. Fixes: d436f90a64f3 ("perf machine: Move machine's threads into its own abstraction") Signed-off-by: Ian Rogers --- tools/perf/util/threads.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c index 7cf951cbc963..9f5f186c7072 100644 --- a/tools/perf/util/threads.c +++ b/tools/perf/util/threads.c @@ -89,10 +89,13 @@ static void __threads_table_entry__set_last_match(struct threads_table_entry *ta } static void threads_table_entry__set_last_match(struct threads_table_entry *table, - struct thread *th) + pid_t tid, struct thread *th) { + struct thread *cur; + down_write(&table->lock); - __threads_table_entry__set_last_match(table, th); + if (hashmap__find(&table->shard, tid, &cur) && RC_CHK_EQUAL(cur, th)) + __threads_table_entry__set_last_match(table, th); up_write(&table->lock); } @@ -100,16 +103,19 @@ struct thread *threads__find(struct threads *threads, pid_t tid) { struct threads_table_entry *table = threads__table(threads, tid); struct thread *res; + bool update_last_match = false; down_read(&table->lock); res = __threads_table_entry__get_last_match(table, tid); if (!res) { - if (hashmap__find(&table->shard, tid, &res)) + if (hashmap__find(&table->shard, tid, &res)) { res = thread__get(res); + update_last_match = true; + } } up_read(&table->lock); - if (res) - threads_table_entry__set_last_match(table, res); + if (update_last_match) + threads_table_entry__set_last_match(table, tid, res); return res; } -- 2.56.1.839.g22a8290db9-goog