Compare

40bb03d3ea6f6a915218508819ecc4d7a7fa260d..98b6cf102b35b2bb48d1fd74d5f664eb66fe83eb

namei: test for a negative dentry the way traverse_mounts() does

lookup_fast_for_open() only hands an O_CREAT open a cached dentry if
it's positive. Otherwise the open goes on to lookup_open(), which takes
the directory lock and creates the file. It tests for that with
!dentry->d_inode, but traverse_mounts() later decides with the type in
d_flags. And when a dentry is instantiated __d_set_inode_and_type()
stores ->d_inode first and only then releases the type into ->d_flags.

So a refwalk that took its reference to a negative dentry right before
another task instantiates it can find ->d_inode already set in
lookup_fast_for_open(), go on to step_into(), and still find the
negative type in traverse_mounts(). The open then fails with -ENOENT,
which an O_CREAT open must never do. rcuwalk doesn't get that far, the
d_seq check in step_into() catches the instantiation.

Use d_is_negative() so both look at the type. While the type is still
negative the open takes lookup_open() and waits for the directory lock
like any other O_CREAT open of a negative dentry.

On its own the window is a couple of instructions in the creating task,
so it takes an interrupt or a preempted vcpu in exactly the right spot.
I haven't caught it without help in about 600 million racing O_CREAT
opens.

The debug patch below stalls the creating task between the two stores
for names starting with "ocrace", the way an interrupt landing there
would. The reproducer below opens "ocrace" with O_CREAT through
symlinks on a strictatime tmpfs: following them updates their atime,
which drops the walk out of rcuwalk, so the name is always looked up in
refwalk. With

  echo 20 > /proc/sys/fs/ocrace_instantiate_us
  ./ocreat_enoent -t 60

