From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795114356; b=0xnZI7/IqTpyNx9n6wdXtJBYf02d+9JAabflsif9c2MAMQfELY2B7ULq3+ZZNFhMWhsy UzvzWRJD3sjGL0E3GtXHEd6cW7NCjyKwBaHZeqhyFZt852qGqLkyXcTfKa0QT95Syynnw PVfWoLvGesgXDHCZ7hAPHclQLQvwfGcnRFa7uYmrpQ58aX7Hv4CYD2OvCMfBExi5sPcXc ZbiNhIVuZ1/CTZdXu+ctr+qgeGq9B1qLJ9ElvixyC9AlRcV+2ADyb+ELzK9n5mXS45RmO oI7PX0aZfbiJrHilA/RRTYip7EjAbZIA+hcSBv/5sDSrekQ39kdBm7ucwzELY/PjMrQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795114356; 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=tt34GVe7kBng/bcouD1dbLOnNpNCEkhyJnpoNOTfYBg=; b=ZLmnWSHrBj4VBzq3nPxKu5ZLPz6grFB79ZHGv4SaPekPJ8mlgbBizGv7YzdP9T879sgp KL0COzEbEOgOLgb3KFNxZy1uehis3SrVCndW6/94MVST4x1HSD20n4SlV3OQTL9Z+T8ZM Eo4c5YZNDBJrFa1TW6eBioXLWbrKcVLfeKqR3uAp0h+fp2QsEim6YWMJKgemqcXegoQqp 6kKKKl7Shlx+rVDR25ePb7+H4PoTU3t3bdwRTHTqKYD90sPB767peuZP2UOaIU6R9lrLB fqtMQ9nWQ1RYkxZPiHqJ7vEkyoCywYydandpBMc3kDbfKyTOygjT0Ppsj3lBF7Ta0Dw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=AdFdvy8E; arc=none smtp.remote-ip=209.85.167.197 Received: from mail-oi1-f197.google.com (mail-oi1-f197.google.com [209.85.167.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 D7624606DC99 for ; Thu, 19 Nov 2026 18:52:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D7624606DC99 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--coltonlewis.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=AdFdvy8E Received: by mail-oi1-f197.google.com with SMTP id 5614622812f47-4a41f46d629so199932b6e.3 for ; Thu, 19 Nov 2026 10:52:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795114355; x=1795719155; 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=tt34GVe7kBng/bcouD1dbLOnNpNCEkhyJnpoNOTfYBg=; b=AdFdvy8E4NZPVwkvjNw0tB2p0ZMpUTxqT2mUaqvzRf5p+o3fnEpN1MACjgez+MOIIZ Tm9AxDcyIo26JT2nI7KisaVoIDv11DBalhRt0gSsXtFIUGXSPqLLCl0sZltfjJW/TY4p WukB4Dod98M3ER9ZzhXP5QmFSnb6bX2EkyZtlgpbtU6kh4RioLfcALeuqz/EumT2raMV KFzgYEuiWElhGlqR3ikK2ujBEjYr1OlicvQAkvBJ/yhX3wMlPLXSPF9BNXhmHGKzK7dh P0RaXExlSyET4MDu3JBlxakAGiS9ajn86t+yl9xy7gd/Vx5KNxTQVHyMj1hgSjbcGAS7 kBPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795114355; x=1795719155; 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=tt34GVe7kBng/bcouD1dbLOnNpNCEkhyJnpoNOTfYBg=; b=rjmbhrT+5xRMzJ8gwquY8xJbZyXzVuO7ZSxzZYPOlK0dCv3W3OyEoqyb3E0eRlXpaG KiPew+j7eafMnOC11ZteXNVgFFQi0QFtCanJWPbtxdE0Lum/O/jNUdvVC9o7rL6druU4 IEwcXaDE/iJnKa/xXPbWiFZQ3+Q0RTGs4WQl16jnRzdjLDAp5gDKHwfT6GWdWz5Q8c4P tcrJpBih1alilGYSp1iA5ASdfiJJi33O9vg0wAPQVcU7pZMoHU9XPXXgF5GS4ZSkrfzt 2zILrbP55Y2YlD2um3dwP/bM9fE518W3R4JtnJ9uvLjNTj5CQ6TnxNWYIHmuMA9zodo1 bHzw== X-Forwarded-Encrypted: i=1; AL+hErFku1DdIHqDqLh+wSKR9DdOzzZDf6KwlKn15+/3cExR1YT2XYilIz8IDy5K+PhB0sdO7q1lMAf4BzY=@vger.kernel.org X-Gm-Message-State: APuU4Z/F9yOraIofKsZkVitNePNI29TWGHNaUoUiUe6FBVbFatEALQXQ Hr2sy3r4fajs/vIRiDbYm4IdHrmu9ivTP0fhOstXLbjZEbnWvCzxA+SlcejrzVmjGKgr18Sm23d l7QZFkqVqh5BpW+EBNJrhiJbI8A== X-Received: from jaho5-n1.prod.google.com ([2002:a05:6638:67c5:10b0:621:d005:6f2c]) (user=coltonlewis job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:3c47:b0:4b9:e65b:8c33 with SMTP id 5614622812f47-57a09212719mr1036647b6e.33.1795114354568; Thu, 19 Nov 2026 10:52:34 -0800 (PST) Date: Thu, 19 Nov 2026 18:52:01 +0000 In-Reply-To: <20261119185215.632334-1-coltonlewis@google.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261119185215.632334-1-coltonlewis@google.com> X-Mailer: git-send-email 2.56.1.809.ga0b61035b5-goog Message-ID: <20261119185215.632334-20-coltonlewis@google.com> Subject: [PATCH v10 19/33] perf: Add perf_pmu_resched_update() From: Colton Lewis To: Marc Zyngier , Oliver Upton Cc: Catalin Marinas , Will Deacon , Mark Rutland , Peter Zijlstra , Ingo Molnar , Paolo Bonzini , Russell King , Jonathan Corbet , Fuad Tabba , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Shuah Khan , James Clark , Alexandru Elisei , Mingwei Zhang , Ganapatrao Kulkarni , kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" To modify PMU guest counter reservations dynamically, the per-CPU available counter mask must be updated while host perf events are temporarily scheduled out. Introduce perf_pmu_resched_update() to update PMU state while holding cpuctx->ctx.lock in between scheduling perf events out and scheduling them back in. It accepts a callback invoked between ctx_sched_out() and perf_event_sched_in(), accomplishing atomic counter mask updates with minimal perf API expansion. Refactor ctx_resched() into __ctx_resched() to invoke the callback at the appropriate point. Tested-by: James Clark Signed-off-by: Colton Lewis --- include/linux/perf_event.h | 3 +++ kernel/events/core.c | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index e1e11500df097..8140457939658 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1242,6 +1242,9 @@ extern int perf_event_task_disable(void); extern int perf_event_task_enable(void); extern void perf_pmu_resched(struct pmu *pmu); +extern void perf_pmu_resched_update(struct pmu *pmu, + void (*update)(struct pmu *, void *), + void *data); extern int perf_event_refresh(struct perf_event *event, int refresh); extern void perf_event_update_userpage(struct perf_event *event); diff --git a/kernel/events/core.c b/kernel/events/core.c index e870f12c3ad81..dbc42ded5f698 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -3010,9 +3010,10 @@ static void perf_event_sched_in(struct perf_cpu_context *cpuctx, * event_type is a bit mask of the types of events involved. For CPU events, * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE. */ -static void ctx_resched(struct perf_cpu_context *cpuctx, - struct perf_event_context *task_ctx, - struct pmu *pmu, enum event_type_t event_type) +static void __ctx_resched(struct perf_cpu_context *cpuctx, + struct perf_event_context *task_ctx, + struct pmu *pmu, enum event_type_t event_type, + void (*update)(struct pmu *, void *), void *data) { bool cpu_event = !!(event_type & EVENT_CPU); struct perf_event_pmu_context *epc; @@ -3048,6 +3049,9 @@ static void ctx_resched(struct perf_cpu_context *cpuctx, else if (event_type & EVENT_PINNED) ctx_sched_out(&cpuctx->ctx, pmu, EVENT_FLEXIBLE); + if (update) + update(pmu, data); + perf_event_sched_in(cpuctx, task_ctx, pmu, 0); for_each_epc(epc, &cpuctx->ctx, pmu, 0) @@ -3059,6 +3063,29 @@ static void ctx_resched(struct perf_cpu_context *cpuctx, } } +static void ctx_resched(struct perf_cpu_context *cpuctx, + struct perf_event_context *task_ctx, + struct pmu *pmu, enum event_type_t event_type) +{ + __ctx_resched(cpuctx, task_ctx, pmu, event_type, NULL, NULL); +} + +void perf_pmu_resched_update(struct pmu *pmu, void (*update)(struct pmu *, void *), void *data) +{ + struct perf_cpu_context *cpuctx; + struct perf_event_context *task_ctx; + unsigned long flags; + + local_irq_save(flags); + cpuctx = this_cpu_ptr(&perf_cpu_context); + task_ctx = cpuctx->task_ctx; + perf_ctx_lock(cpuctx, task_ctx); + __ctx_resched(cpuctx, task_ctx, pmu, EVENT_ALL | EVENT_CPU, update, data); + perf_ctx_unlock(cpuctx, task_ctx); + local_irq_restore(flags); +} +EXPORT_SYMBOL_GPL(perf_pmu_resched_update); + void perf_pmu_resched(struct pmu *pmu) { struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); -- 2.56.1.708.g77570a6a1d-goog