Re: getting processor numbers

From: Oleg Nesterov
Date: Wed Apr 04 2007 - 05:35:50 EST


On 04/03, Andrew Morton wrote:
>
> On Tue, 03 Apr 2007 16:00:50 -0700
> Ulrich Drepper <drepper@xxxxxxxxxx> wrote:
>
> > If there is possibility to treat this case special and make it faster,
> > please do so. It would be best to allow pid==0 as a special case so
> > that callers don't have to find out the TID (which they shouldn't have
> > to know).
> >
>
> OK.
>
> Does anyone see a reason why we cannot do this?
>
> --- a/kernel/sched.c~sched_getaffinity-speedup
> +++ a/kernel/sched.c
> @@ -4381,8 +4381,12 @@ long sched_getaffinity(pid_t pid, cpumas
> struct task_struct *p;
> int retval;
>
> - lock_cpu_hotplug();
> - read_lock(&tasklist_lock);
> + if (pid) {
> + lock_cpu_hotplug();
> + read_lock(&tasklist_lock);
> + } else {
> + preempt_disable(); /* Prevent CPU hotplugging */
> + }

But we don't need tasklist_lock at all, we can use rcu_read_lock/unlock.

Q: don't we need task_rq_lock() to read ->cpus_allowed "atomically" ?

UNTESTED.

--- OLD/kernel/sched.c~ 2007-04-03 13:05:02.000000000 +0400
+++ OLD/kernel/sched.c 2007-04-04 12:29:04.000000000 +0400
@@ -4433,22 +4433,17 @@ long sched_setaffinity(pid_t pid, cpumas
int retval;

mutex_lock(&sched_hotcpu_mutex);
- read_lock(&tasklist_lock);
+ rcu_read_lock();

p = find_process_by_pid(pid);
if (!p) {
- read_unlock(&tasklist_lock);
+ rcu_read_unlock();
mutex_unlock(&sched_hotcpu_mutex);
return -ESRCH;
}

- /*
- * It is not safe to call set_cpus_allowed with the
- * tasklist_lock held. We will bump the task_struct's
- * usage count and then drop tasklist_lock.
- */
get_task_struct(p);
- read_unlock(&tasklist_lock);
+ rcu_read_unlock();

retval = -EPERM;
if ((current->euid != p->euid) && (current->euid != p->uid) &&
@@ -4523,7 +4518,7 @@ long sched_getaffinity(pid_t pid, cpumas
int retval;

mutex_lock(&sched_hotcpu_mutex);
- read_lock(&tasklist_lock);
+ rcu_read_lock();

retval = -ESRCH;
p = find_process_by_pid(pid);
@@ -4537,7 +4532,7 @@ long sched_getaffinity(pid_t pid, cpumas
cpus_and(*mask, p->cpus_allowed, cpu_online_map);

out_unlock:
- read_unlock(&tasklist_lock);
+ rcu_read_unlock();
mutex_unlock(&sched_hotcpu_mutex);
if (retval)
return retval;

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/