Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp1952305ima; Thu, 25 Oct 2018 07:29:40 -0700 (PDT) X-Google-Smtp-Source: AJdET5dU0Lu2VAYwnt2UZSFrmpCznAl6vAasCu+WNnUso7ctxI8tC6iHY09cfp09hmOgZJZt3t/W X-Received: by 2002:a62:75c7:: with SMTP id q190-v6mr1798933pfc.137.1540477780709; Thu, 25 Oct 2018 07:29:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540477780; cv=none; d=google.com; s=arc-20160816; b=GcHLZ85FDpyHxYroGPcixuk42K3zZEhy6pQOCujH5rG8cHtq+pfXV1mgl/xQmbZ8kQ xTHpRd2buu87WfA9oPR1zT05x/YM/1EAqtyDnK0E5qI5wVsYmVYMrzNRlHmjpYeywVdT yKCCwaY6c6+sVlKLod+WOd7I14i4lK7r3UW4LF11zgnIXgX8xs2ZZC2w6iOXKAZ8vi7M WTlRG+z5WuPyuixzL7Jx6PYG2KLT+mb+maDrp8uD7dZ5dKBHIwKaxf6+YWxVXCsuZNwC jQh85JhwYQCJ+vhJKF2Sb06RWoDX1y5eFFOsigz/eGHEe1hw+bIrXYzDfCs/4s2m2Vom bcYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=Ja1k+K0wgyCVxoArrv+WA4OTeB/Xlaug84nzU6XBhQM=; b=YN8AiwSwOA3DMY6z6+4NgN9gkltqvuIjNb3FCKsu+Id8GZtZ23RHj6iTKlAUsYf78A mFQpFtT7os03xWRg94dpx/ds7iBiURHzlIpQrP8XXlMpIIR2pjIx/Ksgjq8HK9+yGhI9 zNYOAM8Bj1NpReaAR2KI0AgWKqLeMhQk1fzBFnsUAhQfV/NGlHxyw6wjiIT9+0C3T2H1 RMoA4f7tv8TOG6bSDFx2MBe+au2vRKYNLHs0ASOpIJoGSgtTq2ilp9ThyWlmtzs/28QA U+ZZNov4vxEu3LIOlHs4ix1UoGZOsJCroXNcZCUAZNCXCdADRgY8u0LfAq/CjlxhuT0u sfiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FaKAX217; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m20-v6si8303723pgk.579.2018.10.25.07.29.02; Thu, 25 Oct 2018 07:29:40 -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; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FaKAX217; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730807AbeJYWw5 (ORCPT + 99 others); Thu, 25 Oct 2018 18:52:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:35242 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727722AbeJYWwz (ORCPT ); Thu, 25 Oct 2018 18:52:55 -0400 Received: from sasha-vm.mshome.net (unknown [167.98.65.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 59A5320848; Thu, 25 Oct 2018 14:19:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1540477198; bh=lzQ1ep5K0E4tagyxO+amRAnZmXoPLH/IoixOiwh8RJc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FaKAX217fh6gifa7sGq8mxtzTENf6zUeWXPyaWCi6WP8Cl8i7NSRMI5OYFG5t61U1 cyaNspn4yEe4NE0bd4DcxhQVA1ltgM1hhhNjISfwBz8q1846TeL9DyYEb3aRwsh1t3 NPRF1XjMpKgs/5fww2J+SNLF94eve4ajOwR4/FYI= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Stephane Eranian , Peter Zijlstra , vincent.weaver@maine.edu, jolsa@redhat.com, Arnaldo Carvalho de Melo , Linus Torvalds , Ingo Molnar , Sasha Levin Subject: [PATCH AUTOSEL 3.18 43/98] perf/rapl: Fix sysfs_show() initialization for RAPL PMU Date: Thu, 25 Oct 2018 10:17:58 -0400 Message-Id: <20181025141853.214051-43-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181025141853.214051-1-sashal@kernel.org> References: <20181025141853.214051-1-sashal@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Stephane Eranian [ Upstream commit 433678bdc6ed39f053c55da96b51de5bf0aeebb1 ] This patch fixes a problem with the initialization of the sysfs_show() routine for the RAPL PMU. The current code was wrongly relying on the EVENT_ATTR_STR() macro which uses the events_sysfs_show() function in the x86 PMU code. That function itself was relying on the x86_pmu data structure. Yet RAPL and the core PMU (x86_pmu) have nothing to do with each other. They should therefore not interact with each other. The x86_pmu structure is initialized at boot time based on the host CPU model. When the host CPU is not supported, the x86_pmu remains uninitialized and some of the callbacks it contains are NULL. The false dependency with x86_pmu could potentially cause crashes in case the x86_pmu is not initialized while the RAPL PMU is. This may, for instance, be the case in virtualized environments. This patch fixes the problem by using a private sysfs_show() routine for exporting the RAPL PMU events. Signed-off-by: Stephane Eranian Signed-off-by: Peter Zijlstra (Intel) Link: http://lkml.kernel.org/r/20150113225953.GA21525@thinkpad Cc: vincent.weaver@maine.edu Cc: jolsa@redhat.com Cc: Arnaldo Carvalho de Melo Cc: Linus Torvalds Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- arch/x86/kernel/cpu/perf_event_intel_rapl.c | 44 +++++++++++++++------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c index 611d821eac1a..040915c87cb6 100644 --- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c +++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c @@ -103,6 +103,13 @@ static struct kobj_attribute format_attr_##_var = \ #define RAPL_CNTR_WIDTH 32 /* 32-bit rapl counters */ +#define RAPL_EVENT_ATTR_STR(_name, v, str) \ +static struct perf_pmu_events_attr event_attr_##v = { \ + .attr = __ATTR(_name, 0444, rapl_sysfs_show, NULL), \ + .id = 0, \ + .event_str = str, \ +}; + struct rapl_pmu { spinlock_t lock; int hw_unit; /* 1/2^hw_unit Joule */ @@ -383,23 +390,36 @@ static struct attribute_group rapl_pmu_attr_group = { .attrs = rapl_pmu_attrs, }; -EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01"); -EVENT_ATTR_STR(energy-pkg , rapl_pkg, "event=0x02"); -EVENT_ATTR_STR(energy-ram , rapl_ram, "event=0x03"); -EVENT_ATTR_STR(energy-gpu , rapl_gpu, "event=0x04"); +static ssize_t rapl_sysfs_show(struct device *dev, + struct device_attribute *attr, + char *page) +{ + struct perf_pmu_events_attr *pmu_attr = \ + container_of(attr, struct perf_pmu_events_attr, attr); + + if (pmu_attr->event_str) + return sprintf(page, "%s", pmu_attr->event_str); + + return 0; +} + +RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01"); +RAPL_EVENT_ATTR_STR(energy-pkg , rapl_pkg, "event=0x02"); +RAPL_EVENT_ATTR_STR(energy-ram , rapl_ram, "event=0x03"); +RAPL_EVENT_ATTR_STR(energy-gpu , rapl_gpu, "event=0x04"); -EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules"); -EVENT_ATTR_STR(energy-pkg.unit , rapl_pkg_unit, "Joules"); -EVENT_ATTR_STR(energy-ram.unit , rapl_ram_unit, "Joules"); -EVENT_ATTR_STR(energy-gpu.unit , rapl_gpu_unit, "Joules"); +RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules"); +RAPL_EVENT_ATTR_STR(energy-pkg.unit , rapl_pkg_unit, "Joules"); +RAPL_EVENT_ATTR_STR(energy-ram.unit , rapl_ram_unit, "Joules"); +RAPL_EVENT_ATTR_STR(energy-gpu.unit , rapl_gpu_unit, "Joules"); /* * we compute in 0.23 nJ increments regardless of MSR */ -EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10"); -EVENT_ATTR_STR(energy-pkg.scale, rapl_pkg_scale, "2.3283064365386962890625e-10"); -EVENT_ATTR_STR(energy-ram.scale, rapl_ram_scale, "2.3283064365386962890625e-10"); -EVENT_ATTR_STR(energy-gpu.scale, rapl_gpu_scale, "2.3283064365386962890625e-10"); +RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890625e-10"); +RAPL_EVENT_ATTR_STR(energy-pkg.scale, rapl_pkg_scale, "2.3283064365386962890625e-10"); +RAPL_EVENT_ATTR_STR(energy-ram.scale, rapl_ram_scale, "2.3283064365386962890625e-10"); +RAPL_EVENT_ATTR_STR(energy-gpu.scale, rapl_gpu_scale, "2.3283064365386962890625e-10"); static struct attribute *rapl_events_srv_attr[] = { EVENT_PTR(rapl_cores), -- 2.17.1