Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull core timer updates from Thomas Gleixner:
"Timers and timekeeping updates:

- A large overhaul of the posix CPU timer code which is a preparation
for moving the CPU timer expiry out into task work so it can be
properly accounted on the task/process.

An update to the bogus permission checks will come later during the
merge window as feedback was not complete before heading of for
travel.

- Switch the timerqueue code to use cached rbtrees and get rid of the
homebrewn caching of the leftmost node.

- Consolidate hrtimer_init() + hrtimer_init_sleeper() calls into a
single function

- Implement the separation of hrtimers to be forced to expire in hard
interrupt context even when PREEMPT_RT is enabled and mark the
affected timers accordingly.

- Implement a mechanism for hrtimers and the timer wheel to protect
RT against priority inversion and live lock issues when a (hr)timer
which should be canceled is currently executing the callback.
Instead of infinitely spinning, the task which tries to cancel the
timer blocks on a per cpu base expiry lock which is held and
released by the (hr)timer expiry code.

- Enable the Hyper-V TSC page based sched_clock for Hyper-V guests
resulting in faster access to timekeeping functions.

- Updates to various clocksource/clockevent drivers and their device
tree bindings.

- The usual small improvements all over the place"

* 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (101 commits)
posix-cpu-timers: Fix permission check regression
posix-cpu-timers: Always clear head pointer on dequeue
hrtimer: Add a missing bracket and hide `migration_base' on !SMP
posix-cpu-timers: Make expiry_active check actually work correctly
posix-timers: Unbreak CONFIG_POSIX_TIMERS=n build
tick: Mark sched_timer to expire in hard interrupt context
hrtimer: Add kernel doc annotation for HRTIMER_MODE_HARD
x86/hyperv: Hide pv_ops access for CONFIG_PARAVIRT=n
posix-cpu-timers: Utilize timerqueue for storage
posix-cpu-timers: Move state tracking to struct posix_cputimers
posix-cpu-timers: Deduplicate rlimit handling
posix-cpu-timers: Remove pointless comparisons
posix-cpu-timers: Get rid of 64bit divisions
posix-cpu-timers: Consolidate timer expiry further
posix-cpu-timers: Get rid of zero checks
rlimit: Rewrite non-sensical RLIMIT_CPU comment
posix-cpu-timers: Respect INFINITY for hard RTTIME limit
posix-cpu-timers: Switch thread group sampling to array
posix-cpu-timers: Restructure expiry array
posix-cpu-timers: Remove cputime_expires
...

+1484 -903
+102
Documentation/devicetree/bindings/timer/allwinner,sun4i-a10-timer.yaml
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/timer/allwinner,sun4i-a10-timer.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Allwinner A10 Timer Device Tree Bindings 8 + 9 + maintainers: 10 + - Chen-Yu Tsai <wens@csie.org> 11 + - Maxime Ripard <maxime.ripard@bootlin.com> 12 + 13 + properties: 14 + compatible: 15 + enum: 16 + - allwinner,sun4i-a10-timer 17 + - allwinner,sun8i-a23-timer 18 + - allwinner,sun8i-v3s-timer 19 + - allwinner,suniv-f1c100s-timer 20 + 21 + reg: 22 + maxItems: 1 23 + 24 + interrupts: 25 + description: 26 + List of timers interrupts 27 + 28 + clocks: 29 + maxItems: 1 30 + 31 + allOf: 32 + - if: 33 + properties: 34 + compatible: 35 + items: 36 + const: allwinner,sun4i-a10-timer 37 + 38 + then: 39 + properties: 40 + interrupts: 41 + minItems: 6 42 + maxItems: 6 43 + 44 + - if: 45 + properties: 46 + compatible: 47 + items: 48 + const: allwinner,sun8i-a23-timer 49 + 50 + then: 51 + properties: 52 + interrupts: 53 + minItems: 2 54 + maxItems: 2 55 + 56 + - if: 57 + properties: 58 + compatible: 59 + items: 60 + const: allwinner,sun8i-v3s-timer 61 + 62 + then: 63 + properties: 64 + interrupts: 65 + minItems: 3 66 + maxItems: 3 67 + 68 + - if: 69 + properties: 70 + compatible: 71 + items: 72 + const: allwinner,suniv-f1c100s-timer 73 + 74 + then: 75 + properties: 76 + interrupts: 77 + minItems: 3 78 + maxItems: 3 79 + 80 + required: 81 + - compatible 82 + - reg 83 + - interrupts 84 + - clocks 85 + 86 + additionalProperties: false 87 + 88 + examples: 89 + - | 90 + timer { 91 + compatible = "allwinner,sun4i-a10-timer"; 92 + reg = <0x01c20c00 0x400>; 93 + interrupts = <22>, 94 + <23>, 95 + <24>, 96 + <25>, 97 + <67>, 98 + <68>; 99 + clocks = <&osc>; 100 + }; 101 + 102 + ...
-19
Documentation/devicetree/bindings/timer/allwinner,sun4i-timer.txt
··· 1 - Allwinner A1X SoCs Timer Controller 2 - 3 - Required properties: 4 - 5 - - compatible : should be one of the following: 6 - "allwinner,sun4i-a10-timer" 7 - "allwinner,suniv-f1c100s-timer" 8 - - reg : Specifies base physical address and size of the registers. 9 - - interrupts : The interrupt of the first timer 10 - - clocks: phandle to the source clock (usually a 24 MHz fixed clock) 11 - 12 - Example: 13 - 14 - timer { 15 - compatible = "allwinner,sun4i-a10-timer"; 16 - reg = <0x01c20c00 0x400>; 17 - interrupts = <22>; 18 - clocks = <&osc>; 19 - };
-26
Documentation/devicetree/bindings/timer/allwinner,sun5i-a13-hstimer.txt
··· 1 - Allwinner SoCs High Speed Timer Controller 2 - 3 - Required properties: 4 - 5 - - compatible : should be "allwinner,sun5i-a13-hstimer" or 6 - "allwinner,sun7i-a20-hstimer" 7 - - reg : Specifies base physical address and size of the registers. 8 - - interrupts : The interrupts of these timers (2 for the sun5i IP, 4 for the sun7i 9 - one) 10 - - clocks: phandle to the source clock (usually the AHB clock) 11 - 12 - Optional properties: 13 - - resets: phandle to a reset controller asserting the timer 14 - 15 - Example: 16 - 17 - timer@1c60000 { 18 - compatible = "allwinner,sun7i-a20-hstimer"; 19 - reg = <0x01c60000 0x1000>; 20 - interrupts = <0 51 1>, 21 - <0 52 1>, 22 - <0 53 1>, 23 - <0 54 1>; 24 - clocks = <&ahb1_gates 19>; 25 - resets = <&ahb1rst 19>; 26 - };
+79
Documentation/devicetree/bindings/timer/allwinner,sun5i-a13-hstimer.yaml
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/timer/allwinner,sun5i-a13-hstimer.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Allwinner A13 High-Speed Timer Device Tree Bindings 8 + 9 + maintainers: 10 + - Chen-Yu Tsai <wens@csie.org> 11 + - Maxime Ripard <maxime.ripard@bootlin.com> 12 + 13 + properties: 14 + compatible: 15 + oneOf: 16 + - const: allwinner,sun5i-a13-hstimer 17 + - const: allwinner,sun7i-a20-hstimer 18 + - items: 19 + - const: allwinner,sun6i-a31-hstimer 20 + - const: allwinner,sun7i-a20-hstimer 21 + 22 + reg: 23 + maxItems: 1 24 + 25 + interrupts: 26 + minItems: 2 27 + maxItems: 4 28 + items: 29 + - description: Timer 0 Interrupt 30 + - description: Timer 1 Interrupt 31 + - description: Timer 2 Interrupt 32 + - description: Timer 3 Interrupt 33 + 34 + clocks: 35 + maxItems: 1 36 + 37 + resets: 38 + maxItems: 1 39 + 40 + required: 41 + - compatible 42 + - reg 43 + - interrupts 44 + - clocks 45 + 46 + if: 47 + properties: 48 + compatible: 49 + items: 50 + const: allwinner,sun5i-a13-hstimer 51 + 52 + then: 53 + properties: 54 + interrupts: 55 + minItems: 2 56 + maxItems: 2 57 + 58 + else: 59 + properties: 60 + interrupts: 61 + minItems: 4 62 + maxItems: 4 63 + 64 + additionalProperties: false 65 + 66 + examples: 67 + - | 68 + timer@1c60000 { 69 + compatible = "allwinner,sun7i-a20-hstimer"; 70 + reg = <0x01c60000 0x1000>; 71 + interrupts = <0 51 1>, 72 + <0 52 1>, 73 + <0 53 1>, 74 + <0 54 1>; 75 + clocks = <&ahb1_gates 19>; 76 + resets = <&ahb1rst 19>; 77 + }; 78 + 79 + ...
+23 -17
Documentation/devicetree/bindings/timer/renesas,cmt.txt
··· 12 12 Required Properties: 13 13 14 14 - compatible: must contain one or more of the following: 15 - - "renesas,cmt-48-sh73a0" for the sh73A0 48-bit CMT 16 - (CMT1) 17 - - "renesas,cmt-48-r8a7740" for the r8a7740 48-bit CMT 18 - (CMT1) 19 - - "renesas,cmt-48" for all non-second generation 48-bit CMT 20 - (CMT1 on sh73a0 and r8a7740) 21 - This is a fallback for the above renesas,cmt-48-* entries. 22 - 23 15 - "renesas,r8a73a4-cmt0" for the 32-bit CMT0 device included in r8a73a4. 24 16 - "renesas,r8a73a4-cmt1" for the 48-bit CMT1 device included in r8a73a4. 17 + - "renesas,r8a7740-cmt0" for the 32-bit CMT0 device included in r8a7740. 18 + - "renesas,r8a7740-cmt1" for the 48-bit CMT1 device included in r8a7740. 19 + - "renesas,r8a7740-cmt2" for the 32-bit CMT2 device included in r8a7740. 20 + - "renesas,r8a7740-cmt3" for the 32-bit CMT3 device included in r8a7740. 21 + - "renesas,r8a7740-cmt4" for the 32-bit CMT4 device included in r8a7740. 25 22 - "renesas,r8a7743-cmt0" for the 32-bit CMT0 device included in r8a7743. 26 23 - "renesas,r8a7743-cmt1" for the 48-bit CMT1 device included in r8a7743. 27 24 - "renesas,r8a7744-cmt0" for the 32-bit CMT0 device included in r8a7744. ··· 28 31 - "renesas,r8a77470-cmt0" for the 32-bit CMT0 device included in r8a77470. 29 32 - "renesas,r8a77470-cmt1" for the 48-bit CMT1 device included in r8a77470. 30 33 - "renesas,r8a774a1-cmt0" for the 32-bit CMT0 device included in r8a774a1. 31 - - "renesas,r8a774a1-cmt1" for the 48-bit CMT1 device included in r8a774a1. 34 + - "renesas,r8a774a1-cmt1" for the 48-bit CMT devices included in r8a774a1. 32 35 - "renesas,r8a774c0-cmt0" for the 32-bit CMT0 device included in r8a774c0. 33 - - "renesas,r8a774c0-cmt1" for the 48-bit CMT1 device included in r8a774c0. 36 + - "renesas,r8a774c0-cmt1" for the 48-bit CMT devices included in r8a774c0. 34 37 - "renesas,r8a7790-cmt0" for the 32-bit CMT0 device included in r8a7790. 35 38 - "renesas,r8a7790-cmt1" for the 48-bit CMT1 device included in r8a7790. 36 39 - "renesas,r8a7791-cmt0" for the 32-bit CMT0 device included in r8a7791. 37 40 - "renesas,r8a7791-cmt1" for the 48-bit CMT1 device included in r8a7791. 41 + - "renesas,r8a7792-cmt0" for the 32-bit CMT0 device included in r8a7792. 42 + - "renesas,r8a7792-cmt1" for the 48-bit CMT1 device included in r8a7792. 38 43 - "renesas,r8a7793-cmt0" for the 32-bit CMT0 device included in r8a7793. 39 44 - "renesas,r8a7793-cmt1" for the 48-bit CMT1 device included in r8a7793. 40 45 - "renesas,r8a7794-cmt0" for the 32-bit CMT0 device included in r8a7794. 41 46 - "renesas,r8a7794-cmt1" for the 48-bit CMT1 device included in r8a7794. 42 47 - "renesas,r8a7795-cmt0" for the 32-bit CMT0 device included in r8a7795. 43 - - "renesas,r8a7795-cmt1" for the 48-bit CMT1 device included in r8a7795. 48 + - "renesas,r8a7795-cmt1" for the 48-bit CMT devices included in r8a7795. 44 49 - "renesas,r8a7796-cmt0" for the 32-bit CMT0 device included in r8a7796. 45 - - "renesas,r8a7796-cmt1" for the 48-bit CMT1 device included in r8a7796. 50 + - "renesas,r8a7796-cmt1" for the 48-bit CMT devices included in r8a7796. 46 51 - "renesas,r8a77965-cmt0" for the 32-bit CMT0 device included in r8a77965. 47 - - "renesas,r8a77965-cmt1" for the 48-bit CMT1 device included in r8a77965. 52 + - "renesas,r8a77965-cmt1" for the 48-bit CMT devices included in r8a77965. 48 53 - "renesas,r8a77970-cmt0" for the 32-bit CMT0 device included in r8a77970. 49 - - "renesas,r8a77970-cmt1" for the 48-bit CMT1 device included in r8a77970. 54 + - "renesas,r8a77970-cmt1" for the 48-bit CMT devices included in r8a77970. 50 55 - "renesas,r8a77980-cmt0" for the 32-bit CMT0 device included in r8a77980. 51 - - "renesas,r8a77980-cmt1" for the 48-bit CMT1 device included in r8a77980. 56 + - "renesas,r8a77980-cmt1" for the 48-bit CMT devices included in r8a77980. 52 57 - "renesas,r8a77990-cmt0" for the 32-bit CMT0 device included in r8a77990. 53 - - "renesas,r8a77990-cmt1" for the 48-bit CMT1 device included in r8a77990. 58 + - "renesas,r8a77990-cmt1" for the 48-bit CMT devices included in r8a77990. 59 + - "renesas,r8a77995-cmt0" for the 32-bit CMT0 device included in r8a77995. 60 + - "renesas,r8a77995-cmt1" for the 48-bit CMT devices included in r8a77995. 61 + - "renesas,sh73a0-cmt0" for the 32-bit CMT0 device included in sh73a0. 62 + - "renesas,sh73a0-cmt1" for the 48-bit CMT1 device included in sh73a0. 63 + - "renesas,sh73a0-cmt2" for the 32-bit CMT2 device included in sh73a0. 64 + - "renesas,sh73a0-cmt3" for the 32-bit CMT3 device included in sh73a0. 65 + - "renesas,sh73a0-cmt4" for the 32-bit CMT4 device included in sh73a0. 54 66 55 67 - "renesas,rcar-gen2-cmt0" for 32-bit CMT0 devices included in R-Car Gen2 56 68 and RZ/G1. ··· 69 63 listed above. 70 64 - "renesas,rcar-gen3-cmt0" for 32-bit CMT0 devices included in R-Car Gen3 71 65 and RZ/G2. 72 - - "renesas,rcar-gen3-cmt1" for 48-bit CMT1 devices included in R-Car Gen3 66 + - "renesas,rcar-gen3-cmt1" for 48-bit CMT devices included in R-Car Gen3 73 67 and RZ/G2. 74 68 These are fallbacks for R-Car Gen3 and RZ/G2 entries listed 75 69 above.
+8
arch/arm64/boot/dts/freescale/imx8mm.dtsi
··· 546 546 #pwm-cells = <2>; 547 547 status = "disabled"; 548 548 }; 549 + 550 + system_counter: timer@306a0000 { 551 + compatible = "nxp,sysctr-timer"; 552 + reg = <0x306a0000 0x20000>; 553 + interrupts = <GIC_SPI 47 IRQ_TYPE_LEVEL_HIGH>; 554 + clocks = <&osc_24m>; 555 + clock-names = "per"; 556 + }; 549 557 }; 550 558 551 559 aips3: bus@30800000 {
+8
arch/arm64/boot/dts/freescale/imx8mq.dtsi
··· 651 651 #pwm-cells = <2>; 652 652 status = "disabled"; 653 653 }; 654 + 655 + system_counter: timer@306a0000 { 656 + compatible = "nxp,sysctr-timer"; 657 + reg = <0x306a0000 0x20000>; 658 + interrupts = <GIC_SPI 47 IRQ_TYPE_LEVEL_HIGH>; 659 + clocks = <&osc_25m>; 660 + clock-names = "per"; 661 + }; 654 662 }; 655 663 656 664 bus@30800000 { /* AIPS3 */
+1 -1
arch/x86/entry/vdso/vma.c
··· 122 122 123 123 if (tsc_pg && vclock_was_used(VCLOCK_HVCLOCK)) 124 124 return vmf_insert_pfn(vma, vmf->address, 125 - vmalloc_to_pfn(tsc_pg)); 125 + virt_to_phys(tsc_pg) >> PAGE_SHIFT); 126 126 } 127 127 128 128 return VM_FAULT_SIGBUS;
-2
arch/x86/hyperv/hv_init.c
··· 315 315 316 316 x86_init.pci.arch_init = hv_pci_init; 317 317 318 - /* Register Hyper-V specific clocksource */ 319 - hv_init_clocksource(); 320 318 return; 321 319 322 320 remove_cpuhp_state:
+3 -3
arch/x86/include/asm/vdso/gettimeofday.h
··· 51 51 __attribute__((visibility("hidden"))); 52 52 #endif 53 53 54 - #ifdef CONFIG_HYPERV_TSCPAGE 54 + #ifdef CONFIG_HYPERV_TIMER 55 55 extern struct ms_hyperv_tsc_page hvclock_page 56 56 __attribute__((visibility("hidden"))); 57 57 #endif ··· 228 228 } 229 229 #endif 230 230 231 - #ifdef CONFIG_HYPERV_TSCPAGE 231 + #ifdef CONFIG_HYPERV_TIMER 232 232 static u64 vread_hvclock(void) 233 233 { 234 234 return hv_read_tsc_page(&hvclock_page); ··· 251 251 return vread_pvclock(); 252 252 } 253 253 #endif 254 - #ifdef CONFIG_HYPERV_TSCPAGE 254 + #ifdef CONFIG_HYPERV_TIMER 255 255 if (clock_mode == VCLOCK_HVCLOCK) { 256 256 barrier(); 257 257 return vread_hvclock();
+10
arch/x86/kernel/cpu/mshyperv.c
··· 29 29 #include <asm/timer.h> 30 30 #include <asm/reboot.h> 31 31 #include <asm/nmi.h> 32 + #include <clocksource/hyperv_timer.h> 32 33 33 34 struct ms_hyperv_info ms_hyperv; 34 35 EXPORT_SYMBOL_GPL(ms_hyperv); ··· 339 338 x2apic_phys = 1; 340 339 # endif 341 340 341 + /* Register Hyper-V specific clocksource */ 342 + hv_init_clocksource(); 343 + #endif 344 + } 345 + 346 + void hv_setup_sched_clock(void *sched_clock) 347 + { 348 + #ifdef CONFIG_PARAVIRT 349 + pv_ops.time.sched_clock = sched_clock; 342 350 #endif 343 351 } 344 352
+3 -3
arch/x86/kvm/lapic.c
··· 1598 1598 likely(ns > apic->lapic_timer.timer_advance_ns)) { 1599 1599 expire = ktime_add_ns(now, ns); 1600 1600 expire = ktime_sub_ns(expire, ktimer->timer_advance_ns); 1601 - hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS); 1601 + hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_HARD); 1602 1602 } else 1603 1603 apic_timer_expired(apic); 1604 1604 ··· 2299 2299 apic->vcpu = vcpu; 2300 2300 2301 2301 hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC, 2302 - HRTIMER_MODE_ABS); 2302 + HRTIMER_MODE_ABS_HARD); 2303 2303 apic->lapic_timer.timer.function = apic_timer_fn; 2304 2304 if (timer_advance_ns == -1) { 2305 2305 apic->lapic_timer.timer_advance_ns = LAPIC_TIMER_ADVANCE_ADJUST_INIT; ··· 2484 2484 2485 2485 timer = &vcpu->arch.apic->lapic_timer.timer; 2486 2486 if (hrtimer_cancel(timer)) 2487 - hrtimer_start_expires(timer, HRTIMER_MODE_ABS); 2487 + hrtimer_start_expires(timer, HRTIMER_MODE_ABS_HARD); 2488 2488 } 2489 2489 2490 2490 /*
+2 -3
block/blk-mq.c
··· 3411 3411 kt = nsecs; 3412 3412 3413 3413 mode = HRTIMER_MODE_REL; 3414 - hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode); 3414 + hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode); 3415 3415 hrtimer_set_expires(&hs.timer, kt); 3416 3416 3417 - hrtimer_init_sleeper(&hs, current); 3418 3417 do { 3419 3418 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE) 3420 3419 break; 3421 3420 set_current_state(TASK_UNINTERRUPTIBLE); 3422 - hrtimer_start_expires(&hs.timer, mode); 3421 + hrtimer_sleeper_start_expires(&hs, mode); 3423 3422 if (hs.task) 3424 3423 io_schedule(); 3425 3424 hrtimer_cancel(&hs.timer);
+1 -1
drivers/clocksource/Kconfig
··· 429 429 430 430 config ATMEL_TCB_CLKSRC 431 431 bool "Atmel TC Block timer driver" if COMPILE_TEST 432 - depends on HAS_IOMEM 432 + depends on ARM && HAS_IOMEM 433 433 select TIMER_OF if OF 434 434 help 435 435 Support for Timer Counter Blocks on Atmel SoCs.
+1 -3
drivers/clocksource/em_sti.c
··· 291 291 platform_set_drvdata(pdev, p); 292 292 293 293 irq = platform_get_irq(pdev, 0); 294 - if (irq < 0) { 295 - dev_err(&pdev->dev, "failed to get irq\n"); 294 + if (irq < 0) 296 295 return irq; 297 - } 298 296 299 297 /* map memory, let base point to the STI instance */ 300 298 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+16 -29
drivers/clocksource/hyperv_timer.c
··· 22 22 #include <asm/mshyperv.h> 23 23 24 24 static struct clock_event_device __percpu *hv_clock_event; 25 + static u64 hv_sched_clock_offset __ro_after_init; 25 26 26 27 /* 27 28 * If false, we're using the old mechanism for stimer0 interrupts ··· 213 212 struct clocksource *hyperv_cs; 214 213 EXPORT_SYMBOL_GPL(hyperv_cs); 215 214 216 - #ifdef CONFIG_HYPERV_TSCPAGE 217 - 218 - static struct ms_hyperv_tsc_page *tsc_pg; 215 + static struct ms_hyperv_tsc_page tsc_pg __aligned(PAGE_SIZE); 219 216 220 217 struct ms_hyperv_tsc_page *hv_get_tsc_page(void) 221 218 { 222 - return tsc_pg; 219 + return &tsc_pg; 223 220 } 224 221 EXPORT_SYMBOL_GPL(hv_get_tsc_page); 225 222 226 - static u64 notrace read_hv_sched_clock_tsc(void) 223 + static u64 notrace read_hv_clock_tsc(struct clocksource *arg) 227 224 { 228 - u64 current_tick = hv_read_tsc_page(tsc_pg); 225 + u64 current_tick = hv_read_tsc_page(&tsc_pg); 229 226 230 227 if (current_tick == U64_MAX) 231 228 hv_get_time_ref_count(current_tick); ··· 231 232 return current_tick; 232 233 } 233 234 234 - static u64 read_hv_clock_tsc(struct clocksource *arg) 235 + static u64 read_hv_sched_clock_tsc(void) 235 236 { 236 - return read_hv_sched_clock_tsc(); 237 + return read_hv_clock_tsc(NULL) - hv_sched_clock_offset; 237 238 } 238 239 239 240 static struct clocksource hyperv_cs_tsc = { ··· 243 244 .mask = CLOCKSOURCE_MASK(64), 244 245 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 245 246 }; 246 - #endif 247 247 248 - static u64 notrace read_hv_sched_clock_msr(void) 248 + static u64 notrace read_hv_clock_msr(struct clocksource *arg) 249 249 { 250 250 u64 current_tick; 251 251 /* ··· 256 258 return current_tick; 257 259 } 258 260 259 - static u64 read_hv_clock_msr(struct clocksource *arg) 261 + static u64 read_hv_sched_clock_msr(void) 260 262 { 261 - return read_hv_sched_clock_msr(); 263 + return read_hv_clock_msr(NULL) - hv_sched_clock_offset; 262 264 } 263 265 264 266 static struct clocksource hyperv_cs_msr = { ··· 269 271 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 270 272 }; 271 273 272 - #ifdef CONFIG_HYPERV_TSCPAGE 273 274 static bool __init hv_init_tsc_clocksource(void) 274 275 { 275 276 u64 tsc_msr; ··· 277 280 if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE)) 278 281 return false; 279 282 280 - tsc_pg = vmalloc(PAGE_SIZE); 281 - if (!tsc_pg) 282 - return false; 283 - 284 283 hyperv_cs = &hyperv_cs_tsc; 285 - phys_addr = page_to_phys(vmalloc_to_page(tsc_pg)); 284 + phys_addr = virt_to_phys(&tsc_pg); 286 285 287 286 /* 288 287 * The Hyper-V TLFS specifies to preserve the value of reserved ··· 295 302 hv_set_clocksource_vdso(hyperv_cs_tsc); 296 303 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100); 297 304 298 - /* sched_clock_register is needed on ARM64 but is a no-op on x86 */ 299 - sched_clock_register(read_hv_sched_clock_tsc, 64, HV_CLOCK_HZ); 305 + hv_sched_clock_offset = hyperv_cs->read(hyperv_cs); 306 + hv_setup_sched_clock(read_hv_sched_clock_tsc); 307 + 300 308 return true; 301 309 } 302 - #else 303 - static bool __init hv_init_tsc_clocksource(void) 304 - { 305 - return false; 306 - } 307 - #endif 308 - 309 310 310 311 void __init hv_init_clocksource(void) 311 312 { ··· 320 333 hyperv_cs = &hyperv_cs_msr; 321 334 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100); 322 335 323 - /* sched_clock_register is needed on ARM64 but is a no-op on x86 */ 324 - sched_clock_register(read_hv_sched_clock_msr, 64, HV_CLOCK_HZ); 336 + hv_sched_clock_offset = hyperv_cs->read(hyperv_cs); 337 + hv_setup_sched_clock(read_hv_sched_clock_msr); 325 338 } 326 339 EXPORT_SYMBOL_GPL(hv_init_clocksource);
+1 -1
drivers/clocksource/renesas-ostm.c
··· 221 221 } 222 222 223 223 rate = clk_get_rate(ostm_clk); 224 - ostm->ticks_per_jiffy = (rate + HZ / 2) / HZ; 224 + ostm->ticks_per_jiffy = DIV_ROUND_CLOSEST(rate, HZ); 225 225 226 226 /* 227 227 * First probed device will be used as system clocksource. Any
+14 -5
drivers/clocksource/sh_cmt.c
··· 776 776 int ret; 777 777 778 778 irq = platform_get_irq(ch->cmt->pdev, ch->index); 779 - if (irq < 0) { 780 - dev_err(&ch->cmt->pdev->dev, "ch%u: failed to get irq\n", 781 - ch->index); 779 + if (irq < 0) 782 780 return irq; 783 - } 784 781 785 782 ret = request_irq(irq, sh_cmt_interrupt, 786 783 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING, ··· 918 921 MODULE_DEVICE_TABLE(platform, sh_cmt_id_table); 919 922 920 923 static const struct of_device_id sh_cmt_of_table[] __maybe_unused = { 921 - { .compatible = "renesas,cmt-48", .data = &sh_cmt_info[SH_CMT_48BIT] }, 924 + { 925 + /* deprecated, preserved for backward compatibility */ 926 + .compatible = "renesas,cmt-48", 927 + .data = &sh_cmt_info[SH_CMT_48BIT] 928 + }, 922 929 { 923 930 /* deprecated, preserved for backward compatibility */ 924 931 .compatible = "renesas,cmt-48-gen2", 925 932 .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2] 933 + }, 934 + { 935 + .compatible = "renesas,r8a7740-cmt1", 936 + .data = &sh_cmt_info[SH_CMT_48BIT] 937 + }, 938 + { 939 + .compatible = "renesas,sh73a0-cmt1", 940 + .data = &sh_cmt_info[SH_CMT_48BIT] 926 941 }, 927 942 { 928 943 .compatible = "renesas,rcar-gen2-cmt0",
+1 -4
drivers/clocksource/sh_tmu.c
··· 462 462 ch->base = tmu->mapbase + 8 + ch->index * 12; 463 463 464 464 ch->irq = platform_get_irq(tmu->pdev, index); 465 - if (ch->irq < 0) { 466 - dev_err(&tmu->pdev->dev, "ch%u: failed to get irq\n", 467 - ch->index); 465 + if (ch->irq < 0) 468 466 return ch->irq; 469 - } 470 467 471 468 ch->cs_enabled = false; 472 469 ch->enable_count = 0;
+18
drivers/clocksource/timer-atmel-tcb.c
··· 6 6 #include <linux/irq.h> 7 7 8 8 #include <linux/clk.h> 9 + #include <linux/delay.h> 9 10 #include <linux/err.h> 10 11 #include <linux/ioport.h> 11 12 #include <linux/io.h> ··· 122 121 } 123 122 124 123 static u64 notrace tc_sched_clock_read32(void) 124 + { 125 + return tc_get_cycles32(&clksrc); 126 + } 127 + 128 + static struct delay_timer tc_delay_timer; 129 + 130 + static unsigned long tc_delay_timer_read(void) 131 + { 132 + return tc_get_cycles(&clksrc); 133 + } 134 + 135 + static unsigned long notrace tc_delay_timer_read32(void) 125 136 { 126 137 return tc_get_cycles32(&clksrc); 127 138 } ··· 445 432 /* setup ony channel 0 */ 446 433 tcb_setup_single_chan(&tc, best_divisor_idx); 447 434 tc_sched_clock = tc_sched_clock_read32; 435 + tc_delay_timer.read_current_timer = tc_delay_timer_read32; 448 436 } else { 449 437 /* we have three clocks no matter what the 450 438 * underlying platform supports. ··· 458 444 /* setup both channel 0 & 1 */ 459 445 tcb_setup_dual_chan(&tc, best_divisor_idx); 460 446 tc_sched_clock = tc_sched_clock_read; 447 + tc_delay_timer.read_current_timer = tc_delay_timer_read; 461 448 } 462 449 463 450 /* and away we go! */ ··· 472 457 goto err_unregister_clksrc; 473 458 474 459 sched_clock_register(tc_sched_clock, 32, divided_rate); 460 + 461 + tc_delay_timer.freq = divided_rate; 462 + register_current_timer_delay(&tc_delay_timer); 475 463 476 464 return 0; 477 465
+5
drivers/clocksource/timer-imx-sysctr.c
··· 20 20 #define SYS_CTR_EN 0x1 21 21 #define SYS_CTR_IRQ_MASK 0x2 22 22 23 + #define SYS_CTR_CLK_DIV 0x3 24 + 23 25 static void __iomem *sys_ctr_base; 24 26 static u32 cmpcr; 25 27 ··· 135 133 ret = timer_of_init(np, &to_sysctr); 136 134 if (ret) 137 135 return ret; 136 + 137 + /* system counter clock is divided by 3 internally */ 138 + to_sysctr.of_clk.rate /= SYS_CTR_CLK_DIV; 138 139 139 140 sys_ctr_base = timer_of_base(&to_sysctr); 140 141 cmpcr = readl(sys_ctr_base + CMPCR);
+3 -6
drivers/clocksource/timer-npcm7xx.c
··· 32 32 #define NPCM7XX_Tx_INTEN BIT(29) 33 33 #define NPCM7XX_Tx_COUNTEN BIT(30) 34 34 #define NPCM7XX_Tx_ONESHOT 0x0 35 - #define NPCM7XX_Tx_OPER GENMASK(27, 3) 35 + #define NPCM7XX_Tx_OPER GENMASK(28, 27) 36 36 #define NPCM7XX_Tx_MIN_PRESCALE 0x1 37 37 #define NPCM7XX_Tx_TDR_MASK_BITS 24 38 38 #define NPCM7XX_Tx_MAX_CNT 0xFFFFFF ··· 84 84 85 85 val = readl(timer_of_base(to) + NPCM7XX_REG_TCSR0); 86 86 val &= ~NPCM7XX_Tx_OPER; 87 - 88 - val = readl(timer_of_base(to) + NPCM7XX_REG_TCSR0); 89 87 val |= NPCM7XX_START_ONESHOT_Tx; 90 88 writel(val, timer_of_base(to) + NPCM7XX_REG_TCSR0); 91 89 ··· 95 97 struct timer_of *to = to_timer_of(evt); 96 98 u32 val; 97 99 100 + writel(timer_of_period(to), timer_of_base(to) + NPCM7XX_REG_TICR0); 101 + 98 102 val = readl(timer_of_base(to) + NPCM7XX_REG_TCSR0); 99 103 val &= ~NPCM7XX_Tx_OPER; 100 - 101 - writel(timer_of_period(to), timer_of_base(to) + NPCM7XX_REG_TICR0); 102 104 val |= NPCM7XX_START_PERIODIC_Tx; 103 - 104 105 writel(val, timer_of_base(to) + NPCM7XX_REG_TCSR0); 105 106 106 107 return 0;
+4 -2
drivers/clocksource/timer-of.c
··· 113 113 of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) : 114 114 of_clk_get(np, of_clk->index); 115 115 if (IS_ERR(of_clk->clk)) { 116 - pr_err("Failed to get clock for %pOF\n", np); 117 - return PTR_ERR(of_clk->clk); 116 + ret = PTR_ERR(of_clk->clk); 117 + if (ret != -EPROBE_DEFER) 118 + pr_err("Failed to get clock for %pOF\n", np); 119 + goto out; 118 120 } 119 121 120 122 ret = clk_prepare_enable(of_clk->clk);
+3 -1
drivers/clocksource/timer-probe.c
··· 29 29 30 30 ret = init_func_ret(np); 31 31 if (ret) { 32 - pr_err("Failed to initialize '%pOF': %d\n", np, ret); 32 + if (ret != -EPROBE_DEFER) 33 + pr_err("Failed to initialize '%pOF': %d\n", np, 34 + ret); 33 35 continue; 34 36 } 35 37
+4
drivers/clocksource/timer-sun4i.c
··· 219 219 } 220 220 TIMER_OF_DECLARE(sun4i, "allwinner,sun4i-a10-timer", 221 221 sun4i_timer_init); 222 + TIMER_OF_DECLARE(sun8i_a23, "allwinner,sun8i-a23-timer", 223 + sun4i_timer_init); 224 + TIMER_OF_DECLARE(sun8i_v3s, "allwinner,sun8i-v3s-timer", 225 + sun4i_timer_init); 222 226 TIMER_OF_DECLARE(suniv, "allwinner,suniv-f1c100s-timer", 223 227 sun4i_timer_init);
-3
drivers/hv/Kconfig
··· 14 14 config HYPERV_TIMER 15 15 def_bool HYPERV 16 16 17 - config HYPERV_TSCPAGE 18 - def_bool HYPERV && X86_64 19 - 20 17 config HYPERV_UTILS 21 18 tristate "Microsoft Hyper-V Utilities driver" 22 19 depends on HYPERV && CONNECTOR && NLS
+3 -5
drivers/staging/android/vsoc.c
··· 437 437 return -EINVAL; 438 438 wake_time = ktime_set(arg->wake_time_sec, arg->wake_time_nsec); 439 439 440 - hrtimer_init_on_stack(&to->timer, CLOCK_MONOTONIC, 441 - HRTIMER_MODE_ABS); 440 + hrtimer_init_sleeper_on_stack(to, CLOCK_MONOTONIC, 441 + HRTIMER_MODE_ABS); 442 442 hrtimer_set_expires_range_ns(&to->timer, wake_time, 443 443 current->timer_slack_ns); 444 - 445 - hrtimer_init_sleeper(to, current); 446 444 } 447 445 448 446 while (1) { ··· 458 460 break; 459 461 } 460 462 if (to) { 461 - hrtimer_start_expires(&to->timer, HRTIMER_MODE_ABS); 463 + hrtimer_sleeper_start_expires(to, HRTIMER_MODE_ABS); 462 464 if (likely(to->task)) 463 465 freezable_schedule(); 464 466 hrtimer_cancel(&to->timer);
+5 -1
fs/timerfd.c
··· 471 471 break; 472 472 } 473 473 spin_unlock_irq(&ctx->wqh.lock); 474 - cpu_relax(); 474 + 475 + if (isalarm(ctx)) 476 + hrtimer_cancel_wait_running(&ctx->t.alarm.timer); 477 + else 478 + hrtimer_cancel_wait_running(&ctx->t.tmr); 475 479 } 476 480 477 481 /*
+1
include/asm-generic/mshyperv.h
··· 167 167 void hyperv_report_panic_msg(phys_addr_t pa, size_t size); 168 168 bool hv_is_hyperv_initialized(void); 169 169 void hyperv_cleanup(void); 170 + void hv_setup_sched_clock(void *sched_clock); 170 171 #else /* CONFIG_HYPERV */ 171 172 static inline bool hv_is_hyperv_initialized(void) { return false; } 172 173 static inline void hyperv_cleanup(void) {}
+3 -5
include/clocksource/hyperv_timer.h
··· 28 28 extern void hv_stimer_global_cleanup(void); 29 29 extern void hv_stimer0_isr(void); 30 30 31 - #if IS_ENABLED(CONFIG_HYPERV) 31 + #ifdef CONFIG_HYPERV_TIMER 32 32 extern struct clocksource *hyperv_cs; 33 33 extern void hv_init_clocksource(void); 34 - #endif /* CONFIG_HYPERV */ 35 34 36 - #ifdef CONFIG_HYPERV_TSCPAGE 37 35 extern struct ms_hyperv_tsc_page *hv_get_tsc_page(void); 38 36 39 37 static inline notrace u64 ··· 89 91 return hv_read_tsc_page_tsc(tsc_pg, &cur_tsc); 90 92 } 91 93 92 - #else /* CONFIG_HYPERV_TSC_PAGE */ 94 + #else /* CONFIG_HYPERV_TIMER */ 93 95 static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void) 94 96 { 95 97 return NULL; ··· 100 102 { 101 103 return U64_MAX; 102 104 } 103 - #endif /* CONFIG_HYPERV_TSCPAGE */ 105 + #endif /* CONFIG_HYPERV_TIMER */ 104 106 105 107 #endif
+2 -1
include/linux/alarmtimer.h
··· 5 5 #include <linux/time.h> 6 6 #include <linux/hrtimer.h> 7 7 #include <linux/timerqueue.h> 8 - #include <linux/rtc.h> 8 + 9 + struct rtc_device; 9 10 10 11 enum alarmtimer_type { 11 12 ALARM_REALTIME,
+44 -4
include/linux/hrtimer.h
··· 32 32 * when starting the timer) 33 33 * HRTIMER_MODE_SOFT - Timer callback function will be executed in 34 34 * soft irq context 35 + * HRTIMER_MODE_HARD - Timer callback function will be executed in 36 + * hard irq context even on PREEMPT_RT. 35 37 */ 36 38 enum hrtimer_mode { 37 39 HRTIMER_MODE_ABS = 0x00, 38 40 HRTIMER_MODE_REL = 0x01, 39 41 HRTIMER_MODE_PINNED = 0x02, 40 42 HRTIMER_MODE_SOFT = 0x04, 43 + HRTIMER_MODE_HARD = 0x08, 41 44 42 45 HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED, 43 46 HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED, ··· 51 48 HRTIMER_MODE_ABS_PINNED_SOFT = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_SOFT, 52 49 HRTIMER_MODE_REL_PINNED_SOFT = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_SOFT, 53 50 51 + HRTIMER_MODE_ABS_HARD = HRTIMER_MODE_ABS | HRTIMER_MODE_HARD, 52 + HRTIMER_MODE_REL_HARD = HRTIMER_MODE_REL | HRTIMER_MODE_HARD, 53 + 54 + HRTIMER_MODE_ABS_PINNED_HARD = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_HARD, 55 + HRTIMER_MODE_REL_PINNED_HARD = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_HARD, 54 56 }; 55 57 56 58 /* ··· 109 101 * @state: state information (See bit values above) 110 102 * @is_rel: Set if the timer was armed relative 111 103 * @is_soft: Set if hrtimer will be expired in soft interrupt context. 104 + * @is_hard: Set if hrtimer will be expired in hard interrupt context 105 + * even on RT. 112 106 * 113 107 * The hrtimer structure must be initialized by hrtimer_init() 114 108 */ ··· 122 112 u8 state; 123 113 u8 is_rel; 124 114 u8 is_soft; 115 + u8 is_hard; 125 116 }; 126 117 127 118 /** ··· 194 183 * @nr_retries: Total number of hrtimer interrupt retries 195 184 * @nr_hangs: Total number of hrtimer interrupt hangs 196 185 * @max_hang_time: Maximum time spent in hrtimer_interrupt 186 + * @softirq_expiry_lock: Lock which is taken while softirq based hrtimer are 187 + * expired 188 + * @timer_waiters: A hrtimer_cancel() invocation waits for the timer 189 + * callback to finish. 197 190 * @expires_next: absolute time of the next event, is required for remote 198 191 * hrtimer enqueue; it is the total first expiry time (hard 199 192 * and soft hrtimer are taken into account) ··· 224 209 unsigned short nr_retries; 225 210 unsigned short nr_hangs; 226 211 unsigned int max_hang_time; 212 + #endif 213 + #ifdef CONFIG_PREEMPT_RT 214 + spinlock_t softirq_expiry_lock; 215 + atomic_t timer_waiters; 227 216 #endif 228 217 ktime_t expires_next; 229 218 struct hrtimer *next_timer; ··· 360 341 361 342 DECLARE_PER_CPU(struct tick_device, tick_cpu_device); 362 343 344 + #ifdef CONFIG_PREEMPT_RT 345 + void hrtimer_cancel_wait_running(const struct hrtimer *timer); 346 + #else 347 + static inline void hrtimer_cancel_wait_running(struct hrtimer *timer) 348 + { 349 + cpu_relax(); 350 + } 351 + #endif 363 352 364 353 /* Exported timer functions: */ 365 354 366 355 /* Initialize timers: */ 367 356 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, 368 357 enum hrtimer_mode mode); 358 + extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id, 359 + enum hrtimer_mode mode); 369 360 370 361 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS 371 362 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock, 372 363 enum hrtimer_mode mode); 364 + extern void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl, 365 + clockid_t clock_id, 366 + enum hrtimer_mode mode); 373 367 374 368 extern void destroy_hrtimer_on_stack(struct hrtimer *timer); 375 369 #else ··· 392 360 { 393 361 hrtimer_init(timer, which_clock, mode); 394 362 } 363 + 364 + static inline void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl, 365 + clockid_t clock_id, 366 + enum hrtimer_mode mode) 367 + { 368 + hrtimer_init_sleeper(sl, clock_id, mode); 369 + } 370 + 395 371 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { } 396 372 #endif 397 373 ··· 434 394 delta = ktime_to_ns(ktime_sub(hard, soft)); 435 395 hrtimer_start_range_ns(timer, soft, delta, mode); 436 396 } 397 + 398 + void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl, 399 + enum hrtimer_mode mode); 437 400 438 401 static inline void hrtimer_restart(struct hrtimer *timer) 439 402 { ··· 506 463 const enum hrtimer_mode mode, 507 464 const clockid_t clockid); 508 465 509 - extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, 510 - struct task_struct *tsk); 511 - 512 466 extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta, 513 - const enum hrtimer_mode mode); 467 + const enum hrtimer_mode mode); 514 468 extern int schedule_hrtimeout_range_clock(ktime_t *expires, 515 469 u64 delta, 516 470 const enum hrtimer_mode mode,
-11
include/linux/init_task.h
··· 36 36 #define INIT_PREV_CPUTIME(x) 37 37 #endif 38 38 39 - #ifdef CONFIG_POSIX_TIMERS 40 - #define INIT_CPU_TIMERS(s) \ 41 - .cpu_timers = { \ 42 - LIST_HEAD_INIT(s.cpu_timers[0]), \ 43 - LIST_HEAD_INIT(s.cpu_timers[1]), \ 44 - LIST_HEAD_INIT(s.cpu_timers[2]), \ 45 - }, 46 - #else 47 - #define INIT_CPU_TIMERS(s) 48 - #endif 49 - 50 39 #define INIT_TASK_COMM "swapper" 51 40 52 41 /* Attach to the init_task data structure for proper alignment */
+117 -14
include/linux/posix-timers.h
··· 4 4 5 5 #include <linux/spinlock.h> 6 6 #include <linux/list.h> 7 - #include <linux/sched.h> 8 - #include <linux/timex.h> 9 7 #include <linux/alarmtimer.h> 8 + #include <linux/timerqueue.h> 10 9 11 - struct siginfo; 12 - 13 - struct cpu_timer_list { 14 - struct list_head entry; 15 - u64 expires; 16 - struct task_struct *task; 17 - int firing; 18 - }; 10 + struct kernel_siginfo; 11 + struct task_struct; 19 12 20 13 /* 21 14 * Bit fields within a clockid: ··· 56 63 return ~(clk >> 3); 57 64 } 58 65 66 + #ifdef CONFIG_POSIX_TIMERS 67 + 68 + /** 69 + * cpu_timer - Posix CPU timer representation for k_itimer 70 + * @node: timerqueue node to queue in the task/sig 71 + * @head: timerqueue head on which this timer is queued 72 + * @task: Pointer to target task 73 + * @elist: List head for the expiry list 74 + * @firing: Timer is currently firing 75 + */ 76 + struct cpu_timer { 77 + struct timerqueue_node node; 78 + struct timerqueue_head *head; 79 + struct task_struct *task; 80 + struct list_head elist; 81 + int firing; 82 + }; 83 + 84 + static inline bool cpu_timer_enqueue(struct timerqueue_head *head, 85 + struct cpu_timer *ctmr) 86 + { 87 + ctmr->head = head; 88 + return timerqueue_add(head, &ctmr->node); 89 + } 90 + 91 + static inline void cpu_timer_dequeue(struct cpu_timer *ctmr) 92 + { 93 + if (ctmr->head) { 94 + timerqueue_del(ctmr->head, &ctmr->node); 95 + ctmr->head = NULL; 96 + } 97 + } 98 + 99 + static inline u64 cpu_timer_getexpires(struct cpu_timer *ctmr) 100 + { 101 + return ctmr->node.expires; 102 + } 103 + 104 + static inline void cpu_timer_setexpires(struct cpu_timer *ctmr, u64 exp) 105 + { 106 + ctmr->node.expires = exp; 107 + } 108 + 109 + /** 110 + * posix_cputimer_base - Container per posix CPU clock 111 + * @nextevt: Earliest-expiration cache 112 + * @tqhead: timerqueue head for cpu_timers 113 + */ 114 + struct posix_cputimer_base { 115 + u64 nextevt; 116 + struct timerqueue_head tqhead; 117 + }; 118 + 119 + /** 120 + * posix_cputimers - Container for posix CPU timer related data 121 + * @bases: Base container for posix CPU clocks 122 + * @timers_active: Timers are queued. 123 + * @expiry_active: Timer expiry is active. Used for 124 + * process wide timers to avoid multiple 125 + * task trying to handle expiry concurrently 126 + * 127 + * Used in task_struct and signal_struct 128 + */ 129 + struct posix_cputimers { 130 + struct posix_cputimer_base bases[CPUCLOCK_MAX]; 131 + unsigned int timers_active; 132 + unsigned int expiry_active; 133 + }; 134 + 135 + static inline void posix_cputimers_init(struct posix_cputimers *pct) 136 + { 137 + memset(pct, 0, sizeof(*pct)); 138 + pct->bases[0].nextevt = U64_MAX; 139 + pct->bases[1].nextevt = U64_MAX; 140 + pct->bases[2].nextevt = U64_MAX; 141 + } 142 + 143 + void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit); 144 + 145 + static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct, 146 + u64 runtime) 147 + { 148 + pct->bases[CPUCLOCK_SCHED].nextevt = runtime; 149 + } 150 + 151 + /* Init task static initializer */ 152 + #define INIT_CPU_TIMERBASE(b) { \ 153 + .nextevt = U64_MAX, \ 154 + } 155 + 156 + #define INIT_CPU_TIMERBASES(b) { \ 157 + INIT_CPU_TIMERBASE(b[0]), \ 158 + INIT_CPU_TIMERBASE(b[1]), \ 159 + INIT_CPU_TIMERBASE(b[2]), \ 160 + } 161 + 162 + #define INIT_CPU_TIMERS(s) \ 163 + .posix_cputimers = { \ 164 + .bases = INIT_CPU_TIMERBASES(s.posix_cputimers.bases), \ 165 + }, 166 + #else 167 + struct posix_cputimers { }; 168 + struct cpu_timer { }; 169 + #define INIT_CPU_TIMERS(s) 170 + static inline void posix_cputimers_init(struct posix_cputimers *pct) { } 171 + static inline void posix_cputimers_group_init(struct posix_cputimers *pct, 172 + u64 cpu_limit) { } 173 + #endif 174 + 59 175 #define REQUEUE_PENDING 1 60 176 61 177 /** ··· 187 85 * @it_process: The task to wakeup on clock_nanosleep (CPU timers) 188 86 * @sigq: Pointer to preallocated sigqueue 189 87 * @it: Union representing the various posix timer type 190 - * internals. Also used for rcu freeing the timer. 88 + * internals. 89 + * @rcu: RCU head for freeing the timer. 191 90 */ 192 91 struct k_itimer { 193 92 struct list_head list; ··· 213 110 struct { 214 111 struct hrtimer timer; 215 112 } real; 216 - struct cpu_timer_list cpu; 113 + struct cpu_timer cpu; 217 114 struct { 218 115 struct alarm alarmtimer; 219 116 } alarm; 220 - struct rcu_head rcu; 221 117 } it; 118 + struct rcu_head rcu; 222 119 }; 223 120 224 - void run_posix_cpu_timers(struct task_struct *task); 121 + void run_posix_cpu_timers(void); 225 122 void posix_cpu_timers_exit(struct task_struct *task); 226 123 void posix_cpu_timers_exit_group(struct task_struct *task); 227 124 void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
+4 -25
include/linux/sched.h
··· 25 25 #include <linux/resource.h> 26 26 #include <linux/latencytop.h> 27 27 #include <linux/sched/prio.h> 28 + #include <linux/sched/types.h> 28 29 #include <linux/signal_types.h> 29 30 #include <linux/mm_types_task.h> 30 31 #include <linux/task_io_accounting.h> 32 + #include <linux/posix-timers.h> 31 33 #include <linux/rseq.h> 32 34 33 35 /* task_struct member predeclarations (sorted alphabetically): */ ··· 245 243 raw_spinlock_t lock; 246 244 #endif 247 245 }; 248 - 249 - /** 250 - * struct task_cputime - collected CPU time counts 251 - * @utime: time spent in user mode, in nanoseconds 252 - * @stime: time spent in kernel mode, in nanoseconds 253 - * @sum_exec_runtime: total time spent on the CPU, in nanoseconds 254 - * 255 - * This structure groups together three kinds of CPU time that are tracked for 256 - * threads and thread groups. Most things considering CPU time want to group 257 - * these counts together and treat all three of them in parallel. 258 - */ 259 - struct task_cputime { 260 - u64 utime; 261 - u64 stime; 262 - unsigned long long sum_exec_runtime; 263 - }; 264 - 265 - /* Alternate field names when used on cache expirations: */ 266 - #define virt_exp utime 267 - #define prof_exp stime 268 - #define sched_exp sum_exec_runtime 269 246 270 247 enum vtime_state { 271 248 /* Task is sleeping or running in a CPU with VTIME inactive: */ ··· 862 881 unsigned long min_flt; 863 882 unsigned long maj_flt; 864 883 865 - #ifdef CONFIG_POSIX_TIMERS 866 - struct task_cputime cputime_expires; 867 - struct list_head cpu_timers[3]; 868 - #endif 884 + /* Empty if CONFIG_POSIX_CPUTIMERS=n */ 885 + struct posix_cputimers posix_cputimers; 869 886 870 887 /* Process credentials: */ 871 888
+7 -5
include/linux/sched/cputime.h
··· 61 61 * Thread group CPU time accounting. 62 62 */ 63 63 void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times); 64 - void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times); 65 - 64 + void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples); 66 65 67 66 /* 68 67 * The following are functions that support scheduler-internal time accounting. ··· 70 71 */ 71 72 72 73 /** 73 - * get_running_cputimer - return &tsk->signal->cputimer if cputimer is running 74 + * get_running_cputimer - return &tsk->signal->cputimer if cputimers are active 74 75 * 75 76 * @tsk: Pointer to target task. 76 77 */ ··· 80 81 { 81 82 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; 82 83 83 - /* Check if cputimer isn't running. This is accessed without locking. */ 84 - if (!READ_ONCE(cputimer->running)) 84 + /* 85 + * Check whether posix CPU timers are active. If not the thread 86 + * group accounting is not active either. Lockless check. 87 + */ 88 + if (!READ_ONCE(tsk->signal->posix_cputimers.timers_active)) 85 89 return NULL; 86 90 87 91 /*
+3 -11
include/linux/sched/signal.h
··· 9 9 #include <linux/sched/task.h> 10 10 #include <linux/cred.h> 11 11 #include <linux/refcount.h> 12 + #include <linux/posix-timers.h> 12 13 13 14 /* 14 15 * Types defining task->signal and task->sighand and APIs using them: ··· 57 56 /** 58 57 * struct thread_group_cputimer - thread group interval timer counts 59 58 * @cputime_atomic: atomic thread group interval timers. 60 - * @running: true when there are timers running and 61 - * @cputime_atomic receives updates. 62 - * @checking_timer: true when a thread in the group is in the 63 - * process of checking for thread group timers. 64 59 * 65 60 * This structure contains the version of task_cputime, above, that is 66 61 * used for thread group CPU timer calculations. 67 62 */ 68 63 struct thread_group_cputimer { 69 64 struct task_cputime_atomic cputime_atomic; 70 - bool running; 71 - bool checking_timer; 72 65 }; 73 66 74 67 struct multiprocess_signals { ··· 143 148 */ 144 149 struct thread_group_cputimer cputimer; 145 150 146 - /* Earliest-expiration cache. */ 147 - struct task_cputime cputime_expires; 148 - 149 - struct list_head cpu_timers[3]; 150 - 151 151 #endif 152 + /* Empty if CONFIG_POSIX_TIMERS=n */ 153 + struct posix_cputimers posix_cputimers; 152 154 153 155 /* PID/PID hash table linkage. */ 154 156 struct pid *pids[PIDTYPE_MAX];
+23
include/linux/sched/types.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef _LINUX_SCHED_TYPES_H 3 + #define _LINUX_SCHED_TYPES_H 4 + 5 + #include <linux/types.h> 6 + 7 + /** 8 + * struct task_cputime - collected CPU time counts 9 + * @stime: time spent in kernel mode, in nanoseconds 10 + * @utime: time spent in user mode, in nanoseconds 11 + * @sum_exec_runtime: total time spent on the CPU, in nanoseconds 12 + * 13 + * This structure groups together three kinds of CPU time that are tracked for 14 + * threads and thread groups. Most things considering CPU time want to group 15 + * these counts together and treat all three of them in parallel. 16 + */ 17 + struct task_cputime { 18 + u64 stime; 19 + u64 utime; 20 + unsigned long long sum_exec_runtime; 21 + }; 22 + 23 + #endif
+1 -1
include/linux/timer.h
··· 183 183 184 184 extern int try_to_del_timer_sync(struct timer_list *timer); 185 185 186 - #ifdef CONFIG_SMP 186 + #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT) 187 187 extern int del_timer_sync(struct timer_list *timer); 188 188 #else 189 189 # define del_timer_sync(t) del_timer(t)
+16 -7
include/linux/timerqueue.h
··· 12 12 }; 13 13 14 14 struct timerqueue_head { 15 - struct rb_root head; 16 - struct timerqueue_node *next; 15 + struct rb_root_cached rb_root; 17 16 }; 18 17 19 18 ··· 28 29 * 29 30 * @head: head of timerqueue 30 31 * 31 - * Returns a pointer to the timer node that has the 32 - * earliest expiration time. 32 + * Returns a pointer to the timer node that has the earliest expiration time. 33 33 */ 34 34 static inline 35 35 struct timerqueue_node *timerqueue_getnext(struct timerqueue_head *head) 36 36 { 37 - return head->next; 37 + struct rb_node *leftmost = rb_first_cached(&head->rb_root); 38 + 39 + return rb_entry(leftmost, struct timerqueue_node, node); 38 40 } 39 41 40 42 static inline void timerqueue_init(struct timerqueue_node *node) ··· 43 43 RB_CLEAR_NODE(&node->node); 44 44 } 45 45 46 + static inline bool timerqueue_node_queued(struct timerqueue_node *node) 47 + { 48 + return !RB_EMPTY_NODE(&node->node); 49 + } 50 + 51 + static inline bool timerqueue_node_expires(struct timerqueue_node *node) 52 + { 53 + return node->expires; 54 + } 55 + 46 56 static inline void timerqueue_init_head(struct timerqueue_head *head) 47 57 { 48 - head->head = RB_ROOT; 49 - head->next = NULL; 58 + head->rb_root = RB_ROOT_CACHED; 50 59 } 51 60 #endif /* _LINUX_TIMERQUEUE_H */
+2 -2
include/linux/wait.h
··· 501 501 int __ret = 0; \ 502 502 struct hrtimer_sleeper __t; \ 503 503 \ 504 - hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); \ 505 - hrtimer_init_sleeper(&__t, current); \ 504 + hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC, \ 505 + HRTIMER_MODE_REL); \ 506 506 if ((timeout) != KTIME_MAX) \ 507 507 hrtimer_start_range_ns(&__t.timer, timeout, \ 508 508 current->timer_slack_ns, \
-2
init/init_task.c
··· 30 30 .posix_timers = LIST_HEAD_INIT(init_signals.posix_timers), 31 31 .cputimer = { 32 32 .cputime_atomic = INIT_CPUTIME_ATOMIC, 33 - .running = false, 34 - .checking_timer = false, 35 33 }, 36 34 #endif 37 35 INIT_CPU_TIMERS(init_signals)
+4 -4
kernel/events/core.c
··· 1103 1103 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); 1104 1104 1105 1105 raw_spin_lock_init(&cpuctx->hrtimer_lock); 1106 - hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); 1106 + hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD); 1107 1107 timer->function = perf_mux_hrtimer_handler; 1108 1108 } 1109 1109 ··· 1121 1121 if (!cpuctx->hrtimer_active) { 1122 1122 cpuctx->hrtimer_active = 1; 1123 1123 hrtimer_forward_now(timer, cpuctx->hrtimer_interval); 1124 - hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); 1124 + hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD); 1125 1125 } 1126 1126 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags); 1127 1127 ··· 9574 9574 period = max_t(u64, 10000, hwc->sample_period); 9575 9575 } 9576 9576 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), 9577 - HRTIMER_MODE_REL_PINNED); 9577 + HRTIMER_MODE_REL_PINNED_HARD); 9578 9578 } 9579 9579 9580 9580 static void perf_swevent_cancel_hrtimer(struct perf_event *event) ··· 9596 9596 if (!is_sampling_event(event)) 9597 9597 return; 9598 9598 9599 - hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 9599 + hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 9600 9600 hwc->hrtimer.function = perf_swevent_hrtimer; 9601 9601 9602 9602 /*
+3 -31
kernel/fork.c
··· 1519 1519 } 1520 1520 } 1521 1521 1522 - #ifdef CONFIG_POSIX_TIMERS 1523 1522 /* 1524 1523 * Initialize POSIX timer handling for a thread group. 1525 1524 */ 1526 1525 static void posix_cpu_timers_init_group(struct signal_struct *sig) 1527 1526 { 1527 + struct posix_cputimers *pct = &sig->posix_cputimers; 1528 1528 unsigned long cpu_limit; 1529 1529 1530 1530 cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 1531 - if (cpu_limit != RLIM_INFINITY) { 1532 - sig->cputime_expires.prof_exp = cpu_limit * NSEC_PER_SEC; 1533 - sig->cputimer.running = true; 1534 - } 1535 - 1536 - /* The timer lists. */ 1537 - INIT_LIST_HEAD(&sig->cpu_timers[0]); 1538 - INIT_LIST_HEAD(&sig->cpu_timers[1]); 1539 - INIT_LIST_HEAD(&sig->cpu_timers[2]); 1531 + posix_cputimers_group_init(pct, cpu_limit); 1540 1532 } 1541 - #else 1542 - static inline void posix_cpu_timers_init_group(struct signal_struct *sig) { } 1543 - #endif 1544 1533 1545 1534 static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 1546 1535 { ··· 1630 1641 p->pi_blocked_on = NULL; 1631 1642 #endif 1632 1643 } 1633 - 1634 - #ifdef CONFIG_POSIX_TIMERS 1635 - /* 1636 - * Initialize POSIX timer handling for a single task. 1637 - */ 1638 - static void posix_cpu_timers_init(struct task_struct *tsk) 1639 - { 1640 - tsk->cputime_expires.prof_exp = 0; 1641 - tsk->cputime_expires.virt_exp = 0; 1642 - tsk->cputime_expires.sched_exp = 0; 1643 - INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1644 - INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1645 - INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1646 - } 1647 - #else 1648 - static inline void posix_cpu_timers_init(struct task_struct *tsk) { } 1649 - #endif 1650 1644 1651 1645 static inline void init_task_pid_links(struct task_struct *task) 1652 1646 { ··· 1917 1945 task_io_accounting_init(&p->ioac); 1918 1946 acct_clear_integrals(p); 1919 1947 1920 - posix_cpu_timers_init(p); 1948 + posix_cputimers_init(&p->posix_cputimers); 1921 1949 1922 1950 p->io_context = NULL; 1923 1951 audit_set_context(p, NULL);
+5 -7
kernel/futex.c
··· 487 487 if (!time) 488 488 return NULL; 489 489 490 - hrtimer_init_on_stack(&timeout->timer, (flags & FLAGS_CLOCKRT) ? 491 - CLOCK_REALTIME : CLOCK_MONOTONIC, 492 - HRTIMER_MODE_ABS); 493 - hrtimer_init_sleeper(timeout, current); 494 - 490 + hrtimer_init_sleeper_on_stack(timeout, (flags & FLAGS_CLOCKRT) ? 491 + CLOCK_REALTIME : CLOCK_MONOTONIC, 492 + HRTIMER_MODE_ABS); 495 493 /* 496 494 * If range_ns is 0, calling hrtimer_set_expires_range_ns() is 497 495 * effectively the same as calling hrtimer_set_expires(). ··· 2611 2613 2612 2614 /* Arm the timer */ 2613 2615 if (timeout) 2614 - hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS); 2616 + hrtimer_sleeper_start_expires(timeout, HRTIMER_MODE_ABS); 2615 2617 2616 2618 /* 2617 2619 * If we have been removed from the hash list, then another task ··· 2897 2899 } 2898 2900 2899 2901 if (unlikely(to)) 2900 - hrtimer_start_expires(&to->timer, HRTIMER_MODE_ABS); 2902 + hrtimer_sleeper_start_expires(to, HRTIMER_MODE_ABS); 2901 2903 2902 2904 ret = rt_mutex_wait_proxy_lock(&q.pi_state->pi_mutex, to, &rt_waiter); 2903 2905
+3 -3
kernel/sched/core.c
··· 255 255 { 256 256 struct hrtimer *timer = &rq->hrtick_timer; 257 257 258 - hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); 258 + hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD); 259 259 } 260 260 261 261 /* ··· 314 314 */ 315 315 delay = max_t(u64, delay, 10000LL); 316 316 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), 317 - HRTIMER_MODE_REL_PINNED); 317 + HRTIMER_MODE_REL_PINNED_HARD); 318 318 } 319 319 #endif /* CONFIG_SMP */ 320 320 ··· 328 328 rq->hrtick_csd.info = rq; 329 329 #endif 330 330 331 - hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 331 + hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 332 332 rq->hrtick_timer.function = hrtick; 333 333 } 334 334 #else /* CONFIG_SCHED_HRTICK */
+4 -4
kernel/sched/deadline.c
··· 287 287 288 288 dl_se->dl_non_contending = 1; 289 289 get_task_struct(p); 290 - hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL); 290 + hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD); 291 291 } 292 292 293 293 static void task_contending(struct sched_dl_entity *dl_se, int flags) ··· 956 956 */ 957 957 if (!hrtimer_is_queued(timer)) { 958 958 get_task_struct(p); 959 - hrtimer_start(timer, act, HRTIMER_MODE_ABS); 959 + hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD); 960 960 } 961 961 962 962 return 1; ··· 1086 1086 { 1087 1087 struct hrtimer *timer = &dl_se->dl_timer; 1088 1088 1089 - hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1089 + hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 1090 1090 timer->function = dl_task_timer; 1091 1091 } 1092 1092 ··· 1325 1325 { 1326 1326 struct hrtimer *timer = &dl_se->inactive_timer; 1327 1327 1328 - hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1328 + hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 1329 1329 timer->function = inactive_task_timer; 1330 1330 } 1331 1331
+8 -5
kernel/sched/rt.c
··· 45 45 46 46 raw_spin_lock_init(&rt_b->rt_runtime_lock); 47 47 48 - hrtimer_init(&rt_b->rt_period_timer, 49 - CLOCK_MONOTONIC, HRTIMER_MODE_REL); 48 + hrtimer_init(&rt_b->rt_period_timer, CLOCK_MONOTONIC, 49 + HRTIMER_MODE_REL_HARD); 50 50 rt_b->rt_period_timer.function = sched_rt_period_timer; 51 51 } 52 52 ··· 67 67 * to update the period. 68 68 */ 69 69 hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0)); 70 - hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED); 70 + hrtimer_start_expires(&rt_b->rt_period_timer, 71 + HRTIMER_MODE_ABS_PINNED_HARD); 71 72 } 72 73 raw_spin_unlock(&rt_b->rt_runtime_lock); 73 74 } ··· 2290 2289 } 2291 2290 2292 2291 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ); 2293 - if (p->rt.timeout > next) 2294 - p->cputime_expires.sched_exp = p->se.sum_exec_runtime; 2292 + if (p->rt.timeout > next) { 2293 + posix_cputimers_rt_watchdog(&p->posix_cputimers, 2294 + p->se.sum_exec_runtime); 2295 + } 2295 2296 } 2296 2297 } 2297 2298 #else
+3 -13
kernel/sys.c
··· 1557 1557 retval = -EPERM; 1558 1558 if (!retval) 1559 1559 retval = security_task_setrlimit(tsk, resource, new_rlim); 1560 - if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) { 1561 - /* 1562 - * The caller is asking for an immediate RLIMIT_CPU 1563 - * expiry. But we use the zero value to mean "it was 1564 - * never set". So let's cheat and make it one second 1565 - * instead 1566 - */ 1567 - new_rlim->rlim_cur = 1; 1568 - } 1569 1560 } 1570 1561 if (!retval) { 1571 1562 if (old_rlim) ··· 1567 1576 task_unlock(tsk->group_leader); 1568 1577 1569 1578 /* 1570 - * RLIMIT_CPU handling. Note that the kernel fails to return an error 1571 - * code if it rejected the user's attempt to set RLIMIT_CPU. This is a 1572 - * very long-standing error, and fixing it now risks breakage of 1573 - * applications, so we live with it 1579 + * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not 1580 + * infite. In case of RLIM_INFINITY the posix CPU timer code 1581 + * ignores the rlimit. 1574 1582 */ 1575 1583 if (!retval && new_rlim && resource == RLIMIT_CPU && 1576 1584 new_rlim->rlim_cur != RLIM_INFINITY &&
+15 -1
kernel/time/alarmtimer.c
··· 432 432 int ret = alarm_try_to_cancel(alarm); 433 433 if (ret >= 0) 434 434 return ret; 435 - cpu_relax(); 435 + hrtimer_cancel_wait_running(&alarm->timer); 436 436 } 437 437 } 438 438 EXPORT_SYMBOL_GPL(alarm_cancel); ··· 603 603 static int alarm_timer_try_to_cancel(struct k_itimer *timr) 604 604 { 605 605 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer); 606 + } 607 + 608 + /** 609 + * alarm_timer_wait_running - Posix timer callback to wait for a timer 610 + * @timr: Pointer to the posixtimer data struct 611 + * 612 + * Called from the core code when timer cancel detected that the callback 613 + * is running. @timr is unlocked and rcu read lock is held to prevent it 614 + * from being freed. 615 + */ 616 + static void alarm_timer_wait_running(struct k_itimer *timr) 617 + { 618 + hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer); 606 619 } 607 620 608 621 /** ··· 847 834 .timer_forward = alarm_timer_forward, 848 835 .timer_remaining = alarm_timer_remaining, 849 836 .timer_try_to_cancel = alarm_timer_try_to_cancel, 837 + .timer_wait_running = alarm_timer_wait_running, 850 838 .nsleep = alarm_timer_nsleep, 851 839 }; 852 840 #endif /* CONFIG_POSIX_TIMERS */
+212 -23
kernel/time/hrtimer.c
··· 140 140 141 141 #define migration_base migration_cpu_base.clock_base[0] 142 142 143 + static inline bool is_migration_base(struct hrtimer_clock_base *base) 144 + { 145 + return base == &migration_base; 146 + } 147 + 143 148 /* 144 149 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock 145 150 * means that all timers which are tied to this base via timer->base are ··· 268 263 } 269 264 270 265 #else /* CONFIG_SMP */ 266 + 267 + static inline bool is_migration_base(struct hrtimer_clock_base *base) 268 + { 269 + return false; 270 + } 271 271 272 272 static inline struct hrtimer_clock_base * 273 273 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) ··· 436 426 __hrtimer_init(timer, clock_id, mode); 437 427 } 438 428 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack); 429 + 430 + static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl, 431 + clockid_t clock_id, enum hrtimer_mode mode); 432 + 433 + void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl, 434 + clockid_t clock_id, enum hrtimer_mode mode) 435 + { 436 + debug_object_init_on_stack(&sl->timer, &hrtimer_debug_descr); 437 + __hrtimer_init_sleeper(sl, clock_id, mode); 438 + } 439 + EXPORT_SYMBOL_GPL(hrtimer_init_sleeper_on_stack); 439 440 440 441 void destroy_hrtimer_on_stack(struct hrtimer *timer) 441 442 { ··· 1117 1096 1118 1097 /* 1119 1098 * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft 1120 - * match. 1099 + * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard 1100 + * expiry mode because unmarked timers are moved to softirq expiry. 1121 1101 */ 1122 - WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft); 1102 + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 1103 + WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft); 1104 + else 1105 + WARN_ON_ONCE(!(mode & HRTIMER_MODE_HARD) ^ !timer->is_hard); 1123 1106 1124 1107 base = lock_hrtimer_base(timer, &flags); 1125 1108 ··· 1172 1147 } 1173 1148 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel); 1174 1149 1150 + #ifdef CONFIG_PREEMPT_RT 1151 + static void hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) 1152 + { 1153 + spin_lock_init(&base->softirq_expiry_lock); 1154 + } 1155 + 1156 + static void hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) 1157 + { 1158 + spin_lock(&base->softirq_expiry_lock); 1159 + } 1160 + 1161 + static void hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) 1162 + { 1163 + spin_unlock(&base->softirq_expiry_lock); 1164 + } 1165 + 1166 + /* 1167 + * The counterpart to hrtimer_cancel_wait_running(). 1168 + * 1169 + * If there is a waiter for cpu_base->expiry_lock, then it was waiting for 1170 + * the timer callback to finish. Drop expiry_lock and reaquire it. That 1171 + * allows the waiter to acquire the lock and make progress. 1172 + */ 1173 + static void hrtimer_sync_wait_running(struct hrtimer_cpu_base *cpu_base, 1174 + unsigned long flags) 1175 + { 1176 + if (atomic_read(&cpu_base->timer_waiters)) { 1177 + raw_spin_unlock_irqrestore(&cpu_base->lock, flags); 1178 + spin_unlock(&cpu_base->softirq_expiry_lock); 1179 + spin_lock(&cpu_base->softirq_expiry_lock); 1180 + raw_spin_lock_irq(&cpu_base->lock); 1181 + } 1182 + } 1183 + 1184 + /* 1185 + * This function is called on PREEMPT_RT kernels when the fast path 1186 + * deletion of a timer failed because the timer callback function was 1187 + * running. 1188 + * 1189 + * This prevents priority inversion: if the soft irq thread is preempted 1190 + * in the middle of a timer callback, then calling del_timer_sync() can 1191 + * lead to two issues: 1192 + * 1193 + * - If the caller is on a remote CPU then it has to spin wait for the timer 1194 + * handler to complete. This can result in unbound priority inversion. 1195 + * 1196 + * - If the caller originates from the task which preempted the timer 1197 + * handler on the same CPU, then spin waiting for the timer handler to 1198 + * complete is never going to end. 1199 + */ 1200 + void hrtimer_cancel_wait_running(const struct hrtimer *timer) 1201 + { 1202 + /* Lockless read. Prevent the compiler from reloading it below */ 1203 + struct hrtimer_clock_base *base = READ_ONCE(timer->base); 1204 + 1205 + /* 1206 + * Just relax if the timer expires in hard interrupt context or if 1207 + * it is currently on the migration base. 1208 + */ 1209 + if (!timer->is_soft || is_migration_base(base)) { 1210 + cpu_relax(); 1211 + return; 1212 + } 1213 + 1214 + /* 1215 + * Mark the base as contended and grab the expiry lock, which is 1216 + * held by the softirq across the timer callback. Drop the lock 1217 + * immediately so the softirq can expire the next timer. In theory 1218 + * the timer could already be running again, but that's more than 1219 + * unlikely and just causes another wait loop. 1220 + */ 1221 + atomic_inc(&base->cpu_base->timer_waiters); 1222 + spin_lock_bh(&base->cpu_base->softirq_expiry_lock); 1223 + atomic_dec(&base->cpu_base->timer_waiters); 1224 + spin_unlock_bh(&base->cpu_base->softirq_expiry_lock); 1225 + } 1226 + #else 1227 + static inline void 1228 + hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) { } 1229 + static inline void 1230 + hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) { } 1231 + static inline void 1232 + hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) { } 1233 + static inline void hrtimer_sync_wait_running(struct hrtimer_cpu_base *base, 1234 + unsigned long flags) { } 1235 + #endif 1236 + 1175 1237 /** 1176 1238 * hrtimer_cancel - cancel a timer and wait for the handler to finish. 1177 1239 * @timer: the timer to be cancelled ··· 1269 1157 */ 1270 1158 int hrtimer_cancel(struct hrtimer *timer) 1271 1159 { 1272 - for (;;) { 1273 - int ret = hrtimer_try_to_cancel(timer); 1160 + int ret; 1274 1161 1275 - if (ret >= 0) 1276 - return ret; 1277 - cpu_relax(); 1278 - } 1162 + do { 1163 + ret = hrtimer_try_to_cancel(timer); 1164 + 1165 + if (ret < 0) 1166 + hrtimer_cancel_wait_running(timer); 1167 + } while (ret < 0); 1168 + return ret; 1279 1169 } 1280 1170 EXPORT_SYMBOL_GPL(hrtimer_cancel); 1281 1171 ··· 1374 1260 enum hrtimer_mode mode) 1375 1261 { 1376 1262 bool softtimer = !!(mode & HRTIMER_MODE_SOFT); 1377 - int base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0; 1378 1263 struct hrtimer_cpu_base *cpu_base; 1264 + int base; 1265 + 1266 + /* 1267 + * On PREEMPT_RT enabled kernels hrtimers which are not explicitely 1268 + * marked for hard interrupt expiry mode are moved into soft 1269 + * interrupt context for latency reasons and because the callbacks 1270 + * can invoke functions which might sleep on RT, e.g. spin_lock(). 1271 + */ 1272 + if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD)) 1273 + softtimer = true; 1379 1274 1380 1275 memset(timer, 0, sizeof(struct hrtimer)); 1381 1276 ··· 1398 1275 if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL) 1399 1276 clock_id = CLOCK_MONOTONIC; 1400 1277 1278 + base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0; 1401 1279 base += hrtimer_clockid_to_base(clock_id); 1402 1280 timer->is_soft = softtimer; 1281 + timer->is_hard = !softtimer; 1403 1282 timer->base = &cpu_base->clock_base[base]; 1404 1283 timerqueue_init(&timer->node); 1405 1284 } ··· 1574 1449 break; 1575 1450 1576 1451 __run_hrtimer(cpu_base, base, timer, &basenow, flags); 1452 + if (active_mask == HRTIMER_ACTIVE_SOFT) 1453 + hrtimer_sync_wait_running(cpu_base, flags); 1577 1454 } 1578 1455 } 1579 1456 } ··· 1586 1459 unsigned long flags; 1587 1460 ktime_t now; 1588 1461 1462 + hrtimer_cpu_base_lock_expiry(cpu_base); 1589 1463 raw_spin_lock_irqsave(&cpu_base->lock, flags); 1590 1464 1591 1465 now = hrtimer_update_base(cpu_base); ··· 1596 1468 hrtimer_update_softirq_timer(cpu_base, true); 1597 1469 1598 1470 raw_spin_unlock_irqrestore(&cpu_base->lock, flags); 1471 + hrtimer_cpu_base_unlock_expiry(cpu_base); 1599 1472 } 1600 1473 1601 1474 #ifdef CONFIG_HIGH_RES_TIMERS ··· 1768 1639 return HRTIMER_NORESTART; 1769 1640 } 1770 1641 1771 - void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) 1642 + /** 1643 + * hrtimer_sleeper_start_expires - Start a hrtimer sleeper timer 1644 + * @sl: sleeper to be started 1645 + * @mode: timer mode abs/rel 1646 + * 1647 + * Wrapper around hrtimer_start_expires() for hrtimer_sleeper based timers 1648 + * to allow PREEMPT_RT to tweak the delivery mode (soft/hardirq context) 1649 + */ 1650 + void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl, 1651 + enum hrtimer_mode mode) 1772 1652 { 1653 + /* 1654 + * Make the enqueue delivery mode check work on RT. If the sleeper 1655 + * was initialized for hard interrupt delivery, force the mode bit. 1656 + * This is a special case for hrtimer_sleepers because 1657 + * hrtimer_init_sleeper() determines the delivery mode on RT so the 1658 + * fiddling with this decision is avoided at the call sites. 1659 + */ 1660 + if (IS_ENABLED(CONFIG_PREEMPT_RT) && sl->timer.is_hard) 1661 + mode |= HRTIMER_MODE_HARD; 1662 + 1663 + hrtimer_start_expires(&sl->timer, mode); 1664 + } 1665 + EXPORT_SYMBOL_GPL(hrtimer_sleeper_start_expires); 1666 + 1667 + static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl, 1668 + clockid_t clock_id, enum hrtimer_mode mode) 1669 + { 1670 + /* 1671 + * On PREEMPT_RT enabled kernels hrtimers which are not explicitely 1672 + * marked for hard interrupt expiry mode are moved into soft 1673 + * interrupt context either for latency reasons or because the 1674 + * hrtimer callback takes regular spinlocks or invokes other 1675 + * functions which are not suitable for hard interrupt context on 1676 + * PREEMPT_RT. 1677 + * 1678 + * The hrtimer_sleeper callback is RT compatible in hard interrupt 1679 + * context, but there is a latency concern: Untrusted userspace can 1680 + * spawn many threads which arm timers for the same expiry time on 1681 + * the same CPU. That causes a latency spike due to the wakeup of 1682 + * a gazillion threads. 1683 + * 1684 + * OTOH, priviledged real-time user space applications rely on the 1685 + * low latency of hard interrupt wakeups. If the current task is in 1686 + * a real-time scheduling class, mark the mode for hard interrupt 1687 + * expiry. 1688 + */ 1689 + if (IS_ENABLED(CONFIG_PREEMPT_RT)) { 1690 + if (task_is_realtime(current) && !(mode & HRTIMER_MODE_SOFT)) 1691 + mode |= HRTIMER_MODE_HARD; 1692 + } 1693 + 1694 + __hrtimer_init(&sl->timer, clock_id, mode); 1773 1695 sl->timer.function = hrtimer_wakeup; 1774 - sl->task = task; 1696 + sl->task = current; 1697 + } 1698 + 1699 + /** 1700 + * hrtimer_init_sleeper - initialize sleeper to the given clock 1701 + * @sl: sleeper to be initialized 1702 + * @clock_id: the clock to be used 1703 + * @mode: timer mode abs/rel 1704 + */ 1705 + void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id, 1706 + enum hrtimer_mode mode) 1707 + { 1708 + debug_init(&sl->timer, clock_id, mode); 1709 + __hrtimer_init_sleeper(sl, clock_id, mode); 1710 + 1775 1711 } 1776 1712 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper); 1777 1713 ··· 1863 1669 { 1864 1670 struct restart_block *restart; 1865 1671 1866 - hrtimer_init_sleeper(t, current); 1867 - 1868 1672 do { 1869 1673 set_current_state(TASK_INTERRUPTIBLE); 1870 - hrtimer_start_expires(&t->timer, mode); 1674 + hrtimer_sleeper_start_expires(t, mode); 1871 1675 1872 1676 if (likely(t->task)) 1873 1677 freezable_schedule(); ··· 1899 1707 struct hrtimer_sleeper t; 1900 1708 int ret; 1901 1709 1902 - hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid, 1903 - HRTIMER_MODE_ABS); 1710 + hrtimer_init_sleeper_on_stack(&t, restart->nanosleep.clockid, 1711 + HRTIMER_MODE_ABS); 1904 1712 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); 1905 - 1906 1713 ret = do_nanosleep(&t, HRTIMER_MODE_ABS); 1907 1714 destroy_hrtimer_on_stack(&t.timer); 1908 1715 return ret; ··· 1919 1728 if (dl_task(current) || rt_task(current)) 1920 1729 slack = 0; 1921 1730 1922 - hrtimer_init_on_stack(&t.timer, clockid, mode); 1731 + hrtimer_init_sleeper_on_stack(&t, clockid, mode); 1923 1732 hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack); 1924 1733 ret = do_nanosleep(&t, mode); 1925 1734 if (ret != -ERESTART_RESTARTBLOCK) ··· 2000 1809 cpu_base->softirq_next_timer = NULL; 2001 1810 cpu_base->expires_next = KTIME_MAX; 2002 1811 cpu_base->softirq_expires_next = KTIME_MAX; 1812 + hrtimer_cpu_base_init_expiry_lock(cpu_base); 2003 1813 return 0; 2004 1814 } 2005 1815 ··· 2119 1927 return -EINTR; 2120 1928 } 2121 1929 2122 - hrtimer_init_on_stack(&t.timer, clock_id, mode); 1930 + hrtimer_init_sleeper_on_stack(&t, clock_id, mode); 2123 1931 hrtimer_set_expires_range_ns(&t.timer, *expires, delta); 2124 - 2125 - hrtimer_init_sleeper(&t, current); 2126 - 2127 - hrtimer_start_expires(&t.timer, mode); 1932 + hrtimer_sleeper_start_expires(&t, mode); 2128 1933 2129 1934 if (likely(t.task)) 2130 1935 schedule();
+4 -8
kernel/time/itimer.c
··· 55 55 val = it->expires; 56 56 interval = it->incr; 57 57 if (val) { 58 - struct task_cputime cputime; 59 - u64 t; 58 + u64 t, samples[CPUCLOCK_MAX]; 60 59 61 - thread_group_cputimer(tsk, &cputime); 62 - if (clock_id == CPUCLOCK_PROF) 63 - t = cputime.utime + cputime.stime; 64 - else 65 - /* CPUCLOCK_VIRT */ 66 - t = cputime.utime; 60 + thread_group_sample_cputime(tsk, samples); 61 + t = samples[clock_id]; 67 62 68 63 if (val < t) 69 64 /* about to fire */ ··· 208 213 /* We are sharing ->siglock with it_real_fn() */ 209 214 if (hrtimer_try_to_cancel(timer) < 0) { 210 215 spin_unlock_irq(&tsk->sighand->siglock); 216 + hrtimer_cancel_wait_running(timer); 211 217 goto again; 212 218 } 213 219 expires = timeval_to_ktime(value->it_value);
+500 -526
kernel/time/posix-cpu-timers.c
··· 20 20 21 21 static void posix_cpu_timer_rearm(struct k_itimer *timer); 22 22 23 + void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit) 24 + { 25 + posix_cputimers_init(pct); 26 + if (cpu_limit != RLIM_INFINITY) { 27 + pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC; 28 + pct->timers_active = true; 29 + } 30 + } 31 + 23 32 /* 24 33 * Called after updating RLIMIT_CPU to run cpu timer and update 25 - * tsk->signal->cputime_expires expiration cache if necessary. Needs 26 - * siglock protection since other code may update expiration cache as 27 - * well. 34 + * tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if 35 + * necessary. Needs siglock protection since other code may update the 36 + * expiration cache as well. 28 37 */ 29 38 void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new) 30 39 { ··· 44 35 spin_unlock_irq(&task->sighand->siglock); 45 36 } 46 37 47 - static int check_clock(const clockid_t which_clock) 38 + /* 39 + * Functions for validating access to tasks. 40 + */ 41 + static struct task_struct *lookup_task(const pid_t pid, bool thread, 42 + bool gettime) 48 43 { 49 - int error = 0; 50 44 struct task_struct *p; 51 - const pid_t pid = CPUCLOCK_PID(which_clock); 52 45 53 - if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX) 54 - return -EINVAL; 46 + /* 47 + * If the encoded PID is 0, then the timer is targeted at current 48 + * or the process to which current belongs. 49 + */ 50 + if (!pid) 51 + return thread ? current : current->group_leader; 55 52 56 - if (pid == 0) 57 - return 0; 53 + p = find_task_by_vpid(pid); 54 + if (!p) 55 + return p; 56 + 57 + if (thread) 58 + return same_thread_group(p, current) ? p : NULL; 59 + 60 + if (gettime) { 61 + /* 62 + * For clock_gettime(PROCESS) the task does not need to be 63 + * the actual group leader. tsk->sighand gives 64 + * access to the group's clock. 65 + * 66 + * Timers need the group leader because they take a 67 + * reference on it and store the task pointer until the 68 + * timer is destroyed. 69 + */ 70 + return (p == current || thread_group_leader(p)) ? p : NULL; 71 + } 72 + 73 + /* 74 + * For processes require that p is group leader. 75 + */ 76 + return has_group_leader_pid(p) ? p : NULL; 77 + } 78 + 79 + static struct task_struct *__get_task_for_clock(const clockid_t clock, 80 + bool getref, bool gettime) 81 + { 82 + const bool thread = !!CPUCLOCK_PERTHREAD(clock); 83 + const pid_t pid = CPUCLOCK_PID(clock); 84 + struct task_struct *p; 85 + 86 + if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX) 87 + return NULL; 58 88 59 89 rcu_read_lock(); 60 - p = find_task_by_vpid(pid); 61 - if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ? 62 - same_thread_group(p, current) : has_group_leader_pid(p))) { 63 - error = -EINVAL; 64 - } 90 + p = lookup_task(pid, thread, gettime); 91 + if (p && getref) 92 + get_task_struct(p); 65 93 rcu_read_unlock(); 94 + return p; 95 + } 66 96 67 - return error; 97 + static inline struct task_struct *get_task_for_clock(const clockid_t clock) 98 + { 99 + return __get_task_for_clock(clock, true, false); 100 + } 101 + 102 + static inline struct task_struct *get_task_for_clock_get(const clockid_t clock) 103 + { 104 + return __get_task_for_clock(clock, true, true); 105 + } 106 + 107 + static inline int validate_clock_permissions(const clockid_t clock) 108 + { 109 + return __get_task_for_clock(clock, false, false) ? 0 : -EINVAL; 68 110 } 69 111 70 112 /* 71 113 * Update expiry time from increment, and increase overrun count, 72 114 * given the current clock sample. 73 115 */ 74 - static void bump_cpu_timer(struct k_itimer *timer, u64 now) 116 + static u64 bump_cpu_timer(struct k_itimer *timer, u64 now) 75 117 { 118 + u64 delta, incr, expires = timer->it.cpu.node.expires; 76 119 int i; 77 - u64 delta, incr; 78 120 79 121 if (!timer->it_interval) 80 - return; 122 + return expires; 81 123 82 - if (now < timer->it.cpu.expires) 83 - return; 124 + if (now < expires) 125 + return expires; 84 126 85 127 incr = timer->it_interval; 86 - delta = now + incr - timer->it.cpu.expires; 128 + delta = now + incr - expires; 87 129 88 130 /* Don't use (incr*2 < delta), incr*2 might overflow. */ 89 131 for (i = 0; incr < delta - incr; i++) ··· 144 84 if (delta < incr) 145 85 continue; 146 86 147 - timer->it.cpu.expires += incr; 87 + timer->it.cpu.node.expires += incr; 148 88 timer->it_overrun += 1LL << i; 149 89 delta -= incr; 150 90 } 91 + return timer->it.cpu.node.expires; 151 92 } 152 93 153 - /** 154 - * task_cputime_zero - Check a task_cputime struct for all zero fields. 155 - * 156 - * @cputime: The struct to compare. 157 - * 158 - * Checks @cputime to see if all fields are zero. Returns true if all fields 159 - * are zero, false if any field is nonzero. 160 - */ 161 - static inline int task_cputime_zero(const struct task_cputime *cputime) 94 + /* Check whether all cache entries contain U64_MAX, i.e. eternal expiry time */ 95 + static inline bool expiry_cache_is_inactive(const struct posix_cputimers *pct) 162 96 { 163 - if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime) 164 - return 1; 165 - return 0; 166 - } 167 - 168 - static inline u64 prof_ticks(struct task_struct *p) 169 - { 170 - u64 utime, stime; 171 - 172 - task_cputime(p, &utime, &stime); 173 - 174 - return utime + stime; 175 - } 176 - static inline u64 virt_ticks(struct task_struct *p) 177 - { 178 - u64 utime, stime; 179 - 180 - task_cputime(p, &utime, &stime); 181 - 182 - return utime; 97 + return !(~pct->bases[CPUCLOCK_PROF].nextevt | 98 + ~pct->bases[CPUCLOCK_VIRT].nextevt | 99 + ~pct->bases[CPUCLOCK_SCHED].nextevt); 183 100 } 184 101 185 102 static int 186 103 posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp) 187 104 { 188 - int error = check_clock(which_clock); 105 + int error = validate_clock_permissions(which_clock); 106 + 189 107 if (!error) { 190 108 tp->tv_sec = 0; 191 109 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ); ··· 180 142 } 181 143 182 144 static int 183 - posix_cpu_clock_set(const clockid_t which_clock, const struct timespec64 *tp) 145 + posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp) 184 146 { 147 + int error = validate_clock_permissions(clock); 148 + 185 149 /* 186 150 * You can never reset a CPU clock, but we check for other errors 187 151 * in the call before failing with EPERM. 188 152 */ 189 - int error = check_clock(which_clock); 190 - if (error == 0) { 191 - error = -EPERM; 192 - } 193 - return error; 153 + return error ? : -EPERM; 194 154 } 195 155 196 - 197 156 /* 198 - * Sample a per-thread clock for the given task. 157 + * Sample a per-thread clock for the given task. clkid is validated. 199 158 */ 200 - static int cpu_clock_sample(const clockid_t which_clock, 201 - struct task_struct *p, u64 *sample) 159 + static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p) 202 160 { 203 - switch (CPUCLOCK_WHICH(which_clock)) { 204 - default: 205 - return -EINVAL; 161 + u64 utime, stime; 162 + 163 + if (clkid == CPUCLOCK_SCHED) 164 + return task_sched_runtime(p); 165 + 166 + task_cputime(p, &utime, &stime); 167 + 168 + switch (clkid) { 206 169 case CPUCLOCK_PROF: 207 - *sample = prof_ticks(p); 208 - break; 170 + return utime + stime; 209 171 case CPUCLOCK_VIRT: 210 - *sample = virt_ticks(p); 211 - break; 212 - case CPUCLOCK_SCHED: 213 - *sample = task_sched_runtime(p); 214 - break; 172 + return utime; 173 + default: 174 + WARN_ON_ONCE(1); 215 175 } 216 176 return 0; 177 + } 178 + 179 + static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime) 180 + { 181 + samples[CPUCLOCK_PROF] = stime + utime; 182 + samples[CPUCLOCK_VIRT] = utime; 183 + samples[CPUCLOCK_SCHED] = rtime; 184 + } 185 + 186 + static void task_sample_cputime(struct task_struct *p, u64 *samples) 187 + { 188 + u64 stime, utime; 189 + 190 + task_cputime(p, &utime, &stime); 191 + store_samples(samples, stime, utime, p->se.sum_exec_runtime); 192 + } 193 + 194 + static void proc_sample_cputime_atomic(struct task_cputime_atomic *at, 195 + u64 *samples) 196 + { 197 + u64 stime, utime, rtime; 198 + 199 + utime = atomic64_read(&at->utime); 200 + stime = atomic64_read(&at->stime); 201 + rtime = atomic64_read(&at->sum_exec_runtime); 202 + store_samples(samples, stime, utime, rtime); 217 203 } 218 204 219 205 /* ··· 255 193 } 256 194 } 257 195 258 - static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, struct task_cputime *sum) 196 + static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, 197 + struct task_cputime *sum) 259 198 { 260 199 __update_gt_cputime(&cputime_atomic->utime, sum->utime); 261 200 __update_gt_cputime(&cputime_atomic->stime, sum->stime); 262 201 __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime); 263 202 } 264 203 265 - /* Sample task_cputime_atomic values in "atomic_timers", store results in "times". */ 266 - static inline void sample_cputime_atomic(struct task_cputime *times, 267 - struct task_cputime_atomic *atomic_times) 268 - { 269 - times->utime = atomic64_read(&atomic_times->utime); 270 - times->stime = atomic64_read(&atomic_times->stime); 271 - times->sum_exec_runtime = atomic64_read(&atomic_times->sum_exec_runtime); 272 - } 273 - 274 - void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times) 204 + /** 205 + * thread_group_sample_cputime - Sample cputime for a given task 206 + * @tsk: Task for which cputime needs to be started 207 + * @iimes: Storage for time samples 208 + * 209 + * Called from sys_getitimer() to calculate the expiry time of an active 210 + * timer. That means group cputime accounting is already active. Called 211 + * with task sighand lock held. 212 + * 213 + * Updates @times with an uptodate sample of the thread group cputimes. 214 + */ 215 + void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples) 275 216 { 276 217 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; 277 - struct task_cputime sum; 218 + struct posix_cputimers *pct = &tsk->signal->posix_cputimers; 219 + 220 + WARN_ON_ONCE(!pct->timers_active); 221 + 222 + proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); 223 + } 224 + 225 + /** 226 + * thread_group_start_cputime - Start cputime and return a sample 227 + * @tsk: Task for which cputime needs to be started 228 + * @samples: Storage for time samples 229 + * 230 + * The thread group cputime accouting is avoided when there are no posix 231 + * CPU timers armed. Before starting a timer it's required to check whether 232 + * the time accounting is active. If not, a full update of the atomic 233 + * accounting store needs to be done and the accounting enabled. 234 + * 235 + * Updates @times with an uptodate sample of the thread group cputimes. 236 + */ 237 + static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples) 238 + { 239 + struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; 240 + struct posix_cputimers *pct = &tsk->signal->posix_cputimers; 278 241 279 242 /* Check if cputimer isn't running. This is accessed without locking. */ 280 - if (!READ_ONCE(cputimer->running)) { 243 + if (!READ_ONCE(pct->timers_active)) { 244 + struct task_cputime sum; 245 + 281 246 /* 282 247 * The POSIX timer interface allows for absolute time expiry 283 248 * values through the TIMER_ABSTIME flag, therefore we have ··· 314 225 update_gt_cputime(&cputimer->cputime_atomic, &sum); 315 226 316 227 /* 317 - * We're setting cputimer->running without a lock. Ensure 318 - * this only gets written to in one operation. We set 319 - * running after update_gt_cputime() as a small optimization, 320 - * but barriers are not required because update_gt_cputime() 228 + * We're setting timers_active without a lock. Ensure this 229 + * only gets written to in one operation. We set it after 230 + * update_gt_cputime() as a small optimization, but 231 + * barriers are not required because update_gt_cputime() 321 232 * can handle concurrent updates. 322 233 */ 323 - WRITE_ONCE(cputimer->running, true); 234 + WRITE_ONCE(pct->timers_active, true); 324 235 } 325 - sample_cputime_atomic(times, &cputimer->cputime_atomic); 236 + proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); 237 + } 238 + 239 + static void __thread_group_cputime(struct task_struct *tsk, u64 *samples) 240 + { 241 + struct task_cputime ct; 242 + 243 + thread_group_cputime(tsk, &ct); 244 + store_samples(samples, ct.stime, ct.utime, ct.sum_exec_runtime); 326 245 } 327 246 328 247 /* 329 - * Sample a process (thread group) clock for the given group_leader task. 330 - * Must be called with task sighand lock held for safe while_each_thread() 331 - * traversal. 248 + * Sample a process (thread group) clock for the given task clkid. If the 249 + * group's cputime accounting is already enabled, read the atomic 250 + * store. Otherwise a full update is required. Task's sighand lock must be 251 + * held to protect the task traversal on a full update. clkid is already 252 + * validated. 332 253 */ 333 - static int cpu_clock_sample_group(const clockid_t which_clock, 334 - struct task_struct *p, 335 - u64 *sample) 254 + static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p, 255 + bool start) 336 256 { 337 - struct task_cputime cputime; 257 + struct thread_group_cputimer *cputimer = &p->signal->cputimer; 258 + struct posix_cputimers *pct = &p->signal->posix_cputimers; 259 + u64 samples[CPUCLOCK_MAX]; 338 260 339 - switch (CPUCLOCK_WHICH(which_clock)) { 340 - default: 261 + if (!READ_ONCE(pct->timers_active)) { 262 + if (start) 263 + thread_group_start_cputime(p, samples); 264 + else 265 + __thread_group_cputime(p, samples); 266 + } else { 267 + proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); 268 + } 269 + 270 + return samples[clkid]; 271 + } 272 + 273 + static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp) 274 + { 275 + const clockid_t clkid = CPUCLOCK_WHICH(clock); 276 + struct task_struct *tsk; 277 + u64 t; 278 + 279 + tsk = get_task_for_clock_get(clock); 280 + if (!tsk) 341 281 return -EINVAL; 342 - case CPUCLOCK_PROF: 343 - thread_group_cputime(p, &cputime); 344 - *sample = cputime.utime + cputime.stime; 345 - break; 346 - case CPUCLOCK_VIRT: 347 - thread_group_cputime(p, &cputime); 348 - *sample = cputime.utime; 349 - break; 350 - case CPUCLOCK_SCHED: 351 - thread_group_cputime(p, &cputime); 352 - *sample = cputime.sum_exec_runtime; 353 - break; 354 - } 282 + 283 + if (CPUCLOCK_PERTHREAD(clock)) 284 + t = cpu_clock_sample(clkid, tsk); 285 + else 286 + t = cpu_clock_sample_group(clkid, tsk, false); 287 + put_task_struct(tsk); 288 + 289 + *tp = ns_to_timespec64(t); 355 290 return 0; 356 - } 357 - 358 - static int posix_cpu_clock_get_task(struct task_struct *tsk, 359 - const clockid_t which_clock, 360 - struct timespec64 *tp) 361 - { 362 - int err = -EINVAL; 363 - u64 rtn; 364 - 365 - if (CPUCLOCK_PERTHREAD(which_clock)) { 366 - if (same_thread_group(tsk, current)) 367 - err = cpu_clock_sample(which_clock, tsk, &rtn); 368 - } else { 369 - if (tsk == current || thread_group_leader(tsk)) 370 - err = cpu_clock_sample_group(which_clock, tsk, &rtn); 371 - } 372 - 373 - if (!err) 374 - *tp = ns_to_timespec64(rtn); 375 - 376 - return err; 377 - } 378 - 379 - 380 - static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec64 *tp) 381 - { 382 - const pid_t pid = CPUCLOCK_PID(which_clock); 383 - int err = -EINVAL; 384 - 385 - if (pid == 0) { 386 - /* 387 - * Special case constant value for our own clocks. 388 - * We don't have to do any lookup to find ourselves. 389 - */ 390 - err = posix_cpu_clock_get_task(current, which_clock, tp); 391 - } else { 392 - /* 393 - * Find the given PID, and validate that the caller 394 - * should be able to see it. 395 - */ 396 - struct task_struct *p; 397 - rcu_read_lock(); 398 - p = find_task_by_vpid(pid); 399 - if (p) 400 - err = posix_cpu_clock_get_task(p, which_clock, tp); 401 - rcu_read_unlock(); 402 - } 403 - 404 - return err; 405 291 } 406 292 407 293 /* ··· 386 322 */ 387 323 static int posix_cpu_timer_create(struct k_itimer *new_timer) 388 324 { 389 - int ret = 0; 390 - const pid_t pid = CPUCLOCK_PID(new_timer->it_clock); 391 - struct task_struct *p; 325 + struct task_struct *p = get_task_for_clock(new_timer->it_clock); 392 326 393 - if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX) 327 + if (!p) 394 328 return -EINVAL; 395 329 396 330 new_timer->kclock = &clock_posix_cpu; 397 - 398 - INIT_LIST_HEAD(&new_timer->it.cpu.entry); 399 - 400 - rcu_read_lock(); 401 - if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) { 402 - if (pid == 0) { 403 - p = current; 404 - } else { 405 - p = find_task_by_vpid(pid); 406 - if (p && !same_thread_group(p, current)) 407 - p = NULL; 408 - } 409 - } else { 410 - if (pid == 0) { 411 - p = current->group_leader; 412 - } else { 413 - p = find_task_by_vpid(pid); 414 - if (p && !has_group_leader_pid(p)) 415 - p = NULL; 416 - } 417 - } 331 + timerqueue_init(&new_timer->it.cpu.node); 418 332 new_timer->it.cpu.task = p; 419 - if (p) { 420 - get_task_struct(p); 421 - } else { 422 - ret = -EINVAL; 423 - } 424 - rcu_read_unlock(); 425 - 426 - return ret; 333 + return 0; 427 334 } 428 335 429 336 /* ··· 405 370 */ 406 371 static int posix_cpu_timer_del(struct k_itimer *timer) 407 372 { 408 - int ret = 0; 409 - unsigned long flags; 373 + struct cpu_timer *ctmr = &timer->it.cpu; 374 + struct task_struct *p = ctmr->task; 410 375 struct sighand_struct *sighand; 411 - struct task_struct *p = timer->it.cpu.task; 376 + unsigned long flags; 377 + int ret = 0; 412 378 413 - WARN_ON_ONCE(p == NULL); 379 + if (WARN_ON_ONCE(!p)) 380 + return -EINVAL; 414 381 415 382 /* 416 383 * Protect against sighand release/switch in exit/exec and process/ ··· 421 384 sighand = lock_task_sighand(p, &flags); 422 385 if (unlikely(sighand == NULL)) { 423 386 /* 424 - * We raced with the reaping of the task. 425 - * The deletion should have cleared us off the list. 387 + * This raced with the reaping of the task. The exit cleanup 388 + * should have removed this timer from the timer queue. 426 389 */ 427 - WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry)); 390 + WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node)); 428 391 } else { 429 392 if (timer->it.cpu.firing) 430 393 ret = TIMER_RETRY; 431 394 else 432 - list_del(&timer->it.cpu.entry); 395 + cpu_timer_dequeue(ctmr); 433 396 434 397 unlock_task_sighand(p, &flags); 435 398 } ··· 440 403 return ret; 441 404 } 442 405 443 - static void cleanup_timers_list(struct list_head *head) 406 + static void cleanup_timerqueue(struct timerqueue_head *head) 444 407 { 445 - struct cpu_timer_list *timer, *next; 408 + struct timerqueue_node *node; 409 + struct cpu_timer *ctmr; 446 410 447 - list_for_each_entry_safe(timer, next, head, entry) 448 - list_del_init(&timer->entry); 411 + while ((node = timerqueue_getnext(head))) { 412 + timerqueue_del(head, node); 413 + ctmr = container_of(node, struct cpu_timer, node); 414 + ctmr->head = NULL; 415 + } 449 416 } 450 417 451 418 /* 452 - * Clean out CPU timers still ticking when a thread exited. The task 453 - * pointer is cleared, and the expiry time is replaced with the residual 454 - * time for later timer_gettime calls to return. 419 + * Clean out CPU timers which are still armed when a thread exits. The 420 + * timers are only removed from the list. No other updates are done. The 421 + * corresponding posix timers are still accessible, but cannot be rearmed. 422 + * 455 423 * This must be called with the siglock held. 456 424 */ 457 - static void cleanup_timers(struct list_head *head) 425 + static void cleanup_timers(struct posix_cputimers *pct) 458 426 { 459 - cleanup_timers_list(head); 460 - cleanup_timers_list(++head); 461 - cleanup_timers_list(++head); 427 + cleanup_timerqueue(&pct->bases[CPUCLOCK_PROF].tqhead); 428 + cleanup_timerqueue(&pct->bases[CPUCLOCK_VIRT].tqhead); 429 + cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead); 462 430 } 463 431 464 432 /* ··· 473 431 */ 474 432 void posix_cpu_timers_exit(struct task_struct *tsk) 475 433 { 476 - cleanup_timers(tsk->cpu_timers); 434 + cleanup_timers(&tsk->posix_cputimers); 477 435 } 478 436 void posix_cpu_timers_exit_group(struct task_struct *tsk) 479 437 { 480 - cleanup_timers(tsk->signal->cpu_timers); 481 - } 482 - 483 - static inline int expires_gt(u64 expires, u64 new_exp) 484 - { 485 - return expires == 0 || expires > new_exp; 438 + cleanup_timers(&tsk->signal->posix_cputimers); 486 439 } 487 440 488 441 /* ··· 486 449 */ 487 450 static void arm_timer(struct k_itimer *timer) 488 451 { 489 - struct task_struct *p = timer->it.cpu.task; 490 - struct list_head *head, *listpos; 491 - struct task_cputime *cputime_expires; 492 - struct cpu_timer_list *const nt = &timer->it.cpu; 493 - struct cpu_timer_list *next; 452 + int clkidx = CPUCLOCK_WHICH(timer->it_clock); 453 + struct cpu_timer *ctmr = &timer->it.cpu; 454 + u64 newexp = cpu_timer_getexpires(ctmr); 455 + struct task_struct *p = ctmr->task; 456 + struct posix_cputimer_base *base; 494 457 495 - if (CPUCLOCK_PERTHREAD(timer->it_clock)) { 496 - head = p->cpu_timers; 497 - cputime_expires = &p->cputime_expires; 498 - } else { 499 - head = p->signal->cpu_timers; 500 - cputime_expires = &p->signal->cputime_expires; 501 - } 502 - head += CPUCLOCK_WHICH(timer->it_clock); 458 + if (CPUCLOCK_PERTHREAD(timer->it_clock)) 459 + base = p->posix_cputimers.bases + clkidx; 460 + else 461 + base = p->signal->posix_cputimers.bases + clkidx; 503 462 504 - listpos = head; 505 - list_for_each_entry(next, head, entry) { 506 - if (nt->expires < next->expires) 507 - break; 508 - listpos = &next->entry; 509 - } 510 - list_add(&nt->entry, listpos); 463 + if (!cpu_timer_enqueue(&base->tqhead, ctmr)) 464 + return; 511 465 512 - if (listpos == head) { 513 - u64 exp = nt->expires; 466 + /* 467 + * We are the new earliest-expiring POSIX 1.b timer, hence 468 + * need to update expiration cache. Take into account that 469 + * for process timers we share expiration cache with itimers 470 + * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME. 471 + */ 472 + if (newexp < base->nextevt) 473 + base->nextevt = newexp; 514 474 515 - /* 516 - * We are the new earliest-expiring POSIX 1.b timer, hence 517 - * need to update expiration cache. Take into account that 518 - * for process timers we share expiration cache with itimers 519 - * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME. 520 - */ 521 - 522 - switch (CPUCLOCK_WHICH(timer->it_clock)) { 523 - case CPUCLOCK_PROF: 524 - if (expires_gt(cputime_expires->prof_exp, exp)) 525 - cputime_expires->prof_exp = exp; 526 - break; 527 - case CPUCLOCK_VIRT: 528 - if (expires_gt(cputime_expires->virt_exp, exp)) 529 - cputime_expires->virt_exp = exp; 530 - break; 531 - case CPUCLOCK_SCHED: 532 - if (expires_gt(cputime_expires->sched_exp, exp)) 533 - cputime_expires->sched_exp = exp; 534 - break; 535 - } 536 - if (CPUCLOCK_PERTHREAD(timer->it_clock)) 537 - tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER); 538 - else 539 - tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER); 540 - } 475 + if (CPUCLOCK_PERTHREAD(timer->it_clock)) 476 + tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER); 477 + else 478 + tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER); 541 479 } 542 480 543 481 /* ··· 520 508 */ 521 509 static void cpu_timer_fire(struct k_itimer *timer) 522 510 { 511 + struct cpu_timer *ctmr = &timer->it.cpu; 512 + 523 513 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { 524 514 /* 525 515 * User don't want any signal. 526 516 */ 527 - timer->it.cpu.expires = 0; 517 + cpu_timer_setexpires(ctmr, 0); 528 518 } else if (unlikely(timer->sigq == NULL)) { 529 519 /* 530 520 * This a special case for clock_nanosleep, 531 521 * not a normal timer from sys_timer_create. 532 522 */ 533 523 wake_up_process(timer->it_process); 534 - timer->it.cpu.expires = 0; 524 + cpu_timer_setexpires(ctmr, 0); 535 525 } else if (!timer->it_interval) { 536 526 /* 537 527 * One-shot timer. Clear it as soon as it's fired. 538 528 */ 539 529 posix_timer_event(timer, 0); 540 - timer->it.cpu.expires = 0; 530 + cpu_timer_setexpires(ctmr, 0); 541 531 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { 542 532 /* 543 533 * The signal did not get queued because the signal ··· 553 539 } 554 540 555 541 /* 556 - * Sample a process (thread group) timer for the given group_leader task. 557 - * Must be called with task sighand lock held for safe while_each_thread() 558 - * traversal. 559 - */ 560 - static int cpu_timer_sample_group(const clockid_t which_clock, 561 - struct task_struct *p, u64 *sample) 562 - { 563 - struct task_cputime cputime; 564 - 565 - thread_group_cputimer(p, &cputime); 566 - switch (CPUCLOCK_WHICH(which_clock)) { 567 - default: 568 - return -EINVAL; 569 - case CPUCLOCK_PROF: 570 - *sample = cputime.utime + cputime.stime; 571 - break; 572 - case CPUCLOCK_VIRT: 573 - *sample = cputime.utime; 574 - break; 575 - case CPUCLOCK_SCHED: 576 - *sample = cputime.sum_exec_runtime; 577 - break; 578 - } 579 - return 0; 580 - } 581 - 582 - /* 583 542 * Guts of sys_timer_settime for CPU timers. 584 543 * This is called with the timer locked and interrupts disabled. 585 544 * If we return TIMER_RETRY, it's necessary to release the timer's lock ··· 561 574 static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, 562 575 struct itimerspec64 *new, struct itimerspec64 *old) 563 576 { 564 - unsigned long flags; 565 - struct sighand_struct *sighand; 566 - struct task_struct *p = timer->it.cpu.task; 577 + clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); 567 578 u64 old_expires, new_expires, old_incr, val; 568 - int ret; 579 + struct cpu_timer *ctmr = &timer->it.cpu; 580 + struct task_struct *p = ctmr->task; 581 + struct sighand_struct *sighand; 582 + unsigned long flags; 583 + int ret = 0; 569 584 570 - WARN_ON_ONCE(p == NULL); 585 + if (WARN_ON_ONCE(!p)) 586 + return -EINVAL; 571 587 572 588 /* 573 589 * Use the to_ktime conversion because that clamps the maximum ··· 587 597 * If p has just been reaped, we can no 588 598 * longer get any information about it at all. 589 599 */ 590 - if (unlikely(sighand == NULL)) { 600 + if (unlikely(sighand == NULL)) 591 601 return -ESRCH; 592 - } 593 602 594 603 /* 595 604 * Disarm any old timer after extracting its expiry time. 596 605 */ 597 - 598 - ret = 0; 599 606 old_incr = timer->it_interval; 600 - old_expires = timer->it.cpu.expires; 607 + old_expires = cpu_timer_getexpires(ctmr); 608 + 601 609 if (unlikely(timer->it.cpu.firing)) { 602 610 timer->it.cpu.firing = -1; 603 611 ret = TIMER_RETRY; 604 - } else 605 - list_del_init(&timer->it.cpu.entry); 612 + } else { 613 + cpu_timer_dequeue(ctmr); 614 + } 606 615 607 616 /* 608 617 * We need to sample the current value to convert the new ··· 611 622 * times (in arm_timer). With an absolute time, we must 612 623 * check if it's already passed. In short, we need a sample. 613 624 */ 614 - if (CPUCLOCK_PERTHREAD(timer->it_clock)) { 615 - cpu_clock_sample(timer->it_clock, p, &val); 616 - } else { 617 - cpu_timer_sample_group(timer->it_clock, p, &val); 618 - } 625 + if (CPUCLOCK_PERTHREAD(timer->it_clock)) 626 + val = cpu_clock_sample(clkid, p); 627 + else 628 + val = cpu_clock_sample_group(clkid, p, true); 619 629 620 630 if (old) { 621 631 if (old_expires == 0) { ··· 622 634 old->it_value.tv_nsec = 0; 623 635 } else { 624 636 /* 625 - * Update the timer in case it has 626 - * overrun already. If it has, 627 - * we'll report it as having overrun 628 - * and with the next reloaded timer 629 - * already ticking, though we are 630 - * swallowing that pending 631 - * notification here to install the 632 - * new setting. 637 + * Update the timer in case it has overrun already. 638 + * If it has, we'll report it as having overrun and 639 + * with the next reloaded timer already ticking, 640 + * though we are swallowing that pending 641 + * notification here to install the new setting. 633 642 */ 634 - bump_cpu_timer(timer, val); 635 - if (val < timer->it.cpu.expires) { 636 - old_expires = timer->it.cpu.expires - val; 643 + u64 exp = bump_cpu_timer(timer, val); 644 + 645 + if (val < exp) { 646 + old_expires = exp - val; 637 647 old->it_value = ns_to_timespec64(old_expires); 638 648 } else { 639 649 old->it_value.tv_nsec = 1; ··· 660 674 * For a timer with no notification action, we don't actually 661 675 * arm the timer (we'll just fake it for timer_gettime). 662 676 */ 663 - timer->it.cpu.expires = new_expires; 677 + cpu_timer_setexpires(ctmr, new_expires); 664 678 if (new_expires != 0 && val < new_expires) { 665 679 arm_timer(timer); 666 680 } ··· 701 715 702 716 static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp) 703 717 { 704 - u64 now; 705 - struct task_struct *p = timer->it.cpu.task; 718 + clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); 719 + struct cpu_timer *ctmr = &timer->it.cpu; 720 + u64 now, expires = cpu_timer_getexpires(ctmr); 721 + struct task_struct *p = ctmr->task; 706 722 707 - WARN_ON_ONCE(p == NULL); 723 + if (WARN_ON_ONCE(!p)) 724 + return; 708 725 709 726 /* 710 727 * Easy part: convert the reload time. 711 728 */ 712 729 itp->it_interval = ktime_to_timespec64(timer->it_interval); 713 730 714 - if (!timer->it.cpu.expires) 731 + if (!expires) 715 732 return; 716 733 717 734 /* 718 735 * Sample the clock to take the difference with the expiry time. 719 736 */ 720 737 if (CPUCLOCK_PERTHREAD(timer->it_clock)) { 721 - cpu_clock_sample(timer->it_clock, p, &now); 738 + now = cpu_clock_sample(clkid, p); 722 739 } else { 723 740 struct sighand_struct *sighand; 724 741 unsigned long flags; ··· 736 747 /* 737 748 * The process has been reaped. 738 749 * We can't even collect a sample any more. 739 - * Call the timer disarmed, nothing else to do. 750 + * Disarm the timer, nothing else to do. 740 751 */ 741 - timer->it.cpu.expires = 0; 752 + cpu_timer_setexpires(ctmr, 0); 742 753 return; 743 754 } else { 744 - cpu_timer_sample_group(timer->it_clock, p, &now); 755 + now = cpu_clock_sample_group(clkid, p, false); 745 756 unlock_task_sighand(p, &flags); 746 757 } 747 758 } 748 759 749 - if (now < timer->it.cpu.expires) { 750 - itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now); 760 + if (now < expires) { 761 + itp->it_value = ns_to_timespec64(expires - now); 751 762 } else { 752 763 /* 753 764 * The timer should have expired already, but the firing ··· 758 769 } 759 770 } 760 771 761 - static unsigned long long 762 - check_timers_list(struct list_head *timers, 763 - struct list_head *firing, 764 - unsigned long long curr) 772 + #define MAX_COLLECTED 20 773 + 774 + static u64 collect_timerqueue(struct timerqueue_head *head, 775 + struct list_head *firing, u64 now) 765 776 { 766 - int maxfire = 20; 777 + struct timerqueue_node *next; 778 + int i = 0; 767 779 768 - while (!list_empty(timers)) { 769 - struct cpu_timer_list *t; 780 + while ((next = timerqueue_getnext(head))) { 781 + struct cpu_timer *ctmr; 782 + u64 expires; 770 783 771 - t = list_first_entry(timers, struct cpu_timer_list, entry); 784 + ctmr = container_of(next, struct cpu_timer, node); 785 + expires = cpu_timer_getexpires(ctmr); 786 + /* Limit the number of timers to expire at once */ 787 + if (++i == MAX_COLLECTED || now < expires) 788 + return expires; 772 789 773 - if (!--maxfire || curr < t->expires) 774 - return t->expires; 775 - 776 - t->firing = 1; 777 - list_move_tail(&t->entry, firing); 790 + ctmr->firing = 1; 791 + cpu_timer_dequeue(ctmr); 792 + list_add_tail(&ctmr->elist, firing); 778 793 } 779 794 780 - return 0; 795 + return U64_MAX; 796 + } 797 + 798 + static void collect_posix_cputimers(struct posix_cputimers *pct, u64 *samples, 799 + struct list_head *firing) 800 + { 801 + struct posix_cputimer_base *base = pct->bases; 802 + int i; 803 + 804 + for (i = 0; i < CPUCLOCK_MAX; i++, base++) { 805 + base->nextevt = collect_timerqueue(&base->tqhead, firing, 806 + samples[i]); 807 + } 781 808 } 782 809 783 810 static inline void check_dl_overrun(struct task_struct *tsk) ··· 804 799 } 805 800 } 806 801 802 + static bool check_rlimit(u64 time, u64 limit, int signo, bool rt, bool hard) 803 + { 804 + if (time < limit) 805 + return false; 806 + 807 + if (print_fatal_signals) { 808 + pr_info("%s Watchdog Timeout (%s): %s[%d]\n", 809 + rt ? "RT" : "CPU", hard ? "hard" : "soft", 810 + current->comm, task_pid_nr(current)); 811 + } 812 + __group_send_sig_info(signo, SEND_SIG_PRIV, current); 813 + return true; 814 + } 815 + 807 816 /* 808 817 * Check for any per-thread CPU timers that have fired and move them off 809 818 * the tsk->cpu_timers[N] list onto the firing list. Here we update the ··· 826 807 static void check_thread_timers(struct task_struct *tsk, 827 808 struct list_head *firing) 828 809 { 829 - struct list_head *timers = tsk->cpu_timers; 830 - struct task_cputime *tsk_expires = &tsk->cputime_expires; 831 - u64 expires; 810 + struct posix_cputimers *pct = &tsk->posix_cputimers; 811 + u64 samples[CPUCLOCK_MAX]; 832 812 unsigned long soft; 833 813 834 814 if (dl_task(tsk)) 835 815 check_dl_overrun(tsk); 836 816 837 - /* 838 - * If cputime_expires is zero, then there are no active 839 - * per thread CPU timers. 840 - */ 841 - if (task_cputime_zero(&tsk->cputime_expires)) 817 + if (expiry_cache_is_inactive(pct)) 842 818 return; 843 819 844 - expires = check_timers_list(timers, firing, prof_ticks(tsk)); 845 - tsk_expires->prof_exp = expires; 846 - 847 - expires = check_timers_list(++timers, firing, virt_ticks(tsk)); 848 - tsk_expires->virt_exp = expires; 849 - 850 - tsk_expires->sched_exp = check_timers_list(++timers, firing, 851 - tsk->se.sum_exec_runtime); 820 + task_sample_cputime(tsk, samples); 821 + collect_posix_cputimers(pct, samples, firing); 852 822 853 823 /* 854 824 * Check for the special case thread timers. 855 825 */ 856 826 soft = task_rlimit(tsk, RLIMIT_RTTIME); 857 827 if (soft != RLIM_INFINITY) { 828 + /* Task RT timeout is accounted in jiffies. RTTIME is usec */ 829 + unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ); 858 830 unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME); 859 831 832 + /* At the hard limit, send SIGKILL. No further action. */ 860 833 if (hard != RLIM_INFINITY && 861 - tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) { 862 - /* 863 - * At the hard limit, we just die. 864 - * No need to calculate anything else now. 865 - */ 866 - if (print_fatal_signals) { 867 - pr_info("CPU Watchdog Timeout (hard): %s[%d]\n", 868 - tsk->comm, task_pid_nr(tsk)); 869 - } 870 - __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); 834 + check_rlimit(rttime, hard, SIGKILL, true, true)) 871 835 return; 872 - } 873 - if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) { 874 - /* 875 - * At the soft limit, send a SIGXCPU every second. 876 - */ 877 - if (soft < hard) { 878 - soft += USEC_PER_SEC; 879 - tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = 880 - soft; 881 - } 882 - if (print_fatal_signals) { 883 - pr_info("RT Watchdog Timeout (soft): %s[%d]\n", 884 - tsk->comm, task_pid_nr(tsk)); 885 - } 886 - __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); 836 + 837 + /* At the soft limit, send a SIGXCPU every second */ 838 + if (check_rlimit(rttime, soft, SIGXCPU, true, false)) { 839 + soft += USEC_PER_SEC; 840 + tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft; 887 841 } 888 842 } 889 - if (task_cputime_zero(tsk_expires)) 843 + 844 + if (expiry_cache_is_inactive(pct)) 890 845 tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER); 891 846 } 892 847 893 848 static inline void stop_process_timers(struct signal_struct *sig) 894 849 { 895 - struct thread_group_cputimer *cputimer = &sig->cputimer; 850 + struct posix_cputimers *pct = &sig->posix_cputimers; 896 851 897 - /* Turn off cputimer->running. This is done without locking. */ 898 - WRITE_ONCE(cputimer->running, false); 852 + /* Turn off the active flag. This is done without locking. */ 853 + WRITE_ONCE(pct->timers_active, false); 899 854 tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER); 900 855 } 901 856 ··· 891 898 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk); 892 899 } 893 900 894 - if (it->expires && (!*expires || it->expires < *expires)) 901 + if (it->expires && it->expires < *expires) 895 902 *expires = it->expires; 896 903 } 897 904 ··· 904 911 struct list_head *firing) 905 912 { 906 913 struct signal_struct *const sig = tsk->signal; 907 - u64 utime, ptime, virt_expires, prof_expires; 908 - u64 sum_sched_runtime, sched_expires; 909 - struct list_head *timers = sig->cpu_timers; 910 - struct task_cputime cputime; 914 + struct posix_cputimers *pct = &sig->posix_cputimers; 915 + u64 samples[CPUCLOCK_MAX]; 911 916 unsigned long soft; 912 917 913 918 /* 914 - * If cputimer is not running, then there are no active 915 - * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU). 919 + * If there are no active process wide timers (POSIX 1.b, itimers, 920 + * RLIMIT_CPU) nothing to check. Also skip the process wide timer 921 + * processing when there is already another task handling them. 916 922 */ 917 - if (!READ_ONCE(tsk->signal->cputimer.running)) 923 + if (!READ_ONCE(pct->timers_active) || pct->expiry_active) 918 924 return; 919 925 920 - /* 926 + /* 921 927 * Signify that a thread is checking for process timers. 922 928 * Write access to this field is protected by the sighand lock. 923 929 */ 924 - sig->cputimer.checking_timer = true; 930 + pct->expiry_active = true; 925 931 926 932 /* 927 - * Collect the current process totals. 933 + * Collect the current process totals. Group accounting is active 934 + * so the sample can be taken directly. 928 935 */ 929 - thread_group_cputimer(tsk, &cputime); 930 - utime = cputime.utime; 931 - ptime = utime + cputime.stime; 932 - sum_sched_runtime = cputime.sum_exec_runtime; 933 - 934 - prof_expires = check_timers_list(timers, firing, ptime); 935 - virt_expires = check_timers_list(++timers, firing, utime); 936 - sched_expires = check_timers_list(++timers, firing, sum_sched_runtime); 936 + proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples); 937 + collect_posix_cputimers(pct, samples, firing); 937 938 938 939 /* 939 940 * Check for the special case process timers. 940 941 */ 941 - check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime, 942 - SIGPROF); 943 - check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime, 944 - SIGVTALRM); 942 + check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], 943 + &pct->bases[CPUCLOCK_PROF].nextevt, 944 + samples[CPUCLOCK_PROF], SIGPROF); 945 + check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], 946 + &pct->bases[CPUCLOCK_VIRT].nextevt, 947 + samples[CPUCLOCK_VIRT], SIGVTALRM); 948 + 945 949 soft = task_rlimit(tsk, RLIMIT_CPU); 946 950 if (soft != RLIM_INFINITY) { 947 - unsigned long psecs = div_u64(ptime, NSEC_PER_SEC); 951 + /* RLIMIT_CPU is in seconds. Samples are nanoseconds */ 948 952 unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU); 949 - u64 x; 950 - if (psecs >= hard) { 951 - /* 952 - * At the hard limit, we just die. 953 - * No need to calculate anything else now. 954 - */ 955 - if (print_fatal_signals) { 956 - pr_info("RT Watchdog Timeout (hard): %s[%d]\n", 957 - tsk->comm, task_pid_nr(tsk)); 958 - } 959 - __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); 953 + u64 ptime = samples[CPUCLOCK_PROF]; 954 + u64 softns = (u64)soft * NSEC_PER_SEC; 955 + u64 hardns = (u64)hard * NSEC_PER_SEC; 956 + 957 + /* At the hard limit, send SIGKILL. No further action. */ 958 + if (hard != RLIM_INFINITY && 959 + check_rlimit(ptime, hardns, SIGKILL, false, true)) 960 960 return; 961 + 962 + /* At the soft limit, send a SIGXCPU every second */ 963 + if (check_rlimit(ptime, softns, SIGXCPU, false, false)) { 964 + sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1; 965 + softns += NSEC_PER_SEC; 961 966 } 962 - if (psecs >= soft) { 963 - /* 964 - * At the soft limit, send a SIGXCPU every second. 965 - */ 966 - if (print_fatal_signals) { 967 - pr_info("CPU Watchdog Timeout (soft): %s[%d]\n", 968 - tsk->comm, task_pid_nr(tsk)); 969 - } 970 - __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); 971 - if (soft < hard) { 972 - soft++; 973 - sig->rlim[RLIMIT_CPU].rlim_cur = soft; 974 - } 975 - } 976 - x = soft * NSEC_PER_SEC; 977 - if (!prof_expires || x < prof_expires) 978 - prof_expires = x; 967 + 968 + /* Update the expiry cache */ 969 + if (softns < pct->bases[CPUCLOCK_PROF].nextevt) 970 + pct->bases[CPUCLOCK_PROF].nextevt = softns; 979 971 } 980 972 981 - sig->cputime_expires.prof_exp = prof_expires; 982 - sig->cputime_expires.virt_exp = virt_expires; 983 - sig->cputime_expires.sched_exp = sched_expires; 984 - if (task_cputime_zero(&sig->cputime_expires)) 973 + if (expiry_cache_is_inactive(pct)) 985 974 stop_process_timers(sig); 986 975 987 - sig->cputimer.checking_timer = false; 976 + pct->expiry_active = false; 988 977 } 989 978 990 979 /* ··· 975 1000 */ 976 1001 static void posix_cpu_timer_rearm(struct k_itimer *timer) 977 1002 { 1003 + clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); 1004 + struct cpu_timer *ctmr = &timer->it.cpu; 1005 + struct task_struct *p = ctmr->task; 978 1006 struct sighand_struct *sighand; 979 1007 unsigned long flags; 980 - struct task_struct *p = timer->it.cpu.task; 981 1008 u64 now; 982 1009 983 - WARN_ON_ONCE(p == NULL); 1010 + if (WARN_ON_ONCE(!p)) 1011 + return; 984 1012 985 1013 /* 986 1014 * Fetch the current sample and update the timer's expiry time. 987 1015 */ 988 1016 if (CPUCLOCK_PERTHREAD(timer->it_clock)) { 989 - cpu_clock_sample(timer->it_clock, p, &now); 1017 + now = cpu_clock_sample(clkid, p); 990 1018 bump_cpu_timer(timer, now); 991 1019 if (unlikely(p->exit_state)) 992 1020 return; ··· 1009 1031 * The process has been reaped. 1010 1032 * We can't even collect a sample any more. 1011 1033 */ 1012 - timer->it.cpu.expires = 0; 1034 + cpu_timer_setexpires(ctmr, 0); 1013 1035 return; 1014 1036 } else if (unlikely(p->exit_state) && thread_group_empty(p)) { 1015 1037 /* If the process is dying, no need to rearm */ 1016 1038 goto unlock; 1017 1039 } 1018 - cpu_timer_sample_group(timer->it_clock, p, &now); 1040 + now = cpu_clock_sample_group(clkid, p, true); 1019 1041 bump_cpu_timer(timer, now); 1020 1042 /* Leave the sighand locked for the call below. */ 1021 1043 } ··· 1029 1051 } 1030 1052 1031 1053 /** 1032 - * task_cputime_expired - Compare two task_cputime entities. 1054 + * task_cputimers_expired - Check whether posix CPU timers are expired 1033 1055 * 1034 - * @sample: The task_cputime structure to be checked for expiration. 1035 - * @expires: Expiration times, against which @sample will be checked. 1056 + * @samples: Array of current samples for the CPUCLOCK clocks 1057 + * @pct: Pointer to a posix_cputimers container 1036 1058 * 1037 - * Checks @sample against @expires to see if any field of @sample has expired. 1038 - * Returns true if any field of the former is greater than the corresponding 1039 - * field of the latter if the latter field is set. Otherwise returns false. 1059 + * Returns true if any member of @samples is greater than the corresponding 1060 + * member of @pct->bases[CLK].nextevt. False otherwise 1040 1061 */ 1041 - static inline int task_cputime_expired(const struct task_cputime *sample, 1042 - const struct task_cputime *expires) 1062 + static inline bool 1063 + task_cputimers_expired(const u64 *sample, struct posix_cputimers *pct) 1043 1064 { 1044 - if (expires->utime && sample->utime >= expires->utime) 1045 - return 1; 1046 - if (expires->stime && sample->utime + sample->stime >= expires->stime) 1047 - return 1; 1048 - if (expires->sum_exec_runtime != 0 && 1049 - sample->sum_exec_runtime >= expires->sum_exec_runtime) 1050 - return 1; 1051 - return 0; 1065 + int i; 1066 + 1067 + for (i = 0; i < CPUCLOCK_MAX; i++) { 1068 + if (sample[i] >= pct->bases[i].nextevt) 1069 + return true; 1070 + } 1071 + return false; 1052 1072 } 1053 1073 1054 1074 /** ··· 1059 1083 * timers and compare them with the corresponding expiration times. Return 1060 1084 * true if a timer has expired, else return false. 1061 1085 */ 1062 - static inline int fastpath_timer_check(struct task_struct *tsk) 1086 + static inline bool fastpath_timer_check(struct task_struct *tsk) 1063 1087 { 1088 + struct posix_cputimers *pct = &tsk->posix_cputimers; 1064 1089 struct signal_struct *sig; 1065 1090 1066 - if (!task_cputime_zero(&tsk->cputime_expires)) { 1067 - struct task_cputime task_sample; 1091 + if (!expiry_cache_is_inactive(pct)) { 1092 + u64 samples[CPUCLOCK_MAX]; 1068 1093 1069 - task_cputime(tsk, &task_sample.utime, &task_sample.stime); 1070 - task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime; 1071 - if (task_cputime_expired(&task_sample, &tsk->cputime_expires)) 1072 - return 1; 1094 + task_sample_cputime(tsk, samples); 1095 + if (task_cputimers_expired(samples, pct)) 1096 + return true; 1073 1097 } 1074 1098 1075 1099 sig = tsk->signal; 1100 + pct = &sig->posix_cputimers; 1076 1101 /* 1077 - * Check if thread group timers expired when the cputimer is 1078 - * running and no other thread in the group is already checking 1079 - * for thread group cputimers. These fields are read without the 1080 - * sighand lock. However, this is fine because this is meant to 1081 - * be a fastpath heuristic to determine whether we should try to 1082 - * acquire the sighand lock to check/handle timers. 1102 + * Check if thread group timers expired when timers are active and 1103 + * no other thread in the group is already handling expiry for 1104 + * thread group cputimers. These fields are read without the 1105 + * sighand lock. However, this is fine because this is meant to be 1106 + * a fastpath heuristic to determine whether we should try to 1107 + * acquire the sighand lock to handle timer expiry. 1083 1108 * 1084 - * In the worst case scenario, if 'running' or 'checking_timer' gets 1085 - * set but the current thread doesn't see the change yet, we'll wait 1086 - * until the next thread in the group gets a scheduler interrupt to 1087 - * handle the timer. This isn't an issue in practice because these 1088 - * types of delays with signals actually getting sent are expected. 1109 + * In the worst case scenario, if concurrently timers_active is set 1110 + * or expiry_active is cleared, but the current thread doesn't see 1111 + * the change yet, the timer checks are delayed until the next 1112 + * thread in the group gets a scheduler interrupt to handle the 1113 + * timer. This isn't an issue in practice because these types of 1114 + * delays with signals actually getting sent are expected. 1089 1115 */ 1090 - if (READ_ONCE(sig->cputimer.running) && 1091 - !READ_ONCE(sig->cputimer.checking_timer)) { 1092 - struct task_cputime group_sample; 1116 + if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) { 1117 + u64 samples[CPUCLOCK_MAX]; 1093 1118 1094 - sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic); 1119 + proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, 1120 + samples); 1095 1121 1096 - if (task_cputime_expired(&group_sample, &sig->cputime_expires)) 1097 - return 1; 1122 + if (task_cputimers_expired(samples, pct)) 1123 + return true; 1098 1124 } 1099 1125 1100 1126 if (dl_task(tsk) && tsk->dl.dl_overrun) 1101 - return 1; 1127 + return true; 1102 1128 1103 - return 0; 1129 + return false; 1104 1130 } 1105 1131 1106 1132 /* ··· 1110 1132 * already updated our counts. We need to check if any timers fire now. 1111 1133 * Interrupts are disabled. 1112 1134 */ 1113 - void run_posix_cpu_timers(struct task_struct *tsk) 1135 + void run_posix_cpu_timers(void) 1114 1136 { 1115 - LIST_HEAD(firing); 1137 + struct task_struct *tsk = current; 1116 1138 struct k_itimer *timer, *next; 1117 1139 unsigned long flags; 1140 + LIST_HEAD(firing); 1118 1141 1119 1142 lockdep_assert_irqs_disabled(); 1120 1143 ··· 1153 1174 * each timer's lock before clearing its firing flag, so no 1154 1175 * timer call will interfere. 1155 1176 */ 1156 - list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) { 1177 + list_for_each_entry_safe(timer, next, &firing, it.cpu.elist) { 1157 1178 int cpu_firing; 1158 1179 1159 1180 spin_lock(&timer->it_lock); 1160 - list_del_init(&timer->it.cpu.entry); 1181 + list_del_init(&timer->it.cpu.elist); 1161 1182 cpu_firing = timer->it.cpu.firing; 1162 1183 timer->it.cpu.firing = 0; 1163 1184 /* ··· 1175 1196 * Set one of the process-wide special case CPU timers or RLIMIT_CPU. 1176 1197 * The tsk->sighand->siglock must be held by the caller. 1177 1198 */ 1178 - void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx, 1199 + void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid, 1179 1200 u64 *newval, u64 *oldval) 1180 1201 { 1181 - u64 now; 1182 - int ret; 1202 + u64 now, *nextevt; 1183 1203 1184 - WARN_ON_ONCE(clock_idx == CPUCLOCK_SCHED); 1185 - ret = cpu_timer_sample_group(clock_idx, tsk, &now); 1204 + if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED)) 1205 + return; 1186 1206 1187 - if (oldval && ret != -EINVAL) { 1207 + nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt; 1208 + now = cpu_clock_sample_group(clkid, tsk, true); 1209 + 1210 + if (oldval) { 1188 1211 /* 1189 1212 * We are setting itimer. The *oldval is absolute and we update 1190 1213 * it to be relative, *newval argument is relative and we update ··· 1207 1226 } 1208 1227 1209 1228 /* 1210 - * Update expiration cache if we are the earliest timer, or eventually 1211 - * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire. 1229 + * Update expiration cache if this is the earliest timer. CPUCLOCK_PROF 1230 + * expiry cache is also used by RLIMIT_CPU!. 1212 1231 */ 1213 - switch (clock_idx) { 1214 - case CPUCLOCK_PROF: 1215 - if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval)) 1216 - tsk->signal->cputime_expires.prof_exp = *newval; 1217 - break; 1218 - case CPUCLOCK_VIRT: 1219 - if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval)) 1220 - tsk->signal->cputime_expires.virt_exp = *newval; 1221 - break; 1222 - } 1232 + if (*newval < *nextevt) 1233 + *nextevt = *newval; 1223 1234 1224 1235 tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER); 1225 1236 } ··· 1233 1260 timer.it_overrun = -1; 1234 1261 error = posix_cpu_timer_create(&timer); 1235 1262 timer.it_process = current; 1263 + 1236 1264 if (!error) { 1237 1265 static struct itimerspec64 zero_it; 1238 1266 struct restart_block *restart; ··· 1249 1275 } 1250 1276 1251 1277 while (!signal_pending(current)) { 1252 - if (timer.it.cpu.expires == 0) { 1278 + if (!cpu_timer_getexpires(&timer.it.cpu)) { 1253 1279 /* 1254 1280 * Our timer fired and was reset, below 1255 1281 * deletion can not fail. ··· 1271 1297 /* 1272 1298 * We were interrupted by a signal. 1273 1299 */ 1274 - expires = timer.it.cpu.expires; 1300 + expires = cpu_timer_getexpires(&timer.it.cpu); 1275 1301 error = posix_cpu_timer_set(&timer, 0, &zero_it, &it); 1276 1302 if (!error) { 1277 1303 /*
+50 -11
kernel/time/posix-timers.c
··· 442 442 443 443 static void k_itimer_rcu_free(struct rcu_head *head) 444 444 { 445 - struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu); 445 + struct k_itimer *tmr = container_of(head, struct k_itimer, rcu); 446 446 447 447 kmem_cache_free(posix_timers_cache, tmr); 448 448 } ··· 459 459 } 460 460 put_pid(tmr->it_pid); 461 461 sigqueue_free(tmr->sigq); 462 - call_rcu(&tmr->it.rcu, k_itimer_rcu_free); 462 + call_rcu(&tmr->rcu, k_itimer_rcu_free); 463 463 } 464 464 465 465 static int common_timer_create(struct k_itimer *new_timer) ··· 805 805 return hrtimer_try_to_cancel(&timr->it.real.timer); 806 806 } 807 807 808 + static void common_timer_wait_running(struct k_itimer *timer) 809 + { 810 + hrtimer_cancel_wait_running(&timer->it.real.timer); 811 + } 812 + 813 + /* 814 + * On PREEMPT_RT this prevent priority inversion against softirq kthread in 815 + * case it gets preempted while executing a timer callback. See comments in 816 + * hrtimer_cancel_wait_running. For PREEMPT_RT=n this just results in a 817 + * cpu_relax(). 818 + */ 819 + static struct k_itimer *timer_wait_running(struct k_itimer *timer, 820 + unsigned long *flags) 821 + { 822 + const struct k_clock *kc = READ_ONCE(timer->kclock); 823 + timer_t timer_id = READ_ONCE(timer->it_id); 824 + 825 + /* Prevent kfree(timer) after dropping the lock */ 826 + rcu_read_lock(); 827 + unlock_timer(timer, *flags); 828 + 829 + if (!WARN_ON_ONCE(!kc->timer_wait_running)) 830 + kc->timer_wait_running(timer); 831 + 832 + rcu_read_unlock(); 833 + /* Relock the timer. It might be not longer hashed. */ 834 + return lock_timer(timer_id, flags); 835 + } 836 + 808 837 /* Set a POSIX.1b interval timer. */ 809 838 int common_timer_set(struct k_itimer *timr, int flags, 810 839 struct itimerspec64 *new_setting, ··· 873 844 return 0; 874 845 } 875 846 876 - static int do_timer_settime(timer_t timer_id, int flags, 847 + static int do_timer_settime(timer_t timer_id, int tmr_flags, 877 848 struct itimerspec64 *new_spec64, 878 849 struct itimerspec64 *old_spec64) 879 850 { 880 851 const struct k_clock *kc; 881 852 struct k_itimer *timr; 882 - unsigned long flag; 853 + unsigned long flags; 883 854 int error = 0; 884 855 885 856 if (!timespec64_valid(&new_spec64->it_interval) || ··· 888 859 889 860 if (old_spec64) 890 861 memset(old_spec64, 0, sizeof(*old_spec64)); 862 + 863 + timr = lock_timer(timer_id, &flags); 891 864 retry: 892 - timr = lock_timer(timer_id, &flag); 893 865 if (!timr) 894 866 return -EINVAL; 895 867 ··· 898 868 if (WARN_ON_ONCE(!kc || !kc->timer_set)) 899 869 error = -EINVAL; 900 870 else 901 - error = kc->timer_set(timr, flags, new_spec64, old_spec64); 871 + error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64); 902 872 903 - unlock_timer(timr, flag); 904 873 if (error == TIMER_RETRY) { 905 - old_spec64 = NULL; // We already got the old time... 874 + // We already got the old time... 875 + old_spec64 = NULL; 876 + /* Unlocks and relocks the timer if it still exists */ 877 + timr = timer_wait_running(timr, &flags); 906 878 goto retry; 907 879 } 880 + unlock_timer(timr, flags); 908 881 909 882 return error; 910 883 } ··· 984 951 struct k_itimer *timer; 985 952 unsigned long flags; 986 953 987 - retry_delete: 988 954 timer = lock_timer(timer_id, &flags); 955 + 956 + retry_delete: 989 957 if (!timer) 990 958 return -EINVAL; 991 959 992 - if (timer_delete_hook(timer) == TIMER_RETRY) { 993 - unlock_timer(timer, flags); 960 + if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) { 961 + /* Unlocks and relocks the timer if it still exists */ 962 + timer = timer_wait_running(timer, &flags); 994 963 goto retry_delete; 995 964 } 996 965 ··· 1273 1238 .timer_forward = common_hrtimer_forward, 1274 1239 .timer_remaining = common_hrtimer_remaining, 1275 1240 .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1241 + .timer_wait_running = common_timer_wait_running, 1276 1242 .timer_arm = common_hrtimer_arm, 1277 1243 }; 1278 1244 ··· 1289 1253 .timer_forward = common_hrtimer_forward, 1290 1254 .timer_remaining = common_hrtimer_remaining, 1291 1255 .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1256 + .timer_wait_running = common_timer_wait_running, 1292 1257 .timer_arm = common_hrtimer_arm, 1293 1258 }; 1294 1259 ··· 1320 1283 .timer_forward = common_hrtimer_forward, 1321 1284 .timer_remaining = common_hrtimer_remaining, 1322 1285 .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1286 + .timer_wait_running = common_timer_wait_running, 1323 1287 .timer_arm = common_hrtimer_arm, 1324 1288 }; 1325 1289 ··· 1336 1298 .timer_forward = common_hrtimer_forward, 1337 1299 .timer_remaining = common_hrtimer_remaining, 1338 1300 .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1301 + .timer_wait_running = common_timer_wait_running, 1339 1302 .timer_arm = common_hrtimer_arm, 1340 1303 }; 1341 1304
+1
kernel/time/posix-timers.h
··· 24 24 int (*timer_try_to_cancel)(struct k_itimer *timr); 25 25 void (*timer_arm)(struct k_itimer *timr, ktime_t expires, 26 26 bool absolute, bool sigev_none); 27 + void (*timer_wait_running)(struct k_itimer *timr); 27 28 }; 28 29 29 30 extern const struct k_clock clock_posix_cpu;
+9 -4
kernel/time/tick-broadcast-hrtimer.c
··· 59 59 * hrtimer_{start/cancel} functions call into tracing, 60 60 * calls to these functions must be bound within RCU_NONIDLE. 61 61 */ 62 - RCU_NONIDLE({ 62 + RCU_NONIDLE( 63 + { 63 64 bc_moved = hrtimer_try_to_cancel(&bctimer) >= 0; 64 - if (bc_moved) 65 + if (bc_moved) { 65 66 hrtimer_start(&bctimer, expires, 66 - HRTIMER_MODE_ABS_PINNED);}); 67 + HRTIMER_MODE_ABS_PINNED_HARD); 68 + } 69 + } 70 + ); 71 + 67 72 if (bc_moved) { 68 73 /* Bind the "device" to the cpu */ 69 74 bc->bound_on = smp_processor_id(); ··· 109 104 110 105 void tick_setup_hrtimer_broadcast(void) 111 106 { 112 - hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 107 + hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD); 113 108 bctimer.function = bc_handler; 114 109 clockevents_register_device(&ce_broadcast_hrtimer); 115 110 }
+10 -7
kernel/time/tick-sched.c
··· 634 634 /* Forward the time to expire in the future */ 635 635 hrtimer_forward(&ts->sched_timer, now, tick_period); 636 636 637 - if (ts->nohz_mode == NOHZ_MODE_HIGHRES) 638 - hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED); 639 - else 637 + if (ts->nohz_mode == NOHZ_MODE_HIGHRES) { 638 + hrtimer_start_expires(&ts->sched_timer, 639 + HRTIMER_MODE_ABS_PINNED_HARD); 640 + } else { 640 641 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1); 642 + } 641 643 642 644 /* 643 645 * Reset to make sure next tick stop doesn't get fooled by past ··· 804 802 } 805 803 806 804 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) { 807 - hrtimer_start(&ts->sched_timer, tick, HRTIMER_MODE_ABS_PINNED); 805 + hrtimer_start(&ts->sched_timer, tick, 806 + HRTIMER_MODE_ABS_PINNED_HARD); 808 807 } else { 809 808 hrtimer_set_expires(&ts->sched_timer, tick); 810 809 tick_program_event(tick, 1); ··· 1233 1230 * Recycle the hrtimer in ts, so we can share the 1234 1231 * hrtimer_forward with the highres code. 1235 1232 */ 1236 - hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 1233 + hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD); 1237 1234 /* Get the next period */ 1238 1235 next = tick_init_jiffy_update(); 1239 1236 ··· 1330 1327 /* 1331 1328 * Emulate tick processing via per-CPU hrtimers: 1332 1329 */ 1333 - hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 1330 + hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD); 1334 1331 ts->sched_timer.function = tick_sched_timer; 1335 1332 1336 1333 /* Get the next period (per-CPU) */ ··· 1345 1342 } 1346 1343 1347 1344 hrtimer_forward(&ts->sched_timer, now, tick_period); 1348 - hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED); 1345 + hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED_HARD); 1349 1346 tick_nohz_activate(ts, NOHZ_MODE_HIGHRES); 1350 1347 } 1351 1348 #endif /* HIGH_RES_TIMERS */
+96 -9
kernel/time/timer.c
··· 196 196 struct timer_base { 197 197 raw_spinlock_t lock; 198 198 struct timer_list *running_timer; 199 + #ifdef CONFIG_PREEMPT_RT 200 + spinlock_t expiry_lock; 201 + atomic_t timer_waiters; 202 + #endif 199 203 unsigned long clk; 200 204 unsigned long next_expiry; 201 205 unsigned int cpu; ··· 1231 1227 } 1232 1228 EXPORT_SYMBOL(try_to_del_timer_sync); 1233 1229 1234 - #ifdef CONFIG_SMP 1230 + #ifdef CONFIG_PREEMPT_RT 1231 + static __init void timer_base_init_expiry_lock(struct timer_base *base) 1232 + { 1233 + spin_lock_init(&base->expiry_lock); 1234 + } 1235 + 1236 + static inline void timer_base_lock_expiry(struct timer_base *base) 1237 + { 1238 + spin_lock(&base->expiry_lock); 1239 + } 1240 + 1241 + static inline void timer_base_unlock_expiry(struct timer_base *base) 1242 + { 1243 + spin_unlock(&base->expiry_lock); 1244 + } 1245 + 1246 + /* 1247 + * The counterpart to del_timer_wait_running(). 1248 + * 1249 + * If there is a waiter for base->expiry_lock, then it was waiting for the 1250 + * timer callback to finish. Drop expiry_lock and reaquire it. That allows 1251 + * the waiter to acquire the lock and make progress. 1252 + */ 1253 + static void timer_sync_wait_running(struct timer_base *base) 1254 + { 1255 + if (atomic_read(&base->timer_waiters)) { 1256 + spin_unlock(&base->expiry_lock); 1257 + spin_lock(&base->expiry_lock); 1258 + } 1259 + } 1260 + 1261 + /* 1262 + * This function is called on PREEMPT_RT kernels when the fast path 1263 + * deletion of a timer failed because the timer callback function was 1264 + * running. 1265 + * 1266 + * This prevents priority inversion, if the softirq thread on a remote CPU 1267 + * got preempted, and it prevents a life lock when the task which tries to 1268 + * delete a timer preempted the softirq thread running the timer callback 1269 + * function. 1270 + */ 1271 + static void del_timer_wait_running(struct timer_list *timer) 1272 + { 1273 + u32 tf; 1274 + 1275 + tf = READ_ONCE(timer->flags); 1276 + if (!(tf & TIMER_MIGRATING)) { 1277 + struct timer_base *base = get_timer_base(tf); 1278 + 1279 + /* 1280 + * Mark the base as contended and grab the expiry lock, 1281 + * which is held by the softirq across the timer 1282 + * callback. Drop the lock immediately so the softirq can 1283 + * expire the next timer. In theory the timer could already 1284 + * be running again, but that's more than unlikely and just 1285 + * causes another wait loop. 1286 + */ 1287 + atomic_inc(&base->timer_waiters); 1288 + spin_lock_bh(&base->expiry_lock); 1289 + atomic_dec(&base->timer_waiters); 1290 + spin_unlock_bh(&base->expiry_lock); 1291 + } 1292 + } 1293 + #else 1294 + static inline void timer_base_init_expiry_lock(struct timer_base *base) { } 1295 + static inline void timer_base_lock_expiry(struct timer_base *base) { } 1296 + static inline void timer_base_unlock_expiry(struct timer_base *base) { } 1297 + static inline void timer_sync_wait_running(struct timer_base *base) { } 1298 + static inline void del_timer_wait_running(struct timer_list *timer) { } 1299 + #endif 1300 + 1301 + #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT) 1235 1302 /** 1236 1303 * del_timer_sync - deactivate a timer and wait for the handler to finish. 1237 1304 * @timer: the timer to be deactivated ··· 1341 1266 */ 1342 1267 int del_timer_sync(struct timer_list *timer) 1343 1268 { 1269 + int ret; 1270 + 1344 1271 #ifdef CONFIG_LOCKDEP 1345 1272 unsigned long flags; 1346 1273 ··· 1360 1283 * could lead to deadlock. 1361 1284 */ 1362 1285 WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE)); 1363 - for (;;) { 1364 - int ret = try_to_del_timer_sync(timer); 1365 - if (ret >= 0) 1366 - return ret; 1367 - cpu_relax(); 1368 - } 1286 + 1287 + do { 1288 + ret = try_to_del_timer_sync(timer); 1289 + 1290 + if (unlikely(ret < 0)) { 1291 + del_timer_wait_running(timer); 1292 + cpu_relax(); 1293 + } 1294 + } while (ret < 0); 1295 + 1296 + return ret; 1369 1297 } 1370 1298 EXPORT_SYMBOL(del_timer_sync); 1371 1299 #endif ··· 1442 1360 if (timer->flags & TIMER_IRQSAFE) { 1443 1361 raw_spin_unlock(&base->lock); 1444 1362 call_timer_fn(timer, fn, baseclk); 1363 + base->running_timer = NULL; 1445 1364 raw_spin_lock(&base->lock); 1446 1365 } else { 1447 1366 raw_spin_unlock_irq(&base->lock); 1448 1367 call_timer_fn(timer, fn, baseclk); 1368 + base->running_timer = NULL; 1369 + timer_sync_wait_running(base); 1449 1370 raw_spin_lock_irq(&base->lock); 1450 1371 } 1451 1372 } ··· 1728 1643 #endif 1729 1644 scheduler_tick(); 1730 1645 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 1731 - run_posix_cpu_timers(p); 1646 + run_posix_cpu_timers(); 1732 1647 } 1733 1648 1734 1649 /** ··· 1743 1658 if (!time_after_eq(jiffies, base->clk)) 1744 1659 return; 1745 1660 1661 + timer_base_lock_expiry(base); 1746 1662 raw_spin_lock_irq(&base->lock); 1747 1663 1748 1664 /* ··· 1770 1684 while (levels--) 1771 1685 expire_timers(base, heads + levels); 1772 1686 } 1773 - base->running_timer = NULL; 1774 1687 raw_spin_unlock_irq(&base->lock); 1688 + timer_base_unlock_expiry(base); 1775 1689 } 1776 1690 1777 1691 /* ··· 2016 1930 base->cpu = cpu; 2017 1931 raw_spin_lock_init(&base->lock); 2018 1932 base->clk = jiffies; 1933 + timer_base_init_expiry_lock(base); 2019 1934 } 2020 1935 } 2021 1936
+2 -2
kernel/watchdog.c
··· 490 490 * Start the timer first to prevent the NMI watchdog triggering 491 491 * before the timer has a chance to fire. 492 492 */ 493 - hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 493 + hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 494 494 hrtimer->function = watchdog_timer_fn; 495 495 hrtimer_start(hrtimer, ns_to_ktime(sample_period), 496 - HRTIMER_MODE_REL_PINNED); 496 + HRTIMER_MODE_REL_PINNED_HARD); 497 497 498 498 /* Initialize timestamp */ 499 499 __touch_watchdog();
+12 -18
lib/timerqueue.c
··· 26 26 */ 27 27 bool timerqueue_add(struct timerqueue_head *head, struct timerqueue_node *node) 28 28 { 29 - struct rb_node **p = &head->head.rb_node; 29 + struct rb_node **p = &head->rb_root.rb_root.rb_node; 30 30 struct rb_node *parent = NULL; 31 - struct timerqueue_node *ptr; 31 + struct timerqueue_node *ptr; 32 + bool leftmost = true; 32 33 33 34 /* Make sure we don't add nodes that are already added */ 34 35 WARN_ON_ONCE(!RB_EMPTY_NODE(&node->node)); ··· 37 36 while (*p) { 38 37 parent = *p; 39 38 ptr = rb_entry(parent, struct timerqueue_node, node); 40 - if (node->expires < ptr->expires) 39 + if (node->expires < ptr->expires) { 41 40 p = &(*p)->rb_left; 42 - else 41 + } else { 43 42 p = &(*p)->rb_right; 43 + leftmost = false; 44 + } 44 45 } 45 46 rb_link_node(&node->node, parent, p); 46 - rb_insert_color(&node->node, &head->head); 47 + rb_insert_color_cached(&node->node, &head->rb_root, leftmost); 47 48 48 - if (!head->next || node->expires < head->next->expires) { 49 - head->next = node; 50 - return true; 51 - } 52 - return false; 49 + return leftmost; 53 50 } 54 51 EXPORT_SYMBOL_GPL(timerqueue_add); 55 52 ··· 64 65 { 65 66 WARN_ON_ONCE(RB_EMPTY_NODE(&node->node)); 66 67 67 - /* update next pointer */ 68 - if (head->next == node) { 69 - struct rb_node *rbn = rb_next(&node->node); 70 - 71 - head->next = rb_entry_safe(rbn, struct timerqueue_node, node); 72 - } 73 - rb_erase(&node->node, &head->head); 68 + rb_erase_cached(&node->node, &head->rb_root); 74 69 RB_CLEAR_NODE(&node->node); 75 - return head->next != NULL; 70 + 71 + return !RB_EMPTY_ROOT(&head->rb_root.rb_root); 76 72 } 77 73 EXPORT_SYMBOL_GPL(timerqueue_del); 78 74
+2 -4
net/core/pktgen.c
··· 2156 2156 s64 remaining; 2157 2157 struct hrtimer_sleeper t; 2158 2158 2159 - hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 2159 + hrtimer_init_sleeper_on_stack(&t, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 2160 2160 hrtimer_set_expires(&t.timer, spin_until); 2161 2161 2162 2162 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer)); ··· 2170 2170 end_time = ktime_get(); 2171 2171 } while (ktime_compare(end_time, spin_until) < 0); 2172 2172 } else { 2173 - /* see do_nanosleep */ 2174 - hrtimer_init_sleeper(&t, current); 2175 2173 do { 2176 2174 set_current_state(TASK_INTERRUPTIBLE); 2177 - hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS); 2175 + hrtimer_sleeper_start_expires(&t, HRTIMER_MODE_ABS); 2178 2176 2179 2177 if (likely(t.task)) 2180 2178 schedule();