Compare
205cccd06e545694754e1bf8d626ea96e742cd8a..e4c7c401c6b9174c0c893084fdaa6a84bd2520d7
perf: Update cgroup time when rotating multiplexed events
Since commit bd2756811766 ("perf: Rewrite core context handling"),
perf_rotate_context() only updates the context time before scheduling
out the flexible events. It no longer updates the cgroup time, which
ctx_sched_out() used to do.
Cgroup events take their times from the per-cpu cgroup clock, so
between two cgroup switches the rotations see a frozen clock. The
events rotated out lose the running time they accumulated, and the next
update, usually a read, credits the whole interval to the events that
happen to be on the PMU at that moment.
As a result, the time_running of multiplexed cgroup events is wrong,
and so are their scaled counts, as long as no other cgroup runs on the
CPU, e.g. a busy job alone on its CPUs. Counting cycles over one second
of a busy loop in a cgroup gives from half to several times the max
frequency of the CPU.
Update the cgroup time before rotating the CPU context, as was done
before the rewrite.
Fixes: bd2756811766 ("perf: Rewrite core context handling")
Closes: https://lore.kernel.org/r/PR3PR07MB69223331EED5BEBE150375B7849C2@PR3PR07MB6922.eurprd07.prod.outlook.com/
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Antoine Huret <antoine.huret@bull.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261015-perf-cgroup-rotation-time-v1-1-93e177dfee5d@bull.com
Antoine Huret <antoine.huret@bull.com> · 2026-10-15 12:55 UTC · committed by Peter Zijlstra
9 files changed,
+49
−78
- arch/x86/events/intel/uncore_snbep.c
- drivers/gpio/gpio-mxc.c
- drivers/gpio/gpio-nct6694.c
- drivers/gpio/gpiolib-kunit.c
- drivers/gpio/gpiolib-shared.c
- drivers/gpio/gpiolib.c
- include/linux/sched/user.h
- kernel/events/core.c
- tools/gpio/Makefile
modified arch/x86/events/intel/uncore_snbep.c
| @@ -474,6 +474,9 @@ | |||
| 474 | 474 | /* DMR */ | |
| 475 | 475 | #define DMR_IMH1_HIOP_MMIO_BASE 0x1ffff6ae7000 | |
| 476 | 476 | #define DMR_HIOP_MMIO_SIZE 0x8000 | |
| 477 | + | #define DMR_CXLCM_PMON_CTL_INVERT BIT(21) | |
| 478 | + | #define DMR_CXLCM_PMON_RAW_EVENT_MASK (GENERIC_PMON_RAW_EVENT_MASK | \ | |
| 479 | + | DMR_CXLCM_PMON_CTL_INVERT) | |
| 477 | 480 | #define DMR_CXLCM_EVENT_MASK_EXT 0xf | |
| 478 | 481 | #define DMR_HAMVF_EVENT_MASK_EXT 0xffffffff | |
| 479 | 482 | #define DMR_PCIE4_EVENT_MASK_EXT 0xffffff | |
| @@ -6696,7 +6699,7 @@ static struct event_constraint dmr_uncore_cxlcm_constraints[] = { | |||
| 6696 | 6699 | ||
| 6697 | 6700 | static struct intel_uncore_type dmr_uncore_cxlcm = { | |
| 6698 | 6701 | .name = "cxlcm", | |
| 6699 | - | .event_mask = GENERIC_PMON_RAW_EVENT_MASK, | |
| 6702 | + | .event_mask = DMR_CXLCM_PMON_RAW_EVENT_MASK, | |
| 6700 | 6703 | .event_mask_ext = DMR_CXLCM_EVENT_MASK_EXT, | |
| 6701 | 6704 | .constraints = dmr_uncore_cxlcm_constraints, | |
| 6702 | 6705 | .format_group = &dmr_cxlcm_uncore_format_group, | |
modified drivers/gpio/gpio-mxc.c
| @@ -7,7 +7,6 @@ | |||
| 7 | 7 | // Authors: Daniel Mack, Juergen Beisert. | |
| 8 | 8 | // Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved. | |
| 9 | 9 | ||
| 10 | - | #include <linux/bitops.h> | |
| 11 | 10 | #include <linux/cleanup.h> | |
| 12 | 11 | #include <linux/clk.h> | |
| 13 | 12 | #include <linux/err.h> | |
| @@ -69,11 +68,11 @@ struct mxc_gpio_port { | |||
| 69 | 68 | struct irq_domain *domain; | |
| 70 | 69 | struct gpio_generic_chip gen_gc; | |
| 71 | 70 | struct device *dev; | |
| 72 | - | unsigned long both_edges; | |
| 71 | + | u32 both_edges; | |
| 73 | 72 | struct mxc_gpio_reg_saved gpio_saved_reg; | |
| 74 | 73 | bool power_off; | |
| 74 | + | u32 wakeup_pads; | |
| 75 | 75 | bool is_pad_wakeup; | |
| 76 | - | unsigned long wakeup_pads; | |
| 77 | 76 | u32 pad_type[32]; | |
| 78 | 77 | const struct mxc_gpio_hwdata *hwdata; | |
| 79 | 78 | }; | |
| @@ -169,7 +168,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type) | |||
| 169 | 168 | int edge; | |
| 170 | 169 | void __iomem *reg = port->base; | |
| 171 | 170 | ||
| 172 | - | clear_bit(gpio_idx, &port->both_edges); | |
| 171 | + | port->both_edges &= ~(1 << gpio_idx); | |
| 173 | 172 | switch (type) { | |
| 174 | 173 | case IRQ_TYPE_EDGE_RISING: | |
| 175 | 174 | edge = GPIO_INT_RISE_EDGE; | |
| @@ -189,7 +188,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type) | |||
| 189 | 188 | edge = GPIO_INT_HIGH_LEV; | |
| 190 | 189 | pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx); | |
| 191 | 190 | } | |
| 192 | - | set_bit(gpio_idx, &port->both_edges); | |
| 191 | + | port->both_edges |= 1 << gpio_idx; | |
| 193 | 192 | } | |
| 194 | 193 | break; | |
| 195 | 194 | case IRQ_TYPE_LEVEL_LOW: | |
| @@ -260,7 +259,7 @@ static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat) | |||
| 260 | 259 | while (irq_stat != 0) { | |
| 261 | 260 | int irqoffset = fls(irq_stat) - 1; | |
| 262 | 261 | ||
| 263 | - | if (test_bit(irqoffset, &port->both_edges)) | |
| 262 | + | if (port->both_edges & (1 << irqoffset)) | |
| 264 | 263 | mxc_flip_edge(port, irqoffset); | |
| 265 | 264 | ||
| 266 | 265 | generic_handle_domain_irq(port->domain, irqoffset); | |
| @@ -331,18 +330,16 @@ static int gpio_set_wake_irq(struct irq_data *d, u32 enable) | |||
| 331 | 330 | ret = enable_irq_wake(port->irq_high); | |
| 332 | 331 | else | |
| 333 | 332 | ret = enable_irq_wake(port->irq); | |
| 333 | + | port->wakeup_pads |= BIT(gpio_idx); | |
| 334 | 334 | } else { | |
| 335 | 335 | if (port->irq_high && (gpio_idx >= 16)) | |
| 336 | 336 | ret = disable_irq_wake(port->irq_high); | |
| 337 | 337 | else | |
| 338 | 338 | ret = disable_irq_wake(port->irq); | |
| 339 | + | port->wakeup_pads &= ~BIT(gpio_idx); | |
| 339 | 340 | } | |
| 340 | - | if (ret) | |
| 341 | - | return ret; | |
| 342 | - | ||
| 343 | - | assign_bit(gpio_idx, &port->wakeup_pads, enable); | |
| 344 | 341 | ||
| 345 | - | return 0; | |
| 342 | + | return ret; | |
| 346 | 343 | } | |
| 347 | 344 | ||
| 348 | 345 | static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base) | |
| @@ -477,6 +474,8 @@ static int mxc_gpio_probe(struct platform_device *pdev) | |||
| 477 | 474 | } else | |
| 478 | 475 | port->mx_irq_handler = mx3_gpio_irq_handler; | |
| 479 | 476 | ||
| 477 | + | mxc_update_irq_chained_handler(port, true); | |
| 478 | + | ||
| 480 | 479 | config.dev = &pdev->dev; | |
| 481 | 480 | config.sz = 4; | |
| 482 | 481 | config.dat = port->base + GPIO_PSR; | |
| @@ -500,6 +499,10 @@ static int mxc_gpio_probe(struct platform_device *pdev) | |||
| 500 | 499 | else /* silence boot time warning */ | |
| 501 | 500 | port->gen_gc.gc.base = -1; | |
| 502 | 501 | ||
| 502 | + | err = devm_gpiochip_add_data(&pdev->dev, &port->gen_gc.gc, port); | |
| 503 | + | if (err) | |
| 504 | + | goto out_bgio; | |
| 505 | + | ||
| 503 | 506 | irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id()); | |
| 504 | 507 | if (irq_base < 0) { | |
| 505 | 508 | err = irq_base; | |
| @@ -520,19 +523,8 @@ static int mxc_gpio_probe(struct platform_device *pdev) | |||
| 520 | 523 | if (err < 0) | |
| 521 | 524 | goto out_irqdomain_remove; | |
| 522 | 525 | ||
| 523 | - | /* | |
| 524 | - | * Register the GPIO chip last: devm_gpiochip_add_data() makes the | |
| 525 | - | * chip visible to consumers immediately, so the IRQ domain and | |
| 526 | - | * generic irq chip must be ready before any to_irq() call. | |
| 527 | - | */ | |
| 528 | - | err = devm_gpiochip_add_data(&pdev->dev, &port->gen_gc.gc, port); | |
| 529 | - | if (err) | |
| 530 | - | goto out_irqdomain_remove; | |
| 531 | - | ||
| 532 | 526 | list_add_tail(&port->node, &mxc_gpio_ports); | |
| 533 | 527 | ||
| 534 | - | mxc_update_irq_chained_handler(port, true); | |
| 535 | - | ||
| 536 | 528 | platform_set_drvdata(pdev, port); | |
| 537 | 529 | pm_runtime_put_autosuspend(&pdev->dev); | |
| 538 | 530 | ||
modified drivers/gpio/gpio-nct6694.c
| @@ -444,7 +444,7 @@ static int nct6694_gpio_probe(struct platform_device *pdev) | |||
| 444 | 444 | data->gpio.set_config = nct6694_set_config; | |
| 445 | 445 | data->gpio.init_valid_mask = nct6694_init_valid_mask; | |
| 446 | 446 | data->gpio.base = -1; | |
| 447 | - | data->gpio.can_sleep = true; | |
| 447 | + | data->gpio.can_sleep = false; | |
| 448 | 448 | data->gpio.owner = THIS_MODULE; | |
| 449 | 449 | data->gpio.ngpio = NCT6694_NR_GPIO; | |
| 450 | 450 | ||
modified drivers/gpio/gpiolib-kunit.c
| @@ -28,8 +28,6 @@ | |||
| 28 | 28 | ||
| 29 | 29 | #define GPIO_TEST_PROVIDER_NGPIO 4 | |
| 30 | 30 | ||
| 31 | - | KUNIT_DEFINE_ACTION_WRAPPER(gpio_device_put_wrapper, gpio_device_put, struct gpio_device *); | |
| 32 | - | ||
| 33 | 31 | /* | |
| 34 | 32 | * The test provider tracks per-line direction and value so that lines can be | |
| 35 | 33 | * driven as both inputs and outputs - this is needed to exercise input as well | |
| @@ -772,7 +770,6 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset, | |||
| 772 | 770 | struct property_entry properties[2]; | |
| 773 | 771 | struct platform_device *pdev; | |
| 774 | 772 | struct fwnode_handle *fwnode; | |
| 775 | - | struct gpio_device *gdev; | |
| 776 | 773 | struct gpio_desc *desc; | |
| 777 | 774 | bool bound = true; | |
| 778 | 775 | int ret; | |
| @@ -792,12 +789,10 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset, | |||
| 792 | 789 | wait_for_device_probe(); | |
| 793 | 790 | ||
| 794 | 791 | /* The hog must have configured the line with the expected direction. */ | |
| 795 | - | gdev = gpio_device_find_by_label(GPIO_CONSUMER_NAME); | |
| 792 | + | struct gpio_device *gdev __free(gpio_device_put) = | |
| 793 | + | gpio_device_find_by_label(GPIO_CONSUMER_NAME); | |
| 796 | 794 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gdev); | |
| 797 | 795 | ||
| 798 | - | ret = kunit_add_action_or_reset(test, gpio_device_put_wrapper, gdev); | |
| 799 | - | KUNIT_ASSERT_EQ(test, ret, 0); | |
| 800 | - | ||
| 801 | 796 | desc = gpio_device_get_desc(gdev, offset); | |
| 802 | 797 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc); | |
| 803 | 798 | ||
modified drivers/gpio/gpiolib.c
| @@ -3954,17 +3954,16 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep, | |||
| 3954 | 3954 | } while ((i < array_size) && | |
| 3955 | 3955 | gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); | |
| 3956 | 3956 | /* push collected bits to outputs */ | |
| 3957 | - | ret = 0; | |
| 3958 | - | if (count != 0) | |
| 3957 | + | if (count != 0) { | |
| 3959 | 3958 | ret = gpiochip_set_multiple(guard.gc, mask, bits); | |
| 3959 | + | if (ret) | |
| 3960 | + | return ret; | |
| 3961 | + | } | |
| 3960 | 3962 | ||
| 3961 | 3963 | if (mask != fastpath_mask) | |
| 3962 | 3964 | bitmap_free(mask); | |
| 3963 | 3965 | if (bits != fastpath_bits) | |
| 3964 | 3966 | bitmap_free(bits); | |
| 3965 | - | ||
| 3966 | - | if (ret) | |
| 3967 | - | return ret; | |
| 3968 | 3967 | } | |
| 3969 | 3968 | return 0; | |
| 3970 | 3969 | } | |
modified include/linux/sched/user.h
| @@ -23,11 +23,13 @@ struct user_struct { | |||
| 23 | 23 | struct hlist_node uidhash_node; | |
| 24 | 24 | kuid_t uid; | |
| 25 | 25 | ||
| 26 | - | #if defined(CONFIG_PERF_EVENTS) || defined(CONFIG_BPF_SYSCALL) || \ | |
| 27 | - | defined(CONFIG_NET) || defined(CONFIG_IO_URING) || \ | |
| 26 | + | #if defined(CONFIG_NET) || defined(CONFIG_IO_URING) || \ | |
| 28 | 27 | defined(CONFIG_VFIO_PCI_ZDEV_KVM) || IS_ENABLED(CONFIG_IOMMUFD) || \ | |
| 29 | 28 | defined(CONFIG_SECRETMEM) | |
| 30 | - | atomic_long_t locked_vm; | |
| 29 | + | atomic_long_t locked_vm; /* pinned pages charged to RLIMIT_MEMLOCK */ | |
| 30 | + | #endif | |
| 31 | + | #ifdef CONFIG_PERF_EVENTS | |
| 32 | + | atomic_long_t perf_mlock; /* perf buffer pages within perf_event_mlock_kb */ | |
| 31 | 33 | #endif | |
| 32 | 34 | #ifdef CONFIG_WATCH_QUEUE | |
| 33 | 35 | atomic_t nr_watches; /* The number of watches this user currently has */ | |
modified kernel/events/core.c
| @@ -4639,6 +4639,7 @@ static bool perf_rotate_context(struct perf_cpu_pmu_context *cpc) | |||
| 4639 | 4639 | ||
| 4640 | 4640 | if (cpu_event) { | |
| 4641 | 4641 | update_context_time(&cpuctx->ctx); | |
| 4642 | + | update_cgrp_time_from_cpuctx(cpuctx, false); | |
| 4642 | 4643 | __pmu_ctx_sched_out(cpu_epc, EVENT_FLEXIBLE); | |
| 4643 | 4644 | rotate_ctx(&cpuctx->ctx, cpu_event); | |
| 4644 | 4645 | __pmu_ctx_sched_in(cpu_epc, EVENT_FLEXIBLE); | |
| @@ -7059,7 +7060,7 @@ static void perf_mmap_close(struct vm_area_struct *vma) | |||
| 7059 | 7060 | perf_pmu_output_stop(event); | |
| 7060 | 7061 | ||
| 7061 | 7062 | /* now it's safe to free the pages */ | |
| 7062 | - | atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); | |
| 7063 | + | atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->perf_mlock); | |
| 7063 | 7064 | atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); | |
| 7064 | 7065 | ||
| 7065 | 7066 | /* this has to be the last one */ | |
| @@ -7242,11 +7243,11 @@ static bool perf_mmap_calc_limits(struct vm_area_struct *vma, long *user_extra, | |||
| 7242 | 7243 | /* Increase the limit linearly with more CPUs */ | |
| 7243 | 7244 | user_lock_limit *= num_online_cpus(); | |
| 7244 | 7245 | ||
| 7245 | - | user_locked = atomic_long_read(&user->locked_vm); | |
| 7246 | + | user_locked = atomic_long_read(&user->perf_mlock); | |
| 7246 | 7247 | ||
| 7247 | 7248 | /* | |
| 7248 | 7249 | * sysctl_perf_event_mlock may have changed, so that | |
| 7249 | - | * user->locked_vm > user_lock_limit | |
| 7250 | + | * user->perf_mlock > user_lock_limit | |
| 7250 | 7251 | */ | |
| 7251 | 7252 | if (user_locked > user_lock_limit) | |
| 7252 | 7253 | user_locked = user_lock_limit; | |
| @@ -7254,7 +7255,7 @@ static bool perf_mmap_calc_limits(struct vm_area_struct *vma, long *user_extra, | |||
| 7254 | 7255 | ||
| 7255 | 7256 | if (user_locked > user_lock_limit) { | |
| 7256 | 7257 | /* | |
| 7257 | - | * charge locked_vm until it hits user_lock_limit; | |
| 7258 | + | * charge perf_mlock until it hits user_lock_limit; | |
| 7258 | 7259 | * charge the rest from pinned_vm | |
| 7259 | 7260 | */ | |
| 7260 | 7261 | *extra = user_locked - user_lock_limit; | |
| @@ -7272,7 +7273,7 @@ static void perf_mmap_account(struct vm_area_struct *vma, long user_extra, long | |||
| 7272 | 7273 | { | |
| 7273 | 7274 | struct user_struct *user = current_user(); | |
| 7274 | 7275 | ||
| 7275 | - | atomic_long_add(user_extra, &user->locked_vm); | |
| 7276 | + | atomic_long_add(user_extra, &user->perf_mlock); | |
| 7276 | 7277 | atomic64_add(extra, &vma->vm_mm->pinned_vm); | |
| 7277 | 7278 | } | |
| 7278 | 7279 | ||
| @@ -7281,7 +7282,7 @@ static void perf_mmap_unaccount(struct vm_area_struct *vma, struct perf_buffer * | |||
| 7281 | 7282 | struct user_struct *user = rb->mmap_user; | |
| 7282 | 7283 | ||
| 7283 | 7284 | atomic_long_sub((perf_data_size(rb) >> PAGE_SHIFT) + 1 - rb->mmap_locked, | |
| 7284 | - | &user->locked_vm); | |
| 7285 | + | &user->perf_mlock); | |
| 7285 | 7286 | atomic64_sub(rb->mmap_locked, &vma->vm_mm->pinned_vm); | |
| 7286 | 7287 | } | |
| 7287 | 7288 | ||
| @@ -14394,23 +14395,18 @@ err: | |||
| 14394 | 14395 | } | |
| 14395 | 14396 | EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); | |
| 14396 | 14397 | ||
| 14397 | - | static void __perf_pmu_remove(struct perf_event_context *ctx, | |
| 14398 | - | int cpu, struct pmu *pmu, | |
| 14399 | - | struct perf_event_groups *groups, | |
| 14398 | + | static void __perf_pmu_remove(struct perf_event_context *ctx, struct pmu *pmu, | |
| 14400 | 14399 | struct list_head *events) | |
| 14401 | 14400 | { | |
| 14402 | - | struct perf_event *event, *sibling; | |
| 14401 | + | struct perf_event *event, *tmp; | |
| 14402 | + | ||
| 14403 | + | list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) { | |
| 14404 | + | if (event->pmu_ctx->pmu != pmu) | |
| 14405 | + | continue; | |
| 14403 | 14406 | ||
| 14404 | - | perf_event_groups_for_cpu_pmu(event, groups, cpu, pmu) { | |
| 14405 | 14407 | perf_remove_from_context(event, 0); | |
| 14406 | 14408 | put_pmu_ctx(event->pmu_ctx); | |
| 14407 | 14409 | list_add(&event->migrate_entry, events); | |
| 14408 | - | ||
| 14409 | - | for_each_sibling_event(sibling, event) { | |
| 14410 | - | perf_remove_from_context(sibling, 0); | |
| 14411 | - | put_pmu_ctx(sibling->pmu_ctx); | |
| 14412 | - | list_add(&sibling->migrate_entry, events); | |
| 14413 | - | } | |
| 14414 | 14410 | } | |
| 14415 | 14411 | } | |
| 14416 | 14412 | ||
| @@ -14486,8 +14482,7 @@ void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) | |||
| 14486 | 14482 | */ | |
| 14487 | 14483 | mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); | |
| 14488 | 14484 | ||
| 14489 | - | __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->pinned_groups, &events); | |
| 14490 | - | __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->flexible_groups, &events); | |
| 14485 | + | __perf_pmu_remove(src_ctx, pmu, &events); | |
| 14491 | 14486 | ||
| 14492 | 14487 | if (!list_empty(&events)) { | |
| 14493 | 14488 | /* | |
modified tools/gpio/Makefile
| @@ -7,13 +7,7 @@ bindir ?= /usr/bin | |||
| 7 | 7 | # isn't set and when invoked from selftests build, where srctree | |
| 8 | 8 | # is set to ".". building_out_of_srctree is undefined for in srctree | |
| 9 | 9 | # builds | |
| 10 | - | ifeq ($(srctree),) | |
| 11 | - | update_srctree := 1 | |
| 12 | - | endif | |
| 13 | 10 | ifndef building_out_of_srctree | |
| 14 | - | update_srctree := 1 | |
| 15 | - | endif | |
| 16 | - | ifeq ($(update_srctree),1) | |
| 17 | 11 | srctree := $(patsubst %/,%,$(dir $(CURDIR))) | |
| 18 | 12 | srctree := $(patsubst %/,%,$(dir $(srctree))) | |
| 19 | 13 | endif | |