2021-11-29 04:07:04

by lianzhi chang

[permalink] [raw]
Subject: [PATCH v18] tty: Fix the keyboard led light display problem

Use the "ctrl+alt+Fn" key combination to switch the system from tty to
desktop or switch the system from desktop to tty. After the switch is
completed, it is found that the state of the keyboard lock is
inconsistent with the state of the keyboard Led light.The reasons are
as follows:

* The desktop environment (Xorg and other services) is bound to a tty
(assuming it is tty1), and the kb->kbdmode attribute value of tty1
will be set to VC_OFF. According to the current code logic, in the
desktop environment, the values of ledstate and kb->ledflagstate
of tty1 will not be modified anymore, so they are always 0.

* When switching between each tty, the final value of ledstate set by
the previous tty is compared with the kb->ledflagstate value of the
current tty to determine whether to set the state of the keyboard
light. The process of switching between desktop and tty is also the
process of switching between tty1 and other ttys. There are two
situations:

- (1) In the desktop environment, tty1 will not set the ledstate,
which will cause when switching from the desktop to other ttys,
if the desktop lights up the keyboard's led, after the switch is
completed, the keyboard's led light will always be on;

- (2) When switching from another tty to the desktop, this
mechanism will trigger tty1 to set the led state. If other tty
lights up the led of the keyboard before switching to the desktop,
the led will be forcibly turned off. This situation should
be avoided.

* The current patch is to solve these problems: by judging the
kb->kbdmode value of the current tty, it is determined whether
the led state is forced to be set when the VT switch is completed.

Signed-off-by: lianzhi chang <[email protected]>
Suggested-by: Dmitry Torokhov <[email protected]>
---
v18:
(1) Modify the description information.
(2) The judgment condition of kb->kbdmode should be "!=",
correct it.
(3) Modify code indentation.

drivers/tty/vt/keyboard.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
index c7fbbcdcc346..bcf8a858afd2 100644
--- a/drivers/tty/vt/keyboard.c
+++ b/drivers/tty/vt/keyboard.c
@@ -153,6 +153,7 @@ static int shift_state = 0;

static unsigned int ledstate = -1U; /* undefined */
static unsigned char ledioctl;
+static bool vt_switch;

