From patchwork Thu Oct 27 21:27:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Machek X-Patchwork-Id: 9400475 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A786F600BA for ; Thu, 27 Oct 2016 21:28:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8D0FA2A38F for ; Thu, 27 Oct 2016 21:28:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E9642A3A5; Thu, 27 Oct 2016 21:28:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 0AD252A38F for ; Thu, 27 Oct 2016 21:28:17 +0000 (UTC) Received: (qmail 25975 invoked by uid 550); 27 Oct 2016 21:28:15 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: kernel-hardening@lists.openwall.com Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 25908 invoked from network); 27 Oct 2016 21:28:11 -0000 Date: Thu, 27 Oct 2016 23:27:47 +0200 From: Pavel Machek To: Kees Cook Cc: Peter Zijlstra , Arnaldo Carvalho de Melo , kernel list , Ingo Molnar , Alexander Shishkin , "kernel-hardening@lists.openwall.com" Message-ID: <20161027212747.GA18147@amd> References: <20161026204748.GA11177@amd> <20161027082801.GE3568@worktop.programming.kicks-ass.net> <20161027091104.GB19469@amd> <20161027093334.GK3102@twins.programming.kicks-ass.net> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Subject: [kernel-hardening] rowhammer protection [was Re: Getting interrupt every million cache misses] X-Virus-Scanned: ClamAV using ClamSMTP Hi! > > if (event) > > perf_event_release_kernel(event); > > } > > } > > This is pretty cool. Are there workloads other than rowhammer that > could trip this, and if so, how bad would this delay be for them? > > At the very least, this could be behind a CONFIG for people that don't > have a way to fix their RAM refresh timings, etc. Yes, CONFIG_ is next. Here's the patch, notice that I reversed the time handling logic -- it should be correct now. We can't tell cache misses on different addresses from cache misses on same address (rowhammer), so this will have false positive. But so far, my machine seems to work. Unfortunately, I don't have machine suitable for testing nearby. Can someone help with testing? [On the other hand... testing this is not going to be easy. This will probably make problem way harder to reproduce it in any case...] I did run rowhammer, and yes, this did trigger and it was getting delayed -- by factor of 2. That is slightly low -- delay should be factor of 8 to get guarantees, if I understand things correctly. Oh and NMI gets quite angry, but that was to be expected. [ 112.476009] perf: interrupt took too long (23660454 > 23654965), lowering ker nel.perf_event_max_sample_rate to 250 [ 170.224007] INFO: NMI handler (perf_event_nmi_handler) took too long to run: 55.844 msecs [ 191.872007] INFO: NMI handler (perf_event_nmi_handler) took too long to run: 55.845 msecs Best regards, Pavel diff --git a/kernel/events/Makefile b/kernel/events/Makefile index 2925188..130a185 100644 --- a/kernel/events/Makefile +++ b/kernel/events/Makefile @@ -2,7 +2,7 @@ ifdef CONFIG_FUNCTION_TRACER CFLAGS_REMOVE_core.o = $(CC_FLAGS_FTRACE) endif -obj-y := core.o ring_buffer.o callchain.o +obj-y := core.o ring_buffer.o callchain.o nohammer.o obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o obj-$(CONFIG_UPROBES) += uprobes.o diff --git a/kernel/events/nohammer.c b/kernel/events/nohammer.c new file mode 100644 index 0000000..01844d2 --- /dev/null +++ b/kernel/events/nohammer.c @@ -0,0 +1,66 @@ +/* + * Thanks to Peter Zijlstra . + */ + +#include +#include +#include + +struct perf_event_attr rh_attr = { + .type = PERF_TYPE_HARDWARE, + .config = PERF_COUNT_HW_CACHE_MISSES, + .size = sizeof(struct perf_event_attr), + .pinned = 1, + /* FIXME: it is 1000000 per cpu. */ + .sample_period = 500000, +}; + +static DEFINE_PER_CPU(struct perf_event *, rh_event); +static DEFINE_PER_CPU(u64, rh_timestamp); + +static void rh_overflow(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs) +{ + u64 *ts = this_cpu_ptr(&rh_timestamp); /* this is NMI context */ + u64 now = ktime_get_mono_fast_ns(); + s64 delta = now - *ts; + + *ts = now; + + /* FIXME msec per usec, reverse logic? */ + if (delta < 64 * NSEC_PER_MSEC) + mdelay(56); +} + +static __init int my_module_init(void) +{ + int cpu; + + /* XXX borken vs hotplug */ + + for_each_online_cpu(cpu) { + struct perf_event *event = per_cpu(rh_event, cpu); + + event = perf_event_create_kernel_counter(&rh_attr, cpu, NULL, rh_overflow, NULL); + if (!event) + pr_err("Not enough resources to initialize nohammer on cpu %d\n", cpu); + pr_info("Nohammer initialized on cpu %d\n", cpu); + + } + return 0; +} + +static __exit void my_module_exit(void) +{ + int cpu; + + for_each_online_cpu(cpu) { + struct perf_event *event = per_cpu(rh_event, cpu); + + if (event) + perf_event_release_kernel(event); + } + return; +} + +module_init(my_module_init); +module_exit(my_module_exit);