Skip to content

treewide: Cleanup LATCH, CLOCK_TICK_RATE and get_cycles() [ab]use

Thomas Gleixner linux-alpha linux-arm-kernel linux-crypto linux-ext4 linux-fbdev linux-hams linux-iommu linux-m68k linux-mm linux-parisc linux-riscv linux-s390 linux-tegra linux-wireless linuxppc-dev lkml loongarch netdev openrisc sparclinux 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest38 patches 33 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/38percpu: Sanitize __percpu_qual include hellarch/x86/include/asm/percpu.h arch/x86/include/asm/percpu_types.h include/asm-generic/Kbuild +2 more+39 -50 replies
2/38x86: Cleanup include recursion hellarch/x86/include/asm/msr.h arch/x86/include/asm/pvclock.h arch/x86/include/asm/smp.h +4 more+9 -70 replies
2/38x86: Cleanup include recursion hellarch/x86/include/asm/cache.h arch/x86/include/asm/msr.h arch/x86/include/asm/paravirt.h +6 more+12 -80 replies
3/38x86/apm: Remove last LATCH usagearch/x86/kernel/apm_32.c include/linux/jiffies.h+2 -50 replies
4/38x86: Use PIT_TICK_RATE instead of CLOCK_TICK_RATEarch/x86/kernel/setup.c+1 -10 replies
5/38treewide: Remove CLOCK_TICK_RATEarch/alpha/include/asm/timex.h arch/arc/include/asm/timex.h arch/arm/mach-omap1/Kconfig +14 more+2 -1212 replies
6/38calibrate: Rework delay timer calibrationarch/Kconfig arch/arm/Kconfig arch/arm/include/asm/delay.h +21 more+50 -751 reply
7/38treewide: Consolidate cycles_tarch/Kconfig arch/alpha/include/asm/timex.h arch/arm/include/asm/timex.h +13 more+12 -254 replies
8/38x86/tsc: Use rdtsc() instead of get_cycles()arch/x86/kernel/tsc.c+6 -60 replies
9/38iommu/vt-d: Use sched_clock() instead of get_cycles()arch/x86/include/asm/iommu.h drivers/iommu/intel/dmar.c drivers/iommu/intel/iommu.h+8 -72 replies
10/38arcnet: Remove function timing codedrivers/net/arcnet/arc-rimi.c drivers/net/arcnet/arcdevice.h drivers/net/arcnet/com20020.c +2 more+9 -311 reply
11/38misc: sgi-gru: Remove get_cycles() [ab]usedrivers/misc/sgi-gru/gruhandles.c drivers/misc/sgi-gru/grukservices.c drivers/misc/sgi-gru/grutlbpurge.c+12 -160 replies
11/38misc: sgi-gru: Remove get_cycles() [ab]usedrivers/misc/sgi-gru/gruhandles.c drivers/misc/sgi-gru/grukservices.c drivers/misc/sgi-gru/grutlbpurge.c+12 -160 replies
12/38wifi: wil6210: Replace get_cyles() usagedrivers/net/wireless/ath/wil6210/debugfs.c drivers/net/wireless/ath/wil6210/txrx.c drivers/net/wireless/ath/wil6210/txrx_edma.c +1 more+8 -70 replies
13/38crypto: tcrypt: Replace get_cycles() with ktime_get()crypto/tcrypt.c+42 -420 replies
14/38slub: Use prandom instead of get_cycles()mm/slub.c+23 -144 replies
15/38ptp: ptp_vmclock: Replace get_cycles() usagearch/arm64/Kconfig arch/arm64/include/asm/ptp_vmclock.h arch/x86/Kconfig +3 more+34 -42 replies
16/38fbdev: udlfb: Replace get_cycles() with ktime_get()Documentation/fb/udlfb.rst drivers/video/fbdev/udlfb.c+14 -140 replies
17/38ext4: Replace get_cycles() usage with ktime_get()fs/ext4/mballoc.c+2 -21 reply
18/38lib/tests: Replace get_cycles() with ktime_get()lib/interval_tree_test.c lib/rbtree_test.c lib/test_vmalloc.c+36 -361 reply
19/38kcsan: Replace get_cycles() usagekernel/kcsan/core.c kernel/kcsan/debugfs.c+5 -51 reply
20/38kasan: sw_tags: Replace get_cycles() by random_get_entropy()mm/kasan/sw_tags.c+1 -10 replies
21/38hamradio: baycom_epp: Remove BAYCOM_DEBUGdrivers/net/hamradio/baycom_epp.c+0 -510 replies
22/38random: Provide CONFIG_ARCH_HAS_RANDOM_ENTROPYarch/Kconfig arch/loongarch/kernel/relocate.c arch/mips/kernel/relocate.c +4 more+24 -00 replies
23/38alpha: Select ARCH_HAS_RANDOM_ENTROPYarch/alpha/Kconfig arch/alpha/include/asm/random.h arch/alpha/include/asm/timex.h+15 -261 reply
24/38ARM: Select ARCH_HAS_RANDOM_ENTROPYarch/arm/Kconfig arch/arm/include/asm/random.h arch/arm/include/asm/timex.h +1 more+17 -200 replies
25/38arm64: Select ARCH_HAS_RANDOM_ENTROPYarch/arm64/Kconfig arch/arm64/include/asm/io.h arch/arm64/include/asm/random.h +9 more+28 -200 replies
26/38loongarch: Select ARCH_HAS_RANDOM_ENTROPYarch/loongarch/Kconfig arch/loongarch/include/asm/random.h arch/loongarch/kernel/syscall.c +1 more+18 -10 replies
27/38m68k: Select ARCH_HAS_RANDOM_ENTROPYarch/m68k/Kconfig arch/m68k/amiga/config.c arch/m68k/include/asm/random.h +2 more+17 -262 replies
28/38mips: Select ARCH_HAS_RANDOM_ENTROPYarch/mips/Kconfig arch/mips/generic/init.c arch/mips/include/asm/random.h +7 more+66 -931 reply
29/38nios2: Select ARCH_HAS_RANDOM_ENTROPYarch/nios2/Kconfig arch/nios2/include/asm/random.h arch/nios2/include/asm/timex.h +1 more+18 -60 replies
30/38openrisc: Select ARCH_HAS_RANDOM_ENTROPYarch/openrisc/Kconfig arch/openrisc/include/asm/random.h arch/openrisc/include/asm/timex.h +1 more+14 -51 reply
31/38parisc: Select ARCH_HAS_RANDOM_ENTROPYarch/parisc/Kconfig arch/parisc/include/asm/random.h arch/parisc/include/asm/timex.h +2 more+15 -61 reply
32/38powerpc/spufs: Use mftb() directlyarch/powerpc/platforms/cell/spufs/switch.c+3 -23 replies
33/38powerpc: Select ARCH_HAS_RANDOM_ENTROPYarch/powerpc/Kconfig arch/powerpc/include/asm/random.h arch/powerpc/include/asm/timex.h+14 -212 replies
34/38riscv: Select ARCH_HAS_RANDOM_ENTROPYarch/riscv/Kconfig arch/riscv/include/asm/random.h arch/riscv/include/asm/timex.h +3 more+29 -130 replies
35/38s390: Select ARCH_HAS_RANDOM_ENTROPYarch/s390/Kconfig arch/s390/include/asm/random.h arch/s390/include/asm/timex.h +2 more+15 -62 replies
36/38sparc: Select ARCH_HAS_RANDOM_ENTROPY for SPARC64arch/sparc/Kconfig arch/sparc/include/asm/random.h arch/sparc/include/asm/timex.h +4 more+16 -270 replies
37/38x86: Select ARCH_HAS_RANDOM_ENTROPYarch/x86/Kconfig arch/x86/include/asm/random.h arch/x86/include/asm/timex.h +1 more+17 -260 replies
38/38treewide: Remove asm/timex.h includes from generic codeinclude/asm-generic/Kbuild include/asm-generic/timex.h include/linux/random.h +4 more+3 -451 reply

