From patchwork Tue Jun 11 16:42:35 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Richter X-Patchwork-Id: 17896 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qe0-f69.google.com (mail-qe0-f69.google.com [209.85.128.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id C39A925DF1 for ; Thu, 13 Jun 2013 10:57:16 +0000 (UTC) Received: by mail-qe0-f69.google.com with SMTP id a11sf7413254qen.8 for ; Thu, 13 Jun 2013 03:57:16 -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=NQ+CVnfwTD4eMcUwCq+uSklhVJmc5Wd1UQ3k8OaqneE=; b=cPrdfkgxxj9yYxHHOruxUvGzAPlfJJWtTlD9tEfB4WTvHidGPuE+0AcJNfejhOLyg/ sRUhjns6oM7EHS/hvPLG3aiQv2grV4ZnSkIt1vaUiGyoLaCuehLLUFflrYy+CkmTe50f 2xOU5RAb1J6ruOcVTaoF5K5q611hpvh3MQzb72HoxcMYfzQIwb9PRrD8HsabPn5AKNgb npulOqDV/qyeK3aEQGXdCABWjheZUOUR6ML80M2JgOZXEZvcPCsxnnakwk5y4J2qf4Cg nO8YtTNldTTHI63FaNi+T+dju3SulYBTCdu/M1QJGt3TsnVTme9lP4WxsIWO4AItneC0 rK9A== 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=NQ+CVnfwTD4eMcUwCq+uSklhVJmc5Wd1UQ3k8OaqneE=; b=EAOpZgtZuE1COHuK7UZFKhl1Uelp/WnFu7EnVwHBS2nkJnVP3b/mrUo7P1hy++4ABZ QEDv5nnXVGpn49IOu12viSEhIPCcVfYINStUDC2ZM7RN/HW6npO0Afsbx2JIEfqJYkcy bNenpiyWdm3uW+rhrNy20y+hKGd+1jHdu+YcqDrrnech5XTCRmtnNNeY/x+8dt5prnjj TqDxKh+ZrFGbsbYb1CZduQyYfFe5wvgXsHoI8wzAh9h05gprJ5GcHZv5RxqV7Zj2N3Bd A3Fxu61Dafd5TeRUavijSYad7lHXhHVYUMRy9c/QjqX13KO8feFrbBLcBotNTEDs68WF pJFw== X-Received: by 10.224.174.145 with SMTP id t17mr1306241qaz.4.1371121036587; Thu, 13 Jun 2013 03:57:16 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.127.114 with SMTP id nf18ls3854572qeb.8.gmail; Thu, 13 Jun 2013 03:57:16 -0700 (PDT) X-Received: by 10.52.230.201 with SMTP id ta9mr61160vdc.119.1371121036436; Thu, 13 Jun 2013 03:57:16 -0700 (PDT) Received: from mail-vb0-x22f.google.com (mail-vb0-x22f.google.com [2607:f8b0:400c:c02::22f]) by mx.google.com with ESMTPS id to9si10200069vdc.45.2013.06.13.03.57.16 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Jun 2013 03:57:16 -0700 (PDT) Received-SPF: neutral (google.com: 2607:f8b0:400c:c02::22f 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:c02::22f; Received: by mail-vb0-f47.google.com with SMTP id x14so6740953vbb.6 for ; Thu, 13 Jun 2013 03:57:16 -0700 (PDT) X-Received: by 10.220.144.67 with SMTP id y3mr120072vcu.17.1371121036318; Thu, 13 Jun 2013 03:57:16 -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 gx3csp16613vec; Thu, 13 Jun 2013 03:57:15 -0700 (PDT) X-Received: by 10.205.99.131 with SMTP id cs3mr42797bkc.169.1371121035221; Thu, 13 Jun 2013 03:57:15 -0700 (PDT) Received: from mail-bk0-x22c.google.com (mail-bk0-x22c.google.com [2a00:1450:4008:c01::22c]) by mx.google.com with ESMTPS id kh7si2351492bkb.147.2013.06.13.03.57.14 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Jun 2013 03:57:15 -0700 (PDT) Received-SPF: pass (google.com: domain of rric.net@gmail.com designates 2a00:1450:4008:c01::22c as permitted sender) client-ip=2a00:1450:4008:c01::22c; Received: by mail-bk0-f44.google.com with SMTP id r7so5101808bkg.3 for ; Thu, 13 Jun 2013 03:57:14 -0700 (PDT) X-Received: by 10.204.172.80 with SMTP id k16mr51454bkz.123.1371121034662; Thu, 13 Jun 2013 03:57:14 -0700 (PDT) Received: from rric.localhost (g224198135.adsl.alicedsl.de. [92.224.198.135]) by mx.google.com with ESMTPSA id jy7sm9174277bkb.6.2013.06.13.03.57.13 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 13 Jun 2013 03:57:14 -0700 (PDT) Resent-From: Robert Richter Resent-Date: Thu, 13 Jun 2013 12:57:11 +0200 Resent-Message-ID: <20130613105711.GJ12218@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.03 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 11 Jun 2013 09:43:04 -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 09/14] perf, persistent: Protect event lists with mutex Date: Tue, 11 Jun 2013 18:42:35 +0200 Message-Id: <1370968960-22527-10-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: ALoCoQkSnBm5BugWV7U2TWgKouQmuGrj4cNHy+sei+/ivY/ccs7S2kYKDpM8lCKDq8dFxKc6tAfw X-Original-Sender: rric.net@gmail.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 2607:f8b0:400c:c02::22f 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 Protect esp. access to struct pers_event_desc *desc. There are race conditions possible where the descriptor could be removed from list while it is used. Signed-off-by: Robert Richter Signed-off-by: Robert Richter --- kernel/events/persistent.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/kernel/events/persistent.c b/kernel/events/persistent.c index 16ed47c..586cea5 100644 --- a/kernel/events/persistent.c +++ b/kernel/events/persistent.c @@ -15,6 +15,7 @@ struct pers_event_desc { }; static DEFINE_PER_CPU(struct list_head, pers_events); +static DEFINE_PER_CPU(struct mutex, pers_events_lock); static struct pers_event_desc *get_persistent_event(int cpu, struct perf_event_attr *attr) @@ -37,9 +38,13 @@ add_persistent_event_on_cpu(unsigned int cpu, struct perf_event_attr *attr, struct pers_event_desc *desc; struct ring_buffer *buf; + mutex_lock(&per_cpu(pers_events_lock, cpu)); + desc = kzalloc(sizeof(*desc), GFP_KERNEL); - if (!desc) - return ERR_PTR(-ENOMEM); + if (!desc) { + event = ERR_PTR(-ENOMEM); + goto out; + } event = perf_event_create_kernel_counter(attr, cpu, NULL, NULL, NULL); if (IS_ERR(event)) @@ -66,6 +71,7 @@ err_rb: err_event: kfree(desc); out: + mutex_unlock(&per_cpu(pers_events_lock, cpu)); return event; } @@ -74,9 +80,11 @@ static void del_persistent_event(int cpu, struct perf_event_attr *attr) struct pers_event_desc *desc; struct perf_event *event; + mutex_lock(&per_cpu(pers_events_lock, cpu)); + desc = get_persistent_event(cpu, attr); if (!desc) - return; + goto out; event = desc->event; list_del(&desc->plist); @@ -85,6 +93,8 @@ static void del_persistent_event(int cpu, struct perf_event_attr *attr) perf_event_release_kernel(event); put_unused_fd(desc->fd); kfree(desc); +out: + mutex_unlock(&per_cpu(pers_events_lock, cpu)); } /* @@ -138,28 +148,33 @@ int perf_add_persistent_event_by_id(int id) int perf_get_persistent_event_fd(unsigned cpu, struct perf_event_attr *attr) { struct pers_event_desc *desc; - int event_fd; + int event_fd = -ENODEV; if (cpu >= (unsigned)nr_cpu_ids) return -EINVAL; + mutex_lock(&per_cpu(pers_events_lock, cpu)); + desc = get_persistent_event(cpu, attr); if (!desc) - return -ENODEV; + goto out; event_fd = anon_inode_getfd("[pers_event]", &perf_fops, desc->event, O_RDONLY); if (event_fd >= 0) desc->fd = event_fd; +out: + mutex_unlock(&per_cpu(pers_events_lock, cpu)); return event_fd; } - void __init persistent_events_init(void) { - int i; + int cpu; - for_each_possible_cpu(i) - INIT_LIST_HEAD(&per_cpu(pers_events, i)); + for_each_possible_cpu(cpu) { + INIT_LIST_HEAD(&per_cpu(pers_events, cpu)); + mutex_init(&per_cpu(pers_events_lock, cpu)); + } }