Skip to content

Commit b5b52de

Browse files
DaveWysochanskiRHtorvalds
authored andcommitted
fscache: Fix oops due to race with cookie_lru and use_cookie
If a cookie expires from the LRU and the LRU_DISCARD flag is set, but the state machine has not run yet, it's possible another thread can call fscache_use_cookie and begin to use it. When the cookie_worker finally runs, it will see the LRU_DISCARD flag set, transition the cookie->state to LRU_DISCARDING, which will then withdraw the cookie. Once the cookie is withdrawn the object is removed the below oops will occur because the object associated with the cookie is now NULL. Fix the oops by clearing the LRU_DISCARD bit if another thread uses the cookie before the cookie_worker runs. BUG: kernel NULL pointer dereference, address: 0000000000000008 ... CPU: 31 PID: 44773 Comm: kworker/u130:1 Tainted: G E 6.0.0-5.dneg.x86_64 #1 Hardware name: Google Compute Engine/Google Compute Engine, BIOS Google 08/26/2022 Workqueue: events_unbound netfs_rreq_write_to_cache_work [netfs] RIP: 0010:cachefiles_prepare_write+0x28/0x90 [cachefiles] ... Call Trace: netfs_rreq_write_to_cache_work+0x11c/0x320 [netfs] process_one_work+0x217/0x3e0 worker_thread+0x4a/0x3b0 kthread+0xd6/0x100 Fixes: 12bb21a ("fscache: Implement cookie user counting and resource pinning") Reported-by: Daire Byrne <[email protected]> Signed-off-by: Dave Wysochanski <[email protected]> Signed-off-by: David Howells <[email protected]> Tested-by: Daire Byrne <[email protected]> Link: https://lore.kernel.org/r/[email protected]/ # v1 Link: https://lore.kernel.org/r/[email protected]/ # v2 Signed-off-by: Linus Torvalds <[email protected]>
1 parent 098e5ed commit b5b52de

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

fs/fscache/cookie.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,14 @@ void __fscache_use_cookie(struct fscache_cookie *cookie, bool will_modify)
605605
set_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
606606
queue = true;
607607
}
608+
/*
609+
* We could race with cookie_lru which may set LRU_DISCARD bit
610+
* but has yet to run the cookie state machine. If this happens
611+
* and another thread tries to use the cookie, clear LRU_DISCARD
612+
* so we don't end up withdrawing the cookie while in use.
613+
*/
614+
if (test_and_clear_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags))
615+
fscache_see_cookie(cookie, fscache_cookie_see_lru_discard_clear);
608616
break;
609617

610618
case FSCACHE_COOKIE_STATE_FAILED:

include/trace/events/fscache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ enum fscache_cookie_trace {
6666
fscache_cookie_put_work,
6767
fscache_cookie_see_active,
6868
fscache_cookie_see_lru_discard,
69+
fscache_cookie_see_lru_discard_clear,
6970
fscache_cookie_see_lru_do_one,
7071
fscache_cookie_see_relinquish,
7172
fscache_cookie_see_withdraw,
@@ -149,6 +150,7 @@ enum fscache_access_trace {
149150
EM(fscache_cookie_put_work, "PQ work ") \
150151
EM(fscache_cookie_see_active, "- activ") \
151152
EM(fscache_cookie_see_lru_discard, "- x-lru") \
153+
EM(fscache_cookie_see_lru_discard_clear,"- lrudc") \
152154
EM(fscache_cookie_see_lru_do_one, "- lrudo") \
153155
EM(fscache_cookie_see_relinquish, "- x-rlq") \
154156
EM(fscache_cookie_see_withdraw, "- x-wth") \

0 commit comments

Comments
 (0)