All of lore.kernel.org
 help / color / mirror / Atom feed
* Question about replacing while_each_thread().
@ 2017-02-01 10:47 Tetsuo Handa
  2017-02-01 17:19 ` Oleg Nesterov
  0 siblings, 1 reply; 2+ messages in thread
From: Tetsuo Handa @ 2017-02-01 10:47 UTC (permalink / raw)
  To: oleg; +Cc: dserrg, snanda, rientjes, linux-kernel

Hello.

I have a question about commit 0c740d0afc3bff0a ("introduce
for_each_thread() to replace the buggy while_each_thread()").

IOPRIO_WHO_USER case in sys_ioprio_set()/sys_ioprio_get() in block/ioprio.c
are using

  rcu_read_lock();
  do_each_thread(g, p) {
    (...snipped...)
  } while_each_thread(g, p);
  rcu_read_unlock();

sequence which is unsafe according to that commit, but
I'm not sure what the correct fix is.

That commit says

  The new for_each_thread(g, t) helper is always safe under
  rcu_read_lock() as long as this task_struct can't go away.

but what is the requirement for "can't go away" ?

Is rcu_read_lock() sufficient (i.e.

  rcu_read_lock();
  for_each_process_thread(g, p) {
    (...snipped...)
  }
  rcu_read_unlock();

is OK) for "can't go away" ?
Is tasklist_lock held for read or write required (i.e.

  read_lock(&tasklist_lock);
  for_each_process_thread(g, p) {
    (...snipped...)
  }
  read_unlock(&tasklist_lock);

is needed) for "can't go away" ?

I hope rcu_read_lock() is sufficient according to usage in
show_state_filter() and check_hung_uninterruptible_tasks().

Likewise, IOPRIO_WHO_PGRP case are using

  rcu_read_lock();
  do {
    if ((pgrp) != NULL)
      hlist_for_each_entry_rcu((p), &(pgrp)->tasks[PIDTYPE_PGID], pids[PIDTYPE_PGID].node) {
        {
          struct task_struct *tg___ = p;
          do {
            (...snipped...)
          } while_each_thread(tg___, p);
          p = tg___;
        }
        if (PIDTYPE_PGID == PIDTYPE_PID)
          break;
      }
  } while (0);
  rcu_read_unlock();

sequence which I guess it is unsafe as well.
In this case updating do_each_pid_thread() to use for_each_thread() and
updating while_each_pid_thread() not to use while_each_thread() is
the correct fix?

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Question about replacing while_each_thread().
  2017-02-01 10:47 Question about replacing while_each_thread() Tetsuo Handa
@ 2017-02-01 17:19 ` Oleg Nesterov
  0 siblings, 0 replies; 2+ messages in thread
From: Oleg Nesterov @ 2017-02-01 17:19 UTC (permalink / raw)
  To: Tetsuo Handa; +Cc: dserrg, snanda, rientjes, linux-kernel

Hi Tetsuo,

On 02/01, Tetsuo Handa wrote:
>
> Is rcu_read_lock() sufficient (i.e.
>
>   rcu_read_lock();
>   for_each_process_thread(g, p) {
>     (...snipped...)
>   }
>   rcu_read_unlock();
>
> is OK) for "can't go away" ?

Yes, this should work just fine,

> Likewise, IOPRIO_WHO_PGRP case are using
>
>   rcu_read_lock();
>   do {
>     if ((pgrp) != NULL)
>       hlist_for_each_entry_rcu((p), &(pgrp)->tasks[PIDTYPE_PGID], pids[PIDTYPE_PGID].node) {
>         {
>           struct task_struct *tg___ = p;
>           do {
>             (...snipped...)
>           } while_each_thread(tg___, p);
>           p = tg___;
>         }
>         if (PIDTYPE_PGID == PIDTYPE_PID)
>           break;
>       }
>   } while (0);
>   rcu_read_unlock();
>
> sequence which I guess it is unsafe as well.

Hmm, indeed, I forgot there is another while_each_thread() hidden in
do_each_pid_thread()

> In this case updating do_each_pid_thread() to use for_each_thread() and
> updating while_each_pid_thread() not to use while_each_thread() is
> the correct fix?

Yes, I think so, just

	--- a/include/linux/pid.h
	+++ b/include/linux/pid.h
	@@ -191,10 +191,10 @@ pid_t pid_vnr(struct pid *pid);
	 #define do_each_pid_thread(pid, type, task)				\
		do_each_pid_task(pid, type, task) {				\
			struct task_struct *tg___ = task;			\
	-		do {
	+		for_each_thread(tg__, task) {
	 
	 #define while_each_pid_thread(pid, type, task)				\
	-		} while_each_thread(tg___, task);			\
	+		}							\
			task = tg___;						\
		} while_each_pid_task(pid, type, task)
	 #endif /* _LINUX_PID_H */


but perhaps we can also cleanup it... the usage of 'tg___' doesn't look nice
either way, so perhaps

	#define do_each_pid_thread(pid, type, task) do {			\
		struct task_struct *tg___;					\
		do_each_pid_task(pid, type, tg___) {				\
			for_each_thread(tg__, task) {

	#define while_each_pid_thread(pid, type, task)				\
			}							\
		} while_each_pid_task(pid, type, task);				\
		} while (0)

will look a bit better? up to you.

Oleg.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-02-01 17:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-01 10:47 Question about replacing while_each_thread() Tetsuo Handa
2017-02-01 17:19 ` Oleg Nesterov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.