Compare
40bb03d3ea6f6a915218508819ecc4d7a7fa260d..bcec7fc2e224088daf94448d663b02e0d0b9c717
9p: free the session after a grace period
v9fs_vfs_get_link_dotl() reads ->cache from the v9fs_session_info in
sb->s_fs_info to decide whether the symlink target sits in the page
cache. It does that before it looks at the dentry, so it also does it in
rcu pathwalk mode. Without loose or metadata caching the dentries have
no ->d_revalidate() that would make the walk leave rcu mode before it
gets there.
v9fs_kill_super() frees the session and clears sb->s_fs_info without
waiting for a grace period. Right now a pathwalk can't still be in
v9fs_vfs_get_link_dotl() at that point, but only by accident. umount(2)
waits for a grace period in namespace_unlock() before it drops the
mount. And a mount whose last reference goes away elsewhere, like the
chdir out of a lazily unmounted directory, still gets a grace period
from bdi_unregister(). v9fs_fill_super() sets up a private bdi, so
generic_shutdown_super() unregisters it from kill_anon_super() and
bdi_remove_from_list() waits in synchronize_rcu_expedited() before
v9fs_kill_super() frees the session. That grace period protects the rcu
readers of bdi_list in the writeback code. It has nothing to do with
pathwalk.
Don't rely on it. Keep sb->s_fs_info and free the session after a grace
period.
Fixes: 712da38d134b ("9p: Enable symlink caching in page cache")
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261009-work-fixes-rcu-v1-3-7446d2d335e6@kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Christian Brauner <brauner@kernel.org> · 2026-10-09 21:51 UTC
11 files changed,
+36
−10
- fs/9p/v9fs.h
- fs/9p/vfs_super.c
- fs/afs/internal.h
- fs/afs/super.c
- fs/ext4/ext4.h
- fs/ext4/extents-test.c
- fs/ext4/extents.c
- fs/ext4/extents_status.c
- fs/ext4/extents_status.h
- fs/ext4/super.c
- kernel/watch_queue.c
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/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 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: | |