From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796078620; b=D1/8KI8OYEeBzr39NYRXe+AGKtSdnKcqktgkErQWYepYiNcyhcRk5P01sYiCsMMhdf1O oiIn5dhgpxalK6Y/JNMYohrxdJ18/pNu9vHK7yfFRe6/DZrujEmHTedjEuEScgVl6gB3L XfEPTOumnybeofvP1PncNlHDDscW3lKGrPpEn9rfDcGZG//Arl/2yso4YupULUppuGpar VGHgqKIFu/fEwHZVhzNG8FG45pG4tnWwAP3/MgYf3nKzOyFp8Vl3nVDTI2lDpahFhktKD T6oJq4sLAbHICKVQDSs0+Q3Rf8xBQxNXIvQrw5sHyI1bmv5cDEHL0cmdiN7VxbNeyow== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796078620; 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=VrqKhiZXWi/UEEY7XTj6CTGlTlVDboVn6/NJLse2eYLEiLOSpGEZwgGhb2CXdiZCOnoD 8KsS+/aEH4CrnWv/usWABAtdl/bYF5gkJ7o643KoA3gWN4zV7So7O3OONnl6Me7QEM/A4 4+L8yxKDujvq77n4GhcfQYUeuc9cQc3K1XNXmxtvebRJkZIGi1iZ2D7rgY54x+3bxp1O7 7Fyi/s8HPsIwXzb4IeylrjrLTrJfhGNNd6O5P4Qgj6SjtZKCeIHehs6uxiGMN/9hOl5Rd FKMtV3XiQbKw+yfu+Ii7efXwUnvQqcV1Ildf7tdWX/PIBepW0zvoIhDhcOTlxm+UMmQ== 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.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (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 83099606A331 for ; Mon, 30 Nov 2026 22:43:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 83099606A331 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-dy1-f197.google.com with SMTP id 5a478bee46e88-3869865a304so11919999eec.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=fuVwCHn7xaFYKvnyEPP8nn2dcr5yhIPNsFlSldvqAsS35IigHMoqT1m3AWTIp+LNjw hwiYqv2x/LUmNYA/4C+F3z1q0YEvLD4cp+OTAzUK1g2Q/iGu5HF9JezF+fMnwbU1OVs1 Q1/uLIjxsm+I8Adp5V3J7ioQR4+el88SjjZFDTE8rYwk2smp4fGzlwXV//L3niOFG7H6 LktA5MS7Gw6gAQ9EIg9a+K7uyFNYWEW9aiVbcYTBNIlzqQKY69aqZIi7aJxSCfp5/Y/h t9XQsJbY6zsO7Bc+AX32LS69iDloO4x7140pn1v8OG4m95R5e4/ysTt7jKN2uQzK6kXG 3YQw== X-Forwarded-Encrypted: i=1; AL+hErF3Sh8HSHoVLAxyiRu6NF8t3GzmNM2ar/frnIzjLUlq2wyqKtsFcyS7T+UuCi2gfZkHKuVnNVul88luzJw=@vger.kernel.org X-Gm-Message-State: APuU4Z+SP7Q3kXK+s1Te8/TPBCQCHJQiVns3c+jSuJiQuih6uba5eNOd o2OjZY2N9N1rSXUfZSwKZCUb2GjHUPxAgRw18If61sZoL9RQem5Wk1wZEuPK5RKNSx5l+mWcQOh rDIxzr4YK0g== 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-kernel@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