Skip to contentSkip to conversation

lttng-abi.c: fix the error check

PATCH Maxin B. John 2 participants 3 messages Mar 23, 2013 3 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20130322215745.GB18746@Krystal

#1 lore raw

I was trying to fix a random problem happened in hawkboard with 3.9.0-rc3 kernel.

root@hawkboard:~# uname -a
Linux hawkboard 3.9.0-rc3-00244-g9217cbb #35 PREEMPT Fri Mar 22
18:03:26 CET 2013 armv5tejl GNU/Linux
root@hawkboard:~# lttng create mysession
Session mysession created.
Traces will be written in
/home/root/lttng-traces/mysession-20130321-131130
root@hawkboard:~# lttng enable-event -a -k
lttng-sessiond: page allocation failure: order:10, mode:0x10c0d0
[<c001358c>] (unwind_backtrace+0x0/0xfc) from [<c037af14>]
(dump_stack+0x20/0x24)
[<c037af14>] (dump_stack+0x20/0x24) from [<c00b7de8>]
(warn_alloc_failed+0xd0/0x114)
[<c00b7de8>] (warn_alloc_failed+0xd0/0x114) from [<c00bad80>]
(__alloc_pages_nodemask+0x620/0x8bc)
[<c00bad80>] (__alloc_pages_nodemask+0x620/0x8bc) from [<c00bb03c>]
(__get_free_pages+0x20/0x5c)
[<c00bb03c>] (__get_free_pages+0x20/0x5c) from [<c00e9e50>]
(kmalloc_order_trace+0x34/0xf4)
[<c00e9e50>] (kmalloc_order_trace+0x34/0xf4) from [<bf09affc>]
(lttng_syscalls_register+0x1a4/0x244 [lttng_tracer])
[<bf09affc>] (lttng_syscalls_register+0x1a4/0x244 [lttng_tracer]) from
[<bf082ce8>] (lttng_abi_create_event+0x7c/0x138 [lttng_tracer])
[<bf082ce8>] (lttng_abi_create_event+0x7c/0x138 [lttng_tracer]) from
[<bf083958>] (lttng_channel_ioctl+0x198/0x53c [lttng_tracer])
[<bf083958>] (lttng_channel_ioctl+0x198/0x53c [lttng_tracer]) from
[<c0100ecc>] (do_vfs_ioctl+0x8c/0x608)
[<c0100ecc>] (do_vfs_ioctl+0x8c/0x608) from [<c01014c8>]
(sys_ioctl+0x80/0x88)
[<c01014c8>] (sys_ioctl+0x80/0x88) from [<c000e2e0>]
(ret_fast_syscall+0x0/0x44)
Mem-info:
DMA per-cpu:
CPU    0: hi:   18, btch:   3 usd:   0
active_anon:789 inactive_anon:12 isolated_anon:0
 active_file:2056 inactive_file:2348 isolated_file:0
 unevictable:0 dirty:0 writeback:0 unstable:0
 free:2790 slab_reclaimable:852 slab_unreclaimable:944
 mapped:589 shmem:21 pagetables:134 bounce:0
 free_cma:0
DMA free:11160kB min:968kB low:1208kB high:1452kB active_anon:3156kB
inactive_anon:48kB active_file:8224kB inactive_file:9392kB
unevictable:0kB isolated(anon):0kB isolated(file):0kB present:65536kB
managed:59016kB mlocked:0kB dirty:0kB writeback:0kB mapped:2356kB
shmem:84kB slab_reclaimable:3408kB slab_unreclaimable:3776kB
kernel_stack:448kB pagetables:536kB unstable:0kB bounce:0kB
free_cma:0kB writeback_tmp:0kB pages_scanned:0 all_unreclaimable? no
lowmem_reserve[]: 0 0 0
DMA: 4*4kB (UEM) 9*8kB (UEM) 4*16kB (UEM) 8*32kB (EMR) 4*64kB (EMR)
4*128kB (EMR) 3*256kB (UER) 4*512kB (UEMR) 3*1024kB (UEM) 2*2048kB
(UE) 0*4096kB = 11160kB
4432 total pagecache pages
16384 pages of RAM
2955 free pages
1571 reserved pages
1571 slab pages
264956 pages shared
0 pages swap cached
PERROR: create event ioctl: Cannot allocate memory [in
kernel_create_event() at kernel.c:206]
All kernel events are enabled in channel channel0
Quoted 6 lines

