* [PATCH] fs/file.c: __fget() and dup2() atomicity rules
@ 2015-06-29 15:10 Eric Dumazet
2015-06-29 17:46 ` Al Viro
0 siblings, 1 reply; 3+ messages in thread
From: Eric Dumazet @ 2015-06-29 15:10 UTC (permalink / raw)
To: Al Viro
Cc: Andrew Morton, linux-kernel@vger.kernel.org, Dmitry Vyukov,
Eric Dumazet, linux-fsdevel
From: Eric Dumazet <edumazet@google.com>
__fget() makes sure a file refcount is not zero before
taking a reference. It should also fetch again file pointer
in order to respect dup2() atomicity requirements.
It should either read a NULL pointer or a file on which
a refcount can be taken.
Dmitry had following test failing sometimes :
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
int fd;
void *Thread(void *x) {
char buf;
int n = read(fd, &buf, 1);
if (n != 1)
exit(printf("read failed: n=%d errno=%d\n", n, errno));
return 0;
}
int main()
{
fd = open("/dev/urandom", O_RDONLY);
int fd2 = open("/dev/urandom", O_RDONLY);
if (fd == -1 || fd2 == -1)
exit(printf("open failed\n"));
pthread_t th;
pthread_create(&th, 0, Thread, 0);
if (dup2(fd2, fd) == -1)
exit(printf("dup2 failed\n"));
pthread_join(th, 0);
if (close(fd) == -1)
exit(printf("close failed\n"));
if (close(fd2) == -1)
exit(printf("close failed\n"));
printf("DONE\n");
return 0;
}
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
---
fs/file.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/fs/file.c b/fs/file.c
index 93c5f89c248b..492bd74c4433 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -635,11 +635,17 @@ static struct file *__fget(unsigned int fd, fmode_t mask)
struct file *file;
rcu_read_lock();
+loop:
file = fcheck_files(files, fd);
if (file) {
- /* File object ref couldn't be taken */
- if ((file->f_mode & mask) || !get_file_rcu(file))
+ /* File object ref couldn't be taken.
+ * dup2() atomicity guarantee is the reason
+ * we loop to catch the new file (or NULL pointer)
+ */
+ if (file->f_mode & mask)
file = NULL;
+ else if (!get_file_rcu(file))
+ goto loop;
}
rcu_read_unlock();
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] fs/file.c: __fget() and dup2() atomicity rules
2015-06-29 15:10 [PATCH] fs/file.c: __fget() and dup2() atomicity rules Eric Dumazet
@ 2015-06-29 17:46 ` Al Viro
2015-06-30 5:02 ` Eric Dumazet
0 siblings, 1 reply; 3+ messages in thread
From: Al Viro @ 2015-06-29 17:46 UTC (permalink / raw)
To: Eric Dumazet
Cc: Andrew Morton, linux-kernel@vger.kernel.org, Dmitry Vyukov,
Eric Dumazet, linux-fsdevel
On Mon, Jun 29, 2015 at 05:10:30PM +0200, Eric Dumazet wrote:
> From: Eric Dumazet <edumazet@google.com>
>
> __fget() makes sure a file refcount is not zero before
> taking a reference. It should also fetch again file pointer
> in order to respect dup2() atomicity requirements.
>
> It should either read a NULL pointer or a file on which
> a refcount can be taken.
Hmm... The problem is real, but I wonder if one could trigger a long
spin there...
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] fs/file.c: __fget() and dup2() atomicity rules
2015-06-29 17:46 ` Al Viro
@ 2015-06-30 5:02 ` Eric Dumazet
0 siblings, 0 replies; 3+ messages in thread
From: Eric Dumazet @ 2015-06-30 5:02 UTC (permalink / raw)
To: Al Viro
Cc: Andrew Morton, linux-kernel@vger.kernel.org, Dmitry Vyukov,
Eric Dumazet, linux-fsdevel
On Mon, 2015-06-29 at 18:46 +0100, Al Viro wrote:
> On Mon, Jun 29, 2015 at 05:10:30PM +0200, Eric Dumazet wrote:
> > From: Eric Dumazet <edumazet@google.com>
> >
> > __fget() makes sure a file refcount is not zero before
> > taking a reference. It should also fetch again file pointer
> > in order to respect dup2() atomicity requirements.
> >
> > It should either read a NULL pointer or a file on which
> > a refcount can be taken.
>
> Hmm... The problem is real, but I wonder if one could trigger a long
> spin there...
I do not believe we can spin a long time.
By the time do_dup2() calls filp_close(tofree, files), we already have a
stable fdt->fd[fd], because of spin_unlock(&files->file_lock) was called
before filp_close()
A loop would require threads doing dup2() calls like crazy on same
destination fd.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-06-30 5:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-29 15:10 [PATCH] fs/file.c: __fget() and dup2() atomicity rules Eric Dumazet
2015-06-29 17:46 ` Al Viro
2015-06-30 5:02 ` Eric Dumazet
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).