Reviews trailers per patch

14 of 40 patches have a Reviewed-by or Acked-by.

PatchMarco ElverLu BaoluDaniel PalmerStafford HorneMagnus LindholmVlastimil Babka (SUSE)Harry Yoo (Oracle)Arnd BergmannDavid WoodhouseHelge DellerChristophe Leroy (CS GROUP)Geert UytterhoevenHeiko CarstensMukesh Kumar Chaurasiya (IBM)
1/38
2/38
2/38
3/38
4/38
5/38R-b A-b
6/38
7/38A-b
8/38
9/38R-b
10/38A-b
11/38
11/38
12/38
13/38
14/38A-b A-b
15/38A-b
16/38
17/38
18/38
19/38R-b
20/38
21/38
22/38
23/38A-b T-b
24/38
25/38
26/38
27/38T-b R-b A-b
28/38
29/38
30/38A-b
31/38T-b
32/38A-b R-b R-b
33/38R-b T-b R-b
34/38
35/38A-b
36/38
37/38
38/38A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 14 reviewers.

Read the v1 thread

Cover letter [patch 00/38] treewide: Cleanup LATCH, CLOCK_TICK_RATE and get_cycles() [ab]use

First of all sorry for the insanely big Cc list, but people can't make their mind up whether they want to be Cc'ed on everything or not. So I'm opting for the worst case to cater to the people who want to be Cc'ed on everything and assume that the rest of you got used to it by now. I really wanted this to be more confined but a treewide cleanup does not give a lot of options.

