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/regulator/rt4803.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/regulator/rt4803.c')
| -rw-r--r-- | drivers/regulator/rt4803.c | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/drivers/regulator/rt4803.c b/drivers/regulator/rt4803.c new file mode 100644 index 000000000..34cd3f249 --- /dev/null +++ b/drivers/regulator/rt4803.c @@ -0,0 +1,215 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 Richtek Technology Corp. + * + * Author: ChiYuan Huang <cy_huang@richtek.com> + */ + +#include <linux/i2c.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/property.h> +#include <linux/regmap.h> +#include <linux/regulator/driver.h> +#include <linux/regulator/of_regulator.h> + +#define RT4803_AUTO_MODE 1 +#define RT4803_FPWM_MODE 2 + +#define RT4803_REG_CONFIG 0x01 +#define RT4803_REG_VSELL 0x02 +#define RT4803_REG_VSELH 0x03 +#define RT4803_REG_ILIM 0x04 +#define RT4803_REG_STAT 0x05 + +#define RT4803_MODE_MASK GENMASK(1, 0) +#define RT4803_VSEL_MASK GENMASK(4, 0) +#define RT4803_ILIM_MASK GENMASK(3, 0) +#define RT4803_TSD_MASK BIT(7) +#define RT4803_HOTDIE_MASK BIT(6) +#define RT4803_FAULT_MASK BIT(1) +#define RT4803_PGOOD_MASK BIT(0) + +#define RT4803_VOUT_MINUV 2850000 +#define RT4803_VOUT_STEPUV 50000 +#define RT4803_VOUT_NUM 32 + +static int rt4803_set_mode(struct regulator_dev *rdev, unsigned int mode) +{ + struct regmap *regmap = rdev_get_regmap(rdev); + unsigned int modeval; + + switch (mode) { + case REGULATOR_MODE_NORMAL: + modeval = RT4803_AUTO_MODE; + break; + case REGULATOR_MODE_FAST: + modeval = RT4803_FPWM_MODE; + break; + default: + return -EINVAL; + } + + modeval <<= ffs(RT4803_MODE_MASK) - 1; + + return regmap_update_bits(regmap, RT4803_REG_CONFIG, RT4803_MODE_MASK, modeval); +} + +static unsigned int rt4803_get_mode(struct regulator_dev *rdev) +{ + struct regmap *regmap = rdev_get_regmap(rdev); + unsigned int modeval; + int ret; + + ret = regmap_read(regmap, RT4803_REG_CONFIG, &modeval); + if (ret) + return REGULATOR_MODE_INVALID; + + modeval >>= ffs(RT4803_MODE_MASK) - 1; + + switch (modeval) { + case RT4803_AUTO_MODE: + return REGULATOR_MODE_NORMAL; + case RT4803_FPWM_MODE: + return REGULATOR_MODE_FAST; + default: + return REGULATOR_MODE_INVALID; + } +} + +static int rt4803_get_error_flags(struct regulator_dev *rdev, unsigned int *flags) +{ + struct regmap *regmap = rdev_get_regmap(rdev); + unsigned int state, events = 0; + int ret; + + ret = regmap_read(regmap, RT4803_REG_STAT, &state); + if (ret) + return ret; + + if (state & RT4803_PGOOD_MASK) + goto out_error_flag; + + if (state & RT4803_FAULT_MASK) + events |= REGULATOR_ERROR_FAIL; + + if (state & RT4803_HOTDIE_MASK) + events |= REGULATOR_ERROR_OVER_TEMP_WARN; + + if (state & RT4803_TSD_MASK) + events |= REGULATOR_ERROR_OVER_TEMP; + +out_error_flag: + *flags = events; + return 0; +} + +static int rt4803_set_suspend_voltage(struct regulator_dev *rdev, int uV) +{ + struct regmap *regmap = rdev_get_regmap(rdev); + unsigned int reg, vsel; + + if (rdev->desc->vsel_reg == RT4803_REG_VSELL) + reg = RT4803_REG_VSELH; + else + reg = RT4803_REG_VSELL; + + vsel = (uV - rdev->desc->min_uV) / rdev->desc->uV_step; + vsel <<= ffs(RT4803_VSEL_MASK) - 1; + + return regmap_update_bits(regmap, reg, RT4803_VSEL_MASK, vsel); +} + +static const struct regulator_ops rt4803_regulator_ops = { + .list_voltage = regulator_list_voltage_linear, + .set_voltage_sel = regulator_set_voltage_sel_regmap, + .get_voltage_sel = regulator_get_voltage_sel_regmap, + .set_mode = rt4803_set_mode, + .get_mode = rt4803_get_mode, + .get_error_flags = rt4803_get_error_flags, + .set_suspend_voltage = rt4803_set_suspend_voltage, +}; + +static unsigned int rt4803_of_map_mode(unsigned int mode) +{ + switch (mode) { + case RT4803_AUTO_MODE: + return REGULATOR_MODE_NORMAL; + case RT4803_FPWM_MODE: + return REGULATOR_MODE_FAST; + default: + return REGULATOR_MODE_INVALID; + } +} + +static const struct regmap_config rt4803_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = RT4803_REG_STAT, +}; + +static int rt4803_probe(struct i2c_client *i2c) +{ + struct device *dev = &i2c->dev; + struct regmap *regmap; + struct regulator_desc *desc; + struct regulator_config cfg = {}; + struct regulator_dev *rdev; + bool vsel_act_high; + int ret; + + desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL); + if (!desc) + return -ENOMEM; + + regmap = devm_regmap_init_i2c(i2c, &rt4803_regmap_config); + if (IS_ERR(regmap)) + return dev_err_probe(dev, PTR_ERR(regmap), "Failed to init regmap\n"); + + /* Always configure the input current limit to max 5A at initial */ + ret = regmap_update_bits(regmap, RT4803_REG_ILIM, RT4803_ILIM_MASK, 0xff); + if (ret) + return dev_err_probe(dev, ret, "Failed to config ILIM to max\n"); + + vsel_act_high = device_property_read_bool(dev, "richtek,vsel-active-high"); + + desc->name = "rt4803-regulator"; + desc->type = REGULATOR_VOLTAGE; + desc->owner = THIS_MODULE; + desc->ops = &rt4803_regulator_ops; + desc->min_uV = RT4803_VOUT_MINUV; + desc->uV_step = RT4803_VOUT_STEPUV; + desc->n_voltages = RT4803_VOUT_NUM; + desc->vsel_mask = RT4803_VSEL_MASK; + desc->of_map_mode = rt4803_of_map_mode; + if (vsel_act_high) + desc->vsel_reg = RT4803_REG_VSELH; + else + desc->vsel_reg = RT4803_REG_VSELL; + + cfg.dev = dev; + cfg.of_node = dev_of_node(dev); + cfg.init_data = of_get_regulator_init_data(dev, dev_of_node(dev), desc); + + rdev = devm_regulator_register(dev, desc, &cfg); + return PTR_ERR_OR_ZERO(rdev); +} + +static const struct of_device_id rt4803_device_match_table[] = { + { .compatible = "richtek,rt4803" }, + {} +}; +MODULE_DEVICE_TABLE(of, rt4803_device_match_table); + +static struct i2c_driver rt4803_driver = { + .driver = { + .name = "rt4803", + .of_match_table = rt4803_device_match_table, + }, + .probe = rt4803_probe, +}; +module_i2c_driver(rt4803_driver); + +MODULE_DESCRIPTION("Richtek RT4803 voltage regulator driver"); +MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>"); +MODULE_LICENSE("GPL"); |
