Update of file offset on write() etc. is non-atomic with I/O
Subsystems (4)
Review
Quoted 5 lines
struct fd {
(quoted diff) +1
-1struct file *file; - int need_put; + unsigned need_put:1, need_pos_unlock:1; };
Since we're rounding up to 2*sizeof(struct file *) anyway, is this a case where wasting space on a couple of char (or bool) flags would generate better code than a bitfield?
In particular, the code to set need_pos_unlock (which will be executed each read/write for most files) is messy in the bitfield case. A byte store is much cleaner.
(If you want to use bits, why not use the two lsbits of the file pointer for the purpose? That would save a lot of space.)
Al Viro
maintainerQuoted 10 lines · George Spelvin
Review
Quoted 5 lines
struct fd {
(quoted diff) +1
-1struct file *file; - int need_put; + unsigned need_put:1, need_pos_unlock:1; };Since we're rounding up to 2*sizeof(struct file *) anyway, is this a case where wasting space on a couple of char (or bool) flags would generate better code than a bitfield?
Quoted 6 lines
In particular, the code to set
need_pos_unlock(which will be executed each read/write for most files) is messy in the bitfield case. A byte store is much cleaner.(If you want to use bits, why not use the two lsbits of the file pointer for the purpose? That would save a lot of space.)
Most of the cases have it kept separately in registers, actually - there's a reason why fdget() and friends are inlined.
Quoted 7 lines · Al Viro
Quoted 3 lines · George Spelvin
(If you want to use bits, why not use the two lsbits of the file pointer for the purpose? That would save a lot of space.)
Most of the cases have it kept separately in registers, actually - there's a reason why
fdget()and friends are inlined.
Yes. And bit test and set ops on registers are actually cheaper than playing around with bytes.
That said, the "fget_light()" interface sucks - exactly because it doesn't do the "return structure in two registers" thing. We should get rid of it - there's just one remaining user in networking code, and it should be rewritten in terms of fdget().
That's a separate issue, though.
Oh, and George, your email setup is broken. Gmail thinks your emails are spam. I'm not sure why (the usual spf problems do not apply), but it's possibly because your name and email looks made up.
Linus
Al Viro
maintainerQuoted 12 lines · Linus Torvalds
Quoted 7 lines · Al Viro
Quoted 3 lines · George Spelvin
(If you want to use bits, why not use the two lsbits of the file pointer for the purpose? That would save a lot of space.)
Most of the cases have it kept separately in registers, actually - there's a reason why
fdget()and friends are inlined.Yes. And bit test and set ops on registers are actually cheaper than playing around with bytes.
Quoted 6 lines
That said, the "
fget_light()" interface sucks - exactly because it doesn't do the "return structure in two registers" thing. We should get rid of it - there's just one remaining user in networking code, and it should be rewritten in terms offdget().That's a separate issue, though.
The thing is, the callers in there do *not* keep struct file * at all - they keep struct socket * and use sock->file to get struct file * back when they need it.
So struct fd is the wrong thing to use there - it only adds to register pressure. A similar pair of struct socket * and "need to fput that" flag would probably be needed, with sockfd_lookup_light() returning that.
Quoted 4 lines · Al Viro
The thing is, the callers in there do *not* keep struct file * at all - they keep struct socket * and use
sock->fileto get struct file * back when they need it.
Not a problem. Just make sockfd_lookup_light() use the broken inefficient calling convention.
So then the networking code can continue to use the old bad interface, without it impacting the normal users.
Something like the attached untested patch. This gets rid of "fget_light()", and instead makes "fdget()" the native interface (same for the "raw" version).
Totally untested patch (on top of my previous one)
Linus
fs/file.c | 30 +++++++++++++++--------------- include/linux/file.h | 24 ++---------------------- net/socket.c | 17 ++++++++++++----- 3 files changed, 29 insertions(+), 42 deletions(-)
fs/file.c +15 -15
@@ -683,33 +683,33 @@ EXPORT_SYMBOL(fget_raw);* The fput_needed flag returned by fget_light should be passed to the * corresponding fput_light. */ -struct file *__fget_light (unsigned int fd,fmode_t mask,int *fput_needed) +static struct fd __fdget (unsigned int fd,fmode_t mask) {struct files_struct *files = current->files; -struct file *file; +struct fd f = {NULL , }; - *fput_needed =0 ;if (atomic_read (&files->count) ==1 ) { - file =__fcheck_files (files, fd); -if (file && (file->f_mode & mask)) - file =NULL ; + f.file =__fcheck_files (files, fd); +if (f.file && (f.file->f_mode & mask)) + f.file =NULL ; }else { - file =__fget (fd, mask); -if (file) - *fput_needed =1 ; + f.file =__fget (fd, mask); +if (f.file) + f.need_put =1 ; } -return file; +return f; } -struct file *fget_light (unsigned int fd,int *fput_needed) + +struct fd fdget (unsigned int fd) { -return __fget_light (fd,FMODE_PATH , fput_needed); +return __fdget (fd,FMODE_PATH ); } -EXPORT_SYMBOL (fget_light); +EXPORT_SYMBOL (fdget); -struct file *fget_raw_light (unsigned int fd,int *fput_needed) +struct fd fdget_raw (unsigned int fd) { -return __fget_light (fd,0 , fput_needed); +return __fdget (fd,0 ); }void set_close_on_exec (unsigned int fd,int flag)
include/linux/file.h +2 -22
@@ -20,12 +20,6 @@ struct path;extern struct file *alloc_file (struct path *,fmode_t mode,const struct file_operations *fop); -static inline void fput_light (struct file *file,int fput_needed) -{ -if (fput_needed) -fput (file); -} -struct fd {struct file *file;unsigned need_put:1 , need_pos_unlock:1 ;
@@ -38,24 +32,10 @@ static inline void fdput(struct fd fd) }extern struct file *fget (unsigned int fd); -extern struct file *fget_light (unsigned int fd,int *fput_needed); - -static inline struct fd fdget (unsigned int fd) -{ -int b; -struct file *f =fget_light (fd, &b); -return (struct fd ){f,b,0 }; -} -extern struct file *fget_raw (unsigned int fd); -extern struct file *fget_raw_light (unsigned int fd,int *fput_needed); -static inline struct fd fdget_raw (unsigned int fd) -{ -int b; -struct file *f =fget_raw_light (fd, &b); -return (struct fd ){f,b,0 }; -} +extern struct fd fdget (unsigned int fd); +extern struct fd fdget_raw (unsigned int fd);extern int f_dupfd (unsigned int from,struct file *file,unsigned flags);extern int replace_fd (unsigned fd,struct file *file,unsigned flags);
net/socket.c +12 -5
@@ -448,18 +448,25 @@ struct socket *sockfd_lookup(int fd, int *err) }EXPORT_SYMBOL (sockfd_lookup); +static void fput_light (struct file *file,int fput_needed) +{ +struct fd f = { file, fput_needed,0 }; +fdput (f); +} +static struct socket *sockfd_lookup_light (int fd,int *err,int *fput_needed) { -struct file *file; +struct fd f;struct socket *sock; *err = -EBADF ; - file =fget_light (fd, fput_needed); -if (file) { - sock =sock_from_file (file, err); + f =fdget (fd); + *fput_needed = f.need_put; +if (f.file) { + sock =sock_from_file (f.file, err);if (sock)return sock; -fput_light (file, *fput_needed); +fdput (f); }return NULL ; }
Al Viro
maintainerQuoted 3 lines · Linus Torvalds
Something like the attached untested patch. This gets rid of "
fget_light()", and instead makes "fdget()" the native interface (same for the "raw" version).
Umm... I would be very surprised if that worked well. You have just
forced fdget() to comply to the ABI. And unless that has such structs returned in register pairs, there's no way for compiler to do about that.
The current variant lets the optimizer see what's going on and decide that since it doesn't need struct fd local variable kept as a single object, so it can turn it into a pair of locals, independently assigned (in the inlined fdget() body) and independently used. If those can be put into registers, it happens separately for both, etc.
Try your variant on e.g. ppc or alpha. Both will pass that struct on stack. So will mips. So will s390. So will 32bit sparc, not that there had been much use of sparc32 kernels...
It will work on amd64, sparc64. On i386 we explicitly pass it -freg-struct-return, so it passes in a pair of registers there as well.
We could start usinmg -freg-struct-return on other architectures, but I'm not sure if it would actually work - e.g. on ppc it still returns such a struct on stack.
Quoted 5 lines · Al Viro
Umm... I would be very surprised if that worked well. You have just
forcedfdget()to comply to the ABI. And unless that has such structs returned in register pairs, there's no way for compiler to do about that.
Register pairs are very much a common return model.
And we've relied on that before. For example, 64-bit pte's on x86-32 very much does that whole thing with the "pte_t" union.
So you can now commence being surprised.
(Side note: I think sparc or something doesn't support it, and may return things in memory. I can't really seem to find it in myself to care)
Linus
Al Viro
maintainerQuoted 9 lines · Linus Torvalds
Quoted 5 lines · Al Viro
Umm... I would be very surprised if that worked well. You have just
forcedfdget()to comply to the ABI. And unless that has such structs returned in register pairs, there's no way for compiler to do about that.Register pairs are very much a common return model.
Quoted 8 lines
And we've relied on that before. For example, 64-bit pte's on x86-32 very much does that whole thing with the "
pte_t" union.So you can now commence being surprised.
(Side note: I think sparc or something doesn't support it, and may return things in memory. I can't really seem to find it in myself to care)
sparc64 actually does support that. So does amd64, and, with explicit flag, i386. No other more or less general purpose architecture does.
Not ppc. Not mips. Not arm. I think that some of those are worth caring about...
Quoted 11 lines · Al Viro
Quoted 4 lines · Linus Torvalds
(Side note: I think sparc or something doesn't support it, and may return things in memory. I can't really seem to find it in myself to care)
sparc64 actually does support that. So does amd64, and, with explicit flag, i386. No other more or less general purpose architecture does.
Not ppc. Not mips. Not arm. I think that some of those are worth caring about...
I doubt it's worth caring about. Even when passing things in memory, the end result isn't that much worse than the fget_light() model that passes just one of the two fields in memory.
If the ARM/PPC people end up caring, they could add the struct-return support to gcc. It should be basically just adding the flag for enabling the calling convention - the core gcc support is obviously all there, and it's just an issue of whether the calling conventions allow it or not.
Linus
Al Viro
maintainerQuoted 3 lines · Linus Torvalds
I doubt it's worth caring about. Even when passing things in memory, the end result isn't that much worse than the
fget_light()model that passes just one of the two fields in memory.
I'm not sure if that's the right approach, TBH. I wonder if something like
static inline struct fd fdget(int fd)
{
unsigned long v = __fdget(fd);
return (struct fd){(struct file *)(v & ~1), v & 1};
}
would not be a better starting point, with __fdget(fd) being
{
struct files_struct *files = current->files;
struct file *file;
if (atomic_read(&files->count) == 1) {
file = __fcheck_files(files, fd);
if (file && (file->f_mode & FMODE_PATH))
return 0;
return (unsigned long)file;
} else {
file = __fget(fd, FMODE_PATH);
return file ? 1 | (unsigned long)file : 0;
}
}Quoted 8 lines · Al Viro
I wonder if something like
static inline struct fd fdget(int fd)
{ unsigned long v = __fdget(fd); return (struct fd){(struct file *)(v & ~1), v & 1}; }
Make it use "&3" so that we have the two bits we need, and I'll happily take that approach, yes.
Linus
Al Viro
maintainerQuoted 4 lines · Linus Torvalds
Quoted 3 lines · Al Viro
I wonder if something like
Quoted 8 lines
Quoted 5 lines
static inline struct fd fdget(int fd)
{ unsigned long v = __fdget(fd); return (struct fd){(struct file *)(v & ~1), v & 1}; }Make it use "&3" so that we have the two bits we need, and I'll happily take that approach, yes.
Hmm... You want to have that second flag dealt with inside fs/file.c as well? OK, but that'll need one more thing - __attribute__((aligned(4))) on struct file declaration. We have at least one weird architecture that is happy to align everything on 16bit boundaries. Granted, it's half-dead, but there might be something besides m68k with the same weirdness...
It's probably not worth replacing struct fd with typedef to unsigned long - too easy to have it confused with a file descriptor itself and pass to something that expects e.g. int. In any case, since we leave fdget() inlined, compiler will see the unsigned long it's been initialized with, so if it decides that it's cheaper to replace f.file with v & ~3 through the whole thing, keep v around and discard local struct fd completely, it'll be able to do so just fine...
I'll play around with cross-builds a bit and see what falls out of all that.
Al Viro
maintainerQuoted 10 lines · Al Viro
It's probably not worth replacing struct fd with typedef to unsigned long - too easy to have it confused with a file descriptor itself and pass to something that expects e.g. int. In any case, since we leave
fdget()inlined, compiler will see the unsigned long it's been initialized with, so if it decides that it's cheaper to replace f.file with v & ~3 through the whole thing, keep v around and discard local struct fd completely, it'll be able to do so just fine...I'll play around with cross-builds a bit and see what falls out of all that.
OK, with the attached set (the first one is essentially unchanged from your first one), it seems to work and produce better code on all targets I've tried. Comments?
I've also pushed those (on top of old ocfs2 fix) into vfs.git#for-linus, if you prefer to read it that way. Should propagate in a few...
Quoted 1 line
From
61660f7691b301dce7ed2d995d9e95ca4353e51dMon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Mon, 3 Mar 2014 09:36:58 -0800
Subject: [PATCH 1/3] vfs: atomic f_pos accesses as per POSIX
Our write() system call has always been atomic in the sense that you get the expected thread-safe contiguous write, but we haven't actually guaranteed that concurrent writes are serialized wrt f_pos accesses, so threads (or processes) that share a file descriptor and use "write()" concurrently would quite likely overwrite each others data.
This violates POSIX.1-2008/SUSv4 Section XSI 2.9.7 that says:
"2.9.7 Thread Interactions with Regular File Operations
All of the following functions shall be atomic with respect to each other in the effects specified in POSIX.1-2008 when they operate on regular files or symbolic links: [...]"
and one of the effects is the file position update.
This unprotected file position behavior is not new behavior, and nobody has ever cared. Until now. Yongzhi Pan reported unexpected behavior to Michael Kerrisk that was due to this.
This resolves the issue with a f_pos-specific lock that is taken by read/write/lseek on file descriptors that may be shared across threads or processes.
--- fs/file_table.c | 1 + fs/namei.c | 2 +- fs/open.c | 4 ++++ fs/read_write.c | 54 +++++++++++++++++++++++++++++++++++++------------- include/linux/file.h | 6 ++++-- include/linux/fs.h | 6 +++++- 6 files changed, 55 insertions(+), 18 deletions(-)
fs/file_table.c +1 -0
@@ -135,6 +135,7 @@ struct file *get_empty_filp(void)atomic_long_set (&f->f_count,1 );rwlock_init (&f->f_owner.lock);spin_lock_init (&f->f_lock); +mutex_init (&f->f_pos_lock);eventpoll_init_file (f);/* f->f_version: 0 */ return f;
fs/namei.c +1 -1
@@ -1884,7 +1884,7 @@ static int path_init(int dfd, const char *name, unsigned int flags, nd->path = f.file->f_path;if (flags &LOOKUP_RCU ) { -if (f.need_put) +if (f.flags &FDPUT_FPUT ) *fp = f.file; nd->seq =__read_seqcount_begin (&nd->path.dentry->d_seq);rcu_read_lock ();
fs/open.c +4 -0
@@ -705,6 +705,10 @@ static int do_dentry_open(struct file *f,return 0 ; } +/* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */ +if (S_ISREG (inode->i_mode)) + f->f_mode |=FMODE_ATOMIC_POS ; + f->f_op =fops_get (inode->i_fop);if (unlikely (WARN_ON (!f->f_op))) { error = -ENODEV ;
fs/read_write.c +40 -14
141 lines not on this page · raw message
include/linux/file.h +4 -2
@@ -28,12 +28,14 @@ static inline void fput_light(struct file *file, int fput_needed)struct fd {struct file *file; -int need_put; +unsigned int flags; }; +#define FDPUT_FPUT 1 +#define FDPUT_POS_UNLOCK 2static inline void fdput (struct fd fd) { -if (fd.need_put) +if (fd.flags &FDPUT_FPUT )fput (fd.file); }
include/linux/fs.h +5 -1
@@ -123,6 +123,9 @@ typedef void (dio_iodone_t)(struct kiocb *iocb, loff_t offset,/* File is opened with O_PATH; almost nothing can be done with it */ #define FMODE_PATH ((__force fmode_t)0x4000) +/* File needs atomic accesses to f_pos */ +#define FMODE_ATOMIC_POS ((__force fmode_t)0x8000) +/* File was opened by fanotify and shouldn't generate fanotify events */ #define FMODE_NONOTIFY ((__force fmode_t)0x1000000)
@@ -780,13 +783,14 @@ struct file {const struct file_operations *f_op;/* -* Protects f_ep_links, f_flags, f_pos vs i_size in lseek SEEK_CUR. +* Protects f_ep_links, f_flags. * Must not be taken from IRQ context. */ spinlock_t f_lock;atomic_long_t f_count;unsigned int f_flags;fmode_t f_mode; +struct mutex f_pos_lock;loff_t f_pos;struct fown_struct f_owner;const struct cred *f_cred;
Signature · 1 line
1.7.10.4
Quoted 1 line
From
60990b46c2ac7bbf87ecfd64843f7f19a06822f6Mon Sep 17 00:00:00 2001
From: Al Viro <[email protected]>
Date: Mon, 3 Mar 2014 23:48:18 -0500
Subject: [PATCH 2/3] sockfd_lookup_light(): switch to fdget^W^Waway from
fget_light
--- net/socket.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
net/socket.c +7 -6
@@ -450,16 +450,17 @@ EXPORT_SYMBOL(sockfd_lookup);static struct socket *sockfd_lookup_light (int fd,int *err,int *fput_needed) { -struct file *file; +struct fd f =fdget (fd);struct socket *sock; *err = -EBADF ; - file =fget_light (fd, fput_needed); -if (file) { - sock =sock_from_file (file, err); -if (sock) +if (f.file) { + sock =sock_from_file (f.file, err); +if (likely (sock)) { + *fput_needed = f.flags;return sock; -fput_light (file, *fput_needed); + } +fdput (f); }return NULL ; }
Signature · 1 line
1.7.10.4
Quoted 1 line
From
6053e1188ff288d95f3fa21965edf1137f478507Mon Sep 17 00:00:00 2001
From: Al Viro <[email protected]>
Date: Tue, 4 Mar 2014 14:54:22 -0500
Subject: [PATCH 3/3] get rid of fget_light()
instead of returning the flags by reference, we can just have the low-level primitive return those in lower bits of unsigned long, with struct file * derived from the rest.
--- fs/file.c | 56 ++++++++++++++++++++++++++++++++++++++------------ fs/read_write.c | 16 +-------------- include/linux/file.h | 21 ++++++++++--------- include/linux/fs.h | 2 +- 4 files changed, 56 insertions(+), 39 deletions(-)
fs/file.c +43 -13
78 lines not on this page · raw message
fs/read_write.c +1 -15
@@ -264,23 +264,9 @@ loff_t vfs_llseek(struct file *file, loff_t offset, int whence) }EXPORT_SYMBOL (vfs_llseek); -/* -* We only lock f_pos if we have threads or if the file might be -* shared with another process. In both cases we'll have an elevated -* file count (done either by fdget() or by fork()). -*/ static inline struct fd fdget_pos (int fd) { -struct fd f =fdget (fd); -struct file *file = f.file; - -if (file && (file->f_mode &FMODE_ATOMIC_POS )) { -if (file_count (file) >1 ) { - f.flags |=FDPUT_POS_UNLOCK ; -mutex_lock (&file->f_pos_lock); - } - } -return f; +return __to_fd (__fdget_pos (fd)); }static inline void fdput_pos (struct fd f)
include/linux/file.h +11 -10
@@ -40,23 +40,24 @@ static inline void fdput(struct fd fd) }extern struct file *fget (unsigned int fd); -extern struct file *fget_light (unsigned int fd,int *fput_needed); +extern struct file *fget_raw (unsigned int fd); +extern unsigned long __fdget (unsigned int fd); +extern unsigned long __fdget_raw (unsigned int fd); +extern unsigned long __fdget_pos (unsigned int fd); -static inline struct fd fdget (unsigned int fd) +static inline struct fd __to_fd (unsigned long v) { -int b; -struct file *f =fget_light (fd, &b); -return (struct fd ){f,b}; +return (struct fd ){(struct file *)(v & ~3 ),v &3 }; } -extern struct file *fget_raw (unsigned int fd); -extern struct file *fget_raw_light (unsigned int fd,int *fput_needed); +static inline struct fd fdget (unsigned int fd) +{ +return __to_fd (__fdget (fd)); +}static inline struct fd fdget_raw (unsigned int fd) { -int b; -struct file *f =fget_raw_light (fd, &b); -return (struct fd ){f,b}; +return __to_fd (__fdget_raw (fd)); }extern int f_dupfd (unsigned int from,struct file *file,unsigned flags);
include/linux/fs.h +1 -1
@@ -812,7 +812,7 @@ struct file {#ifdef CONFIG_DEBUG_WRITECOUNT unsigned long f_mnt_write_state;#endif -}; +} __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */struct file_handle {__u32 handle_bytes;
Signature · 1 line
1.7.10.4
Quoted 4 lines · Al Viro
OK, with the attached set (the first one is essentially unchanged from your first one), it seems to work and produce better code on all targets I've tried. Comments?
I'm certainly ok with it. You seem to have left the fput_light() function around, though, despite removing fget_[raw_]light(). That seems a bit silly, since there is no valid use any more apart from net/socket.c that now doesn't balance things properly.
Quoted 2 lines
I've also pushed those (on top of old ocfs2 fix) into vfs.git#for-linus, if you prefer to read it that way. Should propagate in a few...
Should I pull?
I also get the feeling that the first patch should likely be marked for stable. Hmm?
Linus
Al Viro
maintainerQuoted 10 lines · Linus Torvalds
Quoted 4 lines · Al Viro
OK, with the attached set (the first one is essentially unchanged from your first one), it seems to work and produce better code on all targets I've tried. Comments?
I'm certainly ok with it. You seem to have left the
fput_light()function around, though, despite removing fget_[raw_]light(). That seems a bit silly, since there is no valid use any more apart fromnet/socket.cthat now doesn't balance things properly.
There's also a pile of crap around sockfd_lookup/sockfd_put, related
to that. Moreover, there's net/compat.c, which probably ought to
have the compat syscalls themselves moved to net/socket.c (under ifdef CONFIG_COMPAT) and switched to sockfd_lookup_light(). There's l2tp_tunnel_sock_lookup(), which is simply broken - it assumes that if tunnel->fd still resolves to a socket, that socket must be l2tp one. Trivial to drive into BUG_ON(), in queue_work() callback, no less... There's bluetooth, assuming that pretty much the same (that if it got a file descriptor that resolves to a socket, it must be a bluetooth one). BTW, I wonder what will happen if one gives iscsi_sw_tcp_conn_bind() descriptor of a socket of sufficiently weird sort...
Then there's staging/usbip with its sockfd_to_socket(), which is more or less parallel to sockfd_lookup(). And open-coded analogs in nbd and ncpfs...
Quoted 7 lines
Quoted 2 lines
I've also pushed those (on top of old ocfs2 fix) into vfs.git#for-linus, if you prefer to read it that way. Should propagate in a few...
Should I pull?
I also get the feeling that the first patch should likely be marked for stable. Hmm?
It should; I'll mark it such when I send a pull request. I really want to sort the situation with sockfd_lookup() and friends out, though - at least to the point where I would understand how painful the fixes will be.
Al Viro
maintainerQuoted 13 lines · Al Viro
There's also a pile of crap around sockfd_lookup/sockfd_put, related
to that. Moreover, there'snet/compat.c, which probably ought to
have the compat syscalls themselves moved tonet/socket.c(under ifdefCONFIG_COMPAT) and switched tosockfd_lookup_light(). There'sl2tp_tunnel_sock_lookup(), which is simply broken - it assumes that iftunnel->fdstill resolves to a socket, that socket must be l2tp one. Trivial to drive intoBUG_ON(), inqueue_work()callback, no less... There's bluetooth, assuming that pretty much the same (that if it got a file descriptor that resolves to a socket, it must be a bluetooth one). BTW, I wonder what will happen if one givesiscsi_sw_tcp_conn_bind()descriptor of a socket of sufficiently weird sort...
Quoted 3 lines
Then there's staging/usbip with its
sockfd_to_socket(), which is more or less parallel tosockfd_lookup(). And open-coded analogs in nbd and ncpfs...
OK, I've gone through most of that; bluetooth is, indeed, oopsable (as simple as e.g.
int sv[2];
int fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_CMTP);
struct cmtp_connadd_req r = {};
socketpair(PF_LOCAL, SOCK_STREAM, 0, sv);
r.sock = sv[0];
ioctl(fd, CMTPCONNADD, (unsigned long)&r);and similar with BNEP) and that one is easy to fix. l2tp I'd rather leave for net folks to deal with - the problem there is that we stash sock *and* descriptor number into struct l2tp_tunnel in l2tp_tunnel_create() and expect l2tp_tunnel_sock_lookup() to find that descriptor (tunnel->fd) resolving to nothing (if it got already closed) or to the same socket. Unfortunately, the caller (l2tp_tunnel_del_work()) expects to find l2tp socket in the latter case, so having it replaced with unrelated socket will do nasty things to that caller. It looks rather silly, actually - the actual fuckup happens when l2tp_tunnel_del_work() passes what has come from socket->sk to l2tp_tunnel_sock_put(), which does
struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
to find the tunnel its caller already had. Looks too convoluted for its own good, and my first inclination would be to collapse l2tp_tunnel_sock_* into the (only) caller, but I'm not sure if I'm not missing some subtle race prevention in those back-and-forth lookups. In any case, it can lead to l2tp_sock_to_tunnel() called on a sock that has nothing to do with l2tp, so we do have a bug there.
I've attached bluetooth fixes; this stuff is obviously better off in one of the net trees. Not sure if it's worth Cc:stable - up to Marcel and Davem. These bugs are oopsable, but you need CAP_NET_ADMIN to step into those...
I think that what's in vfs.git#for-linus right now is OK to pull; it survives all the beating I could think of. Linus, could you pull from the usual place?
git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs.git for-linus
Shortlog:
Al Viro (3):
ocfs2 syncs the wrong range... sockfd_lookup_light(): switch to fdget^W^Waway from fget_light get rid of fget_light()
Linus Torvalds (1):
vfs: atomic f_pos accesses as per POSIX
Diffstat:
fs/file.c | 56 +++++++++++++++++++++++++++++++++++++++++++------------- fs/file_table.c | 1 + fs/namei.c | 2 +- fs/ocfs2/file.c | 8 ++++---- fs/open.c | 4 ++++ fs/read_write.c | 40 ++++++++++++++++++++++++++-------------- include/linux/file.h | 27 +++++++++++++++------------ include/linux/fs.h | 8 ++++++-- net/socket.c | 13 +++++++------ 9 files changed, 107 insertions(+), 52 deletions(-)
Quoted 1 line
From
f49d9ab3220ece0b635b18212bfc44444e9b5f41Mon Sep 17 00:00:00 2001
From: Al Viro <[email protected]>
Date: Sun, 9 Mar 2014 13:11:59 -0400
Subject: [PATCH 1/3] bluetooth: hidp_connection_add() unsafe use of
l2cap_pi()
it's OK after we'd verified the sockets, but not before that.
--- net/bluetooth/hidp/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/bluetooth/hidp/core.c +2 -1
@@ -1332,13 +1332,14 @@ int hidp_connection_add(struct hidp_connadd_req *req, {struct hidp_session *session;struct l2cap_conn *conn; -struct l2cap_chan *chan =l2cap_pi (ctrl_sock->sk)->chan; +struct l2cap_chan *chan;int ret; ret =hidp_verify_sockets (ctrl_sock, intr_sock);if (ret)return ret; + chan =l2cap_pi (ctrl_sock->sk)->chan; conn =NULL ;l2cap_chan_lock (chan);if (chan->conn) {
Signature · 1 line
1.7.10.4
Quoted 1 line
From
790f94a74f8214baab44eb346a346640e6335319Mon Sep 17 00:00:00 2001
From: Al Viro <[email protected]>
Date: Mon, 10 Mar 2014 10:50:10 -0400
Subject: [PATCH 2/3] cmtp: cmtp_add_connection() should verify that it's
dealing with l2cap socket
... rather than relying on ciptool(8) never passing it anything else. Give it e.g. an AF_UNIX connected socket (from socketpair(2)) and it'll oops, trying to evaluate &l2cap_pi(sock->sk)->chan->dst...
--- net/bluetooth/cmtp/core.c | 2 ++ 1 file changed, 2 insertions(+)
net/bluetooth/cmtp/core.c +2 -0
@@ -333,6 +333,8 @@ int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)int i, err;BT_DBG ("" ); +if (!l2cap_is_socket (sock)) +return -EBADFD ; session =kzalloc (sizeof (struct cmtp_session ),GFP_KERNEL );if (!session)
Signature · 1 line
1.7.10.4
Quoted 1 line
From
4d86300249b507f27e8c55c0d3bf1fa2653f9b17Mon Sep 17 00:00:00 2001
From: Al Viro <[email protected]>
Date: Mon, 10 Mar 2014 11:08:35 -0400
Subject: [PATCH 3/3] bnep: bnep_add_connection() should verify that it's
dealing with l2cap socket
same story as cmtp
--- net/bluetooth/bnep/core.c | 3 +++ 1 file changed, 3 insertions(+)
net/bluetooth/bnep/core.c +3 -0
@@ -533,6 +533,9 @@ int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)BT_DBG ("" ); +if (!l2cap_is_socket (sock)) +return -EBADFD ; +baswap ((void *) dst, &l2cap_pi (sock->sk)->chan->dst);baswap ((void *) src, &l2cap_pi (sock->sk)->chan->src);
Signature · 1 line
1.7.10.4
Quoted 6 lines · Linus Torvalds
Quoted 3 lines
Most of the cases have it kept separately in registers, actually - there's a reason why
fdget()and friends are inlined.Yes. And bit test and set ops on registers are actually cheaper than playing around with bytes.
Ugh. gcc gets this *horribly* wrong when it's a bitfield in a union.
The
f.need_pos_unlock = 1;
thing *should* be just a simple "orl $2,reg", but bitfields seem to generate really crappy code, and it actually does some insane shifting and masking crud with the constant "1".
gcc has had problems with bitfields before.
I think I'll respin this with the compat readv/writev case fixed and with the bitfield replaced with an "unsigned int" that we just do bitops by hand on. Because that code generation makes me feel slightly ill.
Linus
Quoted 5 lines · Linus Torvalds
I think I'll respin this with the compat readv/writev case fixed and with the bitfield replaced with an "unsigned int" that we just do bitops by hand on. Because that code generation makes me feel slightly ill.
Ok, how about this version? For some reason I thought the compat functions for readv/writev were in fs/compat.c, but they are there in the same fs/read_write.c, so I didn't need to move the helper functions away.
This just uses a "flags" field, and we currently only have two bits that we use: FDPUT_FPUT and FDPUT_POS_UNLOCK. The first patch knows that "fget_light()" writes 0/1 for that, which is the same as the FDPUT_FPUT bit. I didn't bother to comment on it or clean it up, since the second patch just removes that whole fget_light() mess.
Comments?
Linus
From 9ed5720e8a3139cc703ae19aef5fd3a72658d132 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Mon, 3 Mar 2014 09:36:58 -0800
Subject: [PATCH 1/2] vfs: atomic f_pos accesses as per POSIX
Our write() system call has always been atomic in the sense that you get the expected thread-safe contiguous write, but we haven't actually guaranteed that concurrent writes are serialized wrt f_pos accesses, so threads (or processes) that share a file descriptor and use "write()" concurrently would quite likely overwrite each others data.
This violates POSIX.1-2008/SUSv4 Section XSI 2.9.7 that says:
"2.9.7 Thread Interactions with Regular File Operations
All of the following functions shall be atomic with respect to each other in the effects specified in POSIX.1-2008 when they operate on regular files or symbolic links: [...]"
and one of the effects is the file position update.
This unprotected file position behavior is not new behavior, and nobody has ever cared. Until now. Yongzhi Pan reported unexpected behavior to Michael Kerrisk that was due to this.
This resolves the issue with a f_pos-specific lock that is taken by read/write/lseek on file descriptors that may be shared across threads or processes.
--- fs/file_table.c | 1 + fs/namei.c | 2 +- fs/open.c | 4 ++++ fs/read_write.c | 54 ++++++++++++++++++++++++++++++++++++++-------------- include/linux/file.h | 6 ++++-- include/linux/fs.h | 6 +++++- 6 files changed, 55 insertions(+), 18 deletions(-)
fs/file_table.c +1 -0
@@ -135,6 +135,7 @@ struct file *get_empty_filp(void)atomic_long_set (&f->f_count,1 );rwlock_init (&f->f_owner.lock);spin_lock_init (&f->f_lock); +mutex_init (&f->f_pos_lock);eventpoll_init_file (f);/* f->f_version: 0 */ return f;
fs/namei.c +1 -1
@@ -1884,7 +1884,7 @@ static int path_init(int dfd, const char *name, unsigned int flags, nd->path = f.file->f_path;if (flags &LOOKUP_RCU ) { -if (f.need_put) +if (f.flags &FDPUT_FPUT ) *fp = f.file; nd->seq =__read_seqcount_begin (&nd->path.dentry->d_seq);rcu_read_lock ();
fs/open.c +4 -0
@@ -705,6 +705,10 @@ static int do_dentry_open(struct file *f,return 0 ; } +/* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */ +if (S_ISREG (inode->i_mode)) + f->f_mode |=FMODE_ATOMIC_POS ; + f->f_op =fops_get (inode->i_fop);if (unlikely (WARN_ON (!f->f_op))) { error = -ENODEV ;
fs/read_write.c +40 -14
141 lines not on this page · raw message
include/linux/file.h +4 -2
@@ -28,12 +28,14 @@ static inline void fput_light(struct file *file, int fput_needed)struct fd {struct file *file; -int need_put; +unsigned int flags; }; +#define FDPUT_FPUT 1 +#define FDPUT_POS_UNLOCK 2static inline void fdput (struct fd fd) { -if (fd.need_put) +if (fd.flags &FDPUT_FPUT )fput (fd.file); }
include/linux/fs.h +5 -1
@@ -123,6 +123,9 @@ typedef void (dio_iodone_t)(struct kiocb *iocb, loff_t offset,/* File is opened with O_PATH; almost nothing can be done with it */ #define FMODE_PATH ((__force fmode_t)0x4000) +/* File needs atomic accesses to f_pos */ +#define FMODE_ATOMIC_POS ((__force fmode_t)0x8000) +/* File was opened by fanotify and shouldn't generate fanotify events */ #define FMODE_NONOTIFY ((__force fmode_t)0x1000000)
@@ -780,13 +783,14 @@ struct file {const struct file_operations *f_op;/* -* Protects f_ep_links, f_flags, f_pos vs i_size in lseek SEEK_CUR. +* Protects f_ep_links, f_flags. * Must not be taken from IRQ context. */ spinlock_t f_lock;atomic_long_t f_count;unsigned int f_flags;fmode_t f_mode; +struct mutex f_pos_lock;loff_t f_pos;struct fown_struct f_owner;const struct cred *f_cred;
Signature · 1 line
1.9.0
From f55e00e40e6aba3e1e8fc701a7069a0b7a0868b7 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Mon, 3 Mar 2014 14:43:03 -0800
Subject: [PATCH 2/2] vfs: get rid of old legacy "f{get,put}_[raw_]_light()"
interfaces
Everything but the net/socket.c use has long since been converted to the fdget() model that has a cleaner calling convention, so just get rid of the old interface and teach the socket code to use the new model using a trivial shim layer.
--- fs/file.c | 30 +++++++++++++++--------------- include/linux/file.h | 24 ++---------------------- net/socket.c | 17 ++++++++++++----- 3 files changed, 29 insertions(+), 42 deletions(-)
fs/file.c +15 -15
@@ -683,33 +683,33 @@ EXPORT_SYMBOL(fget_raw);* The fput_needed flag returned by fget_light should be passed to the * corresponding fput_light. */ -struct file *__fget_light (unsigned int fd,fmode_t mask,int *fput_needed) +static inline struct fd __fdget (unsigned int fd,fmode_t mask) {struct files_struct *files = current->files; -struct file *file; +struct fd f = {NULL , }; - *fput_needed =0 ;if (atomic_read (&files->count) ==1 ) { - file =__fcheck_files (files, fd); -if (file && (file->f_mode & mask)) - file =NULL ; + f.file =__fcheck_files (files, fd); +if (f.file && (f.file->f_mode & mask)) + f.file =NULL ; }else { - file =__fget (fd, mask); -if (file) - *fput_needed =1 ; + f.file =__fget (fd, mask); +if (f.file) + f.flags =FDPUT_FPUT ; } -return file; +return f; } -struct file *fget_light (unsigned int fd,int *fput_needed) + +struct fd fdget (unsigned int fd) { -return __fget_light (fd,FMODE_PATH , fput_needed); +return __fdget (fd,FMODE_PATH ); } -EXPORT_SYMBOL (fget_light); +EXPORT_SYMBOL (fdget); -struct file *fget_raw_light (unsigned int fd,int *fput_needed) +struct fd fdget_raw (unsigned int fd) { -return __fget_light (fd,0 , fput_needed); +return __fdget (fd,0 ); }void set_close_on_exec (unsigned int fd,int flag)
include/linux/file.h +2 -22
@@ -20,12 +20,6 @@ struct path;extern struct file *alloc_file (struct path *,fmode_t mode,const struct file_operations *fop); -static inline void fput_light (struct file *file,int fput_needed) -{ -if (fput_needed) -fput (file); -} -struct fd {struct file *file;unsigned int flags;
@@ -40,24 +34,10 @@ static inline void fdput(struct fd fd) }extern struct file *fget (unsigned int fd); -extern struct file *fget_light (unsigned int fd,int *fput_needed); - -static inline struct fd fdget (unsigned int fd) -{ -int b; -struct file *f =fget_light (fd, &b); -return (struct fd ){f,b}; -} -extern struct file *fget_raw (unsigned int fd); -extern struct file *fget_raw_light (unsigned int fd,int *fput_needed); -static inline struct fd fdget_raw (unsigned int fd) -{ -int b; -struct file *f =fget_raw_light (fd, &b); -return (struct fd ){f,b}; -} +extern struct fd fdget (unsigned int fd); +extern struct fd fdget_raw (unsigned int fd);extern int f_dupfd (unsigned int from,struct file *file,unsigned flags);extern int replace_fd (unsigned fd,struct file *file,unsigned flags);
net/socket.c +12 -5
@@ -448,18 +448,25 @@ struct socket *sockfd_lookup(int fd, int *err) }EXPORT_SYMBOL (sockfd_lookup); +static void fput_light (struct file *file,int flags) +{ +struct fd f = { file, flags }; +fdput (f); +} +static struct socket *sockfd_lookup_light (int fd,int *err,int *fput_needed) { -struct file *file; +struct fd f;struct socket *sock; *err = -EBADF ; - file =fget_light (fd, fput_needed); -if (file) { - sock =sock_from_file (file, err); + f =fdget (fd); + *fput_needed = f.flags; +if (f.file) { + sock =sock_from_file (f.file, err);if (sock)return sock; -fput_light (file, *fput_needed); +fdput (f); }return NULL ; }
Signature · 1 line
1.9.0
Al Viro
maintainerQuoted 7 lines · Linus Torvalds
This just uses a "flags" field, and we currently only have two bits that we use:
FDPUT_FPUTandFDPUT_POS_UNLOCK. The first patch knows that "fget_light()" writes 0/1 for that, which is the same as theFDPUT_FPUTbit. I didn't bother to comment on it or clean it up, since the second patch just removes that wholefget_light()mess.Comments?
do_sendfile() is also there and this one is even more unpleasant ;-/ We probably can ignore that one (until POSIX learns of its existence), thouhg...
Quoted 4 lines · Al Viro
do_sendfile()is also there and this one is even more unpleasant ;-/ We probably can ignore that one (until POSIX learns of its existence), thouhg...
Yeah, I saw the do_sendfile one and decided we don't care. Not only is is out of POSIX spec (so if you break it you get to keep both pieces), the whole sendfile() thing is a bit of a hack.
But we could take the f_pos_lock explicitly in do_sendfile for the !ppos case if we decide we care. The error handling is the main annoyance.
Linus
Al Viro
maintainerQuoted 8 lines · Al Viro
Quoted 6 lines · Linus Torvalds
This just uses a "flags" field, and we currently only have two bits that we use:
FDPUT_FPUTandFDPUT_POS_UNLOCK. The first patch knows that "fget_light()" writes 0/1 for that, which is the same as theFDPUT_FPUTbit. I didn't bother to comment on it or clean it up, since the second patch just removes that wholefget_light()mess.
Quoted 5 lines
Quoted 1 line
Comments?
do_sendfile()is also there and this one is even more unpleasant ;-/ We probably can ignore that one (until POSIX learns of its existence), thouhg...
OK, other than read/write/readv/writev/lseek we have only sendfile{,64} and splice; everything else either deals with struct file that is guaranteed to be not accessible by any syscall (e.g. coredump code) or is not a regular file. So the only question is whether we care for syscalls that are out of scope for POSIX.
Quoted 8 lines · Al Viro
Quoted 6 lines · Linus Torvalds
This just uses a "flags" field, and we currently only have two bits that we use:
FDPUT_FPUTandFDPUT_POS_UNLOCK. The first patch knows that "fget_light()" writes 0/1 for that, which is the same as theFDPUT_FPUTbit. I didn't bother to comment on it or clean it up, since the second patch just removes that wholefget_light()mess.
Quoted 5 lines
Quoted 1 line
Comments?
do_sendfile()is also there and this one is even more unpleasant ;-/ We probably can ignore that one (until POSIX learns of its existence), thouhg...
I've forwarded your request to the Austin Group (who manage the POSIX stuff).
Ced
Signature · 2 lines
Cedric Blancher <[email protected]> Institute Pasteur
Linus Torvalds <[email protected]> wrote:
Quoted 5 lines · Al Viro
Quoted 2 lines
Most of the cases have it kept separately in registers, actually - there's a reason why
fdget()and friends are inlined.Yes. And bit test and set ops on registers are actually cheaper than playing around with bytes.
Ah. I cut & pasted the code into a separate file and compiled it out of line. But I stubbed a lot, so it went to memory. My bad.
Quoted 3 lines
Oh, and George, your email setup is broken. Gmail thinks your emails are spam. I'm not sure why (the usual spf problems do not apply), but it's possibly because your name and email looks made up.
Thanks, but damn, I wish they'd give a little more information.
The horizon.com MX record is correct, the mail server's forward and reverse DNS matches, the SMTP server is a little creaky but I think it's completely standards-compliant.
I'm using bsd mailx, which is *definitely* creaky (you'll notice a complete lack of MIME or User-Agent headers), but again, it's kind of the baseline standard for RFC(2)822 e-mail.
The one infelicity I'm aware of is that I cut & pasted the headers from an on-line mail archive, but mailx doesn't have an easy way to add an In-Reply-To: header. Perhaps a Subject: beginning with "Re:" and no In-Reply-To: looks odd.
But damnifino.
Ouch! I've just seen that trimming the CC on this reply took me out of a large part of the subsequent conversation. PLEASE don't trim CCs, and especially not the address of the OP!!
Quoted 20 lines · George Spelvin
Review
Quoted 5 lines
struct fd { struct file *file;(quoted diff) +1
-1- int need_put; + unsigned need_put:1, need_pos_unlock:1; };Since we're rounding up to 2*sizeof(struct file *) anyway, is this a case where wasting space on a couple of char (or bool) flags would generate better code than a bitfield?
In particular, the code to set
need_pos_unlock(which will be executed each read/write for most files) is messy in the bitfield case. A byte store is much cleaner.(If you want to use bits, why not use the two lsbits of the file pointer for the purpose? That would save a lot of space.)
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Author of "The Linux Programming Interface", http://blog.man7.org/