summaryrefslogtreecommitdiffstats
path: root/sound/soc/soc-utils-test.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 /sound/soc/soc-utils-test.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 'sound/soc/soc-utils-test.c')
-rw-r--r--sound/soc/soc-utils-test.c232
1 files changed, 232 insertions, 0 deletions
diff --git a/sound/soc/soc-utils-test.c b/sound/soc/soc-utils-test.c
new file mode 100644
index 000000000..616d2c926
--- /dev/null
+++ b/sound/soc/soc-utils-test.c
@@ -0,0 +1,232 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (C) 2022 Cirrus Logic, Inc. and
+// Cirrus Logic International Semiconductor Ltd.
+
+#include <kunit/test.h>
+#include <linux/module.h>
+#include <sound/pcm.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <uapi/sound/asound.h>
+
+static const struct {
+ u32 rate;
+ snd_pcm_format_t fmt;
+ u8 channels;
+ u8 tdm_width;
+ u8 tdm_slots;
+ u8 slot_multiple;
+ u32 bclk;
+} tdm_params_to_bclk_cases[] = {
+ /* rate fmt channels tdm_width tdm_slots slot_multiple bclk */
+
+ /* From params only */
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 1, 0, 0, 0, 128000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 2, 0, 0, 0, 256000 },
+ { 8000, SNDRV_PCM_FORMAT_S24_LE, 1, 0, 0, 0, 192000 },
+ { 8000, SNDRV_PCM_FORMAT_S24_LE, 2, 0, 0, 0, 384000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 1, 0, 0, 0, 256000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 2, 0, 0, 0, 512000 },
+ { 44100, SNDRV_PCM_FORMAT_S16_LE, 1, 0, 0, 0, 705600 },
+ { 44100, SNDRV_PCM_FORMAT_S16_LE, 2, 0, 0, 0, 1411200 },
+ { 44100, SNDRV_PCM_FORMAT_S24_LE, 1, 0, 0, 0, 1058400 },
+ { 44100, SNDRV_PCM_FORMAT_S24_LE, 2, 0, 0, 0, 2116800 },
+ { 44100, SNDRV_PCM_FORMAT_S32_LE, 1, 0, 0, 0, 1411200 },
+ { 44100, SNDRV_PCM_FORMAT_S32_LE, 2, 0, 0, 0, 2822400 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 1, 0, 0, 0, 6144000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 2, 0, 0, 0, 12288000 },
+ { 384000, SNDRV_PCM_FORMAT_S24_LE, 1, 0, 0, 0, 9216000 },
+ { 384000, SNDRV_PCM_FORMAT_S24_LE, 2, 0, 0, 0, 18432000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 1, 0, 0, 0, 12288000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 2, 0, 0, 0, 24576000 },
+
+ /* I2S from params */
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 1, 0, 0, 2, 256000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 2, 0, 0, 2, 256000 },
+ { 8000, SNDRV_PCM_FORMAT_S24_LE, 1, 0, 0, 2, 384000 },
+ { 8000, SNDRV_PCM_FORMAT_S24_LE, 2, 0, 0, 2, 384000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 1, 0, 0, 2, 512000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 2, 0, 0, 2, 512000 },
+ { 44100, SNDRV_PCM_FORMAT_S16_LE, 1, 0, 0, 2, 1411200 },
+ { 44100, SNDRV_PCM_FORMAT_S16_LE, 2, 0, 0, 2, 1411200 },
+ { 44100, SNDRV_PCM_FORMAT_S24_LE, 1, 0, 0, 2, 2116800 },
+ { 44100, SNDRV_PCM_FORMAT_S24_LE, 2, 0, 0, 2, 2116800 },
+ { 44100, SNDRV_PCM_FORMAT_S32_LE, 1, 0, 0, 2, 2822400 },
+ { 44100, SNDRV_PCM_FORMAT_S32_LE, 2, 0, 0, 2, 2822400 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 1, 0, 0, 2, 12288000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 2, 0, 0, 2, 12288000 },
+ { 384000, SNDRV_PCM_FORMAT_S24_LE, 1, 0, 0, 2, 18432000 },
+ { 384000, SNDRV_PCM_FORMAT_S24_LE, 2, 0, 0, 2, 18432000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 1, 0, 0, 2, 24576000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 2, 0, 0, 2, 24576000 },
+
+ /* Fixed 8-slot TDM, other values from params */
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 1, 0, 8, 0, 1024000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 2, 0, 8, 0, 1024000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 3, 0, 8, 0, 1024000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 4, 0, 8, 0, 1024000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 1, 0, 8, 0, 2048000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 2, 0, 8, 0, 2048000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 3, 0, 8, 0, 2048000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 4, 0, 8, 0, 2048000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 1, 0, 8, 0, 49152000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 2, 0, 8, 0, 49152000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 3, 0, 8, 0, 49152000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 4, 0, 8, 0, 49152000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 1, 0, 8, 0, 98304000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 2, 0, 8, 0, 98304000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 3, 0, 8, 0, 98304000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 4, 0, 8, 0, 98304000 },
+
+ /* Fixed 32-bit TDM, other values from params */
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 1, 32, 0, 0, 256000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 2, 32, 0, 0, 512000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 3, 32, 0, 0, 768000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 4, 32, 0, 0, 1024000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 1, 32, 0, 0, 256000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 2, 32, 0, 0, 512000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 3, 32, 0, 0, 768000 },
+ { 8000, SNDRV_PCM_FORMAT_S32_LE, 4, 32, 0, 0, 1024000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 1, 32, 0, 0, 12288000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 2, 32, 0, 0, 24576000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 3, 32, 0, 0, 36864000 },
+ { 384000, SNDRV_PCM_FORMAT_S16_LE, 4, 32, 0, 0, 49152000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 1, 32, 0, 0, 12288000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 2, 32, 0, 0, 24576000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 3, 32, 0, 0, 36864000 },
+ { 384000, SNDRV_PCM_FORMAT_S32_LE, 4, 32, 0, 0, 49152000 },
+
+ /* Fixed 6-slot 24-bit TDM, other values from params */
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 1, 24, 6, 0, 1152000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 2, 24, 6, 0, 1152000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 3, 24, 6, 0, 1152000 },
+ { 8000, SNDRV_PCM_FORMAT_S16_LE, 4, 24, 6, 0, 1152000 },
+ { 8000, SNDRV_PCM_FORMAT_S24_LE, 1, 24, 6, 0, 1152000 },
+ { 8000, SNDRV_PCM_FORMAT_S24_LE, 2, 24, 6, 0, 1152000 },
+ { 8000, SNDRV_PCM_FORMAT_S24_LE, 3, 24, 6, 0, 1152000 },
+ { 8000, SNDRV_PCM_FORMAT_S24_LE, 4, 24, 6, 0, 1152000 },
+ { 192000, SNDRV_PCM_FORMAT_S16_LE, 1, 24, 6, 0, 27648000 },
+ { 192000, SNDRV_PCM_FORMAT_S16_LE, 2, 24, 6, 0, 27648000 },
+ { 192000, SNDRV_PCM_FORMAT_S16_LE, 3, 24, 6, 0, 27648000 },
+ { 192000, SNDRV_PCM_FORMAT_S16_LE, 4, 24, 6, 0, 27648000 },
+ { 192000, SNDRV_PCM_FORMAT_S24_LE, 1, 24, 6, 0, 27648000 },
+ { 192000, SNDRV_PCM_FORMAT_S24_LE, 2, 24, 6, 0, 27648000 },
+ { 192000, SNDRV_PCM_FORMAT_S24_LE, 3, 24, 6, 0, 27648000 },
+ { 192000, SNDRV_PCM_FORMAT_S24_LE, 4, 24, 6, 0, 27648000 },
+};
+
+static void test_tdm_params_to_bclk_one(struct kunit *test,
+ unsigned int rate, snd_pcm_format_t fmt,
+ unsigned int channels,
+ unsigned int tdm_width, unsigned int tdm_slots,
+ unsigned int slot_multiple,
+ unsigned int expected_bclk)
+{
+ struct snd_pcm_hw_params params;
+ int got_bclk;
+
+ _snd_pcm_hw_params_any(&params);
+ snd_mask_none(hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT));
+ hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->min = rate;
+ hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->max = rate;
+ hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->min = channels;
+ hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->max = channels;
+ params_set_format(&params, fmt);
+
+ got_bclk = snd_soc_tdm_params_to_bclk(&params, tdm_width, tdm_slots, slot_multiple);
+ pr_debug("%s: r=%u sb=%u ch=%u tw=%u ts=%u sm=%u expected=%u got=%d\n",
+ __func__,
+ rate, params_width(&params), channels, tdm_width, tdm_slots, slot_multiple,
+ expected_bclk, got_bclk);
+ KUNIT_ASSERT_EQ(test, expected_bclk, (unsigned int)got_bclk);
+}
+
+static void test_tdm_params_to_bclk(struct kunit *test)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(tdm_params_to_bclk_cases); ++i) {
+ test_tdm_params_to_bclk_one(test,
+ tdm_params_to_bclk_cases[i].rate,
+ tdm_params_to_bclk_cases[i].fmt,
+ tdm_params_to_bclk_cases[i].channels,
+ tdm_params_to_bclk_cases[i].tdm_width,
+ tdm_params_to_bclk_cases[i].tdm_slots,
+ tdm_params_to_bclk_cases[i].slot_multiple,
+ tdm_params_to_bclk_cases[i].bclk);
+
+ if (tdm_params_to_bclk_cases[i].slot_multiple > 0)
+ continue;
+
+ /* Slot multiple 1 should have the same effect as multiple 0 */
+ test_tdm_params_to_bclk_one(test,
+ tdm_params_to_bclk_cases[i].rate,
+ tdm_params_to_bclk_cases[i].fmt,
+ tdm_params_to_bclk_cases[i].channels,
+ tdm_params_to_bclk_cases[i].tdm_width,
+ tdm_params_to_bclk_cases[i].tdm_slots,
+ 1,
+ tdm_params_to_bclk_cases[i].bclk);
+ }
+}
+
+static void test_snd_soc_params_to_bclk_one(struct kunit *test,
+ unsigned int rate, snd_pcm_format_t fmt,
+ unsigned int channels,
+ unsigned int expected_bclk)
+{
+ struct snd_pcm_hw_params params;
+ int got_bclk;
+
+ _snd_pcm_hw_params_any(&params);
+ snd_mask_none(hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT));
+ hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->min = rate;
+ hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->max = rate;
+ hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->min = channels;
+ hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->max = channels;
+ params_set_format(&params, fmt);
+
+ got_bclk = snd_soc_params_to_bclk(&params);
+ pr_debug("%s: r=%u sb=%u ch=%u expected=%u got=%d\n",
+ __func__,
+ rate, params_width(&params), channels, expected_bclk, got_bclk);
+ KUNIT_ASSERT_EQ(test, expected_bclk, (unsigned int)got_bclk);
+}
+
+static void test_snd_soc_params_to_bclk(struct kunit *test)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(tdm_params_to_bclk_cases); ++i) {
+ /*
+ * snd_soc_params_to_bclk() is all the test cases where
+ * snd_pcm_hw_params values are not overridden.
+ */
+ if (tdm_params_to_bclk_cases[i].tdm_width |
+ tdm_params_to_bclk_cases[i].tdm_slots |
+ tdm_params_to_bclk_cases[i].slot_multiple)
+ continue;
+
+ test_snd_soc_params_to_bclk_one(test,
+ tdm_params_to_bclk_cases[i].rate,
+ tdm_params_to_bclk_cases[i].fmt,
+ tdm_params_to_bclk_cases[i].channels,
+ tdm_params_to_bclk_cases[i].bclk);
+ }
+}
+
+static struct kunit_case soc_utils_test_cases[] = {
+ KUNIT_CASE(test_tdm_params_to_bclk),
+ KUNIT_CASE(test_snd_soc_params_to_bclk),
+ {}
+};
+
+static struct kunit_suite soc_utils_test_suite = {
+ .name = "soc-utils",
+ .test_cases = soc_utils_test_cases,
+};
+
+kunit_test_suites(&soc_utils_test_suite);
+
+MODULE_DESCRIPTION("ASoC soc-utils kunit test");
+MODULE_LICENSE("GPL");