The expected return values from lttng_syscalls_register are:
·
0 -> success

(quoted diff) +0 -2
-EINVAL -> error
-ENOMEM -> error

·

Quoted 8 lines

so whether we check for error with
·
if (ret)
or
if (ret < 0)
·
should yield to the exact same result, since we don't expect positive return values.

Now, I can see that my idea wasn't right.

Quoted 6 lines

And for lttng_abi_create_event(), the original code flow is to execute 2 kfree(), and then "return ret", which will return either -EINVAL or -ENOMEM to the caller.
·
So by adding this return -ENOMEM, you hide the real cause (-EINVAL or -ENOMEM), and you add 2 memory leaks.

Ahh.. Sorry!

Quoted 4 lines

Or am I missing something ?
·
Thanks,
Mathieu

Best Regards,
Maxin

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 18 lines · * Maxin B. John ([email protected]) wrote:

I was trying to fix a random problem happened in hawkboard with 3.9.0-rc3 kernel.

root@hawkboard:~# uname -a
Linux hawkboard 3.9.0-rc3-00244-g9217cbb #35 PREEMPT Fri Mar 22
18:03:26 CET 2013 armv5tejl GNU/Linux
root@hawkboard:~# lttng create mysession
Session mysession created.
Traces will be written in
/home/root/lttng-traces/mysession-20130321-131130
root@hawkboard:~# lttng enable-event -a -k
lttng-sessiond: page allocation failure: order:10, mode:0x10c0d0
[<c001358c>] (unwind_backtrace+0x0/0xfc) from [<c037af14>]
(dump_stack+0x20/0x24)
[<c037af14>] (dump_stack+0x20/0x24) from [<c00b7de8>]
(warn_alloc_failed+0xd0/0x114)
[<c00b7de8>] (warn_alloc_failed+0xd0/0x114) from [<c00bad80>]
Quoted 8 lines
(__alloc_pages_nodemask+0x620/0x8bc)
[<c00bad80>] (__alloc_pages_nodemask+0x620/0x8bc) from [<c00bb03c>]
(__get_free_pages+0x20/0x5c)
[<c00bb03c>] (__get_free_pages+0x20/0x5c) from [<c00e9e50>]
(kmalloc_order_trace+0x34/0xf4)
[<c00e9e50>] (kmalloc_order_trace+0x34/0xf4) from [<bf09affc>]
(lttng_syscalls_register+0x1a4/0x244 [lttng_tracer])
[<bf09affc>] (lttng_syscalls_register+0x1a4/0x244 [lttng_tracer]) from

Oh! got it.

if we look at:

int lttng_syscalls_register(struct lttng_channel *chan, void *filter)

                /* create syscall table mapping syscall to events */
                chan->sc_table = kzalloc(sizeof(struct lttng_event *)
                                        * ARRAY_SIZE(sc_table), GFP_KERNEL);

we notice that if sc_table is huge (e.g. because some system call numbers would be really, really high), then we could possibly run out of memory when doing this allocation.

What is the range of system call numbers on your architecture ?

Thanks,

Mathieu

Quoted 73 lines

