From: Oleg Nesterov <oleg@redhat.com>
To: Sargun Dhillon <sargun@sargun.me>
Cc: linux-kernel@vger.kernel.org,
containers@lists.linux-foundation.org, linux-api@vger.kernel.org,
linux-fsdevel@vger.kernel.org, tycho@tycho.ws, jannh@google.com,
cyphar@cyphar.com, christian.brauner@ubuntu.com,
luto@amacapital.net, viro@zeniv.linux.org.uk
Subject: Re: [PATCH v2 1/4] vfs, fdtable: Add get_task_file helper
Date: Mon, 9 Dec 2019 16:52:45 +0100 [thread overview]
Message-ID: <20191209155242.GC5388@redhat.com> (raw)
In-Reply-To: <20191209070609.GA32438@ircssh-2.c.rugged-nimbus-611.internal>
On 12/09, Sargun Dhillon wrote:
>
> +struct file *get_task_file(struct task_struct *task, unsigned int fd)
> +{
> + struct file *file = NULL;
> +
> + task_lock(task);
> + rcu_read_lock();
> +
> + if (task->files) {
> + file = fcheck_files(task->files, fd);
> + if (file && !get_file_rcu(file))
> + file = NULL;
> + }
On second thought this is not exactly right, get_file_rcu() can fail if
get_task_file() races with dup2(), in this case we need to do fcheck_files()
again. And this is what __fget() already does, so may be the patch below
makes more sense?
I will leave this to other reviewers, but suddenly I recall that I have
already sent the patch which adds a similar helper a while ago.
See https://lore.kernel.org/lkml/20180915160423.GA31461@redhat.com/
In short, get_files_struct() should be avoided because it can race with
exec() and break POSIX locks which use ->fl_owner = files_struct.
Oleg.
--- x/fs/file.c
+++ x/fs/file.c
@@ -706,9 +706,9 @@ void do_close_on_exec(struct files_struc
spin_unlock(&files->file_lock);
}
-static struct file *__fget(unsigned int fd, fmode_t mask, unsigned int refs)
+static struct file *__fget_files(struct files_struct *files, unsigned int fd,
+ fmode_t mask, unsigned int refs)
{
- struct files_struct *files = current->files;
struct file *file;
rcu_read_lock();
@@ -729,6 +729,23 @@ loop:
return file;
}
+struct file *fget_task(struct task_struct *task, unsigned int fd)
+{
+ struct file *file;
+
+ task_lock(task);
+ if (task->files)
+ file = __fget_files(task->files, fd, 0, 1);
+ task_unlock(task);
+
+ return file;
+}
+
+static struct file *__fget(unsigned int fd, fmode_t mask, unsigned int refs)
+{
+ return __fget_files(current->files, fd, mask, refs);
+}
+
struct file *fget_many(unsigned int fd, unsigned int refs)
{
return __fget(fd, FMODE_PATH, refs);
prev parent reply other threads:[~2019-12-09 15:52 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-09 7:06 [PATCH v2 1/4] vfs, fdtable: Add get_task_file helper Sargun Dhillon
2019-12-09 15:52 ` Oleg Nesterov [this message]
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=20191209155242.GC5388@redhat.com \
--to=oleg@redhat.com \
--cc=christian.brauner@ubuntu.com \
--cc=containers@lists.linux-foundation.org \
--cc=cyphar@cyphar.com \
--cc=jannh@google.com \
--cc=linux-api@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=sargun@sargun.me \
--cc=tycho@tycho.ws \
--cc=viro@zeniv.linux.org.uk \
/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.