Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp746055ybp; Fri, 11 Oct 2019 03:47:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqymwysrr5evugiApsa0h5bWDCbDuScYc9DzWlnuaDNRyNwI3irirxNFbyPhrUpwtKFkzohS X-Received: by 2002:a17:906:68d5:: with SMTP id y21mr12926626ejr.272.1570790867671; Fri, 11 Oct 2019 03:47:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570790867; cv=none; d=google.com; s=arc-20160816; b=gMHfrDgKD/qG/KsXZRNpx5sQrIOwaxBko4831Tu3L4TVjtzsOldJlFkuScyLZjb7G+ xhD0195AP1VhdbzrlvSjQSqtCKlVffQUcpN2W/lUt6oNlztqUOpjNsCIN9tN6X2KA7Fk fmhcxH2eUk82FseMXLFuguK0blqldfiOy8AyIYRF8iufCWplbOWwWg+mE7On/3DHvBLs v60ZfCvf6HeQDUfnGBYMVnRlqbOV87CpUgF4wXOvwWJSB4q2nlmglVL/3VyZ9/HQZG/4 7k1LYwRRxJWPhjR7GFK5K6jMi+4/WLlB+mfU8R211kJuiE7dg9dTS5WWmNlh3NAUEnJs 13jA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=6//5W1KVBTgFdY2EGRaIvYQjFpmynmW/1/5VeV5uNlU=; b=mNjVQ+lhKgi9jC7zhew//Rwb2AX/M7sJdeLobn2ImeKEv3HWQW28r8jxSb8vMRsdoh FdgsPX5hPHwiAnty9zoDQBg+ogDX8xYOddacxtG1QGTsXMPCtJ95kKGNzh2WAQ4b3jCV HvFf5nIC2kwsNc3T0b+ZrhAhgULJbIbgUBI31zVTK8gCAgqibl28mXA5OtSI8he8AqFh SYehu3lRk1EAqPPkk6tNX+ahdz7dGK4dNxb+ZQxdWV/9u0nkjcLguLXwMjXkDcXhUkbV 45GsODnSNluJWUTe3YvedBgBFzDj4DT+sgzuK5ZnAJpvHP1AXS3idlA/6b3Dad7r7dyf +4sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=xE8LpI6K; 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 n6si4931758edq.228.2019.10.11.03.47.24; Fri, 11 Oct 2019 03:47:47 -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=fail header.i=@infradead.org header.s=merlin.20170209 header.b=xE8LpI6K; 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 S1727549AbfJKKqY (ORCPT + 99 others); Fri, 11 Oct 2019 06:46:24 -0400 Received: from merlin.infradead.org ([205.233.59.134]:41702 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727289AbfJKKqY (ORCPT ); Fri, 11 Oct 2019 06:46:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=6//5W1KVBTgFdY2EGRaIvYQjFpmynmW/1/5VeV5uNlU=; b=xE8LpI6KEf311SiQRKeDTTryh Cn4XPN+1kBAk5ZRadMN7p7mcT94qHxKV3RcQOEMxJO43iSepsbH4v+QcTU6Zj9hFIevBn0kuFr2lC JogLpDj9OBXXJdjXjL4Vw4+O3gkc9Z0SiD2MJl5SwDeVThKNBtSPDrDzbANeBO7fWOPQNP/A2Nv18 Rwn0/xlLoGGSC9KHbyfyBBTymUnaS3coMO+9SlAa+uNzPjs6vRDkSFCLHndzX0j6J7yO1LajLONx0 xSiBVmCXFmAJ/d/19Z6OYsTEubJCp5xno4LwfC6GVIlX5OCqmwW7tBrNwfKG1pmuslquRCaE/RSkr 5IwmZfYYQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1iIsQn-0006dX-8q; Fri, 11 Oct 2019 10:45:57 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id BEDFF301224; Fri, 11 Oct 2019 12:44:59 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 590F4201F7DBB; Fri, 11 Oct 2019 12:45:52 +0200 (CEST) Date: Fri, 11 Oct 2019 12:45:52 +0200 From: Peter Zijlstra To: Steven Rostedt Cc: x86@kernel.org, linux-kernel@vger.kernel.org, mhiramat@kernel.org, bristot@redhat.com, jbaron@akamai.com, torvalds@linux-foundation.org, tglx@linutronix.de, mingo@kernel.org, namit@vmware.com, hpa@zytor.com, luto@kernel.org, ard.biesheuvel@linaro.org, jpoimboe@redhat.com Subject: Re: [PATCH v3 5/6] x86/ftrace: Use text_poke() Message-ID: <20191011104552.GW2328@hirez.programming.kicks-ass.net> References: <20191007081716.07616230.8@infradead.org> <20191007081945.10951536.8@infradead.org> <20191008104335.6fcd78c9@gandalf.local.home> <20191009224135.2dcf7767@oasis.local.home> <20191010092054.GR2311@hirez.programming.kicks-ass.net> <20191010091956.48fbcf42@gandalf.local.home> <20191010140513.GT2311@hirez.programming.kicks-ass.net> <20191010115449.22044b53@gandalf.local.home> <20191010172819.GS2328@hirez.programming.kicks-ass.net> <20191010134830.72ccef3d@gandalf.local.home> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191010134830.72ccef3d@gandalf.local.home> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 10, 2019 at 01:48:30PM -0400, Steven Rostedt wrote: > On Thu, 10 Oct 2019 19:28:19 +0200 > Peter Zijlstra wrote: > > > > That is, I really hate the above "set_ro" hack. This is because you > > > moved the ro setting to create_trampoline() and then forcing the > > > text_poke() on text that has just been created. I prefer to just modify > > > it and then setting it to ro before it gets executed. Otherwise we need > > > to do all these dances. > > > > I thought create_trampoline() finished the whole thing; if it does not, > > either make create_trampoline() do everything, or add a > > finish_trampoline() callback to mark it complete. > > I'm good with a finish_trampoline(). I can make a patch that does that. I found it easier to just make create_trampoline do it all. The below patch seems to cure both issues for me. --- --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1213,6 +1213,11 @@ void text_poke_queue(void *addr, const v { struct text_poke_loc *tp; + if (unlikely(system_state == SYSTEM_BOOTING)) { + text_poke_early(addr, opcode, len); + return; + } + text_poke_flush(addr); tp = &tp_vec[tp_vec_nr++]; @@ -1230,10 +1235,15 @@ void text_poke_queue(void *addr, const v * dynamically allocated memory. This function should be used when it is * not possible to allocate memory. */ -void text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate) +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); } --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -34,6 +34,8 @@ #ifdef CONFIG_DYNAMIC_FTRACE +static int ftrace_poke_late = 0; + int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex) { @@ -43,12 +45,15 @@ int ftrace_arch_code_modify_prepare(void * ftrace has it set to "read/write". */ mutex_lock(&text_mutex); + ftrace_poke_late = 1; return 0; } int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex) { + text_poke_finish(); + ftrace_poke_late = 0; mutex_unlock(&text_mutex); return 0; } @@ -116,7 +121,10 @@ ftrace_modify_code_direct(unsigned long return ret; /* replace the text with the new text */ - text_poke_early((void *)ip, new_code, MCOUNT_INSN_SIZE); + if (ftrace_poke_late) + text_poke_queue((void *)ip, new_code, MCOUNT_INSN_SIZE, NULL); + else + text_poke_early((void *)ip, new_code, MCOUNT_INSN_SIZE); return 0; } @@ -308,11 +316,12 @@ union ftrace_op_code_union { #define RET_SIZE 1 static unsigned long -create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) +create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size, ftrace_func_t func) { unsigned long start_offset; unsigned long end_offset; unsigned long op_offset; + unsigned long call_offset; unsigned long offset; unsigned long npages; unsigned long size; @@ -329,10 +338,12 @@ create_trampoline(struct ftrace_ops *ops start_offset = (unsigned long)ftrace_regs_caller; end_offset = (unsigned long)ftrace_regs_caller_end; op_offset = (unsigned long)ftrace_regs_caller_op_ptr; + call_offset = (unsigned long)ftrace_regs_call; } else { start_offset = (unsigned long)ftrace_caller; end_offset = (unsigned long)ftrace_epilogue; op_offset = (unsigned long)ftrace_caller_op_ptr; + call_offset = (unsigned long)ftrace_call; } size = end_offset - start_offset; @@ -389,6 +400,14 @@ create_trampoline(struct ftrace_ops *ops /* put in the new offset to the ftrace_ops */ memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE); + /* put in the call to the function */ + mutex_lock(&text_mutex); + call_offset -= start_offset; + memcpy(trampoline + call_offset, + text_gen_insn(CALL_INSN_OPCODE, trampoline + call_offset, func), + CALL_INSN_SIZE); + mutex_unlock(&text_mutex); + /* ALLOC_TRAMP flags lets us know we created it */ ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP; @@ -426,23 +445,23 @@ void arch_ftrace_update_trampoline(struc unsigned int size; const char *new; - if (ops->trampoline) { - /* - * The ftrace_ops caller may set up its own trampoline. - * In such a case, this code must not modify it. - */ - if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) - return; - } else { - ops->trampoline = create_trampoline(ops, &size); + if (!ops->trampoline) { + ops->trampoline = create_trampoline(ops, &size, ftrace_ops_get_func(ops)); if (!ops->trampoline) return; ops->trampoline_size = size; + return; } + /* + * The ftrace_ops caller may set up its own trampoline. + * In such a case, this code must not modify it. + */ + if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) + return; + offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS); ip = ops->trampoline + offset; - func = ftrace_ops_get_func(ops); mutex_lock(&text_mutex);