[<bf082ce8>] (lttng_abi_create_event+0x7c/0x138 [lttng_tracer]) [<bf082ce8>] (lttng_abi_create_event+0x7c/0x138 [lttng_tracer]) from [<bf083958>] (lttng_channel_ioctl+0x198/0x53c [lttng_tracer]) [<bf083958>] (lttng_channel_ioctl+0x198/0x53c [lttng_tracer]) from [<c0100ecc>] (do_vfs_ioctl+0x8c/0x608)
[<c0100ecc>] (do_vfs_ioctl+0x8c/0x608) from [<c01014c8>] (sys_ioctl+0x80/0x88)
[<c01014c8>] (sys_ioctl+0x80/0x88) from [<c000e2e0>] (ret_fast_syscall+0x0/0x44)
Mem-info:
DMA per-cpu:
CPU 0: hi: 18, btch: 3 usd: 0
active_anon:789 inactive_anon:12 isolated_anon:0
active_file:2056 inactive_file:2348 isolated_file:0 unevictable:0 dirty:0 writeback:0 unstable:0
free:2790 slab_reclaimable:852 slab_unreclaimable:944 mapped:589 shmem:21 pagetables:134 bounce:0
free_cma:0
DMA free:11160kB min:968kB low:1208kB high:1452kB active_anon:3156kB inactive_anon:48kB active_file:8224kB inactive_file:9392kB unevictable:0kB isolated(anon):0kB isolated(file):0kB present:65536kB managed:59016kB mlocked:0kB dirty:0kB writeback:0kB mapped:2356kB shmem:84kB slab_reclaimable:3408kB slab_unreclaimable:3776kB kernel_stack:448kB pagetables:536kB unstable:0kB bounce:0kB free_cma:0kB writeback_tmp:0kB pages_scanned:0 all_unreclaimable? no lowmem_reserve[]: 0 0 0
DMA: 4*4kB (UEM) 9*8kB (UEM) 4*16kB (UEM) 8*32kB (EMR) 4*64kB (EMR) 4*128kB (EMR) 3*256kB (UER) 4*512kB (UEMR) 3*1024kB (UEM) 2*2048kB (UE) 0*4096kB = 11160kB
4432 total pagecache pages
16384 pages of RAM
2955 free pages
1571 reserved pages
1571 slab pages
264956 pages shared
0 pages swap cached
PERROR: create event ioctl: Cannot allocate memory [in kernel_create_event() at kernel.c:206]
All kernel events are enabled in channel channel0

Quoted 6 lines

The expected return values from lttng_syscalls_register are:
·
0 -> success

(quoted diff) +0 -2
-EINVAL -> error
-ENOMEM -> error

·

Quoted 8 lines

so whether we check for error with
·
if (ret)
or
if (ret < 0)
·
should yield to the exact same result, since we don't expect positive return values.

Now, I can see that my idea wasn't right.

Quoted 6 lines

And for lttng_abi_create_event(), the original code flow is to execute 2 kfree(), and then "return ret", which will return either -EINVAL or -ENOMEM to the caller.
·
So by adding this return -ENOMEM, you hide the real cause (-EINVAL or -ENOMEM), and you add 2 memory leaks.

Ahh.. Sorry!

Quoted 4 lines

Or am I missing something ?
·
Thanks,
Mathieu

Best Regards,
Maxin

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): 20130323154658.GA28816@Krystal

#5 lore raw
Quoted 38 lines · * Mathieu Desnoyers ([email protected]) wrote:
Quoted 18 lines · * Maxin B. John ([email protected]) wrote:

I was trying to fix a random problem happened in hawkboard with 3.9.0-rc3 kernel.

root@hawkboard:~# uname -a
Linux hawkboard 3.9.0-rc3-00244-g9217cbb #35 PREEMPT Fri Mar 22
18:03:26 CET 2013 armv5tejl GNU/Linux
root@hawkboard:~# lttng create mysession
Session mysession created.
Traces will be written in
/home/root/lttng-traces/mysession-20130321-131130
root@hawkboard:~# lttng enable-event -a -k
lttng-sessiond: page allocation failure: order:10, mode:0x10c0d0
[<c001358c>] (unwind_backtrace+0x0/0xfc) from [<c037af14>]
(dump_stack+0x20/0x24)
[<c037af14>] (dump_stack+0x20/0x24) from [<c00b7de8>]
(warn_alloc_failed+0xd0/0x114)
[<c00b7de8>] (warn_alloc_failed+0xd0/0x114) from [<c00bad80>]
Quoted 8 lines
(__alloc_pages_nodemask+0x620/0x8bc)
[<c00bad80>] (__alloc_pages_nodemask+0x620/0x8bc) from [<c00bb03c>]
(__get_free_pages+0x20/0x5c)
[<c00bb03c>] (__get_free_pages+0x20/0x5c) from [<c00e9e50>]
(kmalloc_order_trace+0x34/0xf4)
[<c00e9e50>] (kmalloc_order_trace+0x34/0xf4) from [<bf09affc>]
(lttng_syscalls_register+0x1a4/0x244 [lttng_tracer])
[<bf09affc>] (lttng_syscalls_register+0x1a4/0x244 [lttng_tracer]) from

