summaryrefslogtreecommitdiffstats
path: root/drivers/regulator/rt5739.c
diff options
context:
space:
mode:
authorJinke Han <jinkehan@didiglobal.com>2026-09-08 15:37:42 +0800
committerIngo Molnar <mingo@kernel.org>2026-09-17 09:48:42 +0200
commita5f7a5bb3b7f28ba7e4fa246775b29a0e5537255 (patch)
tree73fef824f9a846cf935869d0dbc5070ef6e2f741 /drivers/regulator/rt5739.c
downloadlinux-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/rt5739.c')
-rw-r--r--drivers/regulator/rt5739.c321
1 files changed, 321 insertions, 0 deletions
diff --git a/drivers/regulator/rt5739.c b/drivers/regulator/rt5739.c
new file mode 100644
index 000000000..00706cba9
--- /dev/null
+++ b/drivers/regulator/rt5739.c
@@ -0,0 +1,321 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Device driver for RT5739 regulator
+ *
+ * Copyright (C) 2023 Richtek Technology Corp.
+ *
+ * Author: ChiYuan Huang <cy_huang@richtek.com>
+ */
+
+#include <linux/bits.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/property.h>
+#include <linux/regmap.h>
+#include <linux/regulator/driver.h>
+#include <linux/regulator/of_regulator.h>
+
+#define RT5739_AUTO_MODE 0
+#define RT5739_FPWM_MODE 1
+
+#define RT5739_REG_NSEL0 0x00
+#define RT5739_REG_NSEL1 0x01
+#define RT5739_REG_CNTL1 0x02
+#define RT5739_REG_ID1 0x03
+#define RT5739_REG_ID2 0x04
+#define RT5739_REG_MON 0x05
+#define RT5739_REG_CNTL2 0x06
+#define RT5739_REG_CNTL4 0x08
+
+#define RT5739_VSEL_MASK GENMASK(7, 0)
+#define RT5739_MODEVSEL1_MASK BIT(1)
+#define RT5739_MODEVSEL0_MASK BIT(0)
+#define RT5739_VID_MASK GENMASK(7, 5)
+#define RT5739_DID_MASK GENMASK(3, 0)
+#define RT5739_ACTD_MASK BIT(7)
+#define RT5739_ENVSEL1_MASK BIT(1)
+#define RT5739_ENVSEL0_MASK BIT(0)
+
+#define RT5733_CHIPDIE_ID 0x1
+#define RT5733_VOLT_MINUV 270000
+#define RT5733_VOLT_MAXUV 1401250
+#define RT5733_VOLT_STPUV 6250
+#define RT5733_N_VOLTS 182
+
+#define RT5739_VOLT_MINUV 300000
+#define RT5739_VOLT_MAXUV 1300000
+#define RT5739_VOLT_STPUV 5000
+#define RT5739_N_VOLTS 201
+#define RT5739_I2CRDY_TIMEUS 1000
+
+static int rt5739_set_mode(struct regulator_dev *rdev, unsigned int mode)
+{
+ const struct regulator_desc *desc = rdev->desc;
+ struct regmap *regmap = rdev_get_regmap(rdev);
+ unsigned int mask, val;
+
+ if (desc->vsel_reg == RT5739_REG_NSEL0)
+ mask = RT5739_MODEVSEL0_MASK;
+ else
+ mask = RT5739_MODEVSEL1_MASK;
+
+ switch (mode) {
+ case REGULATOR_MODE_FAST:
+ val = mask;
+ break;
+ case REGULATOR_MODE_NORMAL:
+ val = 0;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return regmap_update_bits(regmap, RT5739_REG_CNTL1, mask, val);
+}
+
+static unsigned int rt5739_get_mode(struct regulator_dev *rdev)
+{
+ const struct regulator_desc *desc = rdev->desc;
+ struct regmap *regmap = rdev_get_regmap(rdev);
+ unsigned int mask, val;
+ int ret;
+
+ if (desc->vsel_reg == RT5739_REG_NSEL0)
+ mask = RT5739_MODEVSEL0_MASK;
+ else
+ mask = RT5739_MODEVSEL1_MASK;
+
+ ret = regmap_read(regmap, RT5739_REG_CNTL1, &val);
+ if (ret)
+ return REGULATOR_MODE_INVALID;
+
+ if (val & mask)
+ return REGULATOR_MODE_FAST;
+
+ return REGULATOR_MODE_NORMAL;
+}
+
+static int rt5739_set_suspend_voltage(struct regulator_dev *rdev, int uV)
+{
+ const struct regulator_desc *desc = rdev->desc;
+ struct regmap *regmap = rdev_get_regmap(rdev);
+ unsigned int reg, vsel;
+ int max_uV;
+
+ max_uV = desc->min_uV + desc->uV_step * (desc->n_voltages - 1);
+
+ if (uV < desc->min_uV || uV > max_uV)
+ return -EINVAL;
+
+ if (desc->vsel_reg == RT5739_REG_NSEL0)
+ reg = RT5739_REG_NSEL1;
+ else
+ reg = RT5739_REG_NSEL0;
+
+ vsel = (uV - desc->min_uV) / desc->uV_step;
+ return regmap_write(regmap, reg, vsel);
+}
+
+static int rt5739_set_suspend_enable(struct regulator_dev *rdev)
+{
+ const struct regulator_desc *desc = rdev->desc;
+ struct regmap *regmap = rdev_get_regmap(rdev);
+ unsigned int mask;
+
+ if (desc->vsel_reg == RT5739_REG_NSEL0)
+ mask = RT5739_ENVSEL1_MASK;
+ else
+ mask = RT5739_ENVSEL0_MASK;
+
+ return regmap_update_bits(regmap, desc->enable_reg, mask, mask);
+}
+
+static int rt5739_set_suspend_disable(struct regulator_dev *rdev)
+{
+ const struct regulator_desc *desc = rdev->desc;
+ struct regmap *regmap = rdev_get_regmap(rdev);
+ unsigned int mask;
+
+ if (desc->vsel_reg == RT5739_REG_NSEL0)
+ mask = RT5739_ENVSEL1_MASK;
+ else
+ mask = RT5739_ENVSEL0_MASK;
+
+ return regmap_update_bits(regmap, desc->enable_reg, mask, 0);
+}
+
+static int rt5739_set_suspend_mode(struct regulator_dev *rdev,
+ unsigned int mode)
+{
+ const struct regulator_desc *desc = rdev->desc;
+ struct regmap *regmap = rdev_get_regmap(rdev);
+ unsigned int mask, val;
+
+ if (desc->vsel_reg == RT5739_REG_NSEL0)
+ mask = RT5739_MODEVSEL1_MASK;
+ else
+ mask = RT5739_MODEVSEL0_MASK;
+
+ switch (mode) {
+ case REGULATOR_MODE_FAST:
+ val = mask;
+ break;
+ case REGULATOR_MODE_NORMAL:
+ val = 0;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return regmap_update_bits(regmap, RT5739_REG_CNTL1, mask, val);
+}
+
+static const struct regulator_ops rt5739_regulator_ops = {
+ .list_voltage = regulator_list_voltage_linear,
+ .get_voltage_sel = regulator_get_voltage_sel_regmap,
+ .set_voltage_sel = regulator_set_voltage_sel_regmap,
+ .enable = regulator_enable_regmap,
+ .disable = regulator_disable_regmap,
+ .is_enabled = regulator_is_enabled_regmap,
+ .set_active_discharge = regulator_set_active_discharge_regmap,
+ .set_mode = rt5739_set_mode,
+ .get_mode = rt5739_get_mode,
+ .set_suspend_voltage = rt5739_set_suspend_voltage,
+ .set_suspend_enable = rt5739_set_suspend_enable,
+ .set_suspend_disable = rt5739_set_suspend_disable,
+ .set_suspend_mode = rt5739_set_suspend_mode,
+};
+
+static unsigned int rt5739_of_map_mode(unsigned int mode)
+{
+ switch (mode) {
+ case RT5739_AUTO_MODE:
+ return REGULATOR_MODE_NORMAL;
+ case RT5739_FPWM_MODE:
+ return REGULATOR_MODE_FAST;
+ default:
+ return REGULATOR_MODE_INVALID;
+ }
+}
+
+static void rt5739_init_regulator_desc(struct regulator_desc *desc,
+ bool vsel_active_high, u8 did)
+{
+ /* Fixed */
+ desc->name = "rt5739-regulator";
+ desc->owner = THIS_MODULE;
+ desc->ops = &rt5739_regulator_ops;
+ desc->vsel_mask = RT5739_VSEL_MASK;
+ desc->enable_reg = RT5739_REG_CNTL2;
+ desc->active_discharge_reg = RT5739_REG_CNTL1;
+ desc->active_discharge_mask = RT5739_ACTD_MASK;
+ desc->active_discharge_on = RT5739_ACTD_MASK;
+ desc->of_map_mode = rt5739_of_map_mode;
+
+ /* Assigned by vsel level */
+ if (vsel_active_high) {
+ desc->vsel_reg = RT5739_REG_NSEL1;
+ desc->enable_mask = RT5739_ENVSEL1_MASK;
+ } else {
+ desc->vsel_reg = RT5739_REG_NSEL0;
+ desc->enable_mask = RT5739_ENVSEL0_MASK;
+ }
+
+ /* Assigned by CHIPDIE ID */
+ switch (did) {
+ case RT5733_CHIPDIE_ID:
+ desc->n_voltages = RT5733_N_VOLTS;
+ desc->min_uV = RT5733_VOLT_MINUV;
+ desc->uV_step = RT5733_VOLT_STPUV;
+ break;
+ default:
+ desc->n_voltages = RT5739_N_VOLTS;
+ desc->min_uV = RT5739_VOLT_MINUV;
+ desc->uV_step = RT5739_VOLT_STPUV;
+ break;
+ }
+}
+
+static bool rt5739_volatile_reg(struct device *dev, unsigned int reg)
+{
+ return reg == RT5739_REG_MON;
+}
+
+static const struct regmap_config rt5739_regmap_config = {
+ .name = "rt5739",
+ .reg_bits = 8,
+ .val_bits = 8,
+ .max_register = RT5739_REG_CNTL4,
+ .cache_type = REGCACHE_MAPLE,
+ .volatile_reg = rt5739_volatile_reg,
+};
+
+static int rt5739_probe(struct i2c_client *i2c)
+{
+ struct device *dev = &i2c->dev;
+ struct regulator_desc *desc;
+ struct regmap *regmap;
+ struct gpio_desc *enable_gpio;
+ struct regulator_config cfg = {};
+ struct regulator_dev *rdev;
+ bool vsel_acth;
+ unsigned int vid;
+ int ret;
+
+ desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
+ if (!desc)
+ return -ENOMEM;
+
+ enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
+ if (IS_ERR(enable_gpio))
+ return dev_err_probe(dev, PTR_ERR(enable_gpio), "Failed to get 'enable' gpio\n");
+ else if (enable_gpio)
+ usleep_range(RT5739_I2CRDY_TIMEUS, RT5739_I2CRDY_TIMEUS + 1000);
+
+ regmap = devm_regmap_init_i2c(i2c, &rt5739_regmap_config);
+ if (IS_ERR(regmap))
+ return dev_err_probe(dev, PTR_ERR(regmap), "Failed to init regmap\n");
+
+ ret = regmap_read(regmap, RT5739_REG_ID1, &vid);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to read VID\n");
+
+ /* RT5739: (VID & MASK) must be 0 */
+ if (vid & RT5739_VID_MASK)
+ return dev_err_probe(dev, -ENODEV, "Incorrect VID (0x%02x)\n", vid);
+
+ vsel_acth = device_property_read_bool(dev, "richtek,vsel-active-high");
+
+ rt5739_init_regulator_desc(desc, vsel_acth, vid & RT5739_DID_MASK);
+
+ 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);
+ if (IS_ERR(rdev))
+ return dev_err_probe(dev, PTR_ERR(rdev), "Failed to register regulator\n");
+
+ return 0;
+}
+
+static const struct of_device_id rt5739_device_table[] = {
+ { .compatible = "richtek,rt5733" },
+ { .compatible = "richtek,rt5739" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, rt5739_device_table);
+
+static struct i2c_driver rt5739_driver = {
+ .driver = {
+ .name = "rt5739",
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
+ .of_match_table = rt5739_device_table,
+ },
+ .probe = rt5739_probe,
+};
+module_i2c_driver(rt5739_driver);
+
+MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
+MODULE_DESCRIPTION("Richtek RT5739 regulator driver");
+MODULE_LICENSE("GPL");