That said, let me explain what this is about.

1) LATCH

The LATCH define goes back to Linux version 0.1 and has survived until today for the very wrong reasons.

Initially it was based on the x86 PIT frequency and also steered the timekeeping conversions.

With the arrival of non x86 architectures it got changed to be based on CLOCK_TICK_RATE in order not to change core code which depended on LATCH.

That all got meaningless when timers, timekeeping and scheduling infrastructure got rewritten more than two decades ago.

But there is still a lonely survivor in arch/x86/kernel/apm_32.c which dates back to Linux 1.3.46 (Dec. 1995)

Which causes the next historical gem

2) CLOCK_TICK_RATE

When Linux got expanded beyond i386 LATCH was made "flexible" by basing it on CLOCK_TICK_RATE to adjust for other frequencies than the i386 PIT frequency.

As LATCH this got meaningless long ago and for amusement value it got copied into new architectures arriving way after it got obsolete for no reason but with comments to the effect that it's meaningless

And of course it had a lonely survivor in arch/x86/kernel/setup.c despite it being only an alias for PIT_TICK_RATE for a very long time.

3) get_cycles()

That was introduced in 2.1.133pre4 (Dec. 1998) to utilize the back then brand new TSC. The introduction broke everything except i386 SMP with a CPU having a TSC and got then fixed up within a couple of days with empty stubs returning 0 and #ifdeffery for CONFIG_TSC before the 2.2.0 release.

It's amusing that the naming deliberately ignored that TSC is the acronym for Time Stamp Counter and not Cycle Counter and rather went for the underlying coincidence that the TSC was running at the same fixed frequency as the CPU core.

That turned out to be interesting when CPUs started to have frequency scaling as the TSC then turned into a variable frequency random number generator.

A decade later CPU designers came to senses and made the TSC invariant usually running at the nominal CPU frequency, which allowed to use it for reliable timekeeping purposes.

Non x86 architectures implemented get_cycles() based on whatever continuously running counter they had available in their CPUs. Some of them actual CPU cycle counters, but many of them running at a fixed frequency which was completely unrelated to CPU cycles.

