From: ebiederm@xmission.com (Eric W. Biederman)
To: Oleg Nesterov <oleg@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Michal Hocko <mhocko@suse.cz>, Sergey Dyasly <dserrg@gmail.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 3/4] proc: simplify proc_task_readdir/first_tid paths
Date: Mon, 03 Jun 2013 15:06:23 -0700 [thread overview]
Message-ID: <87r4girgrk.fsf@xmission.com> (raw)
In-Reply-To: <20130603190702.GA11510@redhat.com> (Oleg Nesterov's message of "Mon, 3 Jun 2013 21:07:02 +0200")
Oleg Nesterov <oleg@redhat.com> writes:
> proc_task_readdir() does not really need "leader", first_tid()
> has to revalidate it anyway. Just pass proc_pid(inode) to
> first_tid() instead, it can do pid_task(PIDTYPE_PID) itself
> and read ->group_leader only if necessary.
>
> The patch also extracts the "inode is dead" code
> from pid_delete_dentry(dentry) into the new trivial helper,
> proc_inode_is_dead(inode), proc_task_readdir() uses it to return
> -ENOENT if this dir was removed. This is a bit racy, but the race
> is very inlikely and the getdents() after openndir() can see the
> empty "." + ".." dir only once.
This version looks good. I especially like the factoring out of
proc_inode_is_dead, that makes the purpose of that code much clearer.
Reviewed-by: "Eric W. Biederman" <ebiederm@xmission.com>
> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
> ---
> fs/proc/base.c | 53 ++++++++++++++++++++++-------------------------------
> 1 files changed, 22 insertions(+), 31 deletions(-)
>
> diff --git a/fs/proc/base.c b/fs/proc/base.c
> index bed1096..5e0e02f 100644
> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -1652,13 +1652,18 @@ int pid_revalidate(struct dentry *dentry, unsigned int flags)
> return 0;
> }
>
> +static inline bool proc_inode_is_dead(struct inode *inode)
> +{
> + return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
> +}
> +
> int pid_delete_dentry(const struct dentry *dentry)
> {
> /* Is the task we represent dead?
> * If so, then don't put the dentry on the lru list,
> * kill it immediately.
> */
> - return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
> + return proc_inode_is_dead(dentry->d_inode);
> }
>
> const struct dentry_operations pid_dentry_operations =
> @@ -3173,34 +3178,35 @@ out_no_task:
> * In the case of a seek we start with the leader and walk nr
> * threads past it.
> */
> -static struct task_struct *first_tid(struct task_struct *leader,
> - int tid, int nr, struct pid_namespace *ns)
> +static struct task_struct *first_tid(struct pid *pid, int tid,
> + int nr, struct pid_namespace *ns)
> {
> - struct task_struct *pos;
> + struct task_struct *pos, *task;
>
> rcu_read_lock();
> - /* Attempt to start with the pid of a thread */
> + task = pid_task(pid, PIDTYPE_PID);
> + if (!task)
> + goto fail;
> +
> + /* Attempt to start with the tid of a thread */
> if (tid && (nr > 0)) {
> pos = find_task_by_pid_ns(tid, ns);
> - if (pos && (pos->group_leader == leader))
> + if (pos && same_thread_group(pos, task))
> goto found;
> }
>
> /* If nr exceeds the number of threads there is nothing todo */
> - if (nr && nr >= get_nr_threads(leader))
> - goto fail;
> - /* It could be unhashed before we take rcu lock */
> - if (!pid_alive(leader))
> + if (nr && nr >= get_nr_threads(task))
> goto fail;
>
> /* If we haven't found our starting place yet start
> * with the leader and walk nr threads forward.
> */
> - pos = leader;
> + pos = task = task->group_leader;
> do {
> if (nr-- <= 0)
> goto found;
> - } while_each_thread(leader, pos);
> + } while_each_thread(task, pos);
> fail:
> pos = NULL;
> goto out;
> @@ -3247,26 +3253,13 @@ static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldi
> {
> struct dentry *dentry = filp->f_path.dentry;
> struct inode *inode = dentry->d_inode;
> - struct task_struct *leader = NULL;
> struct task_struct *task;
> - int retval = -ENOENT;
> ino_t ino;
> int tid;
> struct pid_namespace *ns;
>
> - task = get_proc_task(inode);
> - if (!task)
> - goto out_no_task;
> - rcu_read_lock();
> - if (pid_alive(task)) {
> - leader = task->group_leader;
> - get_task_struct(leader);
> - }
> - rcu_read_unlock();
> - put_task_struct(task);
> - if (!leader)
> - goto out_no_task;
> - retval = 0;
> + if (proc_inode_is_dead(inode))
> + return -ENOENT;
>
> switch ((unsigned long)filp->f_pos) {
> case 0:
> @@ -3289,7 +3282,7 @@ static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldi
> ns = filp->f_dentry->d_sb->s_fs_info;
> tid = (int)filp->f_version;
> filp->f_version = 0;
> - for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
> + for (task = first_tid(proc_pid(inode), tid, filp->f_pos - 2, ns);
> task;
> task = next_tid(task), filp->f_pos++) {
> tid = task_pid_nr_ns(task, ns);
> @@ -3302,9 +3295,7 @@ static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldi
> }
> }
> out:
> - put_task_struct(leader);
> -out_no_task:
> - return retval;
> + return 0;
> }
>
> static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
next prev parent reply other threads:[~2013-06-03 22:06 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-03 19:06 [PATCH v2 0/4] proc: first_tid() fix/cleanup Oleg Nesterov
2013-06-03 19:06 ` [PATCH v2 1/4] proc: first_tid: fix the potential use-after-free Oleg Nesterov
2013-06-03 19:07 ` [PATCH v2 2/4] proc: change first_tid() to use while_each_thread() Oleg Nesterov
2013-06-03 19:07 ` [PATCH v2 3/4] proc: simplify proc_task_readdir/first_tid paths Oleg Nesterov
2013-06-03 22:06 ` Eric W. Biederman [this message]
2013-06-03 19:07 ` [PATCH v2 4/4] proc: avoid ->f_pos overflows in proc_task_readdir() paths Oleg Nesterov
2013-06-03 22:18 ` Eric W. Biederman
2013-06-04 17:14 ` Oleg Nesterov
2013-06-04 17:39 ` Al Viro
2013-06-04 19:57 ` Oleg Nesterov
2013-06-04 21:06 ` Al Viro
2013-06-04 0:58 ` Al Viro
2013-06-04 17:35 ` Oleg Nesterov
2013-06-04 17:32 ` [PATCH v2 0/4] proc: first_tid() fix/cleanup Oleg Nesterov
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=87r4girgrk.fsf@xmission.com \
--to=ebiederm@xmission.com \
--cc=akpm@linux-foundation.org \
--cc=dserrg@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mhocko@suse.cz \
--cc=oleg@redhat.com \
/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 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.