diff options
| author | Jinke Han <jinkehan@didiglobal.com> | 2026-09-08 15:37:42 +0800 |
|---|---|---|
| committer | Ingo Molnar <mingo@kernel.org> | 2026-09-17 09:48:42 +0200 |
| commit | a5f7a5bb3b7f28ba7e4fa246775b29a0e5537255 (patch) | |
| tree | 73fef824f9a846cf935869d0dbc5070ef6e2f741 /drivers/leds/leds-mlxreg.c | |
| download | linux-stable-a5f7a5bb3b7f28ba7e4fa246775b29a0e5537255.tar.gz linux-stable-a5f7a5bb3b7f28ba7e4fa246775b29a0e5537255.zip | |
x86/kprobes: Fix crash when probing CS CALL instructionsgrafted
When using eBPF to probe CS CALL instructions within a function,
a crash can be triggered.
The eBPF tool probes offset 257 of the __hrtimer_run_queues()
function:
<__hrtimer_run_queues+249>: nopl 0x0(%rax,%rax,1)
<__hrtimer_run_queues+254>: mov %r14,%rdi
<__hrtimer_run_queues+257>: cs call <__x86_indirect_thunk_r12>
<__hrtimer_run_queues+263>: mov %eax,%r12d
<__hrtimer_run_queues+266>: xchg %ax,%ax
<__hrtimer_run_queues+268>: mov %r13,%rdi
Which triggers this crash:
BUG: unable to handle page fault for address: 00000000000f41c9
#PF: supervisor write access in kernel mode
#PF: error_code(0x0002) - not-present page
PGD 0 P4D 0
Oops: 0002 [#1] SMP NOPTI
CPU: 1 PID: 0 Comm: swapper/1 Kdump: loaded Tainted: P
RIP: 0010:__hrtimer_run_queues+0x106/0x230
Note that __hrtimer_run_queues+0x106 is __hrtimer_run_queues+262, which is
at the 6th byte of the above CS CALL instruction. Since the CS CALL
instruction occupies 6 bytes, the exception occurred in the middle of that
call instruction.
The root cause is that when using eBPF tools to probe in the middle of a
function, a kprobe with INT3 is used as the underlying implementation.
During single-step emulation of the original CALL instruction,
int3_emulate_call() assumes that the probed CALL instruction is 5 bytes
long. However, the actual CS-prefixed CALL instruction occupies 6 bytes,
so it constructs an incorrect exception return address. When the CPU
returns from the kprobe handler, the next instruction to be executed is at
the address of the last byte of that CS CALL instruction. Coincidentally,
starting from that address, the CPU fetches and decodes a completely
different instruction, which ultimately triggers a kernel crash.
Fix the issue by using the actual instruction length obtained from
the instruction decoder when constructing the exception return
address, rather than relying on the hardcoded CALL_INSN_SIZE macro.
[ mingo: Refined the changelog ]
Fixes: 6256e668b7af ("x86/kprobes: Use int3 instead of debug trap for single-step")
Suggested-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Jinke Han <jinkehan@didiglobal.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Acked-by: Yafang Shao <laoar.shao@gmail.com>
Acked-by: Borislav Petkov <bp@alien8.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://patch.msgid.link/20260908073742.GA10517@didi-ThinkCentre-M920t-N000
Diffstat (limited to 'drivers/leds/leds-mlxreg.c')
| -rw-r--r-- | drivers/leds/leds-mlxreg.c | 293 |
1 files changed, 293 insertions, 0 deletions
diff --git a/drivers/leds/leds-mlxreg.c b/drivers/leds/leds-mlxreg.c new file mode 100644 index 000000000..1b70de723 --- /dev/null +++ b/drivers/leds/leds-mlxreg.c @@ -0,0 +1,293 @@ +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +// +// Copyright (c) 2018 Mellanox Technologies. All rights reserved. +// Copyright (c) 2018 Vadim Pasternak <vadimp@mellanox.com> + +#include <linux/bitops.h> +#include <linux/device.h> +#include <linux/io.h> +#include <linux/leds.h> +#include <linux/module.h> +#include <linux/platform_data/mlxreg.h> +#include <linux/platform_device.h> +#include <linux/regmap.h> + +/* Codes for LEDs. */ +#define MLXREG_LED_OFFSET_BLINK_3HZ 0x01 /* Offset from solid: 3Hz blink */ +#define MLXREG_LED_OFFSET_BLINK_6HZ 0x02 /* Offset from solid: 6Hz blink */ +#define MLXREG_LED_IS_OFF 0x00 /* Off */ +#define MLXREG_LED_RED_SOLID 0x05 /* Solid red */ +#define MLXREG_LED_GREEN_SOLID 0x0D /* Solid green */ +#define MLXREG_LED_AMBER_SOLID 0x09 /* Solid amber */ +#define MLXREG_LED_BLINK_3HZ 167 /* ~167 msec off/on - HW support */ +#define MLXREG_LED_BLINK_6HZ 83 /* ~83 msec off/on - HW support */ +#define MLXREG_LED_CAPABILITY_CLEAR GENMASK(31, 8) /* Clear mask */ + +/** + * struct mlxreg_led_data - led control data: + * + * @data: led configuration data; + * @led_cdev: led class data; + * @base_color: base led color (other colors have constant offset from base); + * @data_parent: pointer to private device control data of parent; + * @led_cdev_name: class device name + */ +struct mlxreg_led_data { + struct mlxreg_core_data *data; + struct led_classdev led_cdev; + u8 base_color; + void *data_parent; + char led_cdev_name[MLXREG_CORE_LABEL_MAX_SIZE]; +}; + +#define cdev_to_priv(c) container_of(c, struct mlxreg_led_data, led_cdev) + +/** + * struct mlxreg_led_priv_data - platform private data: + * + * @pdev: platform device; + * @pdata: platform data; + * @access_lock: mutex for attribute IO access; + */ +struct mlxreg_led_priv_data { + struct platform_device *pdev; + struct mlxreg_core_platform_data *pdata; + struct mutex access_lock; /* protect IO operations */ +}; + +static int +mlxreg_led_store_hw(struct mlxreg_led_data *led_data, u8 vset) +{ + struct mlxreg_led_priv_data *priv = led_data->data_parent; + struct mlxreg_core_platform_data *led_pdata = priv->pdata; + struct mlxreg_core_data *data = led_data->data; + u32 regval; + u32 nib; + int ret; + + /* + * Each LED is controlled through low or high nibble of the relevant + * register byte. Register offset is specified by off parameter. + * Parameter vset provides color code: 0x0 for off, 0x5 for solid red, + * 0x6 for 3Hz blink red, 0xd for solid green, 0xe for 3Hz blink + * green. + * Parameter mask specifies which nibble is used for specific LED: mask + * 0xf0 - lower nibble is to be used (bits from 0 to 3), mask 0x0f - + * higher nibble (bits from 4 to 7). + */ + mutex_lock(&priv->access_lock); + + ret = regmap_read(led_pdata->regmap, data->reg, ®val); + if (ret) + goto access_error; + + nib = (ror32(data->mask, data->bit) == 0xf0) ? rol32(vset, data->bit) : + rol32(vset, data->bit + 4); + regval = (regval & data->mask) | nib; + + ret = regmap_write(led_pdata->regmap, data->reg, regval); + +access_error: + mutex_unlock(&priv->access_lock); + + return ret; +} + +static enum led_brightness +mlxreg_led_get_hw(struct mlxreg_led_data *led_data) +{ + struct mlxreg_led_priv_data *priv = led_data->data_parent; + struct mlxreg_core_platform_data *led_pdata = priv->pdata; + struct mlxreg_core_data *data = led_data->data; + u32 regval; + int err; + + /* + * Each LED is controlled through low or high nibble of the relevant + * register byte. Register offset is specified by off parameter. + * Parameter vset provides color code: 0x0 for off, 0x5 for solid red, + * 0x6 for 3Hz blink red, 0xd for solid green, 0xe for 3Hz blink + * green. + * Parameter mask specifies which nibble is used for specific LED: mask + * 0xf0 - lower nibble is to be used (bits from 0 to 3), mask 0x0f - + * higher nibble (bits from 4 to 7). + */ + err = regmap_read(led_pdata->regmap, data->reg, ®val); + if (err < 0) { + dev_warn(led_data->led_cdev.dev, "Failed to get current brightness, error: %d\n", + err); + /* Assume the LED is OFF */ + return LED_OFF; + } + + regval = regval & ~data->mask; + regval = (ror32(data->mask, data->bit) == 0xf0) ? ror32(regval, + data->bit) : ror32(regval, data->bit + 4); + if (regval >= led_data->base_color && + regval <= (led_data->base_color + MLXREG_LED_OFFSET_BLINK_6HZ)) + return LED_FULL; + + return LED_OFF; +} + +static int +mlxreg_led_brightness_set(struct led_classdev *cled, enum led_brightness value) +{ + struct mlxreg_led_data *led_data = cdev_to_priv(cled); + + if (value) + return mlxreg_led_store_hw(led_data, led_data->base_color); + else + return mlxreg_led_store_hw(led_data, MLXREG_LED_IS_OFF); +} + +static enum led_brightness +mlxreg_led_brightness_get(struct led_classdev *cled) +{ + struct mlxreg_led_data *led_data = cdev_to_priv(cled); + + return mlxreg_led_get_hw(led_data); +} + +static int +mlxreg_led_blink_set(struct led_classdev *cled, unsigned long *delay_on, + unsigned long *delay_off) +{ + struct mlxreg_led_data *led_data = cdev_to_priv(cled); + int err; + + /* + * HW supports two types of blinking: full (6Hz) and half (3Hz). + * For delay on/off zero LED is setting to solid color. For others + * combination blinking is to be controlled by the software timer. + */ + if (!(*delay_on == 0 && *delay_off == 0) && + !(*delay_on == MLXREG_LED_BLINK_3HZ && + *delay_off == MLXREG_LED_BLINK_3HZ) && + !(*delay_on == MLXREG_LED_BLINK_6HZ && + *delay_off == MLXREG_LED_BLINK_6HZ)) + return -EINVAL; + + if (*delay_on == MLXREG_LED_BLINK_6HZ) + err = mlxreg_led_store_hw(led_data, led_data->base_color + + MLXREG_LED_OFFSET_BLINK_6HZ); + else if (*delay_on == MLXREG_LED_BLINK_3HZ) + err = mlxreg_led_store_hw(led_data, led_data->base_color + + MLXREG_LED_OFFSET_BLINK_3HZ); + else + err = mlxreg_led_store_hw(led_data, led_data->base_color); + + return err; +} + +static int mlxreg_led_config(struct mlxreg_led_priv_data *priv) +{ + struct mlxreg_core_platform_data *led_pdata = priv->pdata; + struct mlxreg_core_data *data = led_pdata->data; + struct mlxreg_led_data *led_data; + struct led_classdev *led_cdev; + enum led_brightness brightness; + u32 regval; + int i; + int err; + + for (i = 0; i < led_pdata->counter; i++, data++) { + led_data = devm_kzalloc(&priv->pdev->dev, sizeof(*led_data), + GFP_KERNEL); + if (!led_data) + return -ENOMEM; + + if (data->capability) { + err = regmap_read(led_pdata->regmap, data->capability, + ®val); + if (err) { + dev_err(&priv->pdev->dev, "Failed to query capability register\n"); + return err; + } + if (!(regval & data->bit)) + continue; + /* + * Field "bit" can contain one capability bit in 0 byte + * and offset bit in 1-3 bytes. Clear capability bit and + * keep only offset bit. + */ + data->bit &= MLXREG_LED_CAPABILITY_CLEAR; + } + + led_cdev = &led_data->led_cdev; + led_data->data_parent = priv; + if (strstr(data->label, "red") || + strstr(data->label, "orange")) { + brightness = LED_OFF; + led_data->base_color = MLXREG_LED_RED_SOLID; + } else if (strstr(data->label, "amber")) { + brightness = LED_OFF; + led_data->base_color = MLXREG_LED_AMBER_SOLID; + } else { + brightness = LED_OFF; + led_data->base_color = MLXREG_LED_GREEN_SOLID; + } + snprintf(led_data->led_cdev_name, sizeof(led_data->led_cdev_name), + "mlxreg:%s", data->label); + led_cdev->name = led_data->led_cdev_name; + led_cdev->brightness = brightness; + led_cdev->max_brightness = LED_ON; + led_cdev->brightness_set_blocking = + mlxreg_led_brightness_set; + led_cdev->brightness_get = mlxreg_led_brightness_get; + led_cdev->blink_set = mlxreg_led_blink_set; + led_cdev->flags = LED_CORE_SUSPENDRESUME; + led_data->data = data; + err = devm_led_classdev_register(&priv->pdev->dev, led_cdev); + if (err) + return err; + + if (led_cdev->brightness) + mlxreg_led_brightness_set(led_cdev, + led_cdev->brightness); + dev_info(led_cdev->dev, "label: %s, mask: 0x%02x, offset:0x%02x\n", + data->label, data->mask, data->reg); + } + + return 0; +} + +static int mlxreg_led_probe(struct platform_device *pdev) +{ + struct mlxreg_core_platform_data *led_pdata; + struct mlxreg_led_priv_data *priv; + int err; + + led_pdata = dev_get_platdata(&pdev->dev); + if (!led_pdata) { + dev_err(&pdev->dev, "Failed to get platform data.\n"); + return -EINVAL; + } + + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + err = devm_mutex_init(&pdev->dev, &priv->access_lock); + if (err) + return err; + + priv->pdev = pdev; + priv->pdata = led_pdata; + + return mlxreg_led_config(priv); +} + +static struct platform_driver mlxreg_led_driver = { + .driver = { + .name = "leds-mlxreg", + }, + .probe = mlxreg_led_probe, +}; + +module_platform_driver(mlxreg_led_driver); + +MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>"); +MODULE_DESCRIPTION("Mellanox LED regmap driver"); +MODULE_LICENSE("Dual BSD/GPL"); +MODULE_ALIAS("platform:leds-mlxreg"); |
