From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C7D03E7145; Tue, 30 Jun 2026 09:09:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782810585; cv=none; b=drYRCCZ9TMjD9619Fjrjr4yL7z1zeb5XnxfD6/L3xUbaID2GHbug6cC4a43pCwaYaOnY0FZGhw898k8D9463URsJ/oq+DZD1chUVHMZrUSQnvVBeDMDfMYko5eNaVOAPxQoC+Rbl8G0EI694CkgmDRrQpb+jA2WUDSKDqQZ3T4k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782810585; c=relaxed/simple; bh=9e5DF3yDWOkPeaxdyQigGYz9vO626WtelxCuFD6aWGg=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=kna18IW/imGsVtxQcH0KDLMOJsuS8TB54UEzxU+txpP3BQ+V6tU404E4Qd/SxTdVSPWWi8CYHZXMYDSsjoFA1P9aoZEcStXqBiAwOmDFRDeypRQQw0XmTRQvkTzrp5lFwuiKZB87HWVv4Z6H9gnmLBivRioB7Mdk8VO51g/RI3E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Ye4PaqjZ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lt1oUDtF; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Ye4PaqjZ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lt1oUDtF" Date: Tue, 30 Jun 2026 09:09:40 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1782810582; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i4kbOhCKteNyYR8m/PnKegFM6/+vTEPAPOQbPdApvTM=; b=Ye4PaqjZ6P6KfRzorkp8TiQ9n6YHfrsHnzLiZMmI47FUpXJJe1ZMBfjwhAndao+sEIlcNh d53XzAmChpK57iVusDEYXIootWr3xqbRP4J8f3V2gxd5kLo3/T6YkvwJTmgcZPKlQS2zKv iOSmdLsWj0CTs3Dq9tZ98AuORCBmTZhVxytZuOCwBKFfjXbyAfgPKf8GQCI8AYbO+44bM0 YP3FCq5kXhFsBrt0XHmFV17QAcS0g3XtG1MyPKkLFr9al8UBxihuSQLWLr9QT6DLCY53He MqEfm+V602GenvRmb97XBsUNNKRCWd7J88CQZNESJrt+BqoIfcSIJRYCexjEwA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1782810582; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i4kbOhCKteNyYR8m/PnKegFM6/+vTEPAPOQbPdApvTM=; b=lt1oUDtFwWGoUhWc4z316eoCjCkwmvdAv93xJoFq+Y3N5WJngm3VoT/zfufu8qinMk8n6A QorgQ88ngubeP3AA== From: "tip-bot2 for Zide Chen" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: perf/core] perf/x86/intel/uncore: Fix uncore_box ref/unref ordering Cc: Zide Chen , "Peter Zijlstra (Intel)" , Ian Rogers , Dapeng Mi , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20260611160033.66760-8-zide.chen@intel.com> References: <20260611160033.66760-8-zide.chen@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <178281058081.3843924.4645002155716976564.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the perf/core branch of tip: Commit-ID: 174f0582e38abe03b88e15f04bfe58490f88cb19 Gitweb: https://git.kernel.org/tip/174f0582e38abe03b88e15f04bfe58490f8= 8cb19 Author: Zide Chen AuthorDate: Thu, 11 Jun 2026 09:00:32 -07:00 Committer: Peter Zijlstra CommitterDate: Tue, 30 Jun 2026 10:57:04 +02:00 perf/x86/intel/uncore: Fix uncore_box ref/unref ordering In uncore_event_cpu_online(), uncore_box_ref() was called before uncore_change_context(). uncore_box_ref() gates on box->cpu >=3D 0, but box->cpu is still -1 at that point because uncore_change_context() has not run yet. As a result, the box is never initialized on the first CPU to come online in a die, leaving it permanently uninitialized in the single-CPU-per-die case. Thus, box->refcnt is one count below the true value, and in the CPU offline path, the box will be torn down on the second-to-last CPU. In uncore_event_cpu_offline(), uncore_box_unref() was called after uncore_change_context(), so box->cpu is already -1 when the collector CPU goes offline, which prevents it from tearing down the box. Fix by swapping the call order in both paths so that uncore_box_{ref,unref}() runs at the point where box->cpu reflects the correct context. Move allocate_boxes() out of uncore_box_ref() to enable this reordering. Fixes: c74443d92f68 ("perf/x86/uncore: Support per PMU cpumask") Signed-off-by: Zide Chen Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Ian Rogers Reviewed-by: Dapeng Mi Link: https://patch.msgid.link/20260611160033.66760-8-zide.chen@intel.com --- arch/x86/events/intel/uncore.c | 50 +++++++++++++++------------------ 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/arch/x86/events/intel/uncore.c b/arch/x86/events/intel/uncore.c index feb8c3b..b9ac2f7 100644 --- a/arch/x86/events/intel/uncore.c +++ b/arch/x86/events/intel/uncore.c @@ -1580,9 +1580,15 @@ static int uncore_event_cpu_offline(unsigned int cpu) { int die, target; =20 + /* Clear the references */ + die =3D topology_logical_die_id(cpu); + uncore_box_unref(uncore_msr_uncores, die); + uncore_box_unref(uncore_mmio_uncores, die); + /* Check if exiting cpu is used for collecting uncore events */ if (!cpumask_test_and_clear_cpu(cpu, &uncore_cpu_mask)) - goto unref; + return 0; + /* Find a new cpu to collect uncore events */ target =3D cpumask_any_but(topology_die_cpumask(cpu), cpu); =20 @@ -1595,16 +1601,10 @@ static int uncore_event_cpu_offline(unsigned int cpu) uncore_change_context(uncore_msr_uncores, cpu, target); uncore_change_context(uncore_mmio_uncores, cpu, target); uncore_change_context(uncore_pci_uncores, cpu, target); - -unref: - /* Clear the references */ - die =3D topology_logical_die_id(cpu); - uncore_box_unref(uncore_msr_uncores, die); - uncore_box_unref(uncore_mmio_uncores, die); return 0; } =20 -static int allocate_boxes(struct intel_uncore_type **types, +static void allocate_boxes(struct intel_uncore_type **types, unsigned int die, unsigned int cpu) { struct intel_uncore_box *box, *tmp; @@ -1621,8 +1621,10 @@ static int allocate_boxes(struct intel_uncore_type **t= ypes, if (pmu->boxes[die] || uncore_pmu_broken(pmu)) continue; box =3D uncore_alloc_box(type, cpu_to_node(cpu)); - if (!box) + if (!box) { + uncore_pmu_set_broken(pmu); goto cleanup; + } box->pmu =3D pmu; box->dieid =3D die; list_add(&box->active_list, &allocated); @@ -1633,14 +1635,13 @@ static int allocate_boxes(struct intel_uncore_type **= types, list_del_init(&box->active_list); box->pmu->boxes[die] =3D box; } - return 0; + return; =20 cleanup: list_for_each_entry_safe(box, tmp, &allocated, active_list) { list_del_init(&box->active_list); kfree(box); } - return -ENOMEM; } =20 static int uncore_box_ref(struct intel_uncore_type **types, @@ -1649,11 +1650,7 @@ static int uncore_box_ref(struct intel_uncore_type **t= ypes, struct intel_uncore_type *type; struct intel_uncore_pmu *pmu; struct intel_uncore_box *box; - int i, ret; - - ret =3D allocate_boxes(types, die, cpu); - if (ret) - return ret; + int i; =20 for (; *types; types++) { type =3D *types; @@ -1669,27 +1666,26 @@ static int uncore_box_ref(struct intel_uncore_type **= types, =20 static int uncore_event_cpu_online(unsigned int cpu) { - int die, target, msr_ret, mmio_ret; + int die, target; =20 die =3D topology_logical_die_id(cpu); - msr_ret =3D uncore_box_ref(uncore_msr_uncores, die, cpu); - mmio_ret =3D uncore_box_ref(uncore_mmio_uncores, die, cpu); + allocate_boxes(uncore_msr_uncores, die, cpu); + allocate_boxes(uncore_mmio_uncores, die, cpu); =20 /* * Check if there is an online cpu in the package * which collects uncore events already. */ target =3D cpumask_any_and(&uncore_cpu_mask, topology_die_cpumask(cpu)); - if (target < nr_cpu_ids) - return 0; - - cpumask_set_cpu(cpu, &uncore_cpu_mask); - - if (!msr_ret) + if (target >=3D nr_cpu_ids) { + cpumask_set_cpu(cpu, &uncore_cpu_mask); uncore_change_context(uncore_msr_uncores, -1, cpu); - if (!mmio_ret) uncore_change_context(uncore_mmio_uncores, -1, cpu); - uncore_change_context(uncore_pci_uncores, -1, cpu); + uncore_change_context(uncore_pci_uncores, -1, cpu); + } + + uncore_box_ref(uncore_msr_uncores, die, cpu); + uncore_box_ref(uncore_mmio_uncores, die, cpu); return 0; } =20