rlimits: do not grab tasklist_lock for do_prlimit on current
PATCH 1/1 rlimits: do not grab tasklist_lock for do_prlimit on current
The tasklist_lock can be a scalability bottleneck. For current tasks, we don't need the tasklist_lock to protect tsk->sighand or tsk->signal. If non-current callers become a bottleneck, we could use lock_task_sighand().
--- kernel/sys.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sys.c +4 -2
@@ -1576,7 +1576,8 @@ int do_prlimit(struct task_struct *tsk, unsigned int resource, }/* protect tsk->signal and tsk->sighand from disappearing */ -read_lock (&tasklist_lock); +if (tsk != current) +read_lock (&tasklist_lock);if (!tsk->sighand) { retval = -ESRCH ; goto out;
@@ -1611,7 +1612,8 @@ int do_prlimit(struct task_struct *tsk, unsigned int resource, IS_ENABLED(CONFIG_POSIX_TIMERS))update_rlimit_cpu (tsk, new_rlim->rlim_cur); out: -read_unlock (&tasklist_lock); +if (tsk != current) +read_unlock (&tasklist_lock);return retval; }
Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
Quoted 4 lines · Barret Rhoden
The
tasklist_lockcan be a scalability bottleneck. For current tasks, we don't need thetasklist_lockto protecttsk->sighandortsk->signal. If non-current callers become a bottleneck, we could uselock_task_sighand().
Do you have any numbers? As the entire point of this change is performance it would be good to see how the performance changes.
Especially as a read_lock should not be too bad as it allows sharing, nor do I expect reading or writing the rlimit values to be particularly frequent. So some insight into what kinds of userspace patterns make this a problem would be nice.
This change is a bit scary as it makes taking a lock conditional and increases the probability of causing a locking mistake.
If you are going to make this change I would say that do_prlimit should become static and taking the tasklist_lock should move into prlimit64.
Looking a little closer it looks like that update_rlimit_cpu should use lock_task_sighand, and once lock_task_sighand is used there is actually no need for the tasklist_lock at all. As holding the reference to tsk guarantees that tsk->signal remains valid.
So I completely agree there are cleanups that can happen in this area. Please make those and show numbers in how they improve things, instead of making the code worse with a conditional lock.
Eric
Quoted 28 lines
Signed-off-by Barret Rhoden <[email protected]>--- kernel/sys.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/sys.c +4
-2@@ -1576,7 +1576,8 @@ int do_prlimit(struct task_struct *tsk, unsigned int resource, }/* protect tsk->signal and tsk->sighand from disappearing */ -read_lock (&tasklist_lock); +if (tsk != current) +read_lock (&tasklist_lock);if (!tsk->sighand) { retval = -ESRCH ; goto out;@@ -1611,7 +1612,8 @@ int do_prlimit(struct task_struct *tsk, unsigned int resource, IS_ENABLED(CONFIG_POSIX_TIMERS))update_rlimit_cpu (tsk, new_rlim->rlim_cur); out: -read_unlock (&tasklist_lock); +if (tsk != current) +read_unlock (&tasklist_lock);return retval; }
Hi -
Quoted 7 lines · Eric W. Biederman
Do you have any numbers? As the entire point of this change is performance it would be good to see how the performance changes.
Especially as a
read_lockshould not be too bad as it allows sharing, nor do I expect reading or writing the rlimit values to be particularly frequent. So some insight into what kinds of userspace patterns make this a problem would be nice.
This was motivated by slowdowns we observed on a few machines running tests in a cluster. AFAIK, there were a lot of small tests, many of which mucked with process management syscalls while fork/joining other tasks.
Based on a cycles profile, it looked like ~87% of the time was spent in the kernel, ~42% of which was just trying to get *some* spinlock (queued_spin_lock_slowpath, not necessarily the tasklist_lock).
The big offenders (with rough percentages in cycles of the overall trace):
- do_wait 11%
- setpriority 8% (potential future patch)
- kill 8%
- do_exit 5%
- clone 3%
- prlimit64 2% (this patch)
- getrlimit 1% (this patch)
Even though do_prlimit was using a read_lock, it was still contending on the internal queued_spin_lock.
The prlimit was only 3% of the total. This patch was more of a "oh, this doesn't *need* the tasklist_lock for p == current" - can we fix that? I actually don't even know often those prlimit64 calls had p == current.
setpriority was a bigger one too - is the tasklist lock only needed for the PGRP ops? (I thought so based on where the tasklist_lock is write locked and the comment on task_pgrp()). If so, I could do that in another patch.
Quoted 2 lines
This change is a bit scary as it makes taking a lock conditional and increases the probability of causing a locking mistake.
I definitely see how making the code more brittle might not be worth the small win. If this is more "damage" than "cleanup", then I can drop it.
Quoted 8 lines
If you are going to make this change I would say that
do_prlimitshould become static and taking thetasklist_lockshould move into prlimit64.Looking a little closer it looks like that
update_rlimit_cpushould uselock_task_sighand, and oncelock_task_sighandis used there is actually no need for thetasklist_lockat all. As holding the reference to tsk guarantees thattsk->signalremains valid.
Maybe do both? unconditionally grab lock_task_sighand (instead of tasklist_lock) in prlimit64.
Quoted 3 lines
So I completely agree there are cleanups that can happen in this area. Please make those and show numbers in how they improve things, instead of making the code worse with a conditional lock.
Unfortunately, I can't easily get a "before and after" on this change. The motivating issue popped up sporadically, but getting it to happen in a setup under *my* control is organizationally a pain. So I understand if you wouldn't want the patch for that reason. Ideally, the changes would make the code easier to follow and clearer about why we're locking.
If you're OK with two patches that 1) grab lock_task_sighand in prlimit64 and 2) moving the read_lock in {set,get}priority into the PGRP cases (assuming I was correct on that), I can send them out.
If it's too much of a risk/ugliness for not clear enough gain (in code quality or performance), I'm fine with dropping it.
Thanks for looking,
Barret
Quoted 38 lines · Barret Rhoden
Hi -
Quoted 7 lines · Eric W. Biederman
Do you have any numbers? As the entire point of this change is performance it would be good to see how the performance changes.
Especially as a
read_lockshould not be too bad as it allows sharing, nor do I expect reading or writing the rlimit values to be particularly frequent. So some insight into what kinds of userspace patterns make this a problem would be nice.This was motivated by slowdowns we observed on a few machines running tests in a cluster. AFAIK, there were a lot of small tests, many of which mucked with process management syscalls while fork/joining other tasks.
Based on a cycles profile, it looked like ~87% of the time was spent in the kernel, ~42% of which was just trying to get *some* spinlock (
queued_spin_lock_slowpath, not necessarily thetasklist_lock).The big offenders (with rough percentages in cycles of the overall trace):
-
do_wait11%
- setpriority 8% (potential future patch)
- kill 8%
-do_exit5%
- clone 3%
- prlimit64 2% (this patch)
- getrlimit 1% (this patch)Even though
do_prlimitwas using aread_lock, it was still contending on the internalqueued_spin_lock.The prlimit was only 3% of the total. This patch was more of a "oh, this doesn't *need* the
tasklist_lockfor p == current" - can we fix that? I actually don't even know often those prlimit64 calls had p == current.
Quoted 4 lines
setpriority was a bigger one too - is the tasklist lock only needed for the PGRP ops? (I thought so based on where the
tasklist_lockis write locked and the comment ontask_pgrp()). If so, I could do that in another patch.
That is my understanding. For setpriority to change everything atomically it must hold the tasklist lock when dealing with more than one process at a time.
Quoted 11 lines
Quoted 2 lines
This change is a bit scary as it makes taking a lock conditional and increases the probability of causing a locking mistake.
I definitely see how making the code more brittle might not be worth the small win. If this is more "damage" than "cleanup", then I can drop it.
Quoted 4 lines
If you are going to make this change I would say that
do_prlimitshould become static and taking thetasklist_lockshould move into prlimit64.
Quoted 7 lines
Quoted 4 lines
Looking a little closer it looks like that
update_rlimit_cpushould uselock_task_sighand, and oncelock_task_sighandis used there is actually no need for thetasklist_lockat all. As holding the reference to tsk guarantees thattsk->signalremains valid.Maybe do both? unconditionally grab
lock_task_sighand(instead oftasklist_lock) in prlimit64.
In update_rlimit_cpu use lock_task_sighand instead of unconditionally grabbing sighand->siglock (because without tasklist_lock sighand might be NULL).
Then do_prlimit64 can drop the tasklist lock and the test for sighand == NULL.
This will address every prlimit64 case instead of just when updating current. With prlimit64 in your profile I expect some of those are non-current.
Quoted 10 lines
Quoted 3 lines
So I completely agree there are cleanups that can happen in this area. Please make those and show numbers in how they improve things, instead of making the code worse with a conditional lock.
Unfortunately, I can't easily get a "before and after" on this change. The motivating issue popped up sporadically, but getting it to happen in a setup under *my* control is organizationally a pain. So I understand if you wouldn't want the patch for that reason. Ideally, the changes would make the code easier to follow and clearer about why we're locking.
Even a microbenchmark that stresses the lock and can show the performance impact of the change you are making is useful.
Simply reorganizing and removing an unnecessary lock lowers the bar because the code is then both simpler and pretty much by definition faster. Although sometimes another lock is then hit and the contention moves.
Quoted 3 lines
If you're OK with two patches that 1) grab
lock_task_sighandin prlimit64 and 2) moving theread_lockin {set,get}priority into the PGRP cases (assuming I was correct on that), I can send them out.
I think getpriority can only use the rcu_read_lock. I don't think it has any atomicity guarantees.
For setpriority the single process case should be safe just use rcu. I haven't read through all of what set_one_prio is doing to confirm it is safe, but in principle it should be safe.
Quoted 2 lines
If it's too much of a risk/ugliness for not clear enough gain (in code quality or performance), I'm fine with dropping it.
Removing the tasklist_lock where we can is definitely a clear gain.
Simply shoving tasklist_lock aside and making the code more complicated is much less clear.
Plus anything you can benchmark (even microbenchmark) and show the benefit of is welcome. Especially when you have indications that it makes a difference in a larger context.
Eric
On Wed, Dec 15, 2021 at 01:42:32PM -0600, Eric W. Biederman wrote:
...
Quoted 4 lines
Quoted 3 lines
If it's too much of a risk/ugliness for not clear enough gain (in code quality or performance), I'm fine with dropping it.
Quoted 8 lines
Removing the
tasklist_lockwhere we can is definitely a clear gain.Simply shoving
tasklist_lockaside and making the code more complicated is much less clear.Plus anything you can benchmark (even microbenchmark) and show the benefit of is welcome. Especially when you have indications that it makes a difference in a larger context.
Thanks for looking into this, Eric! I must confess I've a vague memory about this code. Still while you're talking about cleanup I wonder if we should make do_prlimit() being a static function, not global as it now.
Quoted 3 lines · Eric W. Biederman
In
update_rlimit_cpuuselock_task_sighandinstead of unconditionally grabbingsighand->siglock(because withouttasklist_locksighand might be NULL).
this ended up being a minor complication, since update_rlimit_cpu() could fail if the task was exiting, but i think i sorted it out.
i'll send out revised patchset shortly with this change, including making do_prlimit() static.
thanks,
barret