Oh! got it.

if we look at:

int lttng_syscalls_register(struct lttng_channel *chan, void *filter)

                /* create syscall table mapping syscall to events */
                chan->sc_table = kzalloc(sizeof(struct lttng_event *)
                                        * ARRAY_SIZE(sc_table), GFP_KERNEL);
Quoted 5 lines

we notice that if sc_table is huge (e.g. because some system call numbers would be really, really high), then we could possibly run out of memory when doing this allocation.

What is the range of system call numbers on your architecture ?

a look at:

arch/arm/include/uapi/asm/unistd.h

shows:

#define __NR_OABI_SYSCALL_BASE 0x900000

#if defined(__thumb__) || defined(__ARM_EABI__)
#define __NR_SYSCALL_BASE 0
#else
#define __NR_SYSCALL_BASE __NR_OABI_SYSCALL_BASE
#endif

So this offset "base" might be causing the large allocation. Can you try the following patch and see if it helps ?

---

lttng-syscalls.c +14 -8
index 62ed24a..10449b9 100644
@@ -35,6 +35,10 @@
 # endif
 #endif
 
+#ifndef __NR_SYSCALL_BASE
+#define __NR_SYSCALL_BASE	0
+#endif
+
 static
 void syscall_entry_probe(void *__data, struct pt_regs *regs, long id);
 
@@ -133,7 +137,7 @@ struct trace_syscall_entry {
 
 #undef TRACE_SYSCALL_TABLE
 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs)	\
-	[ _nr ] = {						\
+	[ (_nr) - __NR_SYSCALL_BASE ] = {			\
 		.func = __event_probe__##_template,		\
 		.nrargs = (_nrargs),				\
 		.fields = __event_fields___##_template,		\
@@ -147,7 +151,7 @@ static const struct trace_syscall_entry sc_table[] = {
 
 #undef TRACE_SYSCALL_TABLE
 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs)	\
-	[ _nr ] = {						\
+	[ (_nr) - __NR_SYSCALL_BASE] = {			\
 		.func = __event_probe__##compat_##_template,	\
 		.nrargs = (_nrargs),				\
 		.fields = __event_fields___##compat_##_template,\
@@ -180,7 +184,9 @@ void syscall_entry_probe(void *__data, struct pt_regs *regs, long id)
 	struct lttng_event *event, *unknown_event;
 	const struct trace_syscall_entry *table, *entry;
 	size_t table_len;
+	long syscall_offset;
 
+	syscall_offset = id - __NR_SYSCALL_BASE;
 	if (unlikely(is_compat_task())) {
 		table = compat_sc_table;
 		table_len = ARRAY_SIZE(compat_sc_table);
@@ -190,19 +196,19 @@ void syscall_entry_probe(void *__data, struct pt_regs *regs, long id)
 		table_len = ARRAY_SIZE(sc_table);
 		unknown_event = chan->sc_unknown;
 	}
-	if (unlikely(id >= table_len)) {
-		syscall_entry_unknown(unknown_event, regs, id);
+	if (unlikely(syscall_offset >= table_len)) {
+		syscall_entry_unknown(unknown_event, regs, syscall_offset);
 		return;
 	}
 	if (unlikely(is_compat_task()))
-		event = chan->compat_sc_table[id];
+		event = chan->compat_sc_table[syscall_offset];
 	else
-		event = chan->sc_table[id];
+		event = chan->sc_table[syscall_offset];
 	if (unlikely(!event)) {
-		syscall_entry_unknown(unknown_event, regs, id);
+		syscall_entry_unknown(unknown_event, regs, syscall_offset);
 		return;
 	}
-	entry = &table[id];
+	entry = &table[syscall_offset];
 	WARN_ON_ONCE(!entry);
 
 	switch (entry->nrargs) {
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com