From patchwork Mon Apr 7 15:04:34 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean Pihet X-Patchwork-Id: 27906 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ve0-f197.google.com (mail-ve0-f197.google.com [209.85.128.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 9B3A020490 for ; Mon, 7 Apr 2014 15:07:57 +0000 (UTC) Received: by mail-ve0-f197.google.com with SMTP id pa12sf19153872veb.8 for ; Mon, 07 Apr 2014 08:07:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=LCWGM7Lth4M7vVt8cKwJyg4mHXaBxijopXnH4fhBu00=; b=IVP76wJHSWJCSOTx6+WVapQbb+dOHCG8pa/cyqtgDi8rUSqu4vdB9w5VsC0+0HFBDo IaGtfz3PKKGhB0r5HKn+TChwgGTtvRw81H6oQk5n0WXnPKHBNT7rvGzzC0wv1mbQ1waL 9gnBsWynfk2W7XVcZq4OMVsLgzEwTrQIetub1F3kz70UwIWFeNyL9Jkb1P1xjhKYuy5j 8gAvGSnIAkctnEWWJuL0I7ygvUHnX3QK0Un71FojjC41mhQdUGI+WJFJZREilL/jRcLV Il+U8w+QOOVvlzQAhVK47Lvyav7W0vgKEmrsJW9ow1HfRl6cJ0W/Bt6XInzMy/BbI8/2 9k9g== X-Gm-Message-State: ALoCoQnYjK9SgyuT13YYcTSGXqKyC6WasLRka5Lby5SdPHtMXyNkUE8P5waz4/7MjT/MzYc4j9NA X-Received: by 10.224.95.9 with SMTP id b9mr16489978qan.2.1396883277278; Mon, 07 Apr 2014 08:07:57 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.25.147 with SMTP id 19ls1731272qgt.15.gmail; Mon, 07 Apr 2014 08:07:57 -0700 (PDT) X-Received: by 10.220.103.141 with SMTP id k13mr1787865vco.25.1396883277187; Mon, 07 Apr 2014 08:07:57 -0700 (PDT) Received: from mail-ve0-f178.google.com (mail-ve0-f178.google.com [209.85.128.178]) by mx.google.com with ESMTPS id t4si3168901vcz.166.2014.04.07.08.07.57 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Apr 2014 08:07:57 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.178 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.178; Received: by mail-ve0-f178.google.com with SMTP id jw12so3587769veb.23 for ; Mon, 07 Apr 2014 08:07:57 -0700 (PDT) X-Received: by 10.220.170.202 with SMTP id e10mr6849663vcz.20.1396883276951; Mon, 07 Apr 2014 08:07:56 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.12.8 with SMTP id v8csp169831vcv; Mon, 7 Apr 2014 08:07:56 -0700 (PDT) X-Received: by 10.68.137.136 with SMTP id qi8mr31641534pbb.79.1396883276150; Mon, 07 Apr 2014 08:07:56 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l1si4143847paw.315.2014.04.07.08.07.55; Mon, 07 Apr 2014 08:07:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755594AbaDGPHq (ORCPT + 27 others); Mon, 7 Apr 2014 11:07:46 -0400 Received: from mail-ee0-f47.google.com ([74.125.83.47]:33194 "EHLO mail-ee0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755335AbaDGPFY (ORCPT ); Mon, 7 Apr 2014 11:05:24 -0400 Received: by mail-ee0-f47.google.com with SMTP id b15so714947eek.6 for ; Mon, 07 Apr 2014 08:05:23 -0700 (PDT) X-Received: by 10.14.194.133 with SMTP id m5mr3422702een.38.1396883122948; Mon, 07 Apr 2014 08:05:22 -0700 (PDT) Received: from localhost.localdomain (201-179-62-37.mobileinternet.proximus.be. [37.62.179.201]) by mx.google.com with ESMTPSA id o4sm42287649eef.20.2014.04.07.08.05.20 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 07 Apr 2014 08:05:22 -0700 (PDT) From: Jean Pihet To: Borislav Petkov , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Jiri Olsa , linux-kernel@vger.kernel.org, Robert Richter Cc: Robert Richter , Vince Weaver , Jean Pihet Subject: [PATCH 12/16] perf, persistent: ioctl functions to control persistency Date: Mon, 7 Apr 2014 17:04:34 +0200 Message-Id: <1396883078-25320-13-git-send-email-jean.pihet@linaro.org> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1396883078-25320-1-git-send-email-jean.pihet@linaro.org> References: <1396883078-25320-1-git-send-email-jean.pihet@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: jean.pihet@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.178 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Robert Richter Implementing ioctl functions to control persistent events. There are functions to unclaim or claim an event to or from a process. The PERF_EVENT_IOC_UNCLAIM ioctl call makes an event persistent. After closing the event's fd it runs then in the background of the system without the need of a controlling process. The perf_event_open() syscall can be used to reopen the event by any process. The PERF_EVENT_IOC_CLAIM ioctl attaches the event again so that it is removed after closing the event's fd. This is for Linux man-pages: type ... PERF_TYPE_PERSISTENT (Since Linux 3.xx) This indicates a persistent event. There is a unique identifier for each persistent event that needs to be specified in the event's attribute config field. Persistent events are listed under: /sys/bus/event_source/devices/persistent/ ... persistent : 41, /* always-on event */ ... persistent: (Since Linux 3.xx) Put event into persistent state after opening. After closing the event's fd the event is persistent in the system and continues to run. perf_event ioctl calls PERF_EVENT_IOC_UNCLAIM (Since Linux 3.xx) Unclaim the event specified by the file descriptor from the process and make it persistent in the system. After closing the fd the event will continue to run. An unique identifier for the persistent event is returned or an error otherwise. The following allows to connect to the event again: pe.type = PERF_TYPE_PERSISTENT; pe.config = ; ... fd = perf_event_open(...); The event must be reopened on the same cpu. PERF_EVENT_IOC_CLAIM (Since Linux 3.xx) Claim the event specified by the file descriptor to the current process. The event is no longer persistent in the system and will be removed after all users disconnected from the event. Thus, if there are no other users the event will be closed too after closing its file descriptor, the event then no longer exists. [ Jean Pihet: renamed PERF_EVENT_IOC_ATTACH/DETACH to PERF_EVENT_IOC_CLAIM/UNCLAIM ] Cc: Vince Weaver Signed-off-by: Robert Richter Signed-off-by: Robert Richter Signed-off-by: Jean Pihet --- include/uapi/linux/perf_event.h | 2 + kernel/events/core.c | 6 ++ kernel/events/internal.h | 2 + kernel/events/persistent.c | 178 +++++++++++++++++++++++++++++++++------- 4 files changed, 160 insertions(+), 28 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index a3f2761..b2f1943 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -350,6 +350,8 @@ struct perf_event_attr { #define PERF_EVENT_IOC_SET_OUTPUT _IO ('$', 5) #define PERF_EVENT_IOC_SET_FILTER _IOW('$', 6, char *) #define PERF_EVENT_IOC_ID _IOR('$', 7, __u64 *) +#define PERF_EVENT_IOC_UNCLAIM _IO ('$', 8) +#define PERF_EVENT_IOC_CLAIM _IO ('$', 9) enum perf_event_ioc_flags { PERF_IOC_FLAG_GROUP = 1U << 0, diff --git a/kernel/events/core.c b/kernel/events/core.c index 80ada8e..bdf3895 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -3707,6 +3707,12 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) case PERF_EVENT_IOC_SET_FILTER: return perf_event_set_filter(event, (void __user *)arg); + case PERF_EVENT_IOC_UNCLAIM: + return perf_event_unclaim(event); + + case PERF_EVENT_IOC_CLAIM: + return perf_event_claim(event); + default: return -ENOTTY; } diff --git a/kernel/events/internal.h b/kernel/events/internal.h index 6b9a11d..9a871b5 100644 --- a/kernel/events/internal.h +++ b/kernel/events/internal.h @@ -212,5 +212,7 @@ extern void perf_free_rb(struct perf_event *event); extern int perf_get_fd(struct perf_event *event, int f_flags); extern int perf_get_persistent_event_fd(int cpu, int id); extern void __init perf_register_persistent(void); +extern int perf_event_unclaim(struct perf_event *event); +extern int perf_event_claim(struct perf_event *event); #endif /* _KERNEL_EVENTS_INTERNAL_H */ diff --git a/kernel/events/persistent.c b/kernel/events/persistent.c index 49bf889..0d796c6 100644 --- a/kernel/events/persistent.c +++ b/kernel/events/persistent.c @@ -59,6 +59,49 @@ static struct perf_event *__pevent_find(int cpu, int id) return NULL; } +static void pevent_free(struct pevent *pevent) +{ + if (pevent->id) + put_event_id(pevent->id); + + kfree(pevent->name); + kfree(pevent); +} + +static struct pevent *pevent_alloc(char *name) +{ + struct pevent *pevent; + char id_buf[32]; + int ret; + + pevent = kzalloc(sizeof(*pevent), GFP_KERNEL); + if (!pevent) + return ERR_PTR(-ENOMEM); + + atomic_set(&pevent->refcount, 1); + + ret = get_event_id(pevent); + if (ret < 0) + goto fail; + pevent->id = ret; + + if (!name) { + snprintf(id_buf, sizeof(id_buf), "%d", pevent->id); + name = id_buf; + } + + pevent->name = kstrdup(name, GFP_KERNEL); + if (!pevent->name) { + ret = -ENOMEM; + goto fail; + } + + return pevent; +fail: + pevent_free(pevent); + return ERR_PTR(ret); +} + static int pevent_add(struct pevent *pevent, struct perf_event *event) { int ret = -EEXIST; @@ -74,6 +117,7 @@ static int pevent_add(struct pevent *pevent, struct perf_event *event) ret = 0; event->pevent_id = pevent->id; + event->attr.persistent = 1; list_add_tail(&event->pevent_entry, &per_cpu(pevents, cpu)); unlock: mutex_unlock(&per_cpu(pevents_lock, cpu)); @@ -91,6 +135,7 @@ static struct perf_event *pevent_del(struct pevent *pevent, int cpu) if (event) { list_del(&event->pevent_entry); event->pevent_id = 0; + event->attr.persistent = 0; } mutex_unlock(&per_cpu(pevents_lock, cpu)); @@ -160,33 +205,12 @@ static int __maybe_unused persistent_open(char *name, struct perf_event_attr *attr, int nr_pages) { struct pevent *pevent; - char id_buf[32]; int cpu; int ret; - pevent = kzalloc(sizeof(*pevent), GFP_KERNEL); - if (!pevent) - return -ENOMEM; - - atomic_set(&pevent->refcount, 1); - - ret = get_event_id(pevent); - if (ret < 0) - goto fail; - pevent->id = ret; - - if (!name) { - snprintf(id_buf, sizeof(id_buf), "%d", pevent->id); - name = id_buf; - } - - pevent->name = kstrdup(name, GFP_KERNEL); - if (!pevent->name) { - ret = -ENOMEM; - goto fail; - } - - pevent->sysfs.id = pevent->id; + pevent = pevent_alloc(name); + if (IS_ERR(pevent)) + return PTR_ERR(pevent); for_each_possible_cpu(cpu) { ret = persistent_event_open(cpu, pevent, attr, nr_pages); @@ -206,10 +230,7 @@ fail: out: if (atomic_dec_and_test(&pevent->refcount)) { pevent_sysfs_unregister(pevent); - if (pevent->id) - put_event_id(pevent->id); - kfree(pevent->name); - kfree(pevent); + pevent_free(pevent); } return ret; @@ -439,3 +460,104 @@ void __init perf_register_persistent(void) mutex_init(&per_cpu(pevents_lock, cpu)); } } + +/* + * Unclaim an event from a process. The event will remain in the system + * after closing the event's fd, it becomes persistent. + */ +int perf_event_unclaim(struct perf_event *event) +{ + struct pevent *pevent; + int cpu; + int ret; + + if (!try_get_event(event)) + return -ENOENT; + + /* task events not yet supported: */ + cpu = event->cpu; + if ((unsigned)cpu >= nr_cpu_ids) { + ret = -EINVAL; + goto fail_rb; + } + + /* + * Avoid grabbing an id, later checked again in pevent_add() + * with mmap_mutex held. + */ + if (event->pevent_id) { + ret = -EEXIST; + goto fail_rb; + } + + mutex_lock(&event->mmap_mutex); + if (event->rb) + ret = -EBUSY; + else + ret = perf_alloc_rb(event, CPU_BUFFER_NR_PAGES, 0); + mutex_unlock(&event->mmap_mutex); + + if (ret) + goto fail_rb; + + pevent = pevent_alloc(NULL); + if (IS_ERR(pevent)) { + ret = PTR_ERR(pevent); + goto fail_pevent; + } + + ret = pevent_add(pevent, event); + if (ret) + goto fail_add; + + ret = pevent_sysfs_register(pevent); + if (ret) + goto fail_sysfs; + + atomic_inc(&event->mmap_count); + + return pevent->id; +fail_sysfs: + pevent_del(pevent, cpu); +fail_add: + pevent_free(pevent); +fail_pevent: + mutex_lock(&event->mmap_mutex); + if (event->rb) + perf_free_rb(event); + mutex_unlock(&event->mmap_mutex); +fail_rb: + put_event(event); + return ret; +} + +/* + * Claim an event from a process. The event will be removed after all + * users disconnected from it, it's no longer persistent in the + * system. + */ +int perf_event_claim(struct perf_event *event) +{ + int cpu = event->cpu; + struct pevent *pevent; + + if ((unsigned)cpu >= nr_cpu_ids) + return -EINVAL; + + pevent = find_event(event->pevent_id); + if (!pevent) + return -EINVAL; + + event = pevent_del(pevent, cpu); + if (!event) + return -EINVAL; + + if (atomic_dec_and_test(&pevent->refcount)) { + pevent_sysfs_unregister(pevent); + pevent_free(pevent); + } + + persistent_event_release(event); + + return 0; +}