Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1605763yba; Sat, 27 Apr 2019 03:11:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqx27GDY9V6zxYVfJcOpOjOpxIn2aowAbNZMsxSy2TcxmTwjcs7/7ug6Gi5Z8tcImG+0Wble X-Received: by 2002:a17:902:b089:: with SMTP id p9mr51038984plr.185.1556359866772; Sat, 27 Apr 2019 03:11:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556359866; cv=none; d=google.com; s=arc-20160816; b=x+GGunmIn7ipmVa4OUa4K9s+wcoqwhg+DkuX1Y4indUCF5mi/pq70XpOq1v8nXyAgf 5FLiXHLTK4qHearJONrwGPgMPIuWzg/MggDXGT2vcfSRU2IhIZzZeBvqX19+FtZmG/yL ByHwutISGeyGFY+62mOSPKabHL7CN8fVFu17ogWHXjyP9nsC2dOrMqIzgpk86Z4lPjdz fsorZ6tY+cXE3jNhiGc0Y1gzIWDgxvskZoDZuGfHa5dL78Fo9uZST2L5NAR33XtIy+jY tkRArrMA4wsDvAyjU06TQ0GSTRTcC4SYSh1MSTdIX8UCkaiYlwc9lLunmJzPS8jSYuv0 3LRg== 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:cc:to:from; bh=+0VSdMGqa+nK8rmUIs46GVHWvjx3B/XuiKYd/YWL5K8=; b=TjGiKVMz2sJMd1m0KHq7zNVP5XCviqOD0tjk7cr8t5HQqDgNfoM0O54NhDoFuVaZA3 iYM4tXr7CeKBpVbPcz+lYH+BPEUPasvvOh11z6w4odeQBSAidHCF4GxmFGNqb7JE0vTK Ug9NjQPxa4tT+E36ehurH+olVgqmPg+Hh0fYuLk6u1QQBz51WTCQX2gflVkwiOP4r7p8 POREEqbq2tjr01U/y4ltN577M5f6P3RCANiT9QD9Ebm7jodoZ25bbo4gxLSfPc2IpKs5 FMLWAgTd9MFhl/PNDuOeoikwTZ+0oee/E7WNKGVsNZuD6cnMxOZlQaUUCxGBRaQM30Qy GVyA== ARC-Authentication-Results: i=1; mx.google.com; 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 d2si25507062pgv.22.2019.04.27.03.10.51; Sat, 27 Apr 2019 03:11:06 -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; 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 S1726444AbfD0KHt (ORCPT + 99 others); Sat, 27 Apr 2019 06:07:49 -0400 Received: from mx2.suse.de ([195.135.220.15]:51352 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725912AbfD0KHs (ORCPT ); Sat, 27 Apr 2019 06:07:48 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 3F992AD25; Sat, 27 Apr 2019 10:07:46 +0000 (UTC) From: Nicolai Stange To: Steven Rostedt Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , Shuah Khan , Konrad Rzeszutek Wilk , Tim Chen , Sebastian Andrzej Siewior , Mimi Zohar , Juergen Gross , Nick Desaulniers , Nayna Jain , Masahiro Yamada , Andy Lutomirski , Joerg Roedel , linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, linux-kselftest@vger.kernel.org, Nicolai Stange Subject: [PATCH 1/4] x86/thread_info: introduce ->ftrace_int3_stack member Date: Sat, 27 Apr 2019 12:06:36 +0200 Message-Id: <20190427100639.15074-2-nstange@suse.de> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20190427100639.15074-1-nstange@suse.de> References: <20190427100639.15074-1-nstange@suse.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Before actually rewriting an insn, x86' DYNAMIC_FTRACE implementation places an int3 breakpoint on it. Currently, ftrace_int3_handler() simply treats the insn in question as nop and advances %rip past it. An upcoming patch will improve this by making the int3 trap handler emulate the call insn. To this end, ftrace_int3_handler() will be made to change its iret frame's ->ip to some stub which will then mimic the function call in the original context. Somehow the trapping ->ip address will have to get communicated from ftrace_int3_handler() to these stubs though. Note that at any given point in time, there can be at most four such call insn emulations pending: namely at most one per "process", "irq", "softirq" and "nmi" context. Introduce struct ftrace_int3_stack providing four entries for storing the instruction pointer. In principle, it could be made per-cpu, but this would require making ftrace_int3_handler() to return with preemption disabled and to enable it from those emulation stubs again only after the stack's top entry has been consumed. I've been told that this would "break a lot of norms" and that making this stack part of struct thread_info instead would be less fragile. Follow this advice and add a struct ftrace_int3_stack instance to x86's struct thread_info. Note that these stacks will get only rarely accessed (only during ftrace's code modifications) and thus, cache line dirtying won't have any significant impact on the neighbouring fields. Initialization will take place implicitly through INIT_THREAD_INFO as per the rules for missing elements in initializers. The memcpy() in arch_dup_task_struct() will propagate the initial state properly, because it's always run in process context and won't ever see a non-zero ->depth value. Finally, add the necessary bits to asm-offsets for making struct ftrace_int3_stack accessible from assembly. Suggested-by: Steven Rostedt Signed-off-by: Nicolai Stange --- arch/x86/include/asm/thread_info.h | 11 +++++++++++ arch/x86/kernel/asm-offsets.c | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h index e0eccbcb8447..83434a88cfbb 100644 --- a/arch/x86/include/asm/thread_info.h +++ b/arch/x86/include/asm/thread_info.h @@ -56,6 +56,17 @@ struct task_struct; struct thread_info { unsigned long flags; /* low level flags */ u32 status; /* thread synchronous flags */ +#ifdef CONFIG_DYNAMIC_FTRACE + struct ftrace_int3_stack { + int depth; + /* + * There can be at most one slot in use per context, + * i.e. at most one for "normal", "irq", "softirq" and + * "nmi" each. + */ + unsigned long slots[4]; + } ftrace_int3_stack; +#endif }; #define INIT_THREAD_INFO(tsk) \ diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c index 168543d077d7..ca6ee24a0c6e 100644 --- a/arch/x86/kernel/asm-offsets.c +++ b/arch/x86/kernel/asm-offsets.c @@ -105,4 +105,12 @@ static void __used common(void) OFFSET(TSS_sp0, tss_struct, x86_tss.sp0); OFFSET(TSS_sp1, tss_struct, x86_tss.sp1); OFFSET(TSS_sp2, tss_struct, x86_tss.sp2); + +#ifdef CONFIG_DYNAMIC_FTRACE + BLANK(); + OFFSET(TASK_TI_ftrace_int3_depth, task_struct, + thread_info.ftrace_int3_stack.depth); + OFFSET(TASK_TI_ftrace_int3_slots, task_struct, + thread_info.ftrace_int3_stack.slots); +#endif } -- 2.13.7