From patchwork Thu Aug 22 14:13:27 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Richter X-Patchwork-Id: 19418 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ye0-f198.google.com (mail-ye0-f198.google.com [209.85.213.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id E2695248E6 for ; Thu, 22 Aug 2013 14:14:57 +0000 (UTC) Received: by mail-ye0-f198.google.com with SMTP id m12sf1525655yen.5 for ; Thu, 22 Aug 2013 07:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:delivered-to:sender:from:to:cc:subject:date:message-id :in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=a8Opqt3tk+bej0KaNKO0P/GYglsu6wWJ69u7t1tUS0o=; b=0Mm4dmw+LGR5JG+Vxpdm4PgpdPpFAR+dXuywXh8gHtiJ4MypvpLyCW0+fwzoC+9vCg Aj9zPQHuOrjtOCgtcg80fu4bwrya8RPr8gpxKmFxdnObjJBAOZq6tZRvTCQMdwRqnCKc bkRCzIdlnmXZL0HTjqLV+hXrxtR9OoBgqT89rUsDXDP2dJm3B/PPpus4CX0IqxFgf0iO 6U5DU8BJXILGQBxbxUhOyyHkCTkUDFl9UfWSdH8KvZ7I/cKDw1T/Grqc6bDq8ev5MURe BQfFVEqx5Mgkt4V+5S3YCfqIXQzM8ks43V57VMcRwGCYagCiGU0vJ/cEy5TXtNOnW8r0 Mwvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-gm-message-state:delivered-to:sender:from:to:cc :subject:date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=a8Opqt3tk+bej0KaNKO0P/GYglsu6wWJ69u7t1tUS0o=; b=B2jgdlXjBQzKZ6tTaAGOjOBJ1zMffK+9iycDAU2+6zPkB6KVRu2Ts+2gIhfglvkkHS hDGn0+6/v7mQx2hUrvTCVj2ysjNGvVFrx09MOpHM/A/ooPY5EveJztLZtFsob8QhrgiW j/L7ACEQ6Hz2KSXSXp7ttHJ3s3QcVfDPF6iM3K54m//qme0XO3BS+rGHgoKVaj6DQVjy 8LU6pTn2x6zN2+Fj44/1Ceo/+vXRi1RuwGpK5ouHDI2CSEjuP6m1Wxk6zVffat9gTYry aDTSfZl8BYN6xXbs3IT+6huSoafDwzIncLXtj3x+n+LEVp2/IQii3eTEglwJicj8wV/e IWDQ== X-Received: by 10.236.109.134 with SMTP id s6mr4492236yhg.32.1377180897695; Thu, 22 Aug 2013 07:14:57 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.110.134 with SMTP id ia6ls554004qeb.32.gmail; Thu, 22 Aug 2013 07:14:57 -0700 (PDT) X-Received: by 10.52.24.4 with SMTP id q4mr9531482vdf.6.1377180897467; Thu, 22 Aug 2013 07:14:57 -0700 (PDT) Received: from mail-ve0-x230.google.com (mail-ve0-x230.google.com [2607:f8b0:400c:c01::230]) by mx.google.com with ESMTPS id o4si4012946vcx.58.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 22 Aug 2013 07:14:57 -0700 (PDT) Received-SPF: neutral (google.com: 2607:f8b0:400c:c01::230 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=2607:f8b0:400c:c01::230; Received: by mail-ve0-f176.google.com with SMTP id b10so1541220vea.35 for ; Thu, 22 Aug 2013 07:14:57 -0700 (PDT) X-Gm-Message-State: ALoCoQm13OuovC9VY9p2irfNqM1DxznBuhtZZ4aCUYWPIy3+ThA6ad+Q7/gEN7shbEKl02Xs6YLa X-Received: by 10.58.97.238 with SMTP id ed14mr283043veb.34.1377180897375; Thu, 22 Aug 2013 07:14:57 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp33226vcz; Thu, 22 Aug 2013 07:14:56 -0700 (PDT) X-Received: by 10.205.20.133 with SMTP id qo5mr10624300bkb.6.1377180895725; Thu, 22 Aug 2013 07:14:55 -0700 (PDT) Received: from mail-bk0-x22a.google.com (mail-bk0-x22a.google.com [2a00:1450:4008:c01::22a]) by mx.google.com with ESMTPS id re8si2306610bkb.130.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 22 Aug 2013 07:14:55 -0700 (PDT) Received-SPF: pass (google.com: domain of rric.net@gmail.com designates 2a00:1450:4008:c01::22a as permitted sender) client-ip=2a00:1450:4008:c01::22a; Received: by mail-bk0-f42.google.com with SMTP id my10so720333bkb.1 for ; Thu, 22 Aug 2013 07:14:54 -0700 (PDT) X-Received: by 10.204.189.144 with SMTP id de16mr2591601bkb.26.1377180894449; Thu, 22 Aug 2013 07:14:54 -0700 (PDT) Received: from rric.localhost (g224195237.adsl.alicedsl.de. [92.224.195.237]) by mx.google.com with ESMTPSA id jh13sm3079991bkb.13.1969.12.31.16.00.00 (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 22 Aug 2013 07:14:53 -0700 (PDT) Sender: Robert Richter From: Robert Richter To: Peter Zijlstra Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Borislav Petkov , Jiri Olsa , linux-kernel@vger.kernel.org, Robert Richter , Vince Weaver , Robert Richter Subject: [PATCH v3 12/12] [RFC] perf, persistent: ioctl functions to control persistency Date: Thu, 22 Aug 2013 16:13:27 +0200 Message-Id: <1377180807-12758-13-git-send-email-rric@kernel.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1377180807-12758-1-git-send-email-rric@kernel.org> References: <1377180807-12758-1-git-send-email-rric@kernel.org> X-Original-Sender: rric.net@gmail.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 2607:f8b0:400c:c01::230 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; dkim=pass header.i=@gmail.com Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: 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 detach or attach an event to or from a process. The PERF_EVENT_IOC_DETACH 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_ATTACH 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_DETACH (Since Linux 3.xx) Detach 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_ATTACH (Since Linux 3.xx) Attach 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. Cc: Vince Weaver Signed-off-by: Robert Richter Signed-off-by: Robert Richter --- 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 2b84b97..82a8244 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -324,6 +324,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_DETACH _IO ('$', 8) +#define PERF_EVENT_IOC_ATTACH _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 d9d6e67..8d5c6e3 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -3622,6 +3622,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_DETACH: + return perf_event_detach(event); + + case PERF_EVENT_IOC_ATTACH: + return perf_event_attach(event); + default: return -ENOTTY; } diff --git a/kernel/events/internal.h b/kernel/events/internal.h index 94c3f73..f9bc15f 100644 --- a/kernel/events/internal.h +++ b/kernel/events/internal.h @@ -195,5 +195,7 @@ extern void perf_free_rb(struct perf_event *event); extern int perf_get_fd(struct perf_event *event); extern int perf_get_persistent_event_fd(int cpu, int id); extern void __init perf_register_persistent(void); +extern int perf_event_detach(struct perf_event *event); +extern int perf_event_attach(struct perf_event *event); #endif /* _KERNEL_EVENTS_INTERNAL_H */ diff --git a/kernel/events/persistent.c b/kernel/events/persistent.c index a0ef6d4..e156afe 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 @@ persistent_open(char *name, struct perf_event_attr *attr, int nr_pages) 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)); } } + +/* + * Detach an event from a process. The event will remain in the system + * after closing the event's fd, it becomes persistent. + */ +int perf_event_detach(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; +} + +/* + * Attach an event to a process. The event will be removed after all + * users disconnected from it, it's no longer persistent in the + * system. + */ +int perf_event_attach(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; +}