Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp283053ybz; Thu, 30 Apr 2020 21:51:39 -0700 (PDT) X-Google-Smtp-Source: APiQypK0V6qk+qZ526BBlMH/UfR7twX3V1TwJQ1/nbzdwbJxxCPjdfTDENJqG7paW7Y20JGHp6PU X-Received: by 2002:a17:906:808:: with SMTP id e8mr1631239ejd.372.1588308699117; Thu, 30 Apr 2020 21:51:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588308699; cv=none; d=google.com; s=arc-20160816; b=KCtwfiV8JTGW+GVySu6lsmFAdUoe0vr+CnKXWpeLjdQ6f3MmfEeV4fj0Z8naBw9ZYM ovQJUNxztbtZNSpL40aTP9qaASdk9wJVJ3iMTBowphgac4K+mHL4WBfgHUsb9GZd2e9u Jq5hf+LoZedI9Mn1eUxbpw1WkhUgNpxrWIS5Y2JAhXyhIApaSSO6ZSdeSOAggmwex4kk e/EueUrbKYgu1T11YjFUf4CSBcdkIQAA5TncgLEieXorw2aDvIRW+lFnzIUYRG2TC5xm WDTyrZsGZN7oz1jl2eQNXHgKTWjnId4z7S1IUIWywwRRQ0CU2lBZTT2oLy5na+p0j+DX 94OA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=NT93Ylw+ngaHdbY/ecfijM8YRJCeyUONnkx665ymxd0=; b=DlAUgfughkCHkwHvxLNk3+QY9LvYNTIuDunCyiCkIYAsADVbHQGZAxE3l4CCZUQ4dI g+6/xZKfDmBsAeQLnyJEvW7kv890pNPVFGeQFMaUZh+vY+beOq1UGOUHMwZTJd4xBJzg 2Nc1FnI0YiKK0zsL1p91yd4QxdIuUA8H9tS6YfWGnCjGhJM4Cam7s7leTC7X29Dvmvp1 YbGkkiJJqR5Y+EbTvM9okLHN4yB3leQXyMeKeKudpGIMIgnW2beSXfpPfYqqxekavQux tqDmCA8BhPo5VnGma8M7GUBJCeHlMYbvIFFNjSGtyYn9HTSJ/RdDFxMsc9uVRTdWpvmg 5kvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="QbpP6/JC"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o17si1025076edv.44.2020.04.30.21.51.13; Thu, 30 Apr 2020 21:51:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="QbpP6/JC"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728150AbgEAEro (ORCPT + 99 others); Fri, 1 May 2020 00:47:44 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:52175 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726452AbgEAErn (ORCPT ); Fri, 1 May 2020 00:47:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588308462; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NT93Ylw+ngaHdbY/ecfijM8YRJCeyUONnkx665ymxd0=; b=QbpP6/JCLyIeysUpdYGoMYSF02bJL9S/eUFGGqDYUhSbByv2WdBaxzFqevwxriM50HfON0 XnBLflz5wAlJziaRdl+87s9HiNp4PMevYQo/11+ZT95sg33Qud7uHLkmrc8/g6jFk5QCJv jKLrb3MpDUUMnVqAxRGEO7tQnuUZOGE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-448-sgIUT4CbPQSNkgSrIqtJBg-1; Fri, 01 May 2020 00:47:39 -0400 X-MC-Unique: sgIUT4CbPQSNkgSrIqtJBg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6714845F; Fri, 1 May 2020 04:47:37 +0000 (UTC) Received: from treble (ovpn-113-19.rdu2.redhat.com [10.10.113.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 44AA09323; Fri, 1 May 2020 04:47:35 +0000 (UTC) Date: Thu, 30 Apr 2020 23:47:33 -0500 From: Josh Poimboeuf To: Steven Rostedt Cc: LKML , Peter Zijlstra , Ingo Molnar , Thomas Gleixner , Andy Lutomirski , Borislav Petkov , "H. Peter Anvin" Subject: Re: [RFC][PATCH] x86/ftrace: Have ftrace trampolines turn read-only at the end of system boot up Message-ID: <20200501044733.eqf6hc6erucsd43x@treble> References: <20200430202147.4dc6e2de@oasis.local.home> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200430202147.4dc6e2de@oasis.local.home> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 30, 2020 at 08:21:47PM -0400, Steven Rostedt wrote: > The cause is the "ftrace=function" would register the function tracer > and create a trampoline, and it will set it as executable and > read-only. Then the "trace_options=func_stack_trace" would then update > the same trampoline to include the stack tracer version of the function > tracer. But since the trampoline already exists, it updates it with > text_poke_bp(). The problem is that text_poke_bp() called while > system_state == SYSTEM_BOOTING, it will simply do a memcpy() and not > the page mapping, as it would think that the text is still read-write. > But in this case it is not, and we take a fault and crash. > > Instead, lets keep the ftrace trampolines read-write during boot up, > and then when the kernel executable text is set to read-only, the > ftrace trampolines get set to read-only as well. Would it be easier to just call a new __text_poke_bp() which skips the SYSTEM_BOOTING check, since you know the trampoline will always be read-only? Like: diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h index 67315fa3956a..710106256916 100644 --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -45,6 +45,7 @@ extern void *text_poke(void *addr, const void *opcode, size_t len); extern void text_poke_sync(void); extern void *text_poke_kgdb(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, const void *emulate); extern void text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate); extern void text_poke_queue(void *addr, const void *opcode, size_t len, const void *emulate); diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 7867dfb3963e..9cc983cc9291 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1265,6 +1265,14 @@ void __ref text_poke_queue(void *addr, const void *opcode, size_t len, const voi text_poke_loc_init(tp, addr, opcode, len, emulate); } +void __ref __text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate) +{ + struct text_poke_loc tp; + + text_poke_loc_init(&tp, addr, opcode, len, emulate); + text_poke_bp_batch(&tp, 1); +} + /** * text_poke_bp() -- update instructions on live kernel on SMP * @addr: address to patch @@ -1278,13 +1286,10 @@ void __ref text_poke_queue(void *addr, const void *opcode, size_t len, const voi */ void __ref text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate) { - struct text_poke_loc tp; - if (unlikely(system_state == SYSTEM_BOOTING)) { text_poke_early(addr, opcode, len); return; } - text_poke_loc_init(&tp, addr, opcode, len, emulate); - text_poke_bp_batch(&tp, 1); + __text_poke_bp(addr, opcode, len, emulate); } diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c index 867c126ddabe..c36f51f01f6e 100644 --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -469,7 +469,7 @@ void arch_ftrace_update_trampoline(struct ftrace_ops *ops) mutex_lock(&text_mutex); /* Do a safe modify in case the trampoline is executing */ new = ftrace_call_replace(ip, (unsigned long)func); - text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL); + __text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL); mutex_unlock(&text_mutex); }