Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2139463imm; Thu, 19 Jul 2018 14:00:21 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfixc/HHvMR1o43d65OK6G27z0zfTW61y+BAVM+/42vw/cffXsuMnyOpkeucdv+uzsZ4e/w X-Received: by 2002:a17:902:f83:: with SMTP id 3-v6mr11486148plz.282.1532034021128; Thu, 19 Jul 2018 14:00:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532034021; cv=none; d=google.com; s=arc-20160816; b=gvzbuMp8vLm/M8NtAnQYk1jd20Dtw3A5iWXL6oZj6vflypYOtCKf0bBul5jAXo2iwe YKrbXuTaJ6i4z2xlmUt+SWBFh8JOhA6L1QzMo42EXjhx6Vgtwx1t5AkbQxVihXlvozny p+zJKxfBtJZGfyHAq7e8uIKlGONfzK3CZF23GUMpWwlcFMkMMdMbfXXEbd+SH054RSZW i+CTat6ht4LnIzPdTkWYtLnJh3+Qtggydpr+u8m8ctCNa/CI3LUlA74ORel94ixHiXWN mO7j6WoA94wRgkv+hWNWvlI4rU/+ivXVp3rFSu4Myz4UblqUmcsr2e1g5QXzyfB2DuRu l/Ug== 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=uAuMWI2Ms5FAflkGTyvXqXMrpyGjdu4sicwkzYvF4DY=; b=DFAwPr3XGX43aigcqXgaGKA4adgJHLvsI+cpuvIQgdewSlm9W2aQeCSRqUmtKbobCu AfTasmm3188p7Bd7EaYHZUpbudyvBV9lz2i01U3zKMj/wX2u84osccuxLY8+mdKBXHu4 RC87sY2WAJmGp13boWx7bYoNA2frFGj0PZEsOZygoZR7vsvOrB6eiWyWkzkOxeM+xvIW v7XDwWEFhFD9cONI1XSdQrr72uOe7qn8VjUdCI8GqvRJJVBcHmocfOxaKspRUJ6UYMKk BpNNTrttbEKRovB27BxEImjFAlEjPyhFRilorEiR5i0wnghcxbSBj8/jf3LEbtK4x54O FhUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=b5Mdzc6n; 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 i34-v6si117483pgm.586.2018.07.19.14.00.06; Thu, 19 Jul 2018 14:00:21 -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=@oracle.com header.s=corp-2018-07-02 header.b=b5Mdzc6n; 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 S1731050AbeGSVnu (ORCPT + 99 others); Thu, 19 Jul 2018 17:43:50 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:51064 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730349AbeGSVmO (ORCPT ); Thu, 19 Jul 2018 17:42:14 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w6JKsSF4156834; Thu, 19 Jul 2018 20:56: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-2018-07-02; bh=uAuMWI2Ms5FAflkGTyvXqXMrpyGjdu4sicwkzYvF4DY=; b=b5Mdzc6nRNWsNpuvhGHKfs2nom/Ux86lANF7Q98Jbfais+TS9AB32YLLvlhyCff3kS+g jCbvDY6uk+YWoiILkPirNX7pqPCjsvqb0xsrD7fswaBiuLV/gVf9PRNOyala9ILfH76P UG1lazCTjs0WLXTwd2FNsIvpJUd/WrGm7Bl6lW5G+5LKdKiyKXDVg2muQlKYOaxiebQw r0/jrFRyAuSXrQx8HxmD1VV6PcKqaJn10mEHlpeqHZYJgQ6N2a2MtRzpyPVC0i8Eg82U azlqVHEK16fsa5pKjiIWPqvIAGIyLFOZMGX5CNwDAbnffg4ytBdaQZy2SDo7OHRA0cRo Kg== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2120.oracle.com with ESMTP id 2k9yjx8uuk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 Jul 2018 20:56:29 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w6JKuRj9010197 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 Jul 2018 20:56:28 GMT Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w6JKuQ3T026844; Thu, 19 Jul 2018 20:56:26 GMT Received: from localhost.localdomain (/73.69.118.222) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 19 Jul 2018 20:56:26 +0000 From: Pavel Tatashin To: steven.sistare@oracle.com, daniel.m.jordan@oracle.com, linux@armlinux.org.uk, schwidefsky@de.ibm.com, heiko.carstens@de.ibm.com, john.stultz@linaro.org, sboyd@codeaurora.org, x86@kernel.org, linux-kernel@vger.kernel.org, mingo@redhat.com, tglx@linutronix.de, hpa@zytor.com, douly.fnst@cn.fujitsu.com, peterz@infradead.org, prarit@redhat.com, feng.tang@intel.com, pmladek@suse.com, gnomes@lxorguk.ukuu.org.uk, linux-s390@vger.kernel.org, pasha.tatashin@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, pbonzini@redhat.com Subject: [PATCH v15 08/26] x86: text_poke() may access uninitialized struct pages Date: Thu, 19 Jul 2018 16:55:27 -0400 Message-Id: <20180719205545.16512-9-pasha.tatashin@oracle.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180719205545.16512-1-pasha.tatashin@oracle.com> References: <20180719205545.16512-1-pasha.tatashin@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8959 signatures=668706 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 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-1806210000 definitions=main-1807190218 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It supposed to be safe to modify static branches after jump_label_init(). But, because static key modifying code eventually calls text_poke() we may end up with accessing struct page that have not been initialized. Here is how to quickly reproduce the problem. Insert code like this into init/main.c: | +static DEFINE_STATIC_KEY_FALSE(__test); | asmlinkage __visible void __init start_kernel(void) | { | char *command_line; |@@ -587,6 +609,10 @@ asmlinkage __visible void __init start_kernel(void) | vfs_caches_init_early(); | sort_main_extable(); | trap_init(); |+ { |+ static_branch_enable(&__test); |+ WARN_ON(!static_branch_likely(&__test)); |+ } | mm_init(); The following warnings show-up: WARNING: CPU: 0 PID: 0 at arch/x86/kernel/alternative.c:701 text_poke+0x20d/0x230 Modules linked in: CPU: 0 PID: 0 Comm: swapper Not tainted 4.18.0-rc1_pt_t1 #30 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-20171110_100015-anatol 04/01/2014 RIP: 0010:text_poke+0x20d/0x230 Code: 0f 0b 4c 89 e2 4c 89 ee 4c 89 f7 e8 7d 4b 9b 00 31 d2 31 f6 bf 86 02 00 00 48 8b 05 95 8e 24 01 e8 78 18 d8 00 e9 55 ff ff ff <0f> 0b e9 54 fe ff ff 48 8b 05 75 a8 38 01 e9 64 fe ff ff 48 8b 1d RSP: 0000:ffffffff94e03e30 EFLAGS: 00010046 RAX: 0100000000000000 RBX: fffff7b2c011f300 RCX: ffffffff94fcccf4 RDX: 0000000000000001 RSI: ffffffff94e03e77 RDI: ffffffff94fcccef RBP: ffffffff94fcccef R08: 00000000fffffe00 R09: 00000000000000a0 R10: 0000000000000000 R11: 0000000000000040 R12: 0000000000000001 R13: ffffffff94e03e77 R14: ffffffff94fcdcef R15: fffff7b2c0000000 FS: 0000000000000000(0000) GS:ffff9adc87c00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffff9adc8499d000 CR3: 000000000460a001 CR4: 00000000000606b0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: ? start_kernel+0x23e/0x4c8 ? start_kernel+0x23f/0x4c8 ? text_poke_bp+0x50/0xda ? arch_jump_label_transform+0x89/0xe0 ? __jump_label_update+0x78/0xb0 ? static_key_enable_cpuslocked+0x4d/0x80 ? static_key_enable+0x11/0x20 ? start_kernel+0x23e/0x4c8 ? secondary_startup_64+0xa5/0xb0 ---[ end trace abdc99c031b8a90a ]--- If the code above is moved after mm_init(), no warning is shown, as struct pages are initialized during handover from memblock. Use text_poke_early() in static branching until early boot IRQs are enabled, at which time switch to text_poke. Also, ensure text_poke() is never invoked when unitialized memory access may happen by using: BUG_ON(!after_bootmem); assertion. Signed-off-by: Pavel Tatashin --- arch/x86/include/asm/text-patching.h | 1 + arch/x86/kernel/alternative.c | 7 +++++++ arch/x86/kernel/jump_label.c | 11 +++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h index 2ecd34e2d46c..e85ff65c43c3 100644 --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -37,5 +37,6 @@ extern void *text_poke_early(void *addr, const void *opcode, size_t len); extern void *text_poke(void *addr, const void *opcode, size_t len); extern int poke_int3_handler(struct pt_regs *regs); extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler); +extern int after_bootmem; #endif /* _ASM_X86_TEXT_PATCHING_H */ diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index a481763a3776..014f214da581 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -668,6 +668,7 @@ void *__init_or_module text_poke_early(void *addr, const void *opcode, local_irq_save(flags); memcpy(addr, opcode, len); local_irq_restore(flags); + sync_core(); /* Could also do a CLFLUSH here to speed up CPU recovery; but that causes hangs on some VIA CPUs. */ return addr; @@ -693,6 +694,12 @@ void *text_poke(void *addr, const void *opcode, size_t len) struct page *pages[2]; int i; + /* + * While boot memory allocator is runnig we cannot use struct + * pages as they are not yet initialized. + */ + BUG_ON(!after_bootmem); + if (!core_kernel_text((unsigned long)addr)) { pages[0] = vmalloc_to_page(addr); pages[1] = vmalloc_to_page(addr + PAGE_SIZE); diff --git a/arch/x86/kernel/jump_label.c b/arch/x86/kernel/jump_label.c index e56c95be2808..eeea935e9bb5 100644 --- a/arch/x86/kernel/jump_label.c +++ b/arch/x86/kernel/jump_label.c @@ -37,15 +37,18 @@ static void bug_at(unsigned char *ip, int line) BUG(); } -static void __jump_label_transform(struct jump_entry *entry, - enum jump_label_type type, - void *(*poker)(void *, const void *, size_t), - int init) +static void __ref __jump_label_transform(struct jump_entry *entry, + enum jump_label_type type, + void *(*poker)(void *, const void *, size_t), + int init) { union jump_code_union code; const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP }; const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5]; + if (early_boot_irqs_disabled) + poker = text_poke_early; + if (type == JUMP_LABEL_JMP) { if (init) { /* -- 2.18.0