Skip to content

Commit db203d5

Browse files
Nick Piggintorvalds
authored andcommitted
mm: tiny-shmem fix lock ordering: mmap_sem vs i_mutex
tiny-shmem calls do_truncate in shmem_file_setup. do_truncate takes i_mutex, and shmem_file_setup is called with mmap_sem held. However i_mutex nests outside mmap_sem. Copy the code in shmem.c to avoid this problem. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Nick Piggin <npiggin@suse.de> Reported-and-tested-by: Ingo Molnar <mingo@elte.hu> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Matt Mackall <mpm@selenic.com> Cc: Hugh Dickins <hugh@veritas.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2d4c826 commit db203d5

1 file changed

Lines changed: 9 additions & 15 deletions

File tree

‎mm/tiny-shmem.c‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,25 @@ struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
6565
if (!dentry)
6666
goto put_memory;
6767

68-
error = -ENOSPC;
69-
inode = ramfs_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
70-
if (!inode)
71-
goto put_dentry;
72-
73-
d_instantiate(dentry, inode);
7468
error = -ENFILE;
75-
file = alloc_file(shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
76-
&ramfs_file_operations);
69+
file = get_empty_filp();
7770
if (!file)
7871
goto put_dentry;
7972

80-
inode->i_nlink = 0; /* It is unlinked */
81-
82-
/* notify everyone as to the change of file size */
83-
error = do_truncate(dentry, size, 0, file);
84-
if (error < 0)
73+
error = -ENOSPC;
74+
inode = ramfs_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
75+
if (!inode)
8576
goto close_file;
8677

78+
d_instantiate(dentry, inode);
79+
inode->i_size = size;
80+
inode->i_nlink = 0; /* It is unlinked */
81+
init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
82+
&ramfs_file_operations);
8783
return file;
8884

8985
close_file:
9086
put_filp(file);
91-
return ERR_PTR(error);
92-
9387
put_dentry:
9488
dput(dentry);
9589
put_memory:

0 commit comments

Comments
 (0)