proc/mounts: add cursor
PATCH v3 1/1 proc/mounts: add cursor
Hi Al,
I think this version fixes your worries about cachline pingpong and excessively long list due to cursors.
Thanks,
Miklos
---
From: Miklos Szeredi <[email protected]>
Subject: proc/mounts: add cursor
If mounts are deleted after a read(2) call on /proc/self/mounts (or its kin), the subsequent read(2) could miss a mount that comes after the deleted one in the list. This is because the file position is interpreted as the number mount entries from the start of the list.
E.g. first read gets entries #0 to #9; the seq file index will be 10. Then entry #5 is deleted, resulting in #10 becoming #9 and #11 becoming #10, etc... The next read will continue from entry #10, and #9 is missed.
Solve this by adding a cursor entry for each open instance. Taking the global namespace_sem for write seems excessive, since we are only dealing with a per-namespace list. Instead add a per-namespace spinlock and use that together with namespace_sem taken for read to protect against concurrent modification of the mount list. This may reduce parallelism of is_local_mountpoint(), but it's hardly a big contention point. We could also use RCU freeing of cursors to make traversal not need additional locks, if that turns out to be neceesary.
---
Differences from v2:
- only update cursor in m_end(): a lot less cachline invalidations
- remove cursor when at EOF, this means no cursors on the list when not actively reading the file
Differences from v1:
- removed unnecessary code that wanted to handle lseeks
- fixed double entry at the start of a read
fs/mount.h | 12 +++++--
fs/namespace.c | 85 ++++++++++++++++++++++++++++++++++++++++---------- fs/proc_namespace.c | 4 +- include/linux/mount.h | 4 +- 4 files changed, 83 insertions(+), 22 deletions(-)
fs/mount.h +9 -3
@@ -9,7 +9,13 @@ struct mnt_namespace {atomic_t count;struct ns_common ns;struct mount * root; +/* +* Traversal and modification of .list is protected by either +* - taking namespace_sem for write, OR +* - taking namespace_sem for read AND taking .ns_lock. +*/ struct list_head list; +spinlock_t ns_lock;struct user_namespace *user_ns;struct ucounts *ucounts;u64 seq;/* Sequence number to prevent loops */
@@ -133,9 +139,7 @@ struct proc_mounts {struct mnt_namespace *ns;struct path root;int (*show)(struct seq_file *,struct vfsmount *); -void *cached_mount; -u64 cached_event; -loff_t cached_index; +struct mount cursor; };extern const struct seq_operations mounts_op;
@@ -153,3 +157,5 @@ static inline bool is_anon_ns(struct mnt {return ns->seq ==0 ; } + +extern void mnt_cursor_del (struct mnt_namespace *ns,struct mount *cursor);
fs/namespace.c +68 -17
155 lines not on this page · raw message
fs/proc_namespace.c +3 -1
@@ -279,7 +279,8 @@ static int mounts_open_common(struct ino p->ns = ns; p->root = root; p->show = show; - p->cached_event = ~0ULL ; +INIT_LIST_HEAD (&p->cursor.mnt_list); + p->cursor.mnt.mnt_flags =MNT_CURSOR ;return 0 ;
@@ -296,6 +297,7 @@ static int mounts_release(struct inode *struct seq_file *m = file->private_data;struct proc_mounts *p = m->private;path_put (&p->root); +mnt_cursor_del (p->ns, &p->cursor);put_mnt_ns (p->ns);return seq_release_private (inode, file); }
include/linux/mount.h +3 -1
@@ -50,7 +50,8 @@ struct fs_context;#define MNT_ATIME_MASK (MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME )#define MNT_INTERNAL_FLAGS (MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL | \ - MNT_DOOMED | MNT_SYNC_UMOUNT | MNT_MARKED) + MNT_DOOMED | MNT_SYNC_UMOUNT | MNT_MARKED | \ + MNT_CURSOR)#define MNT_INTERNAL 0x4000
@@ -64,6 +65,7 @@ struct fs_context;#define MNT_SYNC_UMOUNT 0x2000000#define MNT_MARKED 0x4000000#define MNT_UMOUNT 0x8000000 +#define MNT_CURSOR 0x10000000struct vfsmount {struct dentry *mnt_root;/* root of the mounted tree */
Al Viro
maintainerQuoted 12 lines · Miklos Szeredi
fs/namespace.c +1
-5@@ -1249,42 +1277,50 @@ struct vfsmount *mnt_clone_internal(consstatic void *m_start (struct seq_file *m,loff_t *pos) {struct proc_mounts *p = m->private; +struct mount *mnt =NULL ;down_read (&namespace_sem); -if (p->cached_event == p->ns->event) { -void *v = p->cached_mount; -if (*pos == p->cached_index) -return v; -if (*pos == p->cached_index +1 ) {
fs/namespace.c struct vfsmount *mnt_clone_internal(cons · line 1288
Quoted 10 lines
fs/namespace.c +6
-4- v =seq_list_next (v, &p->ns->list, &p->cached_index); -return p->cached_mount = v; - } - } +lock_ns_list (p->ns); +if (!*pos) +list_move (&p->cursor.mnt_list, &p->ns->list); +if (!list_empty (&p->cursor.mnt_list)) + mnt =mnt_skip_cursors (p->ns, &p->cursor); +unlock_ns_list (p->ns);
Huh? What's that if (!list_empty()) about? The case where we have reached the end of list, then did a read() with an lseek() in between?
If so, then this is out of place under your spinlock; "is on the list" state changes only synchronously (seq_file ->lock serializes all of that). *If* this is what you've meant, I'd suggest
/* read after we'd reached the end? */ if (*pos && list_empty(...)) return NULL; lock_ns_list(p->ns); if (!*pos) list_move(...); /* rewind on lseek or initial read */ mnt = mnt_skip_cursors(...); unlock_ns_list(p->ns);
Or am I misreading your intent there? Confused...
Quoted 20 lines · Al Viro
Quoted 17 lines · Miklos Szeredi
fs/namespace.c +2
-9@@ -1249,42 +1277,50 @@ struct vfsmount *mnt_clone_internal(consstatic void *m_start (struct seq_file *m,loff_t *pos) {struct proc_mounts *p = m->private; +struct mount *mnt =NULL ;down_read (&namespace_sem); -if (p->cached_event == p->ns->event) { -void *v = p->cached_mount; -if (*pos == p->cached_index) -return v; -if (*pos == p->cached_index +1 ) { - v =seq_list_next (v, &p->ns->list, &p->cached_index); -return p->cached_mount = v; - } - } +lock_ns_list (p->ns);
Quoted 8 lines
fs/namespace.c struct vfsmount *mnt_clone_internal(cons · line 1288
Quoted 5 lines
fs/namespace.c +5
-0+if (!*pos) +list_move (&p->cursor.mnt_list, &p->ns->list); +if (!list_empty (&p->cursor.mnt_list)) + mnt =mnt_skip_cursors (p->ns, &p->cursor); +unlock_ns_list (p->ns);Huh? What's that if (!list_empty()) about? The case where we have reached the end of list, then did a
read()with anlseek()in between?
Yes. Also no need to move the cursor in m_start(), since we are going to do it anyway in m_stop().
Posted v4.
Thanks,
Miklos