From patchwork Tue Jun 11 16:42:40 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Richter X-Patchwork-Id: 17901 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 9FC1625DF1 for ; Thu, 13 Jun 2013 10:57:33 +0000 (UTC) Received: by mail-ve0-f197.google.com with SMTP id d10sf11181542vea.8 for ; Thu, 13 Jun 2013 03:57:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-beenthere:x-forwarded-to:x-forwarded-for :delivered-to:resent-from:resent-date:resent-message-id:resent-to :sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-google-group-id:list-post :list-help:list-archive:list-unsubscribe; bh=I98GnwEtI099E76xlptk0mbUmFxj3z7lHvLFQw3nF4s=; b=VRajuOt8M8B43FERMIEMngmiWSsq1drLCdKMWIkhyoETU+K96epFGv6wFt1vsgiVbs RnB+DB3AAkjiT3IFFW5GUlCEpRy0Pu7//GYeF9fgB65lwzHZWvyqyNnlWa69eE7Jweqk KAnSnkPCGue8NTObTQ5+jtG/Fq/ijb1tJ7d9oJxJpdUuNPUsWpMEuLOzBznYgpTHt5kn i0R52YzRaRl3l4kdAZZjyiHG5tU1zQOvqe+WrD8XTkd3aV5rNkzTfc071wEnc9JZ3CNh R0UFQ1hPO1L3plqMvpAfcTxHvWhkEKFOKwHUaPe2upYRWzVjSXKSpeq1AHcborzIVjzD VSKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-beenthere:x-forwarded-to:x-forwarded-for :delivered-to:resent-from:resent-date:resent-message-id:resent-to :sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-google-group-id:list-post:list-help:list-archive:list-unsubscribe; bh=I98GnwEtI099E76xlptk0mbUmFxj3z7lHvLFQw3nF4s=; b=oZe7kkFFcTStYOuXYf8z4SscCocw0gtD6Lm6WeHKhVemLNkVw/M6666+9qTXMuKEE5 t7++Qn2w8dC9yVMXZNJaaSQ9JnD4rmtbeWVWCreoRnsP1KsxXqSQB/UhIm2bJgvIKyLv WVtMGgCw239bTA9uG7GrtgMUkUB8/6P+ruFTyJcPaVz7pgsCGHfrZHvjpj1ncBUjeon5 2b3D1/sv5BQ6V0tO3lc5dK3E7OFLzW1E15C/9ZPSfNxL3DrCVu7ZYQN0WvQiaT3atxD0 J4w41FpQ6A2ilmejLz4sEpD6PXUq3D+14Qo4zKwELfPsLUCO5E6HZ/wvguSvt6AeN901 aZUw== X-Received: by 10.224.86.200 with SMTP id t8mr1311085qal.0.1371121053389; Thu, 13 Jun 2013 03:57:33 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.122.5 with SMTP id lo5ls3978900qeb.27.gmail; Thu, 13 Jun 2013 03:57:33 -0700 (PDT) X-Received: by 10.52.101.230 with SMTP id fj6mr57959vdb.125.1371121053209; Thu, 13 Jun 2013 03:57:33 -0700 (PDT) Received: from mail-vc0-f173.google.com (mail-vc0-f173.google.com [209.85.220.173]) by mx.google.com with ESMTPS id s7si8192850vcr.24.2013.06.13.03.57.33 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Jun 2013 03:57:33 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.173 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.173; Received: by mail-vc0-f173.google.com with SMTP id ht10so3537724vcb.18 for ; Thu, 13 Jun 2013 03:57:33 -0700 (PDT) X-Received: by 10.58.211.7 with SMTP id my7mr72425vec.54.1371121053089; Thu, 13 Jun 2013 03:57:33 -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.58.191.99 with SMTP id gx3csp16625vec; Thu, 13 Jun 2013 03:57:32 -0700 (PDT) X-Received: by 10.205.13.194 with SMTP id pn2mr63354bkb.76.1371121051941; Thu, 13 Jun 2013 03:57:31 -0700 (PDT) Received: from mail-bk0-x229.google.com (mail-bk0-x229.google.com [2a00:1450:4008:c01::229]) by mx.google.com with ESMTPS id rg4si3527792bkb.280.2013.06.13.03.57.31 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Jun 2013 03:57:31 -0700 (PDT) Received-SPF: pass (google.com: domain of rric.net@gmail.com designates 2a00:1450:4008:c01::229 as permitted sender) client-ip=2a00:1450:4008:c01::229; Received: by mail-bk0-f41.google.com with SMTP id jc3so5101661bkc.0 for ; Thu, 13 Jun 2013 03:57:31 -0700 (PDT) X-Received: by 10.204.165.15 with SMTP id g15mr47327bky.140.1371121051384; Thu, 13 Jun 2013 03:57:31 -0700 (PDT) Received: from rric.localhost (g224198135.adsl.alicedsl.de. [92.224.198.135]) by mx.google.com with ESMTPSA id da16sm9180251bkb.2.2013.06.13.03.57.30 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 13 Jun 2013 03:57:30 -0700 (PDT) Resent-From: Robert Richter Resent-Date: Thu, 13 Jun 2013 12:57:28 +0200 Resent-Message-ID: <20130613105728.GO12218@rric.localhost> Resent-To: linaro-kernel@lists.linaro.org, patches@linaro.org Received: from rric.localhost (f053086247.adsl.alicedsl.de. [78.53.86.247]) by mx.google.com with ESMTPSA id b12sm6160884bkz.0.2013.06.11.09.43.11 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 11 Jun 2013 09:43:11 -0700 (PDT) Sender: Robert Richter From: Robert Richter To: Borislav Petkov Cc: Ingo Molnar , Peter Zijlstra , Arnaldo Carvalho de Melo , Jiri Olsa , linux-kernel@vger.kernel.org, Robert Richter , Robert Richter Subject: [PATCH v2 14/14] perf, persistent: Allow multiple users for an event Date: Tue, 11 Jun 2013 18:42:40 +0200 Message-Id: <1370968960-22527-15-git-send-email-rric@kernel.org> X-Mailer: git-send-email 1.8.1.1 In-Reply-To: <1370968960-22527-1-git-send-email-rric@kernel.org> References: <1370968960-22527-1-git-send-email-rric@kernel.org> X-Gm-Message-State: ALoCoQkRJaRqbR2X9JXMbz4Bh2GQNVvWbptRiOXTpr+Hkbim4/UrdijJTI8GzALXQDwnks76MzzO X-Original-Sender: rric.net@gmail.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.173 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 Usually a fd close leads to the release of the event too. For persistent events this is different as the events should be permanently enabled in the system. Using reference counting to avoid releasing an event during a fd close. This also allows it to have multiple users (open file descriptors) for a single persistent event. While at this, we don't need desc->fd any longer. The fd is attached to a task and reference counting keeps the event. Removing desc->fd. Signed-off-by: Robert Richter Signed-off-by: Robert Richter --- kernel/events/persistent.c | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/kernel/events/persistent.c b/kernel/events/persistent.c index 8be7c05..dd20b55 100644 --- a/kernel/events/persistent.c +++ b/kernel/events/persistent.c @@ -11,7 +11,6 @@ struct pers_event_desc { struct perf_event *event; struct list_head plist; - int fd; }; struct pers_event { @@ -88,6 +87,18 @@ out: return event; } +static void detach_persistent_event(struct pers_event_desc *desc) +{ + list_del(&desc->plist); + kfree(desc); +} + +static void release_persistent_event(struct perf_event *event) +{ + perf_event_disable(event); + perf_event_release_kernel(event); +} + static void del_persistent_event(int cpu, struct perf_event_attr *attr) { struct pers_event_desc *desc; @@ -100,12 +111,14 @@ static void del_persistent_event(int cpu, struct perf_event_attr *attr) goto out; event = desc->event; - list_del(&desc->plist); - - perf_event_disable(event); - perf_event_release_kernel(event); - put_unused_fd(desc->fd); - kfree(desc); + /* + * We primarily want to remove desc from the list. If there + * are no open files, the refcount is 0 and we need to release + * the event too. + */ + detach_persistent_event(desc); + if (atomic_long_dec_and_test(&event->refcount)) + release_persistent_event(event); out: mutex_unlock(&per_cpu(pers_events_lock, cpu)); } @@ -182,6 +195,7 @@ fail: int perf_get_persistent_event_fd(unsigned cpu, struct perf_event_attr *attr) { struct pers_event_desc *desc; + struct perf_event *event; int event_fd = -ENODEV; if (cpu >= (unsigned)nr_cpu_ids) @@ -190,13 +204,25 @@ int perf_get_persistent_event_fd(unsigned cpu, struct perf_event_attr *attr) mutex_lock(&per_cpu(pers_events_lock, cpu)); desc = get_persistent_event(cpu, attr); - if (!desc) + + /* Increment refcount to keep event on put_event() */ + if (!desc || !atomic_long_inc_not_zero(&desc->event->refcount)) goto out; event_fd = anon_inode_getfd("[pers_event]", &perf_fops, desc->event, O_RDONLY); - if (event_fd >= 0) - desc->fd = event_fd; + + if (event_fd < 0) { + event = desc->event; + if (WARN_ON(atomic_long_dec_and_test(&event->refcount))) { + /* + * May not happen since decrementing refcount is + * protected by pers_events_lock. + */ + detach_persistent_event(desc); + release_persistent_event(event); + } + } out: mutex_unlock(&per_cpu(pers_events_lock, cpu));