Skip to contentSkip to conversation

Char: vt, make sysfs operations atomic

PATCH In review Jiri Slaby 4 participants 6 messages May 26, 2008 – May 28, 2008 Char and misc drivers

lore
Share & tools
lore

Jiri Slaby

author
#0 lore raw

PATCH 1/1 Char: vt, make sysfs operations atomic

Hold console sem while creating/destroying sysfs files. Serialisation is so far done by BKL held in tty release_dev and chrdev_open, but no other locks are held in open path.

---
 drivers/char/vt.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/char/vt.c +2 -2
index fa1ffbf..0043cff 100644
@@ -2746,8 +2746,8 @@ static int con_open(struct tty_struct *tty, struct file *filp)
 				tty->termios->c_iflag |= IUTF8;
 			else
 				tty->termios->c_iflag &= ~IUTF8;
-			release_console_sem();
 			vcs_make_sysfs(tty);
+			release_console_sem();
 			return ret;
 		}
 	}
@@ -2772,8 +2772,8 @@ static void con_close(struct tty_struct *tty, struct file *filp)
 		if (vc)
 			vc->vc_tty = NULL;
 		tty->driver_data = NULL;
-		release_console_sem();
 		vcs_remove_sysfs(tty);
+		release_console_sem();
 		mutex_unlock(&tty_mutex);
 		/*
 		 * tty_mutex is released, but we still hold BKL, so there is
Signature · 1 line
1.5.4.5
Acked-by on v1 1/1
#1 lore raw
Quoted 3 lines · On Mon, 26 May 2008 13:53:32 +0200 Jiri Slaby <[email protected]> wrote:

Hold console sem while creating/destroying sysfs files. Serialisation is so far done by BKL held in tty release_dev and chrdev_open, but no other locks are held in open path.

#2 lore raw
In reply to Jiri Slaby
Quoted 14 lines

Hold console sem while creating/destroying sysfs files. Serialisation is so far done by BKL held in tty release_dev and chrdev_open, but no other locks are held in open path.

drivers/char/vt.c +1 -1
 				tty->termios->c_iflag |= IUTF8;
 			else
 				tty->termios->c_iflag &= ~IUTF8;
-			release_console_sem();
 			vcs_make_sysfs(tty);
+			release_console_sem();
 			return ret;
 		}
 	}

drivers/char/vt.c static void con_close(struct tty_struct *tty, struct file *filp) · line 2779

Quoted 9 lines
drivers/char/vt.c +1 -1
@@ -2772,8 +2772,8 @@ static void con_close(struct tty_struct *tty, struct file *filp)
 		if (vc)
 			vc->vc_tty = NULL;
 		tty->driver_data = NULL;
-		release_console_sem();
 		vcs_remove_sysfs(tty);
+		release_console_sem();
 		mutex_unlock(&tty_mutex);
 		/*
 		 * tty_mutex is released, but we still hold BKL, so there is

the reason for the code be the way it is is because vcs_{add,remove}_sysfs() may sleep

Signature · 1 line
Aristeu
author
#3 lore raw
Quoted 17 lines · Aristeu Rozanski
Quoted 17 lines

Hold console sem while creating/destroying sysfs files. Serialisation is so far done by BKL held in tty release_dev and chrdev_open, but no other locks are held in open path.

drivers/char/vt.c +1 -1
 				tty->termios->c_iflag |= IUTF8;
 			else
 				tty->termios->c_iflag &= ~IUTF8;
-			release_console_sem();
 			vcs_make_sysfs(tty);
+			release_console_sem();
 			return ret;
 		}
 	}
@@ -2772,8 +2772,8 @@ static void con_close(struct tty_struct *tty, struct file *filp)
 		if (vc)
 			vc->vc_tty = NULL;
 		tty->driver_data = NULL;
Quoted 8 lines

drivers/char/vt.c static void con_close(struct tty_struct *tty, struct file *filp) · line 2779

Quoted 6 lines
drivers/char/vt.c +1 -1
-		release_console_sem();
 		vcs_remove_sysfs(tty);
+		release_console_sem();
 		mutex_unlock(&tty_mutex);
 		/*
 		 * tty_mutex is released, but we still hold BKL, so there is

the reason for the code be the way it is is because vcs_{add,remove}_sysfs() may sleep

What's the point? To have races in the code but not sleep inside the semaphore?

#4 lore raw
Quoted 4 lines
Quoted 2 lines

the reason for the code be the way it is is because vcs_{add,remove}_sysfs() may sleep

What's the point? To have races in the code but not sleep inside the semaphore?

what about fixing the code to remove the race _and_ not sleep inside the semaphore? :)

Signature · 1 line
Aristeu
#5 lore raw
Quoted 7 lines · On Tue, 27 May 2008 16:48:58 -0400 Aristeu Rozanski <[email protected]> wrote:
Quoted 4 lines
Quoted 2 lines

the reason for the code be the way it is is because vcs_{add,remove}_sysfs() may sleep

What's the point? To have races in the code but not sleep inside the semaphore?

what about fixing the code to remove the race _and_ not sleep inside the semaphore? :)

The patch does fix a race, by extending console_sem coverage to provide exclusion between the sysfs creation and teardown operations.

I assume - no race was identified in the changelog. Can we actually simultaneously run con_open() and con_close() against the same device? I guess it might be possible if userspace tried hard enough.

I renamed the patch to the much more accurate "vt: hold console_sem across sysfs operations" - "atomic" in the kernel context means "cannot context switch" and nothing "atomic" is happening in this patch.