Around 2004/5 the timekeeping subsystem was completely rewritten and made generic along with the scheduling clock and other related infrastructure. With that rewrite get_cycles() got mostly obsolete, but despite it being on the todo list of quite some people it never ceased to exist and it was just a conveniance vehicle to base other things like the recent addition of random_get_entropy() on top with a hideous #ifdef/#define macro maze.

The other remaining use cases are mostly debugging and testing code. Especially the usage in performance test code is hillarious at best. While the name get_cycles() suggests that it provides access to CPU cycles the reality is that it provides a unspecified counter for most systems, where a lot of architectures simply return 0 because they either do not have such a counter or did not bother to implement it at all.

So in fact get_cycles() should have been renamed to get_bogo_cycles() long ago matching the BogoMIPS "impress your friends" printk which still exists for historical amusement value.

But the real solution is to remove it all together instead of proliferating the bogosity.

This is what this series does with the following steps:

1) Cleanup some header dependency hell which got unearthed by the restructuring and went unnoticed so far. It's amazing how the kernel build system magically "works". This affects not only x86, but the main fallout was observed and fixed there. ARM64 and MIPS are at least as bad as they silently rely on the accidental asm/timex.h include through a variety of generic headers to make their architecture code compile. See the changelog and patches specific to those two.

2) Removal of LATCH

3) Removal of CLOCK_TICK_RATE

4) Consolidation of cycles_t which was a typedef in asm/timex.h

5) Cleanup of read_current_timer() which is only used for delay calibration and has nothing to do with get_cycles()

6) Cleanup of get_cycles() usage in debug and test code

7) Decoupling of random_get_entropy() from get_cycles()

8) Removal of asm/timex.h includes except for architecture internal usage where necessary.

At the end get_cycles() survives in a couple of architectures as a purely architecture internal implementation detail.

This survives compile testing on all architectures except hexagon and nios2 because the current cross tools based on gcc15 do not offer a compiler for them anymore. Boot tested on x86 and some qemu instances covering only a few architectures.

The series applies on v7.0-rc7 and with very minor conflicts on -next. It is also available from git:

git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git getcycles-v1

Thanks,

tglx

---
 arch/alpha/include/asm/timex.h                 |   33 --------
 arch/arc/include/asm/timex.h                   |   15 ---
 arch/arm/include/asm/timex.h                   |   16 ---
 arch/arm64/include/asm/timex.h                 |   18 ----
 arch/hexagon/include/asm/timex.h               |   23 -----
 arch/m68k/include/asm/timex.h                  |   42 ----------
 arch/microblaze/include/asm/timex.h            |   13 ---
 arch/mips/include/asm/timex.h                  |  102 -------------------------
 arch/sh/include/asm/timex.h                    |   24 -----
 arch/sparc/include/asm/timex.h                 |    9 --
 arch/sparc/include/asm/timex_32.h              |   14 ---
 arch/um/include/asm/timex.h                    |    9 --
 b/Documentation/fb/udlfb.rst                   |    4 
 b/arch/Kconfig                                 |   10 ++
 b/arch/alpha/Kconfig                           |    1 
 b/arch/alpha/include/asm/random.h              |   14 +++
 b/arch/arm/Kconfig                             |    2 
 b/arch/arm/include/asm/delay.h                 |    1 
 b/arch/arm/include/asm/random.h                |   14 +++
 b/arch/arm/lib/delay.c                         |   14 +--
 b/arch/arm/mach-omap1/Kconfig                  |    2 
 b/arch/arm64/Kconfig                           |    2 
 b/arch/arm64/include/asm/io.h                  |    5 -
 b/arch/arm64/include/asm/ptp_vmclock.h         |   12 ++
 b/arch/arm64/include/asm/random.h              |   11 ++
 b/arch/arm64/include/asm/rqspinlock.h          |    1 
 b/arch/arm64/kernel/time.c                     |    6 +
 b/arch/arm64/kernel/topology.c                 |    1 
 b/arch/arm64/kernel/traps.c                    |    1 
 b/arch/arm64/kvm/emulate-nested.c              |    1 
 b/arch/arm64/kvm/hyp/include/hyp/switch.h      |    1 
 b/arch/arm64/lib/delay.c                       |    1 
 b/arch/hexagon/Kconfig                         |    1 
 b/arch/hexagon/kernel/time.c                   |    8 +
 b/arch/loongarch/Kconfig                       |    1 
 b/arch/loongarch/include/asm/random.h          |   15 +++
 b/arch/loongarch/include/asm/timex.h           |    2 
 b/arch/loongarch/kernel/relocate.c             |    1 
 b/arch/loongarch/kernel/syscall.c              |    1 
 b/arch/loongarch/lib/delay.c                   |    2 
 b/arch/m68k/Kconfig                            |    1 
 b/arch/m68k/amiga/config.c                     |    1 
 b/arch/m68k/include/asm/random.h               |   14 +++
 b/arch/m68k/kernel/time.c                      |    2 
 b/arch/mips/Kconfig                            |    1 
 b/arch/mips/generic/init.c                     |    1 
 b/arch/mips/include/asm/random.h               |    7 +
 b/arch/mips/kernel/pm-cps.c                    |    1 
 b/arch/mips/kernel/proc.c                      |    1 
 b/arch/mips/kernel/relocate.c                  |    2 
 b/arch/mips/kernel/time.c                      |   53 ++++++++++++
 b/arch/mips/lib/dump_tlb.c                     |    1 
 b/arch/mips/mm/cache.c                         |    1 
 b/arch/nios2/Kconfig                           |    1 
 b/arch/nios2/include/asm/random.h              |   14 +++
 b/arch/nios2/include/asm/timex.h               |    7 -
 b/arch/nios2/kernel/time.c                     |    4 
 b/arch/openrisc/Kconfig                        |    2 
 b/arch/openrisc/include/asm/random.h           |   12 ++
 b/arch/openrisc/include/asm/timex.h            |   10 --
 b/arch/openrisc/lib/delay.c                    |    8 -
 b/arch/parisc/Kconfig                          |    1 
 b/arch/parisc/include/asm/random.h             |   12 ++
 b/arch/parisc/include/asm/timex.h              |   10 --
 b/arch/parisc/kernel/processor.c               |    1 
 b/arch/parisc/kernel/time.c                    |    1 
 b/arch/powerpc/Kconfig                         |    1 
 b/arch/powerpc/include/asm/random.h            |   13 +++
 b/arch/powerpc/include/asm/timex.h             |   25 ------
 b/arch/powerpc/platforms/cell/spufs/switch.c   |    5 -
 b/arch/riscv/Kconfig                           |    2 
 b/arch/riscv/include/asm/random.h              |   25 ++++++
 b/arch/riscv/include/asm/timex.h               |   23 -----
 b/arch/riscv/kernel/unaligned_access_speed.c   |    1 
 b/arch/riscv/kvm/vcpu_timer.c                  |    1 
 b/arch/riscv/lib/delay.c                       |    8 +
 b/arch/s390/Kconfig                            |    1 
 b/arch/s390/include/asm/random.h               |   12 ++
 b/arch/s390/include/asm/timex.h                |   10 --
 b/arch/s390/kernel/time.c                      |    1 
 b/arch/s390/kernel/vtime.c                     |    1 
 b/arch/sparc/Kconfig                           |    2 
 b/arch/sparc/include/asm/random.h              |   15 +++
 b/arch/sparc/include/asm/timex_64.h            |   20 ----
 b/arch/sparc/kernel/pcic.c                     |    1 
 b/arch/sparc/kernel/time_32.c                  |    1 
 b/arch/sparc/kernel/time_64.c                  |    4 
 b/arch/sparc/vdso/vclock_gettime.c             |    1 
 b/arch/x86/Kconfig                             |    4 
 b/arch/x86/include/asm/iommu.h                 |    3 
 b/arch/x86/include/asm/msr.h                   |    5 -
 b/arch/x86/include/asm/percpu.h                |    5 -
 b/arch/x86/include/asm/percpu_types.h          |   17 ++++
 b/arch/x86/include/asm/ptp_vmclock.h           |   12 ++
 b/arch/x86/include/asm/pvclock.h               |    1 
 b/arch/x86/include/asm/random.h                |   12 --
 b/arch/x86/include/asm/smp.h                   |    2 
 b/arch/x86/include/asm/tsc.h                   |   11 --
 b/arch/x86/include/asm/vdso/gettimeofday.h     |    5 -
 b/arch/x86/kernel/apm_32.c                     |    4 
 b/arch/x86/kernel/cpu/mce/core.c               |    1 
 b/arch/x86/kernel/nmi.c                        |    1 
 b/arch/x86/kernel/setup.c                      |    2 
 b/arch/x86/kernel/smpboot.c                    |    1 
 b/arch/x86/kernel/tsc.c                        |   12 +-
 b/arch/x86/lib/delay.c                         |    8 -
 b/crypto/jitterentropy-kcapi.c                 |    1 
 b/crypto/tcrypt.c                              |   84 ++++++++++----------
 b/drivers/iommu/intel/dmar.c                   |    4 
 b/drivers/iommu/intel/iommu.h                  |    8 +
 b/drivers/irqchip/irq-apple-aic.c              |    1 
 b/drivers/misc/sgi-gru/gruhandles.c            |   20 +---
 b/drivers/misc/sgi-gru/grukservices.c          |    3 
 b/drivers/misc/sgi-gru/grutlbpurge.c           |    5 -
 b/drivers/net/arcnet/arc-rimi.c                |    4 
 b/drivers/net/arcnet/arcdevice.h               |   20 ----
 b/drivers/net/arcnet/com20020.c                |    6 -
 b/drivers/net/arcnet/com90io.c                 |    6 -
 b/drivers/net/arcnet/com90xx.c                 |    4 
 b/drivers/net/hamradio/baycom_epp.c            |   51 ------------
 b/drivers/net/wireless/ath/wil6210/debugfs.c   |    2 
 b/drivers/net/wireless/ath/wil6210/txrx.c      |    6 -
 b/drivers/net/wireless/ath/wil6210/txrx_edma.c |    4 
 b/drivers/net/wireless/ath/wil6210/wil6210.h   |    3 
 b/drivers/ptp/Kconfig                          |    6 -
 b/drivers/ptp/ptp_vmclock.c                    |    6 -
 b/drivers/video/fbdev/udlfb.c                  |   24 ++---
 b/fs/ext4/mballoc.c                            |    4 
 b/include/asm-generic/Kbuild                   |    2 
 b/include/asm-generic/percpu_types.h           |   20 ++++
 b/include/linux/compiler_types.h               |    1 
 b/include/linux/delay.h                        |    2 
 b/include/linux/jiffies.h                      |    3 
 b/include/linux/random.h                       |   18 ++++
 b/include/linux/timex.h                        |   26 ------
 b/include/linux/types.h                        |    6 +
 b/init/calibrate.c                             |   19 ++--
 b/kernel/kcsan/core.c                          |    3 
 b/kernel/kcsan/debugfs.c                       |    8 -
 b/kernel/time/timer.c                          |    1 
 b/lib/interval_tree_test.c                     |   17 +---
 b/lib/rbtree_test.c                            |   47 +++++------
 b/lib/test_vmalloc.c                           |   10 +-
 b/mm/kasan/sw_tags.c                           |    2 
 b/mm/slub.c                                    |   37 +++++----
 include/asm-generic/timex.h                    |   23 -----
 146 files changed, 622 insertions(+), 796 deletions(-)