on a KASAN kernel open() failed with -ENOENT 43 times in 4.9 million
calls. With this patch it never failed in 4.8 million calls. Tracing
step_into_slowpath() during such a run, each of the 98 -ENOENTs it
returned came right after the same task had entered it with a dentry
that was still typed negative.

  // SPDX-License-Identifier: GPL-2.0
  /*
   * ocreat_enoent: open(O_CREAT) racing the creation of the same name.
   *
   * Writers unlink "ocrace" and create it again. Readers open it with
   * O_CREAT through a symlink of their own on a strictatime tmpfs.
   * Following the symlink updates its atime, which drops the walk out of
   * rcu mode, so the name is always looked up in refwalk. An O_CREAT open
   * must never fail with ENOENT.
   *
   * -p arms a cpu-clock perf timer with that period in ns on every writer,
   * so that interrupts land at random points in it.
   *
   *   usage: ocreat_enoent [-t secs] [-w writers] [-r readers] [-p ns]
   */
  #define _GNU_SOURCE
  #include <errno.h>
  #include <fcntl.h>
  #include <pthread.h>
  #include <stdatomic.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <sys/mount.h>
  #include <sys/stat.h>
  #include <sys/syscall.h>
  #include <unistd.h>
  #include <linux/perf_event.h>

  #define DIR "/tmp/oc"

  static atomic_int stop;
  static atomic_long opens, creates, enoent, other;
  static long period;

  static void die(const char *what)
  {
  	fprintf(stderr, "FATAL %s: %s\n", what, strerror(errno));
  	exit(2);
  }

  static void *writer(void *arg)
  {
  	(void)arg;
  	if (period) {
  		struct perf_event_attr attr = {
  			.type = PERF_TYPE_SOFTWARE,
  			.size = sizeof(attr),
  			.config = PERF_COUNT_SW_CPU_CLOCK,
  			.sample_period = period,
  		};

  		if (syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0) < 0)
  			die("perf_event_open");
  	}
  	while (!atomic_load(&stop)) {
  		int fd;

  		unlink(DIR "/ocrace");
  		fd = open(DIR "/ocrace", O_CREAT | O_RDWR | O_CLOEXEC, 0644);
  		if (fd >= 0)
  			close(fd);
  		atomic_fetch_add(&creates, 1);
  	}
  	return NULL;
  }

  static void *reader(void *arg)
  {
  	char lnk[64];

  	snprintf(lnk, sizeof(lnk), DIR "/lnk.%ld", (long)arg);
  	while (!atomic_load(&stop)) {
  		int fd = open(lnk, O_CREAT | O_RDWR | O_CLOEXEC, 0644);

  		if (fd >= 0)
  			close(fd);
  		else if (errno == ENOENT)
  			atomic_fetch_add(&enoent, 1);
  		else
  			atomic_fetch_add(&other, 1);
  		atomic_fetch_add(&opens, 1);
  	}
  	return NULL;
  }

  int main(int argc, char **argv)
  {
  	int secs = 60, nw = 4, nr = 12, c, n = 0;
  	pthread_t t[256];

  	while ((c = getopt(argc, argv, "t:w:r:p:")) != -1) {
  		switch (c) {
  		case 't': secs = atoi(optarg); break;
  		case 'w': nw = atoi(optarg); break;
  		case 'r': nr = atoi(optarg); break;
  		case 'p': period = atol(optarg); break;
  		default: return 2;
  		}
  	}
  	if (nw + nr > 256)
  		return 2;
  	mkdir(DIR, 0755);
  	if (mount("oc", DIR, "tmpfs", MS_STRICTATIME, "mode=0755"))
  		die("tmpfs");
  	for (long i = 0; i < nr; i++) {
  		char lnk[64];

  		snprintf(lnk, sizeof(lnk), DIR "/lnk.%ld", i);
  		if (symlink("ocrace", lnk))
  			die("symlink");
  	}
  	for (long i = 0; i < nw; i++)
  		if (pthread_create(&t[n++], NULL, writer, NULL))
  			die("writer");
  	for (long i = 0; i < nr; i++)
  		if (pthread_create(&t[n++], NULL, reader, (void *)i))
  			die("reader");
  	sleep(secs);
  	atomic_store(&stop, 1);
  	for (int i = 0; i < n; i++)
  		pthread_join(t[i], NULL);
  	printf("%ds: %ld opens, %ld creates, ENOENT %ld, other errors %ld\n",
  	       secs, atomic_load(&opens), atomic_load(&creates),
  	       atomic_load(&enoent), atomic_load(&other));
  	umount2(DIR, MNT_DETACH);
  	return atomic_load(&enoent) ? 1 : 0;
  }

  diff --git a/fs/dcache.c b/fs/dcache.c
  --- a/fs/dcache.c
  +++ b/fs/dcache.c
  @@ -32,9 +32,13 @@
   #include <linux/bit_spinlock.h>
   #include <linux/rculist_bl.h>
   #include <linux/list_lru.h>
  +#include <linux/delay.h>
   #include "internal.h"
   #include "mount.h"

  +/* debug: stall "ocrace*" names between ->d_inode and the type */
  +static int sysctl_ocrace_instantiate_us;
  +
   #include <asm/runtime-const.h>

   /*
  @@ -200,6 +204,13 @@
   }

   static const struct ctl_table fs_dcache_sysctls[] = {
  +	{
  +		.procname	= "ocrace_instantiate_us",
  +		.data		= &sysctl_ocrace_instantiate_us,
  +		.maxlen		= sizeof(int),
  +		.mode		= 0644,
  +		.proc_handler	= proc_dointvec,
  +	},
   	{
   		.procname	= "dentry-state",
   		.data		= &dentry_stat,
  @@ -405,6 +416,10 @@
   	unsigned flags;

   	dentry->d_inode = inode;
  +	/* debug: an interrupt landing right here */
  +	if (unlikely(READ_ONCE(sysctl_ocrace_instantiate_us)) &&
  +	    !strncmp(dentry->d_name.name, "ocrace", 6))
  +		udelay(READ_ONCE(sysctl_ocrace_instantiate_us));
   	flags = READ_ONCE(dentry->d_flags);
   	flags &= ~DCACHE_ENTRY_TYPE;
   	flags |= type_flags;

Fixes: e747e15156b7 ("fs: try an opportunistic lookup for O_CREAT opens too")
Cc: stable@vger.kernel.org # v6.12+
Link: https://patch.msgid.link/20261012-work-namei-ocreate-v1-1-c7883b17ee28@kernel.org
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Christian Brauner <brauner@kernel.org> · 2026-10-12 12:57 UTC

