From: David Laight <david.laight.linux@gmail.com>
To: Eric Dumazet <edumazet@google.com>
Cc: Anna-Maria Behnsen <anna-maria@linutronix.de>,
Frederic Weisbecker <frederic@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
linux-kernel <linux-kernel@vger.kernel.org>,
Benjamin Segall <bsegall@google.com>,
Eric Dumazet <eric.dumazet@gmail.com>
Subject: Re: [PATCH V2 4/4] posix-timers: Use RCU in posix_timer_add()
Date: Wed, 19 Feb 2025 19:38:32 +0000 [thread overview]
Message-ID: <20250219193832.6c3fa40f@pumpkin> (raw)
In-Reply-To: <20250219125522.2535263-5-edumazet@google.com>
On Wed, 19 Feb 2025 12:55:22 +0000
Eric Dumazet <edumazet@google.com> wrote:
> If many posix timers are hashed in posix_timers_hashtable,
> hash_lock can be held for long durations.
>
> This can be really bad in some cases as Thomas
> explained in https://lore.kernel.org/all/87ednpyyeo.ffs@tglx/
>
> We can perform all searches under RCU, then acquire
> the lock only when there is a good chance to need it,
> and after cpu caches were populated.
>
> Also add a cond_resched() in the possible long loop.
Since this code fragment has a 'free choice' of the timer id, why not
select an empty table slot and then pick a value that maps to it?
You can run a free-list through the empty table slots so the allocate
is (almost always) fixed cost and trivial.
The only complexity arises when the table is full and needs to be
reallocated twice as large.
The high bits of the 'id' can be incremented every time the id is allocated
so stale ids can be detected (until a quite large number of allocate/free).
David
>
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> ---
> kernel/time/posix-timers.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
> index ed27c7eab456..bd73bc4707c1 100644
> --- a/kernel/time/posix-timers.c
> +++ b/kernel/time/posix-timers.c
> @@ -125,7 +125,19 @@ static int posix_timer_add(struct k_itimer *timer)
>
> head = &posix_timers_hashtable[hash(sig, id)];
>
> + rcu_read_lock();
> + if (posix_timers_find(head, sig, id)) {
> + rcu_read_unlock();
> + cond_resched();
> + continue;
> + }
> + rcu_read_unlock();
> spin_lock(&hash_lock);
> + /*
> + * We must perform the lookup under hash_lock protection
> + * because another thread could have used the same id.
> + * This is very unlikely, but possible.
> + */
> if (!posix_timers_find(head, sig, id)) {
> timer->it_id = (timer_t)id;
> timer->it_signal = (struct signal_struct *)((unsigned long)sig | 1UL);
next prev parent reply other threads:[~2025-02-19 19:38 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-19 12:55 [PATCH V2 0/4] posix-timers: Reduce spinlock contention Eric Dumazet
2025-02-19 12:55 ` [PATCH V2 1/4] posix-timers: Make next_posix_timer_id an atomic_t Eric Dumazet
2025-02-20 8:09 ` Thomas Gleixner
2025-02-20 8:49 ` Eric Dumazet
2025-02-20 14:04 ` Thomas Gleixner
2025-02-20 14:32 ` Thomas Gleixner
2025-02-20 15:55 ` Eric Dumazet
2025-02-20 16:19 ` Thomas Gleixner
2025-02-19 12:55 ` [PATCH V2 2/4] posix-timers: Initialise timer->it_id in posix_timer_add() Eric Dumazet
2025-02-20 8:12 ` Thomas Gleixner
2025-02-20 8:48 ` Eric Dumazet
2025-02-20 14:10 ` Thomas Gleixner
2025-02-19 12:55 ` [PATCH V2 3/4] posix-timers: Initialise timer->it_signal " Eric Dumazet
2025-02-20 8:19 ` Thomas Gleixner
2025-02-20 8:44 ` Eric Dumazet
2025-02-20 14:13 ` Thomas Gleixner
2025-02-19 12:55 ` [PATCH V2 4/4] posix-timers: Use RCU " Eric Dumazet
2025-02-19 19:38 ` David Laight [this message]
2025-02-19 19:46 ` Eric Dumazet
2025-02-24 9:33 ` Thomas Gleixner
2025-02-24 9:58 ` Eric Dumazet
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=20250219193832.6c3fa40f@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=anna-maria@linutronix.de \
--cc=bsegall@google.com \
--cc=edumazet@google.com \
--cc=eric.dumazet@gmail.com \
--cc=frederic@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--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