From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751760AbaGGQmG (ORCPT ); Mon, 7 Jul 2014 12:42:06 -0400 Received: from e06smtp11.uk.ibm.com ([195.75.94.107]:59186 "EHLO e06smtp11.uk.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751119AbaGGQmE (ORCPT ); Mon, 7 Jul 2014 12:42:04 -0400 Date: Mon, 07 Jul 2014 20:41:57 +0400 Message-ID: <874myt86ju.wl%yarygin@linux.vnet.ibm.com> From: Alexander Yarygin To: Arnaldo Carvalho de Melo , Jiri Olsa CC: Peter Zijlstra , Paul Mackerras , Ingo Molnar , Christian Borntraeger , linux-kernel@vger.kernel.org Subject: [BUG] perf stat: events inheritance can break task targets User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/24.3 (i686-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 14070716-5024-0000-0000-0000009DF86C Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org perf stat can block pthread_create() for a multithreaded userspace process (i.e. qemu) when: - process is running with non-root privileges - perf stat is running as root with trace events in -e option - it is attached to the process's pid. Here is a simple test scenario: ~$ cat test.c #include #include #include #include #define THREADS 50 static pthread_t threads[THREADS]; void *loop() { while(1); } int main() { for (int i = 0; i < THREADS; i++) { int err = pthread_create(&threads[i], NULL, loop, 0); if (!err) printf("thread created: %i\n", i); else printf("couldn't create thread %i: %s\n", i, strerror(err)); sleep(1); } return 0; } ~$ gcc test.c -lpthread -std=c99 -o test ~$ ./test thread created: 0 thread created: 1 # now perf is running: # ~$ sudo perf stat -e "kvm:*" -p `pidof test` couldn't create thread 2: Operation not permitted couldn't create thread 3: Operation not permitted couldn't create thread 4: Operation not permitted # here is perf was stopped thread created: 5 thread created: 6 ^C When perf is running, every invoke of pthread_create() returns -EPERM. On the kernel side, copy_process() creates a task, scheduled it, than perf_event_init_task() (kernel/events/core.c) returns an error, and the kernel cleans task's resources. It looks like child process doesn't have access to trace events, so perf_trace_event_perm() (kernel/trace/trace_event_perf.c) returns -EPERM: static int perf_trace_event_perm(struct ftrace_event_call *tp_event, struct perf_event *p_event) { ... /* * ...otherwise raw tracepoint data can be a severe data leak, * only allow root to have these. */ if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN)) return -EPERM; ... } If we explicitly use the --no-inherit option, payload wouldn't die.