Received: by 10.223.185.116 with SMTP id b49csp3210484wrg; Mon, 5 Mar 2018 16:37:25 -0800 (PST) X-Google-Smtp-Source: AG47ELtuHoauSYE+qxsRYJxKcPXrDHc2i3CKSINhxTr7ixvdqnG3+gE//5/cNKfpaSaJE8k91Mr4 X-Received: by 10.99.179.14 with SMTP id i14mr13548241pgf.45.1520296645624; Mon, 05 Mar 2018 16:37:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520296645; cv=none; d=google.com; s=arc-20160816; b=iMaWHSg0uExevOmvFCaJp3xkABHiGLKc4pxu9+05/Hw2HIyuaA7G+ojgmSf/PnYGb+ 4PBnjfI7GMbZMvW7EzXZ2ioSbbIygiBvkQN9p8IH9rEMJMgq/MEM3z7+NaGuze7YdRs9 BO85DtY6MTsRKjWrWa8jybnF94LQ9ml0iPZdN3bCCZgWss0P3UJEvNGPAzHZ9lz1i3Ai gtR1SqTwTBosThWvbnXNTR0YGCoTccRkdsK7MJ8kHjKZb/Pr8iqn0PPKAS8FYhaXWfkf nLhh/018eR5lE5xVuXgdJe6BKz5Y+pLQg53AZSFiyGAAjcQr7JDIMjcvGG3fKu11k3uV w4Iw== 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:to:from:dkim-signature:arc-authentication-results; bh=+jNj5dPcyp9GnEobcMFqRbimfPH/MIVzibsZ8/rhCZU=; b=yuXryQEi6L0qoXetQDY2YEK89UDFtpdaaGqXXofpZ/JmPPTf9ySAcEfvr4DevB0DrR jdHrKssxDJ0Rt/tLVZ+xDyC6SuDitpyzw/QKhsaqApYD5gP/T+pmehH/wRGlDLO7vwy9 HYGUFIxDOZnlOqiBNrEgR6h7QBPGYAFFsG1xMsj3lstjkEoMeDYMmTdwojZMJYGp4hZz T/BM9j6DRC6fx14DwLgQ96mL3wQXHhW6JfRJ6fHfXhgey+aMEZc+pPbiCi7e6cwqPCsI Jn7P+Lr5dbOPqGbNm4DI9FzlO0TIJ0ySlaYeok2eMslWTOqS9DbTh9haJNDcNI19jKg0 ASJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=QCJnSFLf; 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=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f1-v6si10168453plb.73.2018.03.05.16.37.11; Mon, 05 Mar 2018 16:37:25 -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; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=QCJnSFLf; 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=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933550AbeCFAf3 (ORCPT + 99 others); Mon, 5 Mar 2018 19:35:29 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:40764 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933251AbeCFA0d (ORCPT ); Mon, 5 Mar 2018 19:26:33 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w260MDkl187622; Tue, 6 Mar 2018 00:26:29 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : in-reply-to : references; s=corp-2017-10-26; bh=+jNj5dPcyp9GnEobcMFqRbimfPH/MIVzibsZ8/rhCZU=; b=QCJnSFLfcjYXhP+XYcZfiDw89dx189f9RrSQxse7QEJQYtMWUcNpCEcFxhRil+vUBUKj gzRyWMXolCgHbo+O6WpMfZW3As7ddurNExz1VaAY22OCUfUEJOF+aR09kcQ4RqJi9fWj uq+GdqJYvAlWS64coPxO7xomlJGfllqiYHx8KLAEpEf1TqB3kDos9/niGwGL1tOVKEu/ ddUKO1QghNpxd1UF+yn505vjMS4yakqgAWvDQq142g/n/13crztVOwgqg5xAaOMRUS5a OXfHIX3drluxWeuOWlYcVyANJlyk/QJaejqSeT9Ky6NTR7QFiR9gb/FoZ30Z3rMBDzj8 0A== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2130.oracle.com with ESMTP id 2ghdxf8jr7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 06 Mar 2018 00:26:28 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w260QSUC011416 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 6 Mar 2018 00:26:28 GMT Received: from abhmp0008.oracle.com (abhmp0008.oracle.com [141.146.116.14]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w260QSnp025108; Tue, 6 Mar 2018 00:26:28 GMT Received: from localhost.localdomain (/98.216.35.41) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 05 Mar 2018 16:26:27 -0800 From: Pavel Tatashin To: steven.sistare@oracle.com, daniel.m.jordan@oracle.com, linux-kernel@vger.kernel.org, Alexander.Levin@microsoft.com, dan.j.williams@intel.com, sathyanarayanan.kuppuswamy@intel.com, pankaj.laxminarayan.bharadiya@intel.com, akuster@mvista.com, cminyard@mvista.com, pasha.tatashin@oracle.com, gregkh@linuxfoundation.org, stable@vger.kernel.org Subject: [PATCH 4.1 33/65] kaiser: fix perf crashes Date: Mon, 5 Mar 2018 19:25:06 -0500 Message-Id: <20180306002538.1761-34-pasha.tatashin@oracle.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180306002538.1761-1-pasha.tatashin@oracle.com> References: <20180306002538.1761-1-pasha.tatashin@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8823 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=2 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1803060003 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hugh Dickins Avoid perf crashes: place debug_store in the user-mapped per-cpu area instead of allocating, and use page allocator plus kaiser_add_mapping() to keep the BTS and PEBS buffers user-mapped (that is, present in the user mapping, though visible only to kernel and hardware). The PEBS fixup buffer does not need this treatment. The need for a user-mapped struct debug_store showed up before doing any conscious perf testing: in a couple of kernel paging oopses on Westmere, implicating the debug_store offset of the per-cpu area. Signed-off-by: Hugh Dickins Acked-by: Jiri Kosina Signed-off-by: Greg Kroah-Hartman (cherry picked from commit 20cbe9a3aa2e341824da57ce0ac6d52cbffaa570) Signed-off-by: Pavel Tatashin Conflicts: arch/x86/kernel/cpu/perf_event_intel_ds.c --- arch/x86/kernel/cpu/perf_event_intel_ds.c | 57 ++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/cpu/perf_event_intel_ds.c b/arch/x86/kernel/cpu/perf_event_intel_ds.c index 7f73b3553e2e..e4f109a7ed9a 100644 --- a/arch/x86/kernel/cpu/perf_event_intel_ds.c +++ b/arch/x86/kernel/cpu/perf_event_intel_ds.c @@ -2,11 +2,15 @@ #include #include +#include #include #include #include "perf_event.h" +static +DEFINE_PER_CPU_SHARED_ALIGNED_USER_MAPPED(struct debug_store, cpu_debug_store); + /* The size of a BTS record in bytes: */ #define BTS_RECORD_SIZE 24 @@ -246,6 +250,39 @@ void fini_debug_store_on_cpu(int cpu) static DEFINE_PER_CPU(void *, insn_buffer); +static void *dsalloc(size_t size, gfp_t flags, int node) +{ +#ifdef CONFIG_KAISER + unsigned int order = get_order(size); + struct page *page; + unsigned long addr; + + page = alloc_pages_node(node, flags | __GFP_ZERO, order); + if (!page) + return NULL; + addr = (unsigned long)page_address(page); + if (kaiser_add_mapping(addr, size, __PAGE_KERNEL) < 0) { + __free_pages(page, order); + addr = 0; + } + return (void *)addr; +#else + return kmalloc_node(size, flags | __GFP_ZERO, node); +#endif +} + +static void dsfree(const void *buffer, size_t size) +{ +#ifdef CONFIG_KAISER + if (!buffer) + return; + kaiser_remove_mapping((unsigned long)buffer, size); + free_pages((unsigned long)buffer, get_order(size)); +#else + kfree(buffer); +#endif +} + static int alloc_pebs_buffer(int cpu) { struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; @@ -256,7 +293,7 @@ static int alloc_pebs_buffer(int cpu) if (!x86_pmu.pebs) return 0; - buffer = kzalloc_node(PEBS_BUFFER_SIZE, GFP_KERNEL, node); + buffer = dsalloc(PEBS_BUFFER_SIZE, GFP_KERNEL, node); if (unlikely(!buffer)) return -ENOMEM; @@ -267,7 +304,7 @@ static int alloc_pebs_buffer(int cpu) if (x86_pmu.intel_cap.pebs_format < 2) { ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node); if (!ibuffer) { - kfree(buffer); + dsfree(buffer, PEBS_BUFFER_SIZE); return -ENOMEM; } per_cpu(insn_buffer, cpu) = ibuffer; @@ -296,7 +333,8 @@ static void release_pebs_buffer(int cpu) kfree(per_cpu(insn_buffer, cpu)); per_cpu(insn_buffer, cpu) = NULL; - kfree((void *)(unsigned long)ds->pebs_buffer_base); + dsfree((void *)(unsigned long)ds->pebs_buffer_base, + PEBS_BUFFER_SIZE); ds->pebs_buffer_base = 0; } @@ -310,7 +348,7 @@ static int alloc_bts_buffer(int cpu) if (!x86_pmu.bts) return 0; - buffer = kzalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, node); + buffer = dsalloc(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, node); if (unlikely(!buffer)) { WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__); return -ENOMEM; @@ -336,19 +374,15 @@ static void release_bts_buffer(int cpu) if (!ds || !x86_pmu.bts) return; - kfree((void *)(unsigned long)ds->bts_buffer_base); + dsfree((void *)(unsigned long)ds->bts_buffer_base, BTS_BUFFER_SIZE); ds->bts_buffer_base = 0; } static int alloc_ds_buffer(int cpu) { - int node = cpu_to_node(cpu); - struct debug_store *ds; - - ds = kzalloc_node(sizeof(*ds), GFP_KERNEL, node); - if (unlikely(!ds)) - return -ENOMEM; + struct debug_store *ds = per_cpu_ptr(&cpu_debug_store, cpu); + memset(ds, 0, sizeof(*ds)); per_cpu(cpu_hw_events, cpu).ds = ds; return 0; @@ -362,7 +396,6 @@ static void release_ds_buffer(int cpu) return; per_cpu(cpu_hw_events, cpu).ds = NULL; - kfree(ds); } void release_ds_buffers(void) -- 2.16.2