/*
* Notifier list for console keyboard events
@@ -412,9 +413,21 @@ static void do_compute_shiftstate(void)
/* We still have to export this method to vt.c */
void vt_set_leds_compute_shiftstate(void)
{
+ struct kbd_struct *kb;
unsigned long flags;

- set_leds();
+ /*
+ * When switching VT, according to the value of kb->kbdmode,
+ * judge whether it is necessary to force the keyboard light
+ * state to be issued.
+ */
+ kb = kbd_table + fg_console;
+ if (kb->kbdmode != VC_RAW ||
+ kb->kbdmode != VC_MEDIUMRAW ||
+ kb->kbdmode != VC_OFF) {
+ vt_switch = true;
+ set_leds();
+ }

spin_lock_irqsave(&kbd_event_lock, flags);
do_compute_shiftstate();
@@ -1255,6 +1268,11 @@ static void kbd_bh(struct tasklet_struct *unused)
leds |= (unsigned int)kbd->lockstate << 8;
spin_unlock_irqrestore(&led_lock, flags);

+ if (vt_switch) {
+ ledstate = ~leds;
+ vt_switch = false;
+ }
+
if (leds != ledstate) {
kbd_propagate_led_state(ledstate, leds);
ledstate = leds;
--
2.20.1





2021-11-29 07:19:09

by Dmitry Torokhov

[permalink] [raw]
Subject: Re: [PATCH v18] tty: Fix the keyboard led light display problem

Hi,

On Mon, Nov 29, 2021 at 12:04:45PM +0800, lianzhi chang wrote:
> + /*
> + * When switching VT, according to the value of kb->kbdmode,
> + * judge whether it is necessary to force the keyboard light
> + * state to be issued.
> + */
> + kb = kbd_table + fg_console;
> + if (kb->kbdmode != VC_RAW ||
> + kb->kbdmode != VC_MEDIUMRAW ||
> + kb->kbdmode != VC_OFF) {

Please do not do that. Even if kbdmode is one of those states if might
be set up to show LED pattern set by ioctls (KDSETLED), so you still
want to restore LEDs.

Also, you are trying optimize something that happens pretty infrequently
and it is really easy to get it wrong. It is much safer to let the
kernel [re]set LEDs regardless of kbdmode and then let X do its own
thing.

Thanks.

--
Dmitry

2021-11-29 11:46:30

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v18] tty: Fix the keyboard led light display problem

On Mon, Nov 29, 2021 at 12:04:45PM +0800, lianzhi chang wrote:
> Use the "ctrl+alt+Fn" key combination to switch the system from tty to
> desktop or switch the system from desktop to tty. After the switch is
> completed, it is found that the state of the keyboard lock is
> inconsistent with the state of the keyboard Led light.The reasons are
> as follows:
>
> * The desktop environment (Xorg and other services) is bound to a tty
> (assuming it is tty1), and the kb->kbdmode attribute value of tty1
> will be set to VC_OFF. According to the current code logic, in the
> desktop environment, the values of ledstate and kb->ledflagstate
> of tty1 will not be modified anymore, so they are always 0.
>
> * When switching between each tty, the final value of ledstate set by
> the previous tty is compared with the kb->ledflagstate value of the
> current tty to determine whether to set the state of the keyboard
> light. The process of switching between desktop and tty is also the
> process of switching between tty1 and other ttys. There are two
> situations:
>
> - (1) In the desktop environment, tty1 will not set the ledstate,
> which will cause when switching from the desktop to other ttys,
> if the desktop lights up the keyboard's led, after the switch is
> completed, the keyboard's led light will always be on;
>
> - (2) When switching from another tty to the desktop, this
> mechanism will trigger tty1 to set the led state. If other tty
> lights up the led of the keyboard before switching to the desktop,
> the led will be forcibly turned off. This situation should
> be avoided.
>
> * The current patch is to solve these problems: by judging the
> kb->kbdmode value of the current tty, it is determined whether
> the led state is forced to be set when the VT switch is completed.

Thanks for the update, looks pretty much good!

> Signed-off-by: lianzhi chang <[email protected]>
> Suggested-by: Dmitry Torokhov <[email protected]>
> ---
> v18:
> (1) Modify the description information.
> (2) The judgment condition of kb->kbdmode should be "!=",
> correct it.

> (3) Modify code indentation.

It's still not updated.

> drivers/tty/vt/keyboard.c | 20 +++++++++++++++++++-
> 1 file changed, 19 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
> index c7fbbcdcc346..bcf8a858afd2 100644
> --- a/drivers/tty/vt/keyboard.c
> +++ b/drivers/tty/vt/keyboard.c
> @@ -153,6 +153,7 @@ static int shift_state = 0;
>
> static unsigned int ledstate = -1U; /* undefined */
> static unsigned char ledioctl;
> +static bool vt_switch;
>
> /*
> * Notifier list for console keyboard events
> @@ -412,9 +413,21 @@ static void do_compute_shiftstate(void)
> /* We still have to export this method to vt.c */
> void vt_set_leds_compute_shiftstate(void)
> {
> + struct kbd_struct *kb;
> unsigned long flags;
>
> - set_leds();
> + /*
> + * When switching VT, according to the value of kb->kbdmode,
> + * judge whether it is necessary to force the keyboard light
> + * state to be issued.
> + */
> + kb = kbd_table + fg_console;
> + if (kb->kbdmode != VC_RAW ||

> + kb->kbdmode != VC_MEDIUMRAW ||
> + kb->kbdmode != VC_OFF) {

These two are still too right shifted.

> + vt_switch = true;
> + set_leds();
> + }
>
> spin_lock_irqsave(&kbd_event_lock, flags);
> do_compute_shiftstate();
> @@ -1255,6 +1268,11 @@ static void kbd_bh(struct tasklet_struct *unused)
> leds |= (unsigned int)kbd->lockstate << 8;
> spin_unlock_irqrestore(&led_lock, flags);
>
> + if (vt_switch) {
> + ledstate = ~leds;
> + vt_switch = false;
> + }
> +
> if (leds != ledstate) {
> kbd_propagate_led_state(ledstate, leds);
> ledstate = leds;
> --
> 2.20.1

--
With Best Regards,
Andy Shevchenko



2021-11-30 07:54:28

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH v18] tty: Fix the keyboard led light display problem

Hi lianzhi,

url: https://github.com/0day-ci/linux/commits/lianzhi-chang/tty-Fix-the-keyboard-led-light-display-problem/20211129-120853
base: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git tty-testing
config: i386-randconfig-m021-20211128 (https://download.01.org/0day-ci/archive/20211129/[email protected]/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>
Reported-by: Dan Carpenter <[email protected]>

New smatch warnings:
drivers/tty/vt/keyboard.c:425 vt_set_leds_compute_shiftstate() warn: was && intended here instead of ||?

vim +425 drivers/tty/vt/keyboard.c

63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby 2021-01-05 414 void vt_set_leds_compute_shiftstate(void)
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox 2012-02-28 415 {
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 416 struct kbd_struct *kb;
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox 2012-02-28 417 unsigned long flags;
63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby 2021-01-05 418
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 419 /*
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 420 * When switching VT, according to the value of kb->kbdmode,
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 421 * judge whether it is necessary to force the keyboard light
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 422 * state to be issued.
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 423 */
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 424 kb = kbd_table + fg_console;
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 @425 if (kb->kbdmode != VC_RAW ||
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 426 kb->kbdmode != VC_MEDIUMRAW ||
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 427 kb->kbdmode != VC_OFF) {

&& was intended instead of ||.

560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 428 vt_switch = true;
63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby 2021-01-05 429 set_leds();
560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 430 }
63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby 2021-01-05 431
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox 2012-02-28 432 spin_lock_irqsave(&kbd_event_lock, flags);
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox 2012-02-28 433 do_compute_shiftstate();
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox 2012-02-28 434 spin_unlock_irqrestore(&kbd_event_lock, flags);
079c9534a96da9 drivers/tty/vt/keyboard.c Alan Cox 2012-02-28 435 }

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


2021-11-30 08:50:52

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v18] tty: Fix the keyboard led light display problem

On Tue, Nov 30, 2021 at 10:53:37AM +0300, Dan Carpenter wrote:
> Hi lianzhi,
>
> url: https://github.com/0day-ci/linux/commits/lianzhi-chang/tty-Fix-the-keyboard-led-light-display-problem/20211129-120853
> base: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git tty-testing
> config: i386-randconfig-m021-20211128 (https://download.01.org/0day-ci/archive/20211129/[email protected]/config)
> compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <[email protected]>
> Reported-by: Dan Carpenter <[email protected]>
>
> New smatch warnings:
> drivers/tty/vt/keyboard.c:425 vt_set_leds_compute_shiftstate() warn: was && intended here instead of ||?

Indeed, how this had been tested?

> 560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 @425 if (kb->kbdmode != VC_RAW ||
> 560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 426 kb->kbdmode != VC_MEDIUMRAW ||
> 560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 427 kb->kbdmode != VC_OFF) {
>
> && was intended instead of ||.
>
> 560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 428 vt_switch = true;
> 63f24a7fafd448 drivers/tty/vt/keyboard.c Jiri Slaby 2021-01-05 429 set_leds();
> 560757cc1f5e54 drivers/tty/vt/keyboard.c lianzhi chang 2021-11-29 430 }

--
With Best Regards,
Andy Shevchenko