Skip to content

Commit 2f488f6

Browse files
desmondcheongzxjtlayton
authored andcommitted
fcntl: fix potential deadlock for &fasync_struct.fa_lock
There is an existing lock hierarchy of &dev->event_lock --> &fasync_struct.fa_lock --> &f->f_owner.lock from the following call chain: input_inject_event(): spin_lock_irqsave(&dev->event_lock,...); input_handle_event(): input_pass_values(): input_to_handler(): evdev_events(): evdev_pass_values(): spin_lock(&client->buffer_lock); __pass_event(): kill_fasync(): kill_fasync_rcu(): read_lock(&fa->fa_lock); send_sigio(): read_lock_irqsave(&fown->lock,...); &dev->event_lock is HARDIRQ-safe, so interrupts have to be disabled while grabbing &fasync_struct.fa_lock, otherwise we invert the lock hierarchy. However, since kill_fasync which calls kill_fasync_rcu is an exported symbol, it may not necessarily be called with interrupts disabled. As kill_fasync_rcu may be called with interrupts disabled (for example, in the call chain above), we replace calls to read_lock/read_unlock on &fasync_struct.fa_lock in kill_fasync_rcu with read_lock_irqsave/read_unlock_irqrestore. Signed-off-by: Desmond Cheong Zhi Xi <desmondcheongzx@gmail.com> Signed-off-by: Jeff Layton <jlayton@kernel.org>
1 parent f671a69 commit 2f488f6

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

‎fs/fcntl.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,13 +1004,14 @@ static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
10041004
{
10051005
while (fa) {
10061006
struct fown_struct *fown;
1007+
unsigned long flags;
10071008

10081009
if (fa->magic != FASYNC_MAGIC) {
10091010
printk(KERN_ERR "kill_fasync: bad magic number in "
10101011
"fasync_struct!\n");
10111012
return;
10121013
}
1013-
read_lock(&fa->fa_lock);
1014+
read_lock_irqsave(&fa->fa_lock, flags);
10141015
if (fa->fa_file) {
10151016
fown = &fa->fa_file->f_owner;
10161017
/* Don't send SIGURG to processes which have not set a
@@ -1019,7 +1020,7 @@ static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
10191020
if (!(sig == SIGURG && fown->signum == 0))
10201021
send_sigio(fown, fa->fa_fd, band);
10211022
}
1022-
read_unlock(&fa->fa_lock);
1023+
read_unlock_irqrestore(&fa->fa_lock, flags);
10231024
fa = rcu_dereference(fa->fa_next);
10241025
}
10251026
}

0 commit comments

Comments
 (0)