All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tracing: Optimize event type allocation with IDA
@ 2022-11-09  3:23 Zheng Yejian
  2022-11-09 13:26 ` Masami Hiramatsu
  2022-11-16 15:50 ` kernel test robot
  0 siblings, 2 replies; 6+ messages in thread
From: Zheng Yejian @ 2022-11-09  3:23 UTC (permalink / raw)
  To: rostedt, mhiramat; +Cc: linux-kernel, bpf, zhengyejian1

After commit 060fa5c83e67 ("tracing/events: reuse trace event ids after
 overflow"), trace events with dynamic type are linked up in list
'ftrace_event_list' through field 'trace_event.list'. Then when max
event type number used up, it's possible to reuse type number of some
freed one by traversing 'ftrace_event_list'.

As instead, using IDA to manage available type numbers can make codes
simpler and then the field 'trace_event.list' can be dropped.

Since 'struct trace_event' is used in static tracepoints, drop
'trace_event.list' can make vmlinux smaller. Local test with about 2000
tracepoints, vmlinux reduced about 64KB:
  before:-rwxrwxr-x 1 root root 76669448 Nov  8 17:14 vmlinux
  after: -rwxrwxr-x 1 root root 76604176 Nov  8 17:15 vmlinux

Signed-off-by: Zheng Yejian <zhengyejian1@huawei.com>
---
 include/linux/trace_events.h |  1 -
 kernel/trace/trace_output.c  | 65 +++++++++---------------------------
 2 files changed, 15 insertions(+), 51 deletions(-)

diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
index 20749bd9db71..bb2053246d6a 100644
--- a/include/linux/trace_events.h
+++ b/include/linux/trace_events.h
@@ -136,7 +136,6 @@ struct trace_event_functions {
 
 struct trace_event {
 	struct hlist_node		node;
-	struct list_head		list;
 	int				type;
 	struct trace_event_functions	*funcs;
 };
diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
index 67f47ea27921..314d175dee3a 100644
--- a/kernel/trace/trace_output.c
+++ b/kernel/trace/trace_output.c
@@ -21,8 +21,6 @@ DECLARE_RWSEM(trace_event_sem);
 
 static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
 
-static int next_event_type = __TRACE_LAST_TYPE;
-
 enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
 {
 	struct trace_seq *s = &iter->seq;
@@ -688,38 +686,23 @@ struct trace_event *ftrace_find_event(int type)
 	return NULL;
 }
 
-static LIST_HEAD(ftrace_event_list);
+static DEFINE_IDA(trace_event_ida);
 
-static int trace_search_list(struct list_head **list)
+static void free_trace_event_type(int type)
 {
-	struct trace_event *e = NULL, *iter;
-	int next = __TRACE_LAST_TYPE;
-
-	if (list_empty(&ftrace_event_list)) {
-		*list = &ftrace_event_list;
-		return next;
-	}
+	if (type >= __TRACE_LAST_TYPE)
+		ida_free(&trace_event_ida, type);
+}
 
-	/*
-	 * We used up all possible max events,
-	 * lets see if somebody freed one.
-	 */
-	list_for_each_entry(iter, &ftrace_event_list, list) {
-		if (iter->type != next) {
-			e = iter;
-			break;
-		}
-		next++;
-	}
+static int alloc_trace_event_type(void)
+{
+	int next;
 
-	/* Did we used up all 65 thousand events??? */
-	if (next > TRACE_EVENT_TYPE_MAX)
+	/* Skip static defined type numbers */
+	next = ida_alloc_range(&trace_event_ida, __TRACE_LAST_TYPE,
+			       TRACE_EVENT_TYPE_MAX, GFP_KERNEL);
+	if (next < 0)
 		return 0;
-
-	if (e)
-		*list = &e->list;
-	else
-		*list = &ftrace_event_list;
 	return next;
 }
 
@@ -761,28 +744,10 @@ int register_trace_event(struct trace_event *event)
 	if (WARN_ON(!event->funcs))
 		goto out;
 
-	INIT_LIST_HEAD(&event->list);
-
 	if (!event->type) {
-		struct list_head *list = NULL;
-
-		if (next_event_type > TRACE_EVENT_TYPE_MAX) {
-
-			event->type = trace_search_list(&list);
-			if (!event->type)
-				goto out;
-
-		} else {
-
-			event->type = next_event_type++;
-			list = &ftrace_event_list;
-		}
-
-		if (WARN_ON(ftrace_find_event(event->type)))
+		event->type = alloc_trace_event_type();
+		if (!event->type)
 			goto out;
-
-		list_add_tail(&event->list, list);
-
 	} else if (WARN(event->type > __TRACE_LAST_TYPE,
 			"Need to add type to trace.h")) {
 		goto out;
@@ -819,7 +784,7 @@ EXPORT_SYMBOL_GPL(register_trace_event);
 int __unregister_trace_event(struct trace_event *event)
 {
 	hlist_del(&event->node);
-	list_del(&event->list);
+	free_trace_event_type(event->type);
 	return 0;
 }
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread
* Re: [PATCH] tracing: Optimize event type allocation with IDA
@ 2022-11-23  2:43 Yujie Liu
  0 siblings, 0 replies; 6+ messages in thread
From: Yujie Liu @ 2022-11-23  2:43 UTC (permalink / raw)
  To: Zheng Yejian; +Cc: bpf, linux-kernel, lkp, mhiramat, oe-lkp, rostedt

Hi Yejian,

On 11/18/2022 14:41, Zheng Yejian wrote:
> On Wed, 16 Nov 2022 23:50:04 +0800, kernel test robot <yujie.liu@intel.com> wrote:
>> [-- Attachment #1: Type: text/plain, Size: 8013 bytes --]
>>
>> Greeting,
>>
>> FYI, we noticed BUG:KASAN:use-after-free_in_string due to commit (built with gcc-11):
>>
>> commit: bb10be779a5fc1147d5765257c64a2fbea9607c5 ("[PATCH] tracing: Optimize event type allocation with IDA")
>> url: https://github.com/intel-lab-lkp/linux/commits/Zheng-Yejian/tracing-Optimize-event-type-allocation-with-IDA/20221109-112530
>> base: https://git.kernel.org/cgit/linux/kernel/git/rostedt/linux-trace.git for-next
>> patch link: https://lore.kernel.org/all/20221109032352.254502-1-zhengyejian1@huawei.com/
>> patch subject: [PATCH] tracing: Optimize event type allocation with IDA
>>
>> in testcase: kernel-selftests
>> version: kernel-selftests-x86_64-9313ba54-1_20221017
>> with following parameters:
>>
>> 	group: ftrace
>>
>> test-description: The kernel contains a set of "self tests" under the tools/testing/selftests/ directory. These are intended to be small unit tests to exercise individual code paths in the kernel.
>> test-url: https://www.kernel.org/doc/Documentation/kselftest.txt
>>
>> on test machine: 4 threads Intel(R) Xeon(R) CPU E3-1225 v5 @ 3.30GHz (Skylake) with 16G memory
>>
>> caused below changes (please refer to attached dmesg/kmsg for entire log/backtrace):
>>
>>
>> [ 341.472391][ T5846] BUG: KASAN: use-after-free in string (vsprintf.c:?)
>> [  341.478762][ T5846] Read of size 1 at addr ffff88812a630000 by task grep/5846
>> [  341.485921][ T5846]
>> [  341.488122][ T5846] CPU: 3 PID: 5846 Comm: grep Not tainted 6.0.0-rc7-00021-gbb10be779a5f #1
>> [  341.496580][ T5846] Hardware name: HP HP Z238 Microtower Workstation/8183, BIOS N51 Ver. 01.63 10/05/2017
>> [  341.506170][ T5846] Call Trace:
>> [  341.509326][ T5846]  <TASK>
>> [ 341.512133][ T5846] dump_stack_lvl (??:?)
>> [ 341.516511][ T5846] print_address_description+0x1f/0x1e0
>> [ 341.522974][ T5846] print_report.cold (report.c:?)
>> [ 341.527698][ T5846] ? do_raw_spin_lock (??:?)
>> [ 341.532596][ T5846] ? string (vsprintf.c:?)
>> [ 341.536625][ T5846] kasan_report (??:?)
>> [ 341.540909][ T5846] ? kallsyms_lookup_buildid (kallsyms.c:?)
>> [ 341.546332][ T5846] ? string (vsprintf.c:?)
>> [ 341.550362][ T5846] string (vsprintf.c:?)
>> [ 341.554220][ T5846] ? ip6_addr_string_sa (vsprintf.c:?)
>> [ 341.559293][ T5846] ? enable_ptr_key_workfn (vsprintf.c:?)
>> [ 341.564454][ T5846] vsnprintf (??:?)
>> [ 341.568657][ T5846] ? pointer (??:?)
>> [ 341.572771][ T5846] ? pointer (??:?)
>> [ 341.576889][ T5846] seq_buf_vprintf (??:?)
>> [ 341.581440][ T5846] trace_seq_printf (??:?)
>> [ 341.586165][ T5846] ? trace_seq_bitmask (??:?)
>> [ 341.591151][ T5846] ? trace_seq_bitmask (??:?)
>> [ 341.596136][ T5846] ? memcpy (??:?)
>> [ 341.599991][ T5846] ? seq_buf_putmem (??:?)
>> [ 341.604631][ T5846] ? print_type_symbol (??:?)
>> [ 341.609440][ T5846] print_type_string (??:?)
>> [ 341.614165][ T5846] ? print_type_symbol (??:?)
>> [ 341.618976][ T5846] print_kprobe_event (trace_kprobe.c:?)
>> [ 341.623876][ T5846] print_trace_line (??:?)
>> [ 341.628600][ T5846] ? tracing_buffers_read (??:?)
>> [ 341.633844][ T5846] ? trace_hardirqs_on (??:?)
>> [ 341.638737][ T5846] ? _raw_spin_unlock_irqrestore (??:?)
>> [ 341.644417][ T5846] ? trace_find_next_entry_inc (??:?)
>> [ 341.650099][ T5846] s_show (??:?)
>> [ 341.653865][ T5846] seq_read_iter (??:?)
>> [ 341.658419][ T5846] ? cp_new_stat (stat.c:?)
>> [ 341.662882][ T5846] seq_read (??:?)
>> [ 341.666911][ T5846] ? seq_read_iter (??:?)
>> [ 341.671729][ T5846] ? fsnotify_perm+0x13b/0x4a0
>> [ 341.676973][ T5846] vfs_read (??:?)
>> [ 341.681003][ T5846] ? kernel_read (??:?)
>> [ 341.685470][ T5846] ? syscall_exit_to_user_mode (??:?)
>> [ 341.690975][ T5846] ? __fget_light (file.c:?)
>> [ 341.695438][ T5846] ksys_read (??:?)
>> [ 341.699467][ T5846] ? __ia32_sys_pwrite64 (??:?)
>> [ 341.704620][ T5846] ? lockdep_hardirqs_on_prepare (lockdep.c:?)
>> [ 341.711083][ T5846] ? syscall_enter_from_user_mode (??:?)
>> [ 341.716853][ T5846] do_syscall_64 (??:?)
>> [ 341.721143][ T5846] ? lockdep_hardirqs_on_prepare (lockdep.c:?)
>> [ 341.727609][ T5846] entry_SYSCALL_64_after_hwframe (??:?)
>> [  341.733374][ T5846] RIP: 0033:0x7f9da419902d
>> [ 341.737663][ T5846] Code: 31 c0 e9 c6 fe ff ff 50 48 8d 3d b6 55 0a 00 e8 99 fe 01 00 66 0f 1f 84 00 00 00 00 00 80 3d b1 25 0e 00 00 74 17 31 c0 0f 05 <48> 3d 00 f0 ff ff 77 5b c3 66 2e 0f 1f 84 00 00 00 00 00 48 83 ec
>> All code
>> ========
>>     0:	31 c0			xor    %eax,%eax
>>     2:	e9 c6 fe ff ff		jmpq   0xfffffffffffffecd
>>     7:	50			push   %rax
>>     8:	48 8d 3d b6 55 0a 00	lea    0xa55b6(%rip),%rdi        # 0xa55c5
>>     f:	e8 99 fe 01 00		callq  0x1fead
>>    14:	66 0f 1f 84 00 00 00	nopw   0x0(%rax,%rax,1)
>>    1b:	00 00
>>    1d:	80 3d b1 25 0e 00 00	cmpb   $0x0,0xe25b1(%rip)        # 0xe25d5
>>    24:	74 17			je     0x3d
>>    26:	31 c0			xor    %eax,%eax
>>    28:	0f 05			syscall
>>    2a:*	48 3d 00 f0 ff ff	cmp    $0xfffffffffffff000,%rax		<-- trapping instruction
>>    30:	77 5b			ja     0x8d
>>    32:	c3			retq
>>    33:	66 2e 0f 1f 84 00 00	nopw   %cs:0x0(%rax,%rax,1)
>>    3a:	00 00 00
>>    3d:	48			rex.W
>>    3e:	83			.byte 0x83
>>    3f:	ec			in     (%dx),%al
>>
>> Code starting with the faulting instruction
>> ===========================================
>>     0:	48 3d 00 f0 ff ff	cmp    $0xfffffffffffff000,%rax
>>     6:	77 5b			ja     0x63
>>     8:	c3			retq
>>     9:	66 2e 0f 1f 84 00 00	nopw   %cs:0x0(%rax,%rax,1)
>>    10:	00 00 00
>>    13:	48			rex.W
>>    14:	83			.byte 0x83
>>    15:	ec			in     (%dx),%al
>> [  341.757163][ T5846] RSP: 002b:00007fff7b2e0818 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
>> [  341.765446][ T5846] RAX: ffffffffffffffda RBX: 0000000000018000 RCX: 00007f9da419902d
>> [  341.773296][ T5846] RDX: 0000000000018000 RSI: 00005612d0f5e000 RDI: 0000000000000005
>> [  341.781148][ T5846] RBP: 00005612d0f5e000 R08: 00000000000395e1 R09: 0000000000000000
>> [  341.789000][ T5846] R10: 0000000000000000 R11: 0000000000000246 R12: 00007fff7b2e08e0
>> [  341.796852][ T5846] R13: 0000000000000005 R14: 0000000000000000 R15: 0000000000000005
>> [  341.804710][ T5846]  </TASK>
>> [  341.807601][ T5846]
>> [  341.809798][ T5846] The buggy address belongs to the physical page:
>> [  341.816082][ T5846] page:00000000345a71f1 refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x12a630
>> [  341.826198][ T5846] flags: 0x17ffffc0000000(node=0|zone=2|lastcpupid=0x1fffff)
>> [  341.833443][ T5846] raw: 0017ffffc0000000 ffffea0005b73208 ffffea0004a98e08 0000000000000000
>> [  341.841901][ T5846] raw: 0000000000000000 0000000000070000 00000000ffffffff 0000000000000000
>> [  341.850361][ T5846] page dumped because: kasan: bad access detected
>> [  341.856650][ T5846]
>> [  341.858850][ T5846] Memory state around the buggy address:
>> [  341.864354][ T5846]  ffff88812a62ff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>> [  341.872284][ T5846]  ffff88812a62ff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>> [  341.880221][ T5846] >ffff88812a630000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
>> [  341.888159][ T5846]                    ^
>> [  341.892100][ T5846]  ffff88812a630080: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
>> [  341.900036][ T5846]  ffff88812a630100: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
>> [  341.907978][ T5846] ==================================================================
>> [  341.915979][ T5846] Disabling lock debugging due to kernel taint
>>
>>
>> If you fix the issue, kindly add following tag
>> | Reported-by: kernel test robot <yujie.liu@intel.com>
>> | Link: https://lore.kernel.org/oe-lkp/202211162205.6cb42ae6-yujie.liu@intel.com
>>
>>
>> To reproduce:
>>
>>          git clone https://github.com/intel/lkp-tests.git
>>          cd lkp-tests
>>          sudo bin/lkp install job.yaml           # job file is attached in this email
>>          bin/lkp split-job --compatible job.yaml # generate the yaml file for lkp run
>>          sudo bin/lkp run generated-yaml-file
>>
>>          # if come across any failure that blocks the test,
>>          # please remove ~/.lkp and /lkp dir to run from a clean state.
>>
> 
> Thanks for your test!
> 
> Looking into above call trace, it seems something wrong happened when read
> 'string' type data of some kprobe events.
> 
> But I currently feel like it hard to relate this issue to my modification
> since I change event type allocation in the patch and it can only affect
> event registration.
> 
> I cannot run the lkp-tests in my environment, sorry for that. I'd like to
> know if this problem would always happen after applying my patch? Could you
> please provide exact testcase or more detailed logs? Thanks!

This problem is not 100% reproducible in our test. We did 29 rounds of test,
19 of which can reproduce it, while the base commit is always clean.

=========================================================================================
tbox_group/testcase/rootfs/kconfig/compiler/group:
   lkp-skl-d06/kernel-selftests/debian-12-x86_64-20220629.cgz/x86_64-rhel-8.3-kselftests/gcc-11/ftrace

commit:
   0ce0638edf5ec ("ftrace: Properly unset FTRACE_HASH_FL_MOD")
   bb10be779a5fc ("tracing: Optimize event type allocation with IDA")

    0ce0638edf5ec                 bb10be779a5fc
---------------- --------------- ------------
        fail:runs  %reproduction    fail:runs
            |             |             |
            :20          95%          19:29    dmesg.BUG:KASAN:use-after-free_in_string


Running lkp-tests seems to be a bit complex, sorry about that. We reconfirmed
that the problem can be reproduced by simply run a ftrace selftest case below.
Could you please have a try?

$ cd linux/tools/testing/selftests/ftrace
$ ./ftracetest test.d/kprobe/kprobe_args_string.tc
$ dmesg
...
[  339.517182][ T5803] ==================================================================
[  339.525127][ T5803] BUG: KASAN: use-after-free in string+0x29c/0x320
[  339.531492][ T5803] Read of size 1 at addr ffff888108d7c578 by task grep/5803
[  339.538645][ T5803]
[  339.540845][ T5803] CPU: 3 PID: 5803 Comm: grep Not tainted 6.0.0-rc7-00021-gbb10be779a5f #1
[  339.549295][ T5803] Hardware name: HP HP Z238 Microtower Workstation/8183, BIOS N51 Ver. 01.63 10/05/2017
[  339.558877][ T5803] Call Trace:
[  339.562030][ T5803]  <TASK>
[  339.564835][ T5803]  dump_stack_lvl+0x45/0x59
[  339.569210][ T5803]  print_address_description+0x1f/0x1e0
[  339.575663][ T5803]  print_report.cold+0x55/0x232
[  339.580380][ T5803]  ? do_raw_spin_lock+0x12e/0x270
[  339.585275][ T5803]  ? string+0x29c/0x320
[  339.589299][ T5803]  kasan_report+0xb1/0x190
[  339.593582][ T5803]  ? kallsyms_lookup_buildid+0xb0/0x2c0
[  339.598998][ T5803]  ? string+0x29c/0x320
[  339.603026][ T5803]  string+0x29c/0x320
...


The full dmesg and test log are attached in the original report, please check
them for more details.

https://lore.kernel.org/oe-lkp/202211162205.6cb42ae6-yujie.liu@intel.com

--
Best Regards,
Yujie

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-11-23  2:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-09  3:23 [PATCH] tracing: Optimize event type allocation with IDA Zheng Yejian
2022-11-09 13:26 ` Masami Hiramatsu
2022-11-10  1:36   ` Zheng Yejian
2022-11-16 15:50 ` kernel test robot
2022-11-18  6:41   ` Zheng Yejian
2022-11-23  2:43 Yujie Liu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.