Received: by 10.223.164.202 with SMTP id h10csp591891wrb; Mon, 13 Nov 2017 11:23:53 -0800 (PST) X-Google-Smtp-Source: AGs4zMZYbNhULPUazZoyVGSX4B6neRWEOJVANodN3j1HCK9XkLDrBo6dzYR1gXR8zz4JSx6FhkOM X-Received: by 10.98.18.79 with SMTP id a76mr11032652pfj.204.1510601033187; Mon, 13 Nov 2017 11:23:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510601033; cv=none; d=google.com; s=arc-20160816; b=pzzSbSKv1qzJrfe3yxnHWzdnu0x46SsBFFS2W2pjJcQrENm8JSnBTUgW/lzDk9UFiw Tct+mH5YShQNeIuzelx+kqIKU74vAroVYxHNPuwjJlgTpRxyuiPJuIrrn7AV7cc7/cBZ e9QbJ+Zh/jIISHh+9/wGLWlZ+u3YVtuxsm1k37wscwPYEcmbdrnM1RmBzcexNoCJ4z/C nw1HvfiLIICWeh23y/4g9oeuZeDRihP/rN+q0QajktVoOfR0/vrLcIG7p6j90ojxWzHZ oV4PbIn/dCvI4a3RY6fI6DivvyuxzOGKDT4ikHni9A54yiS9Lk8SqSzeniIE/fkdQZxk 66yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :dlp-reaction:dlp-version:dlp-product:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:arc-authentication-results; bh=INFymdxhYuly0cZswm3JUXVhbZcFfnErqs6NVi136Ac=; b=mtcsa5dOrCaKALJkow23NO94rP5YDP9v8l4fccQziWuH0siPptMGtOaOSW6nMjy/N6 58vlqiPjQ3Ke1QZF/W96MWkQy9Qfok5jGf1/7s0pAx+AzpSoeNuG+nk/0DQKvnMzrlYL debmzZ5rg3JXHRO5Az0l8tIBGl2Nn8bByg3FTcj9SYVWZ+JPx1LVrMECmi+F81W5ZQiv nXL/ocjLcmiiBL8IX2vCahUrF+1d6ingfj6U6WpJY4mpVlZURJ6y5B4aYrKxRJZL43Ad snfOLUc5vGXEkWzQ0bPU6hl7MuioZ0av8wr/XmAvIAgYuPydGyyAATcCKfygiQLmvwaY HphA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w22si14740964plk.381.2017.11.13.11.23.40; Mon, 13 Nov 2017 11:23:53 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754631AbdKMTWs convert rfc822-to-8bit (ORCPT + 87 others); Mon, 13 Nov 2017 14:22:48 -0500 Received: from mga11.intel.com ([192.55.52.93]:29676 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754305AbdKMTWn (ORCPT ); Mon, 13 Nov 2017 14:22:43 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Nov 2017 11:22:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,389,1505804400"; d="scan'208";a="1488264" Received: from orsmsx103.amr.corp.intel.com ([10.22.225.130]) by FMSMGA003.fm.intel.com with ESMTP; 13 Nov 2017 11:22:42 -0800 Received: from orsmsx111.amr.corp.intel.com ([169.254.12.135]) by ORSMSX103.amr.corp.intel.com ([169.254.5.89]) with mapi id 14.03.0319.002; Mon, 13 Nov 2017 11:22:41 -0800 From: "Dey, Megha" To: Peter Zijlstra , Megha Dey CC: "x86@kernel.org" , "linux-kernel@vger.kernel.org" , "linux-doc@vger.kernel.org" , "tglx@linutronix.de" , "mingo@redhat.com" , "hpa@zytor.com" , "andriy.shevchenko@linux.intel.com" , "kstewart@linuxfoundation.org" , "Yu, Yu-cheng" , "Brown, Len" , "gregkh@linuxfoundation.org" , "acme@kernel.org" , "alexander.shishkin@linux.intel.com" , "jolsa@redhat.com" , "namhyung@kernel.org" , "vikas.shivappa@linux.intel.com" , "pombredanne@nexb.com" , "me@kylehuey.com" , "bp@suse.de" , "Andrejczuk, Grzegorz" , "Luck, Tony" , "corbet@lwn.net" , "Shankar, Ravi V" Subject: RE: [PATCH V1 2/3] perf/x86/intel/bm.c: Add Intel Branch Monitoring support Thread-Topic: [PATCH V1 2/3] perf/x86/intel/bm.c: Add Intel Branch Monitoring support Thread-Index: AQHTWzC6dld/j8KdRkeXxuUzEdtlu6MSi2AAgAAh/QA= Date: Mon, 13 Nov 2017 19:22:40 +0000 Message-ID: References: <1510435206-16110-1-git-send-email-megha.dey@linux.intel.com> <1510435206-16110-3-git-send-email-megha.dey@linux.intel.com> <20171113090016.o2l6356ktkgan7ch@hirez.programming.kicks-ass.net> In-Reply-To: <20171113090016.o2l6356ktkgan7ch@hirez.programming.kicks-ass.net> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiYzBiZDRjNTAtMDlhZi00ZDYyLTlkN2UtYWFjNTFmODI4MzUzIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX0lDIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE2LjUuOS4zIiwiVHJ1c3RlZExhYmVsSGFzaCI6InRxbEdoUmtVT0t0MnpIaWxHTmhKMUxIXC9yWm5rbHNLcFYySEJiTkU3QWFnPSJ9 x-ctpclassification: CTP_IC dlp-product: dlpe-windows dlp-version: 11.0.0.116 dlp-reaction: no-action x-originating-ip: [10.22.254.140] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org >-----Original Message----- >From: Peter Zijlstra [mailto:peterz@infradead.org] >Sent: Monday, November 13, 2017 1:00 AM >To: Megha Dey >Cc: x86@kernel.org; linux-kernel@vger.kernel.org; linux- >doc@vger.kernel.org; tglx@linutronix.de; mingo@redhat.com; >hpa@zytor.com; andriy.shevchenko@linux.intel.com; >kstewart@linuxfoundation.org; Yu, Yu-cheng ; >Brown, Len ; gregkh@linuxfoundation.org; >acme@kernel.org; alexander.shishkin@linux.intel.com; jolsa@redhat.com; >namhyung@kernel.org; vikas.shivappa@linux.intel.com; >pombredanne@nexb.com; me@kylehuey.com; bp@suse.de; Andrejczuk, >Grzegorz ; Luck, Tony >; corbet@lwn.net; Shankar, Ravi V >; Dey, Megha >Subject: Re: [PATCH V1 2/3] perf/x86/intel/bm.c: Add Intel Branch >Monitoring support > >On Sat, Nov 11, 2017 at 01:20:05PM -0800, Megha Dey wrote: >> Currently, the cannonlake family of Intel processors support the >> branch monitoring feature. Intel's Branch monitoring feature is trying >> to utilize heuristics to detect the occurrence of an ROP (Return >> Oriented Programming) attack. >> >> A perf-based kernel driver has been used to monitor the occurrence of >> one of the 6 branch monitoring events. There are 2 counters that each >> can select between one of these events for evaluation over a specified >> instruction window size (0 to 1023). For each counter, a threshold >> value >> (0 to 127) can be configured to set a point at which ROP detection >> event action is taken (determined by user-space). Each task can >> monitor a maximum of 2 events at any given time. >> >> Apart from window_size(global) and threshold(per-counter), various >> sysfs entries are provided for the user to configure: guest_disable, >> lbr_freeze, window_cnt_sel, cnt_and_mode (all global) and >mispred_evt_cnt(per-counter). >> For all events belonging to the same task, the global parameters are >> shared. > >Is there any sensible documentation on this except the MSR listings? I have documented these sysfs entries in the next patch of this patch set: Add Documentation for branch monitoring. Apart from that, unfortunately there is only the MSR listings. > >> >> Everytime a task is scheduled out, we save current window and count >> associated with the event being monitored. When the task is scheduled >> next, we start counting from previous count associated with this event. >> Thus, a full context switch in this case is not necessary. > >What? That doesn't make any sense. The fact that we scheduled out and >then in again _is_ a full context switch no? What I meant was we need not save and restore all the branch monitoring MSRs during a context switch. I agree this is confusing. Will remove this line. > >> >> Signed-off-by: Megha Dey >> Signed-off-by: Yu-Cheng Yu > >That SoB chain is buggered. Will change the ordering. > > >> +static int intel_bm_event_nmi_handler(unsigned int cmd, struct >> +pt_regs *regs) { >> + struct perf_event *event; >> + union bm_detect_status stat; >> + int i; >> + unsigned long x; >> + >> + rdmsrl(BR_DETECT_STATUS_MSR, stat.raw); > > if (!stat.event) > return NMI_DONE; > >saves you a whole bunch of indentation, no? Yep, it does. > >> + >> + if (stat.event) { >> + wrmsrl(BR_DETECT_STATUS_MSR, 0); >> + apic_write(APIC_LVTPC, APIC_DM_NMI); >> + /* >> + * Issue wake-up to corresponding polling event >> + */ >> + x = stat.ctrl_hit; >> + for_each_set_bit(i, &x, BM_MAX_COUNTERS) { >> + event = current->thread.bm_counter_owner[i]; >> + local64_inc(&event->count); >> + atomic_set(&event->hw.bm_poll, POLLIN); >> + event->pending_wakeup = 1; >> + irq_work_queue(&event->pending); >> + } >> + return NMI_HANDLED; >> + } >> + return NMI_DONE; >> +} >> + >> +/* >> + * Unmask the NMI bit of the local APIC the first time task is >> +scheduled >> + * on a particular CPU. >> + */ >> +static void intel_bm_unmask_nmi(void) { >> + this_cpu_write(bm_unmask_apic, 0); >> + >> + if (!(this_cpu_read(bm_unmask_apic))) { >> + apic_write(APIC_LVTPC, APIC_DM_NMI); >> + this_cpu_inc(bm_unmask_apic); >> + } >> +} > >What? Why? Normally, other drivers using perf create an event on every CPU (thereby calling perf_init on every CPU), where this bit(APIC_DM_NMI)is explicitly unmasked. In our driver, we do not do this (since we are worried only about a particular task) and hence this bit is only disabled on the local APIC where the perf event is initialized. As such, if the task is scheduled out to some other CPU, this bit is set and hence would stop the interrupt from reaching the processing core. > >> +static int intel_bm_event_add(struct perf_event *event, int mode) { >> + union bm_detect_status cur_stat, prev_stat; >> + >> + WARN_ON(event->hw.id >= BM_MAX_COUNTERS); >> + >> + prev_stat.raw = local64_read(&event->hw.prev_count); >> + >> + /* >> + * Start counting from previous count associated with this event >> + */ >> + rdmsrl(BR_DETECT_STATUS_MSR, cur_stat.raw); >> + >> + cur_stat.count[event->hw.id] = prev_stat.count[event->hw.id]; >> + cur_stat.count_window = prev_stat.count_window; >> + wrmsrl(BR_DETECT_STATUS_MSR, cur_stat.raw); > >Why are you writing back the value you read? Just to waste cycles? We only wanted to update the window count and event count associated with one of the 2 event IDs. But you are right, we don't read to read the MSR, will directly write to it. > >> + wrmsrl(BR_DETECT_CONTROL_MSR, event->hw.bm_ctrl); >> + >> + intel_bm_unmask_nmi(); >> + >> + wrmsrl(BR_DETECT_COUNTER_CONFIG_BASE + event->hw.id, >> + (event->hw.bm_counter_conf | 1)); > >Please use a named construct for that enable bit. I will switch to using bitfields for this MSR, similar to the status register. > >> + >> + return 0; >> +} >> + >> +static void intel_bm_event_update(struct perf_event *event) { >> + union bm_detect_status cur_stat; >> + >> + rdmsrl(BR_DETECT_STATUS_MSR, cur_stat.raw); >> + local64_set(&event->hw.prev_count, (uint64_t)cur_stat.raw); } > >That looks wrong... the general point of update functions is to update the >count, the above does not in fact do that. Ok will remove this and add this functionality to event_del() > >> + >> +static void intel_bm_event_del(struct perf_event *event, int flags) { >> + WARN_ON(event->hw.id >= BM_MAX_COUNTERS); >> + >> + wrmsrl(BR_DETECT_COUNTER_CONFIG_BASE + event->hw.id, >> + (event->hw.bm_counter_conf & ~1)); > >Either that EN bit is part of the bm_counter_conf, in which case you didn't >need to add it in _add(), or its not and you don't need to clear it here. Make >up your mind. We are starting the count in add() (last bit of bm_counter_conf) and stopping it here. I am not sure what you mean by this. Are you saying that we don't explicitly have to enable or disable the count if the enable bit is a part of the MSR? > >> + >> + intel_bm_event_update(event); > >Except of course, that does not in fact update... Will remove this. > >> +} >> + >> +static void intel_bm_event_destroy(struct perf_event *event) { >> + bm_counter_owner[event->hw.id] = NULL; } >> + >> +static DEFINE_MUTEX(bm_counter_mutex); >> + >> +static int intel_bm_event_init(struct perf_event *event) { >> + u64 cfg; >> + int counter_to_use = -1, i; >> + >> + local64_set(&event->hw.prev_count, 0); >> + > >> + /* >> + * Find a hardware counter for the target task >> + */ >> + bm_counter_owner = event->hw.target- >>thread.bm_counter_owner; >> + >> + mutex_lock(&bm_counter_mutex); >> + for (i = 0; i < BM_MAX_COUNTERS; i++) { >> + if (bm_counter_owner[i] == NULL) { >> + counter_to_use = i; >> + bm_counter_owner[i] = event; >> + break; >> + } >> + } >> + mutex_unlock(&bm_counter_mutex); >> + >> + if (counter_to_use == -1) >> + return -EBUSY; >> + >> + event->hw.bm_ctrl = (bm_window_size << >BM_WINDOW_SIZE_SHIFT) | >> + (bm_guest_disable << BM_GUEST_DISABLE_SHIFT) >| >> + (bm_lbr_freeze << BM_LBR_FREEZE_SHIFT) | >> + (bm_window_cnt_sel << >BM_WINDOW_CNT_SEL_SHIFT) | >> + (bm_cnt_and_mode << >BM_CNT_AND_MODE_SHIFT) | >> + BM_ENABLE; >> + event->hw.bm_counter_conf = (bm_threshold << >BM_THRESHOLD_SHIFT) | >> + (bm_mispred_evt_cnt << >BM_MISPRED_EVT_CNT_SHIFT) | >> + (cfg << BM_EVENT_TYPE_SHIFT); >> + >> + event->hw.id = counter_to_use; >> + local64_set(&event->count, 0); > >That is just a really ugly hack to work around: Actually, this was intended to make sure event->count is always zero for a new event. This wasn't intended to be a hack to work around the below. Hence, was not mentioned in the changelog. > > >> +static struct pmu intel_bm_pmu = { >> + .task_ctx_nr = perf_sw_context, > > >this. And you didn't bother to mention that atrocity in your Changelog. We don't need this. Will remove it in the next patch series. > > > >NAK. From 1583940860513008855@xxx Mon Nov 13 09:01:57 +0000 2017 X-GM-THRID: 1583805290382379451 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread