perf: Validate cpu early in perf_event_alloc()

Starting from perf_event_alloc()->perf_init_event(), the kernel
assumes that event->cpu is either -1 or the valid CPU number.

Change perf_event_alloc() to validate this argument early. This
also means we can remove the similar check in
find_get_context().

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Prasad <prasad@linux.vnet.ibm.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: gregkh@suse.de
Cc: stable@kernel.org
LKML-Reference: <20110118161032.GC693@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>

authored by Oleg Nesterov and committed by Ingo Molnar 66832eb4 22a4ec72

+6 -4
+6 -4
kernel/perf_event.c
··· 2233 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) 2234 return ERR_PTR(-EACCES); 2235 2236 - if (cpu < 0 || cpu >= nr_cpumask_bits) 2237 - return ERR_PTR(-EINVAL); 2238 - 2239 /* 2240 * We could be clever and allow to attach a event to an 2241 * offline CPU and activate it when the CPU comes up, but ··· 5538 struct hw_perf_event *hwc; 5539 long err; 5540 5541 event = kzalloc(sizeof(*event), GFP_KERNEL); 5542 if (!event) 5543 return ERR_PTR(-ENOMEM); ··· 5591 5592 if (!overflow_handler && parent_event) 5593 overflow_handler = parent_event->overflow_handler; 5594 - 5595 event->overflow_handler = overflow_handler; 5596 5597 if (attr->disabled)
··· 2233 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) 2234 return ERR_PTR(-EACCES); 2235 2236 /* 2237 * We could be clever and allow to attach a event to an 2238 * offline CPU and activate it when the CPU comes up, but ··· 5541 struct hw_perf_event *hwc; 5542 long err; 5543 5544 + if ((unsigned)cpu >= nr_cpu_ids) { 5545 + if (!task || cpu != -1) 5546 + return ERR_PTR(-EINVAL); 5547 + } 5548 + 5549 event = kzalloc(sizeof(*event), GFP_KERNEL); 5550 if (!event) 5551 return ERR_PTR(-ENOMEM); ··· 5589 5590 if (!overflow_handler && parent_event) 5591 overflow_handler = parent_event->overflow_handler; 5592 + 5593 event->overflow_handler = overflow_handler; 5594 5595 if (attr->disabled)