Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ocfs2: cluster: fix potential deadlock on &qs->qs_lock

&qs->qs_lock is acquired by timer o2net_idle_timer() along the following
call chain. Thus the acquisition of the lock under process context should
disable bottom half, otherwise deadlock could happen if the timer happens
to preempt the execution while the lock is held in process context on the
same CPU.

<timer interrupt>
-> o2net_idle_timer()
-> o2quo_conn_err()
-> spin_lock(&qs->qs_lock)

Several lock acquisition of &qs->qs_lock under process contex do not
disable irq or bottom half. The patch fixes these potential deadlocks
scenerio by using spin_lock_bh() on &qs->qs_lock.

This flaw was found by an experimental static analysis tool I am
developing for irq-related deadlock. x86_64 allmodconfig using gcc shows
no new warning.

Link: https://lkml.kernel.org/r/20230802123824.15301-1-dg573847474@gmail.com
Signed-off-by: Chengfeng Ye <dg573847474@gmail.com>
Cc: Mark Fasheh <mark@fasheh.com>
Cc: Joel Becker <jlbec@evilplan.org>
Cc: Junxiao Bi <junxiao.bi@oracle.com>
Cc: Joseph Qi <jiangqi903@gmail.com>
Cc: Gang He <ghe@suse.com>
Cc: Jun Piao <piaojun@huawei.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Chengfeng Ye and committed by
Andrew Morton
28a45ef8 fb40b053

+13 -13
+13 -13
fs/ocfs2/cluster/quorum.c
··· 93 93 int lowest_hb, lowest_reachable = 0, fence = 0; 94 94 struct o2quo_state *qs = &o2quo_state; 95 95 96 - spin_lock(&qs->qs_lock); 96 + spin_lock_bh(&qs->qs_lock); 97 97 98 98 lowest_hb = find_first_bit(qs->qs_hb_bm, O2NM_MAX_NODES); 99 99 if (lowest_hb != O2NM_MAX_NODES) ··· 146 146 147 147 out: 148 148 if (fence) { 149 - spin_unlock(&qs->qs_lock); 149 + spin_unlock_bh(&qs->qs_lock); 150 150 o2quo_fence_self(); 151 151 } else { 152 152 mlog(ML_NOTICE, "not fencing this node, heartbeating: %d, " 153 153 "connected: %d, lowest: %d (%sreachable)\n", 154 154 qs->qs_heartbeating, qs->qs_connected, lowest_hb, 155 155 lowest_reachable ? "" : "un"); 156 - spin_unlock(&qs->qs_lock); 156 + spin_unlock_bh(&qs->qs_lock); 157 157 158 158 } 159 159 ··· 196 196 { 197 197 struct o2quo_state *qs = &o2quo_state; 198 198 199 - spin_lock(&qs->qs_lock); 199 + spin_lock_bh(&qs->qs_lock); 200 200 201 201 qs->qs_heartbeating++; 202 202 mlog_bug_on_msg(qs->qs_heartbeating == O2NM_MAX_NODES, ··· 211 211 else 212 212 o2quo_clear_hold(qs, node); 213 213 214 - spin_unlock(&qs->qs_lock); 214 + spin_unlock_bh(&qs->qs_lock); 215 215 } 216 216 217 217 /* hb going down releases any holds we might have had due to this node from ··· 220 220 { 221 221 struct o2quo_state *qs = &o2quo_state; 222 222 223 - spin_lock(&qs->qs_lock); 223 + spin_lock_bh(&qs->qs_lock); 224 224 225 225 qs->qs_heartbeating--; 226 226 mlog_bug_on_msg(qs->qs_heartbeating < 0, ··· 233 233 234 234 o2quo_clear_hold(qs, node); 235 235 236 - spin_unlock(&qs->qs_lock); 236 + spin_unlock_bh(&qs->qs_lock); 237 237 } 238 238 239 239 /* this tells us that we've decided that the node is still heartbeating ··· 245 245 { 246 246 struct o2quo_state *qs = &o2quo_state; 247 247 248 - spin_lock(&qs->qs_lock); 248 + spin_lock_bh(&qs->qs_lock); 249 249 250 250 mlog(0, "node %u\n", node); 251 251 252 252 qs->qs_pending = 1; 253 253 o2quo_clear_hold(qs, node); 254 254 255 - spin_unlock(&qs->qs_lock); 255 + spin_unlock_bh(&qs->qs_lock); 256 256 } 257 257 258 258 /* This is analogous to hb_up. as a node's connection comes up we delay the ··· 264 264 { 265 265 struct o2quo_state *qs = &o2quo_state; 266 266 267 - spin_lock(&qs->qs_lock); 267 + spin_lock_bh(&qs->qs_lock); 268 268 269 269 qs->qs_connected++; 270 270 mlog_bug_on_msg(qs->qs_connected == O2NM_MAX_NODES, ··· 279 279 else 280 280 o2quo_clear_hold(qs, node); 281 281 282 - spin_unlock(&qs->qs_lock); 282 + spin_unlock_bh(&qs->qs_lock); 283 283 } 284 284 285 285 /* we've decided that we won't ever be connecting to the node again. if it's ··· 290 290 { 291 291 struct o2quo_state *qs = &o2quo_state; 292 292 293 - spin_lock(&qs->qs_lock); 293 + spin_lock_bh(&qs->qs_lock); 294 294 295 295 if (test_bit(node, qs->qs_conn_bm)) { 296 296 qs->qs_connected--; ··· 307 307 mlog(0, "node %u, %d total\n", node, qs->qs_connected); 308 308 309 309 310 - spin_unlock(&qs->qs_lock); 310 + spin_unlock_bh(&qs->qs_lock); 311 311 } 312 312 313 313 void o2quo_init(void)