Browse files
26 files changed, +157 −49
modified fs/9p/v9fs.h
@@ -137,6 +137,7 @@ struct v9fs_session_info {
137 137 struct list_head slist; /* list of sessions registered with v9fs */
138 138 struct rw_semaphore rename_sem;
139 139 long session_lock_timeout; /* retry interval for blocking locks */
140 + struct rcu_head rcu; /* v9fs_vfs_get_link_dotl() reads ->cache in rcu pathwalk */
140 141 };
141 142
142 143 #define NDENTRY_TIMEOUT_NEVER (-1U)
modified fs/9p/vfs_super.c
@@ -173,8 +173,8 @@ static void v9fs_kill_super(struct super_block *s)
173 173
174 174 v9fs_session_cancel(v9ses);
175 175 v9fs_session_close(v9ses);
176 - kfree(v9ses);
177 - s->s_fs_info = NULL;
176 + /* v9fs_vfs_get_link_dotl() may still read ->cache in rcu pathwalk */
177 + kfree_rcu(v9ses, rcu);
178 178 p9_debug(P9_DEBUG_VFS, "exiting kill_super\n");
179 179 }
180 180
modified fs/afs/internal.h
@@ -251,6 +251,8 @@ struct afs_super_info {
251 251 struct afs_volume *volume; /* volume record */
252 252 enum afs_flock_mode flock_mode:8; /* File locking emulation mode */
253 253 bool dyn_root; /* True if dynamic root */
254 + /* afs_atcell_get_link() reads ->net_ns in rcu pathwalk */
255 + struct rcu_head rcu;
254 256 };
255 257
256 258 static inline struct afs_super_info *AFS_FS_S(struct super_block *sb)
modified fs/afs/super.c
@@ -523,7 +523,7 @@ static void afs_destroy_sbi(struct afs_super_info *as)
523 523 afs_put_volume(as->volume, afs_volume_trace_put_destroy_sbi);
524 524 afs_unuse_cell(as->cell, afs_cell_trace_unuse_sbi);
525 525 put_net(as->net_ns);
526 - kfree(as);
526 + kfree_rcu(as, rcu);
527 527 }
528 528 }
529 529
modified fs/autofs/waitq.c
@@ -12,15 +12,13 @@
12 12 */
13 13 static autofs_wqt_t autofs_next_wait_queue = 1;
14 14
15 - void autofs_catatonic_mode(struct autofs_sb_info *sbi)
15 + static void autofs_catatonic_mode_locked(struct autofs_sb_info *sbi)
16 16 {
17 17 struct autofs_wait_queue *wq, *nwq;
18 18
19 - mutex_lock(&sbi->wq_mutex);
20 - if (sbi->flags & AUTOFS_SBI_CATATONIC) {
21 - mutex_unlock(&sbi->wq_mutex);
19 + lockdep_assert_held(&sbi->wq_mutex);
20 + if (sbi->flags & AUTOFS_SBI_CATATONIC)
22 21 return;
23 - }
24 22
25 23 pr_debug("entering catatonic mode\n");
26 24
@@ -40,6 +38,21 @@ void autofs_catatonic_mode(struct autofs_sb_info *sbi)
40 38 fput(sbi->pipe); /* Close the pipe */
41 39 sbi->pipe = NULL;
42 40 sbi->pipefd = -1;
41 + }
42 +
43 + void autofs_catatonic_mode(struct autofs_sb_info *sbi)
44 + {
45 + mutex_lock(&sbi->wq_mutex);
46 + autofs_catatonic_mode_locked(sbi);
47 + mutex_unlock(&sbi->wq_mutex);
48 + }
49 +
50 + static void autofs_catatonic_mode_for_pipe(struct autofs_sb_info *sbi,
51 + struct file *pipe)
52 + {
53 + mutex_lock(&sbi->wq_mutex);
54 + if (sbi->pipe == pipe)
55 + autofs_catatonic_mode_locked(sbi);
43 56 mutex_unlock(&sbi->wq_mutex);
44 57 }
45 58
@@ -170,7 +183,7 @@ static void autofs_notify_daemon(struct autofs_sb_info *sbi,
170 183 autofs_wait_release(sbi, wq->wait_queue_token, ret);
171 184 break;
172 185 default:
173 - autofs_catatonic_mode(sbi);
186 + autofs_catatonic_mode_for_pipe(sbi, pipe);
174 187 break;
175 188 }
176 189 fput(pipe);
modified fs/ext4/ext4.h
@@ -1774,6 +1774,8 @@ struct ext4_sb_info {
1774 1774 struct list_head s_es_list; /* List of inodes with reclaimable extents */
1775 1775 long s_es_nr_inode;
1776 1776 struct ext4_es_stats s_es_stats;
1777 + /* ext4_get_link() reads the sbi in rcu pathwalk */
1778 + struct rcu_head s_rcu;
1777 1779 struct mb_cache *s_ea_block_cache;
1778 1780 struct mb_cache *s_ea_inode_cache;
1779 1781
modified fs/ext4/extents-test.c
@@ -151,6 +151,7 @@ static void extents_kunit_exit(struct kunit *test)
151 151
152 152 sbi = k_ctx.k_ei->vfs_inode.i_sb->s_fs_info;
153 153 ext4_es_unregister_shrinker(sbi);
154 + ext4_es_destroy_stats(sbi);
154 155 deactivate_super(sbi->s_sb);
155 156 kfree(sbi);
156 157 kfree(k_ctx.k_ei);
@@ -340,6 +341,7 @@ out:
340 341 k_ctx.k_data = NULL;
341 342
342 343 ext4_es_unregister_shrinker(sbi);
344 + ext4_es_destroy_stats(sbi);
343 345 out_deactivate:
344 346 deactivate_locked_super(sb);
345 347 kfree(sbi);
modified fs/ext4/extents.c
@@ -6409,6 +6409,7 @@ EXPORT_SYMBOL_FOR_EXT4_TEST(__ext4_ext_dirty);
6409 6409 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_ext_zeroout);
6410 6410 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_register_shrinker);
6411 6411 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_unregister_shrinker);
6412 + EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_destroy_stats);
6412 6413 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_map_create_blocks);
6413 6414 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_init_tree);
6414 6415 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_lookup_extent);
modified fs/ext4/extents_status.c
@@ -1881,12 +1881,17 @@ err1:
1881 1881 }
1882 1882
1883 1883 void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
1884 + {
1885 + shrinker_free(sbi->s_es_shrinker);
1886 + }
1887 +
1888 + /* ext4_get_link() bumps the hit and miss counters in rcu pathwalk */
1889 + void ext4_es_destroy_stats(struct ext4_sb_info *sbi)
1884 1890 {
1885 1891 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
1886 1892 percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
1887 1893 percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
1888 1894 percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
1889 - shrinker_free(sbi->s_es_shrinker);
1890 1895 }
1891 1896
1892 1897 /*
modified fs/ext4/extents_status.h
@@ -238,6 +238,7 @@ static inline void ext4_es_store_pblock_status(struct extent_status *es,
238 238
239 239 extern int ext4_es_register_shrinker(struct ext4_sb_info *sbi);
240 240 extern void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi);
241 + extern void ext4_es_destroy_stats(struct ext4_sb_info *sbi);
241 242
242 243 extern int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v);
243 244
modified fs/ext4/super.c
@@ -1279,6 +1279,14 @@ static void ext4_flex_groups_free(struct ext4_sb_info *sbi)
1279 1279 }
1280 1280 }
1281 1281
1282 + static void ext4_put_sbi_rcu(struct rcu_head *head)
1283 + {
1284 + struct ext4_sb_info *sbi = container_of(head, struct ext4_sb_info, s_rcu);
1285 +
1286 + ext4_es_destroy_stats(sbi);
1287 + kfree(sbi);
1288 + }
1289 +
1282 1290 static void ext4_put_super(struct super_block *sb)
1283 1291 {
1284 1292 struct ext4_sb_info *sbi = EXT4_SB(sb);
@@ -1374,7 +1382,6 @@ static void ext4_put_super(struct super_block *sb)
1374 1382 ext4_stop_mmpd(sbi);
1375 1383
1376 1384 brelse(sbi->s_sbh);
1377 - sb->s_fs_info = NULL;
1378 1385 /*
1379 1386 * Now that we are completely done shutting down the
1380 1387 * superblock, we need to actually destroy the kobject.
@@ -1387,7 +1394,8 @@ static void ext4_put_super(struct super_block *sb)
1387 1394 #if IS_ENABLED(CONFIG_UNICODE)
1388 1395 utf8_unload(sb->s_encoding);
1389 1396 #endif
1390 - kfree(sbi);
1397 + /* ext4_get_link() reads the sbi and the counters in rcu pathwalk */
1398 + call_rcu(&sbi->s_rcu, ext4_put_sbi_rcu);
1391 1399 }
1392 1400
1393 1401 static struct kmem_cache *ext4_inode_cachep;
@@ -5806,6 +5814,7 @@ failed_mount3a:
5806 5814 /* Drain deferred EA inode iputs from journal replay */
5807 5815 flush_delayed_work(&sbi->s_ea_inode_work);
5808 5816 ext4_es_unregister_shrinker(sbi);
5817 + ext4_es_destroy_stats(sbi);
5809 5818 failed_mount3:
5810 5819 /* flush s_sb_upd_work before sbi destroy */
5811 5820 flush_work(&sbi->s_sb_upd_work);
modified fs/namei.c
@@ -4750,8 +4750,7 @@ static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)
4750 4750 return dentry;
4751 4751
4752 4752 if (open_flag & O_CREAT) {
4753 - /* Discard negative dentries. Need inode_lock to do the create */
4754 - if (!dentry->d_inode) {
4753 + if (d_is_negative(dentry)) {
4755 4754 if (!(nd->flags & LOOKUP_RCU))
4756 4755 dput(dentry);
4757 4756 dentry = NULL;
modified fs/namespace.c
@@ -297,7 +297,11 @@ int mnt_get_count(struct mount *mnt)
297 297 /* puts first, so a put counted here has its get counted below */
298 298 for_each_possible_cpu(cpu)
299 299 puts += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_puts;
300 - smp_mb(); /* pairs with the smp_wmb() in mntput_no_expire() */
300 + /*
301 + * Pairs with the smp_wmb() in mntput_no_expire() and with the
302 + * smp_mb() in __legitimize_mnt().
303 + */
304 + smp_mb();
301 305 for_each_possible_cpu(cpu)
302 306 gets += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_gets;
303 307
@@ -782,7 +786,7 @@ int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
782 786 return 0;
783 787 mnt = real_mount(bastard);
784 788 mnt_inc_count(mnt);
785 - smp_mb(); /* see mntput_no_expire_slowpath() and do_umount() */
789 + smp_mb(); /* pairs with the smp_mb() in mnt_get_count() */
786 790 if (likely(!read_seqretry(&mount_lock, seq)))
787 791 return 0;
788 792 lock_mount_hash();
@@ -1419,11 +1423,6 @@ static void noinline mntput_no_expire_slowpath(struct mount *mnt)
1419 1423
1420 1424 VFS_BUG_ON(mnt->mnt_ns);
1421 1425 lock_mount_hash();
1422 - /*
1423 - * make sure that if __legitimize_mnt() has not seen us grab
1424 - * mount_lock, we'll see their refcount increment here.
1425 - */
1426 - smp_mb();
1427 1426 mnt_dec_count(mnt);
1428 1427 count = mnt_get_count(mnt);
1429 1428 if (count != 0) {
@@ -2006,7 +2005,6 @@ static int do_umount(struct mount *mnt, int flags)
2006 2005 umount_tree(mnt, UMOUNT_PROPAGATE);
2007 2006 retval = 0;
2008 2007 } else {
2009 - smp_mb(); // paired with __legitimize_mnt()
2010 2008 shrink_submounts(mnt);
2011 2009 retval = -EBUSY;
2012 2010 if (!propagate_mount_busy(mnt, 2)) {
modified fs/notify/mark.c
@@ -929,6 +929,11 @@ restart:
929 929 goto restart;
930 930 }
931 931
932 + if (conn->flags & FSNOTIFY_CONN_FLAG_DESTROYING) {
933 + err = -ENOENT;
934 + goto out_err;
935 + }
936 +
932 937 /* is mark the first mark? */
933 938 if (hlist_empty(&conn->list)) {
934 939 hlist_add_head_rcu(&mark->obj_list, &conn->list);
@@ -1116,6 +1121,12 @@ void fsnotify_destroy_marks(fsnotify_connp_t *connp)
1116 1121 conn = fsnotify_grab_connector(connp);
1117 1122 if (!conn)
1118 1123 return;
1124 + /*
1125 + * New marks could be inserted before our cursor and survive the final
1126 + * detachment of the connector from the object. Prevent additions to
1127 + * this connector before dropping its lock.
1128 + */
1129 + conn->flags |= FSNOTIFY_CONN_FLAG_DESTROYING;
1119 1130 /*
1120 1131 * We have to be careful since we can race with e.g.
1121 1132 * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
modified fs/ntfs3/dir.c
@@ -186,11 +186,20 @@ int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len,
186 186 {
187 187 int ret, slen, i;
188 188 const u8 *end;
189 - struct nls_table *nls = sbi->options->nls;
189 + struct ntfs_mount_options *opts;
190 + struct nls_table *nls;
191 + bool ads;
190 192 u16 *uname = uni->name;
191 193
192 194 static_assert(sizeof(wchar_t) == sizeof(u16));
193 195
196 + /* ntfs_fs_reconfigure() may replace the options, but not ->nls */
197 + rcu_read_lock();
198 + opts = READ_ONCE(sbi->options);
199 + nls = opts->nls;
200 + ads = opts->ads;
201 + rcu_read_unlock();
202 +
194 203 if (!nls) {
195 204 /* utf8 -> utf16 */
196 205 ret = _utf8s_to_utf16s(name, name_len, endian, uname, max_ulen);
@@ -230,7 +239,7 @@ int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len,
230 239
231 240 uni->len = ret;
232 241 uni->ads_len = 0;
233 - if (ret > 0 && sbi->options->ads) {
242 + if (ret > 0 && ads) {
234 243 uname = uni->name;
235 244 /* Find delimiter in range [1 : ret-2). */
236 245 for (i = 1; i + 1 < ret; i++) {
modified fs/ntfs3/ntfs_fs.h
@@ -91,6 +91,8 @@ enum utf16_endian;
91 91 struct ntfs_mount_options {
92 92 char *nls_name;
93 93 struct nls_table *nls;
94 + /* ->d_hash() and ->d_compare() read the options in rcu pathwalk */
95 + struct rcu_head rcu;
94 96
95 97 kuid_t fs_uid;
96 98 kgid_t fs_gid;
@@ -211,6 +213,7 @@ struct ntfs_index {
211 213 /* Ntfs file system in-core superblock data. */
212 214 struct ntfs_sb_info {
213 215 struct super_block *sb;
216 + struct rcu_head rcu;
214 217
215 218 u32 discard_granularity;
216 219 u64 discard_granularity_mask_inv; // ~(discard_granularity_mask_inv-1)
modified fs/ntfs3/super.c
@@ -196,7 +196,7 @@ void *ntfs_set_shared(void *ptr, u32 bytes)
196 196 void *ret = NULL;
197 197 int i, j = -1;
198 198
199 - spin_lock(&s_shared_lock);
199 + spin_lock_bh(&s_shared_lock);
200 200 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
201 201 if (!s_shared[i].cnt) {
202 202 j = i;
@@ -214,7 +214,7 @@ void *ntfs_set_shared(void *ptr, u32 bytes)
214 214 s_shared[j].cnt = 1;
215 215 ret = ptr;
216 216 }
217 - spin_unlock(&s_shared_lock);
217 + spin_unlock_bh(&s_shared_lock);
218 218
219 219 return ret;
220 220 }
@@ -231,7 +231,7 @@ void *ntfs_put_shared(void *ptr)
231 231 void *ret = ptr;
232 232 int i;
233 233
234 - spin_lock(&s_shared_lock);
234 + spin_lock_bh(&s_shared_lock);
235 235 for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
236 236 if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
237 237 if (--s_shared[i].cnt)
@@ -239,7 +239,7 @@ void *ntfs_put_shared(void *ptr)
239 239 break;
240 240 }
241 241 }
242 - spin_unlock(&s_shared_lock);
242 + spin_unlock_bh(&s_shared_lock);
243 243
244 244 return ret;
245 245 }
@@ -251,6 +251,11 @@ static inline void put_mount_options(struct ntfs_mount_options *options)
251 251 kfree(options);
252 252 }
253 253
254 + static void put_mount_options_rcu(struct rcu_head *head)
255 + {
256 + put_mount_options(container_of(head, struct ntfs_mount_options, rcu));
257 + }
258 +
254 259 enum Opt {
255 260 Opt_uid,
256 261 Opt_gid,
@@ -444,6 +449,7 @@ static int ntfs_fs_reconfigure(struct fs_context *fc)
444 449 struct super_block *sb = fc->root->d_sb;
445 450 struct ntfs_sb_info *sbi = sb->s_fs_info;
446 451 struct ntfs_mount_options *new_opts = fc->fs_private;
452 + struct ntfs_mount_options *old_opts;
447 453 int ro_rw;
448 454
449 455 ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
@@ -473,7 +479,15 @@ static int ntfs_fs_reconfigure(struct fs_context *fc)
473 479 }
474 480
475 481 sync_filesystem(sb);
476 - swap(sbi->options, fc->fs_private);
482 + old_opts = sbi->options;
483 + /* pairs with the READ_ONCE() in ntfs_nls_to_utf16() */
484 + smp_store_release(&sbi->options, new_opts);
485 + fc->fs_private = NULL;
486 + /*
487 + * ntfs_d_hash() and ntfs_d_compare() of a pathwalk in rcu mode may
488 + * still read the old options through the sbi.
489 + */
490 + call_rcu(&old_opts->rcu, put_mount_options_rcu);
477 491
478 492 return 0;
479 493 }
@@ -708,6 +722,8 @@ static noinline void ntfs3_put_sbi(struct ntfs_sb_info *sbi)
708 722
709 723 static void ntfs3_free_sbi(struct ntfs_sb_info *sbi)
710 724 {
725 + if (sbi->options)
726 + put_mount_options(sbi->options);
711 727 kfree(sbi->new_rec);
712 728 kvfree(ntfs_put_shared(sbi->upcase));
713 729 kvfree(sbi->def_table);
@@ -719,6 +735,11 @@ static void ntfs3_free_sbi(struct ntfs_sb_info *sbi)
719 735 kfree(sbi);
720 736 }
721 737
738 + static void ntfs3_free_sbi_rcu(struct rcu_head *head)
739 + {
740 + ntfs3_free_sbi(container_of(head, struct ntfs_sb_info, rcu));
741 + }
742 +
722 743 static void ntfs_put_super(struct super_block *sb)
723 744 {
724 745 struct ntfs_sb_info *sbi = sb->s_fs_info;
@@ -728,11 +749,11 @@ static void ntfs_put_super(struct super_block *sb)
728 749 /* Mark rw ntfs as clear, if possible. */
729 750 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
730 751
731 - if (sbi->options) {
732 - put_mount_options(sbi->options);
733 - sbi->options = NULL;
734 - }
735 -
752 + /*
753 + * The mount options stay until the sbi is freed: ->d_hash() and
754 + * ->d_compare() of a pathwalk in rcu mode read the nls table through
755 + * them.
756 + */
736 757 ntfs3_put_sbi(sbi);
737 758 }
738 759
@@ -1939,9 +1960,11 @@ static void ntfs3_kill_sb(struct super_block *sb)
1939 1960
1940 1961 kill_block_super(sb);
1941 1962
1942 - if (sbi->options)
1943 - put_mount_options(sbi->options);
1944 - ntfs3_free_sbi(sbi);
1963 + /*
1964 + * A pathwalk in rcu mode may still be in ->d_hash() or ->d_compare()
1965 + * and read the upcase table and the nls table through the sbi.
1966 + */
1967 + call_rcu(&sbi->rcu, ntfs3_free_sbi_rcu);
1945 1968 }
1946 1969
1947 1970 // clang-format off
modified fs/overlayfs/params.c
@@ -216,6 +216,9 @@ static ssize_t ovl_parse_param_split_lowerdirs(char *str)
216 216 if (*s == '\\') {
217 217 /* keep esc chars in split lowerdir */
218 218 *d++ = *s++;
219 + /* A trailing backslash escapes nothing. */
220 + if (!*s)
221 + return -EINVAL;
219 222 } else if (*s == ':') {
220 223 bool next_colon = (*(s + 1) == ':');
221 224
modified fs/tracefs/event_inode.c
@@ -91,6 +91,14 @@ static void free_ei_rcu(struct rcu_head *rcu)
91 91 }
92 92 }
93 93
94 + /* read under eventfs_srcu and, by tracefs_d_revalidate(), in rcu pathwalk */
95 + static void free_ei_srcu(struct rcu_head *rcu)
96 + {
97 + struct eventfs_inode *ei = container_of(rcu, struct eventfs_inode, rcu);
98 +
99 + call_rcu(&ei->rcu, free_ei_rcu);
100 + }
101 +
94 102 /*
95 103 * eventfs_inode reference count management.
96 104 *
@@ -112,7 +120,7 @@ static void release_ei(struct kref *ref)
112 120 entry->release(entry->name, ei->data);
113 121 }
114 122
115 - call_srcu(&eventfs_srcu, &ei->rcu, free_ei_rcu);
123 + call_srcu(&eventfs_srcu, &ei->rcu, free_ei_srcu);
116 124 }
117 125
118 126 static inline void put_ei(struct eventfs_inode *ei)
modified fs/unicode/utf8-core.c
@@ -3,6 +3,7 @@
3 3 #include <linux/kernel.h>
4 4 #include <linux/string.h>
5 5 #include <linux/slab.h>
6 + #include <linux/rcupdate.h>
6 7 #include <linux/parser.h>
7 8 #include <linux/errno.h>
8 9 #include <linux/stringhash.h>
@@ -183,12 +184,24 @@ out_free_um:
183 184 }
184 185 EXPORT_SYMBOL(utf8_load);
185 186
187 + static void utf8_unload_rcu(struct rcu_head *head)
188 + {
189 + struct unicode_map *um = container_of(head, struct unicode_map, rcu);
190 +
191 + symbol_put(utf8_data_table);
192 + kfree(um);
193 + }
194 +
195 + /*
196 + * A pathwalk in rcu mode may still be in ->d_hash() or ->d_compare() of a
197 + * filesystem that is being torn down and read the map through its
198 + * superblock, so the map and the tables it points to have to outlive the
199 + * grace period.
200 + */
186 201 void utf8_unload(struct unicode_map *um)
187 202 {
188 - if (um) {
189 - symbol_put(utf8_data_table);
190 - kfree(um);
191 - }
203 + if (um)
204 + call_rcu(&um->rcu, utf8_unload_rcu);
192 205 }
193 206 EXPORT_SYMBOL(utf8_unload);
194 207
modified include/linux/fsnotify_backend.h
@@ -566,6 +566,7 @@ struct fsnotify_mark_connector {
566 566 unsigned char prio; /* Highest priority group */
567 567 #define FSNOTIFY_CONN_FLAG_IS_WATCHED 0x01
568 568 #define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02
569 + #define FSNOTIFY_CONN_FLAG_DESTROYING 0x04
569 570 unsigned short flags; /* flags [lock] */
570 571 union {
571 572 /* Object pointer [lock] */
modified include/linux/unicode.h
@@ -53,6 +53,7 @@ struct unicode_map {
53 53 unsigned int version;
54 54 const struct utf8data *ntab[UTF8_NMAX];
55 55 const struct utf8data_table *tables;
56 + struct rcu_head rcu;
56 57 };
57 58
58 59 int utf8_validate(const struct unicode_map *um, const struct qstr *str);
modified ipc/namespace.c
@@ -96,7 +96,7 @@ fail_ipc:
96 96 fail_mq_sysctls:
97 97 retire_mq_sysctls(ns);
98 98 fail_mq_mount:
99 - mntput(ns->mq_mnt);
99 + kern_unmount(ns->mq_mnt);
100 100 fail_put:
101 101 put_user_ns(ns->user_ns);
102 102 ns_common_free(ns);
modified kernel/exit.c
@@ -1729,7 +1729,7 @@ static long do_wait(struct wait_opts *wo)
1729 1729 add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
1730 1730
1731 1731 do {
1732 - set_current_state(TASK_INTERRUPTIBLE);
1732 + set_current_state(TASK_INTERRUPTIBLE | TASK_FREEZABLE);
1733 1733 retval = __do_wait(wo);
1734 1734 if (retval != -ERESTARTSYS)
1735 1735 break;
modified kernel/pid_namespace.c
@@ -268,7 +268,7 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns)
268 268 * free_pid() will awaken this task.
269 269 */
270 270 for (;;) {
271 - set_current_state(TASK_INTERRUPTIBLE);
271 + set_current_state(TASK_IDLE | TASK_FREEZABLE);
272 272 if (pid_ns->pid_allocated == init_pids)
273 273 break;
274 274 schedule();
modified kernel/watch_queue.c
@@ -101,7 +101,7 @@ static bool post_one_notification(struct watch_queue *wqueue,
101 101 struct pipe_inode_info *pipe = wqueue->pipe;
102 102 struct pipe_buffer *buf;
103 103 struct page *page;
104 - unsigned int head, tail, note, offset, len;
104 + unsigned int head, tail, note, offset, len, nr_notes;
105 105 bool done = false;
106 106
107 107 spin_lock_irq(&pipe->rd_wait.lock);
@@ -111,8 +111,10 @@ static bool post_one_notification(struct watch_queue *wqueue,
111 111 if (pipe_full(head, tail, pipe->ring_size))
112 112 goto lost;
113 113
114 - note = find_first_bit(wqueue->notes_bitmap, wqueue->nr_notes);
115 - if (note >= wqueue->nr_notes)
114 + /* Pairs with the smp_store_release() in watch_queue_set_size(). */
115 + nr_notes = smp_load_acquire(&wqueue->nr_notes);
116 + note = find_first_bit(wqueue->notes_bitmap, nr_notes);
117 + if (note >= nr_notes)
116 118 goto lost;
117 119
118 120 page = wqueue->notes[note / WATCH_QUEUE_NOTES_PER_PAGE];
@@ -297,7 +299,8 @@ long watch_queue_set_size(struct pipe_inode_info *pipe, unsigned int nr_notes)
297 299 wqueue->notes = pages;
298 300 wqueue->notes_bitmap = bitmap;
299 301 wqueue->nr_pages = nr_pages;
300 - wqueue->nr_notes = nr_notes;
302 + /* Pairs with the smp_load_acquire() in post_one_notification(). */
303 + smp_store_release(&wqueue->nr_notes, nr_notes);
301 304 return 0;
302 305
303 306 error_p: