From: David Laight <david.laight.linux@gmail.com>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: LKML <linux-kernel@vger.kernel.org>,
Anna-Maria Behnsen <anna-maria@linutronix.de>,
Frederic Weisbecker <frederic@kernel.org>,
Benjamin Segall <bsegall@google.com>,
Eric Dumazet <edumazet@google.com>,
Andrey Vagin <avagin@openvz.org>,
Pavel Tikhomirov <ptikhomirov@virtuozzo.com>,
Peter Zijlstra <peterz@infradead.org>,
Cyrill Gorcunov <gorcunov@gmail.com>
Subject: Re: [patch V3 14/18] posix-timers: Avoid false cacheline sharing
Date: Thu, 13 Mar 2025 22:13:58 +0000 [thread overview]
Message-ID: <20250313221358.26e270db@pumpkin> (raw)
In-Reply-To: <20250308155624.341108067@linutronix.de>
On Sat, 8 Mar 2025 17:48:42 +0100 (CET)
Thomas Gleixner <tglx@linutronix.de> wrote:
> struct k_itimer has the hlist_node, which is used for lookup in the hash
> bucket, and the timer lock in the same cache line.
>
> That's obviously bad, if one CPU fiddles with a timer and the other is
> walking the hash bucket on which that timer is queued.
>
> Avoid this by restructuring struct k_itimer, so that the read mostly (only
> modified during setup and teardown) fields are in the first cache line and
> the lock and the rest of the fields which get written to are in cacheline
> 2-N.
How big is the structure?
If I count it correctly the first 'cacheline' is 64 bytes on 64bit
(and somewhat smaller on 32bit - if anyone cares).
But there are some cpu (probably ppc) with quite large cache lines.
In that case you either need to waste the space by aligning the 2nd
part the structure into an actual cache line, or just align the
structure to a 64 byte boundary.
David
>
> Reduces cacheline contention in a test case of 64 processes creating and
> accessing 20000 timers each by almost 30% according to perf.
>
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
>
> ---
> V2: New patch
> ---
> include/linux/posix-timers.h | 21 ++++++++++++---------
> kernel/time/posix-timers.c | 4 ++--
> 2 files changed, 14 insertions(+), 11 deletions(-)
>
> --- a/include/linux/posix-timers.h
> +++ b/include/linux/posix-timers.h
> @@ -177,23 +177,26 @@ static inline void posix_cputimers_init_
> * @rcu: RCU head for freeing the timer.
> */
> struct k_itimer {
> - struct hlist_node list;
> - struct hlist_node ignored_list;
> + /* 1st cacheline contains read-mostly fields */
> struct hlist_node t_hash;
> - spinlock_t it_lock;
> - const struct k_clock *kclock;
> - clockid_t it_clock;
> + struct hlist_node list;
> timer_t it_id;
> + clockid_t it_clock;
> + int it_sigev_notify;
> + enum pid_type it_pid_type;
> + struct signal_struct *it_signal;
> + const struct k_clock *kclock;
> +
> + /* 2nd cacheline and above contain fields which are modified regularly */
> + spinlock_t it_lock;
> int it_status;
> bool it_sig_periodic;
> s64 it_overrun;
> s64 it_overrun_last;
> unsigned int it_signal_seq;
> unsigned int it_sigqueue_seq;
> - int it_sigev_notify;
> - enum pid_type it_pid_type;
> ktime_t it_interval;
> - struct signal_struct *it_signal;
> + struct hlist_node ignored_list;
> union {
> struct pid *it_pid;
> struct task_struct *it_process;
> @@ -210,7 +213,7 @@ struct k_itimer {
> } alarm;
> } it;
> struct rcu_head rcu;
> -};
> +} ____cacheline_aligned_in_smp;
>
> void run_posix_cpu_timers(void);
> void posix_cpu_timers_exit(struct task_struct *task);
> --- a/kernel/time/posix-timers.c
> +++ b/kernel/time/posix-timers.c
> @@ -260,8 +260,8 @@ static int posix_get_hrtimer_res(clockid
>
> static __init int init_posix_timers(void)
> {
> - posix_timers_cache = kmem_cache_create("posix_timers_cache", sizeof(struct k_itimer), 0,
> - SLAB_ACCOUNT, NULL);
> + posix_timers_cache = kmem_cache_create("posix_timers_cache", sizeof(struct k_itimer),
> + __alignof__(struct k_itimer), SLAB_ACCOUNT, NULL);
> return 0;
> }
> __initcall(init_posix_timers);
>
>
next prev parent reply other threads:[~2025-03-13 22:14 UTC|newest]
Thread overview: 68+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-08 16:48 [patch V3 00/18] posix-timers: Rework the global hash table and provide a sane mechanism for CRIU Thomas Gleixner
2025-03-08 16:48 ` [patch V3 01/18] posix-timers: Ensure that timer initialization is fully visible Thomas Gleixner
2025-03-08 21:39 ` Frederic Weisbecker
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 02/18] posix-timers: Initialise timer before adding it to the hash table Thomas Gleixner
2025-03-11 13:25 ` Frederic Weisbecker
2025-03-11 14:16 ` Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Eric Dumazet
2025-03-08 16:48 ` [patch V3 03/18] posix-timers: Add cond_resched() to posix_timer_add() search loop Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Eric Dumazet
2025-03-08 16:48 ` [patch V3 04/18] posix-timers: Cleanup includes Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 05/18] posix-timers: Remove a few paranoid warnings Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 06/18] posix-timers: Remove SLAB_PANIC from kmem cache Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 07/18] posix-timers: Use guards in a few places Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 08/18] posix-timers: Simplify lock/unlock_timer() Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 09/18] posix-timers: Rework timer removal Thomas Gleixner
2025-03-09 23:17 ` Frederic Weisbecker
2025-03-10 6:33 ` Thomas Gleixner
2025-03-10 8:13 ` [patch V3a " Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 10/18] posix-timers: Make lock_timer() use guard() Thomas Gleixner
2025-03-10 11:57 ` Frederic Weisbecker
2025-03-10 17:36 ` Thomas Gleixner
2025-03-10 22:16 ` Frederic Weisbecker
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Peter Zijlstra
2025-03-08 16:48 ` [patch V3 11/18] posix-timers: Make signal_struct:: Next_posix_timer_id an atomic_t Thomas Gleixner
2025-03-10 22:57 ` Frederic Weisbecker
2025-03-11 13:41 ` Frederic Weisbecker
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Eric Dumazet
2025-03-08 16:48 ` [patch V3 12/18] posix-timers: Improve hash table performance Thomas Gleixner
2025-03-11 13:44 ` Frederic Weisbecker
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 13/18] posix-timers: Switch to jhash32() Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 14/18] posix-timers: Avoid false cacheline sharing Thomas Gleixner
2025-03-11 13:53 ` Frederic Weisbecker
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-13 22:13 ` David Laight [this message]
2025-03-17 6:20 ` [patch V3 14/18] " Nysal Jan K.A.
2025-03-08 16:48 ` [patch V3 15/18] posix-timers: Make per process list RCU safe Thomas Gleixner
2025-03-11 15:29 ` Frederic Weisbecker
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 16/18] posix-timers: Dont iterate /proc/$PID/timers with sighand:: Siglock held Thomas Gleixner
2025-03-08 22:38 ` Cyrill Gorcunov
2025-03-11 15:26 ` Frederic Weisbecker
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-08 16:48 ` [patch V3 17/18] posix-timers: Provide a mechanism to allocate a given timer ID Thomas Gleixner
2025-03-08 22:25 ` Cyrill Gorcunov
2025-03-11 21:35 ` Frederic Weisbecker
2025-03-11 22:05 ` Thomas Gleixner
2025-03-11 22:07 ` [patch V3a " Thomas Gleixner
2025-03-11 22:32 ` Frederic Weisbecker
2025-03-12 7:56 ` Cyrill Gorcunov
2025-03-12 11:24 ` Thomas Gleixner
2025-03-12 11:31 ` Thomas Gleixner
2025-03-12 12:41 ` Cyrill Gorcunov
2025-03-12 17:45 ` Thomas Gleixner
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
2025-03-12 12:59 ` [patch V3 17/18] " Cyrill Gorcunov
2025-03-08 16:48 ` [patch V3 18/18] selftests/timers/posix-timers: Add a test for exact allocation mode Thomas Gleixner
2025-03-10 8:11 ` [patch V3a " Thomas Gleixner
2025-03-11 21:44 ` Frederic Weisbecker
2025-03-13 11:31 ` [tip: timers/core] " tip-bot2 for Thomas Gleixner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250313221358.26e270db@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=anna-maria@linutronix.de \
--cc=avagin@openvz.org \
--cc=bsegall@google.com \
--cc=edumazet@google.com \
--cc=frederic@kernel.org \
--cc=gorcunov@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=ptikhomirov@virtuozzo.com \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox