From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933304AbdKQFrP (ORCPT ); Fri, 17 Nov 2017 00:47:15 -0500 Received: from mx2.suse.de ([195.135.220.15]:38978 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756746AbdKQFrG (ORCPT ); Fri, 17 Nov 2017 00:47:06 -0500 Subject: Re: [PATCH 10/13] x86/alternative: Support indirect call replacement To: Josh Poimboeuf Cc: x86@kernel.org, linux-kernel@vger.kernel.org, Andy Lutomirski , Linus Torvalds , Sasha Levin , live-patching@vger.kernel.org, Jiri Slaby , Ingo Molnar , "H. Peter Anvin" , Peter Zijlstra , Mike Galbraith , Chris Wright , Alok Kataria , Rusty Russell , virtualization@lists.linux-foundation.org, Boris Ostrovsky , xen-devel@lists.xenproject.org, Thomas Gleixner , Borislav Petkov References: <20171116211929.g4q5wa52sq64nhe5@treble> From: Juergen Gross Message-ID: <360d5f88-d840-837a-4520-81bc087a9444@suse.com> Date: Fri, 17 Nov 2017 06:46:59 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <20171116211929.g4q5wa52sq64nhe5@treble> Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 16/11/17 22:19, Josh Poimboeuf wrote: > On Wed, Oct 25, 2017 at 01:25:02PM +0200, Juergen Gross wrote: >> On 04/10/17 17:58, Josh Poimboeuf wrote: >>> Add alternative patching support for replacing an instruction with an >>> indirect call. This will be needed for the paravirt alternatives. >>> >>> Signed-off-by: Josh Poimboeuf >>> --- >>> arch/x86/kernel/alternative.c | 22 +++++++++++++++------- >>> 1 file changed, 15 insertions(+), 7 deletions(-) >>> >>> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c >>> index 3344d3382e91..81c577c7deba 100644 >>> --- a/arch/x86/kernel/alternative.c >>> +++ b/arch/x86/kernel/alternative.c >>> @@ -410,20 +410,28 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, >>> insnbuf_sz = a->replacementlen; >>> >>> /* >>> - * 0xe8 is a relative jump; fix the offset. >>> - * >>> - * Instruction length is checked before the opcode to avoid >>> - * accessing uninitialized bytes for zero-length replacements. >>> + * Fix the address offsets for call and jump instructions which >>> + * use PC-relative addressing. >>> */ >>> if (a->replacementlen == 5 && *insnbuf == 0xe8) { >>> + /* direct call */ >>> *(s32 *)(insnbuf + 1) += replacement - instr; >>> - DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx", >>> + DPRINTK("Fix direct CALL offset: 0x%x, CALL 0x%lx", >>> *(s32 *)(insnbuf + 1), >>> (unsigned long)instr + *(s32 *)(insnbuf + 1) + 5); >>> - } >>> >>> - if (a->replacementlen && is_jmp(replacement[0])) >>> + } else if (a->replacementlen == 6 && *insnbuf == 0xff && >>> + *(insnbuf+1) == 0x15) { >>> + /* indirect call */ >>> + *(s32 *)(insnbuf + 2) += replacement - instr; >>> + DPRINTK("Fix indirect CALL offset: 0x%x, CALL *0x%lx", >>> + *(s32 *)(insnbuf + 2), >>> + (unsigned long)instr + *(s32 *)(insnbuf + 2) + 6); >>> + >>> + } else if (a->replacementlen && is_jmp(replacement[0])) { >> >> Is this correct? Without your patch this was: >> >> if (*insnbuf == 0xe8) ... >> if (is_jmp(replacement[0])) ... >> >> Now you have: >> >> if (*insnbuf == 0xe8) ... >> else if (*insnbuf == 0xff15) ... >> else if (is_jmp(replacement[0])) ... >> >> So only one or none of the three variants will be executed. In the past >> it could be none, one or both. > > It can't be a call *and* a jump. It's either one or the other. > > Maybe it's a little confusing that the jump check uses replacement[0] > while the other checks use *insnbuf? They have the same value, so the Right, I was fooled by that. > same variable should probably be used everywhere for consistency. I can > make them more consistent. > I'd appreciate that. :-) Juergen From mboxrd@z Thu Jan 1 00:00:00 1970 From: Juergen Gross Subject: Re: [PATCH 10/13] x86/alternative: Support indirect call replacement Date: Fri, 17 Nov 2017 06:46:59 +0100 Message-ID: <360d5f88-d840-837a-4520-81bc087a9444@suse.com> References: <20171116211929.g4q5wa52sq64nhe5@treble> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <20171116211929.g4q5wa52sq64nhe5@treble> Content-Language: de-DE List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: virtualization-bounces@lists.linux-foundation.org Errors-To: virtualization-bounces@lists.linux-foundation.org To: Josh Poimboeuf Cc: Boris Ostrovsky , Rusty Russell , Mike Galbraith , xen-devel@lists.xenproject.org, Peter Zijlstra , Jiri Slaby , x86@kernel.org, linux-kernel@vger.kernel.org, Sasha Levin , Chris Wright , Thomas Gleixner , Andy Lutomirski , "H. Peter Anvin" , Borislav Petkov , live-patching@vger.kernel.org, Alok Kataria , virtualization@lists.linux-foundation.org, Linus Torvalds , Ingo Molnar List-Id: virtualization@lists.linuxfoundation.org On 16/11/17 22:19, Josh Poimboeuf wrote: > On Wed, Oct 25, 2017 at 01:25:02PM +0200, Juergen Gross wrote: >> On 04/10/17 17:58, Josh Poimboeuf wrote: >>> Add alternative patching support for replacing an instruction with an >>> indirect call. This will be needed for the paravirt alternatives. >>> >>> Signed-off-by: Josh Poimboeuf >>> --- >>> arch/x86/kernel/alternative.c | 22 +++++++++++++++------- >>> 1 file changed, 15 insertions(+), 7 deletions(-) >>> >>> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c >>> index 3344d3382e91..81c577c7deba 100644 >>> --- a/arch/x86/kernel/alternative.c >>> +++ b/arch/x86/kernel/alternative.c >>> @@ -410,20 +410,28 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, >>> insnbuf_sz = a->replacementlen; >>> >>> /* >>> - * 0xe8 is a relative jump; fix the offset. >>> - * >>> - * Instruction length is checked before the opcode to avoid >>> - * accessing uninitialized bytes for zero-length replacements. >>> + * Fix the address offsets for call and jump instructions which >>> + * use PC-relative addressing. >>> */ >>> if (a->replacementlen == 5 && *insnbuf == 0xe8) { >>> + /* direct call */ >>> *(s32 *)(insnbuf + 1) += replacement - instr; >>> - DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx", >>> + DPRINTK("Fix direct CALL offset: 0x%x, CALL 0x%lx", >>> *(s32 *)(insnbuf + 1), >>> (unsigned long)instr + *(s32 *)(insnbuf + 1) + 5); >>> - } >>> >>> - if (a->replacementlen && is_jmp(replacement[0])) >>> + } else if (a->replacementlen == 6 && *insnbuf == 0xff && >>> + *(insnbuf+1) == 0x15) { >>> + /* indirect call */ >>> + *(s32 *)(insnbuf + 2) += replacement - instr; >>> + DPRINTK("Fix indirect CALL offset: 0x%x, CALL *0x%lx", >>> + *(s32 *)(insnbuf + 2), >>> + (unsigned long)instr + *(s32 *)(insnbuf + 2) + 6); >>> + >>> + } else if (a->replacementlen && is_jmp(replacement[0])) { >> >> Is this correct? Without your patch this was: >> >> if (*insnbuf == 0xe8) ... >> if (is_jmp(replacement[0])) ... >> >> Now you have: >> >> if (*insnbuf == 0xe8) ... >> else if (*insnbuf == 0xff15) ... >> else if (is_jmp(replacement[0])) ... >> >> So only one or none of the three variants will be executed. In the past >> it could be none, one or both. > > It can't be a call *and* a jump. It's either one or the other. > > Maybe it's a little confusing that the jump check uses replacement[0] > while the other checks use *insnbuf? They have the same value, so the Right, I was fooled by that. > same variable should probably be used everywhere for consistency. I can > make them more consistent. > I'd appreciate that. :-) Juergen