Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3546042imm; Mon, 8 Oct 2018 05:56:05 -0700 (PDT) X-Google-Smtp-Source: ACcGV62yN9yCdB/U0YhDTAag9DkPVqswUqbIo5c6/u7EGXFlxcXlKHuEvzwSvTPLH/vpzyHPFQuG X-Received: by 2002:a17:902:9:: with SMTP id 9-v6mr11993313pla.293.1539003365129; Mon, 08 Oct 2018 05:56:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539003365; cv=none; d=google.com; s=arc-20160816; b=lwea3gDfFc3C2duq9nfM/0yZaN0zIfGVj2A1zVbgibeNZ1G8XOq6ZL8Ay8o+Y6rEpU hc5EyzQMKrSzcLLHwN6aLFU/n3PPmCiHRqqCW+BY28OgW5D0ph4AvYz4s8NESNsdsXbQ +ypUsjLqxRPRAyPwLjackiKa2yEtkwRoU8wVWeXwYyXirOksN2k3gV6NJUilyMSNCaOp ppqWFuHRn4OXZFCFo8EUAQWPvdLmLDJL5gHzBMAXG3UQfgeNzItohd8rksjtU+MDnd4+ wSSi1AIVK31CCqnDLNEcehbhKBC++8dJQPZS5ph9eUJoygd1B/6oZDWdeNkhzHlDA4hI SXfg== 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=5y7h43rbA0DT0gX7pYMBz95m8IMKUd3bUkbODyKCBM0=; b=BmRt5v4Pbe+Q8qfxSnuHRwWcPrfS4PLKwbKvaG+QipDJCGAsLy6Ooqp0w+yuC2ZKmN rgcZaGXiFH0g2Bk3kmS1PNbDWJrA/zt56Gox3vNqiVzwn7tF6ShKlmW1t17fyLsjpaTJ tFNNOJXGXAEn5h4zuPDSOYxPAd4bulZkN3gmf4Zat0fyYJh2tBezMb5kzRiD040Fv4Hg OPX/oLe6VHqvJoXEXKuRWUOe+MqKbVYUD9QqBkzw+939cx+6J4dHs8ecaRQPwnDbEqpT pVZ8MrPlRkCaqUStMggjFZ0Fy8jc2LsP2BzX07vZ9XgsP7gw7FKztNtVpEZ447Oid008 4fJA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o10-v6si18199533pgg.195.2018.10.08.05.55.49; Mon, 08 Oct 2018 05:56:05 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726750AbeJHUF1 (ORCPT + 99 others); Mon, 8 Oct 2018 16:05:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39208 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725893AbeJHUF0 (ORCPT ); Mon, 8 Oct 2018 16:05:26 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D3E9F309174E; Mon, 8 Oct 2018 12:53:51 +0000 (UTC) Received: from f28server.default (ovpn-116-135.phx2.redhat.com [10.3.116.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4C59C61D3F; Mon, 8 Oct 2018 12:53:48 +0000 (UTC) From: Daniel Bristot de Oliveira To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Greg Kroah-Hartman , Pavel Tatashin , Masami Hiramatsu , "Steven Rostedt (VMware)" , Zhou Chengming , Jiri Kosina , Josh Poimboeuf , "Peter Zijlstra (Intel)" , Chris von Recklinghausen , Jason Baron , Scott Wood , Marcelo Tosatti , Clark Williams Subject: [RFC PATCH 5/6] x86/alternative: Split text_poke_bp() into tree steps Date: Mon, 8 Oct 2018 14:53:04 +0200 Message-Id: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Mon, 08 Oct 2018 12:53:52 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org text_poke_bp() updates instructions on live kernel on SMP in three steps: 1) add a int3 trap to the address that will be patched 2) update all but the first byte of the patched range 3) replace the first byte (int3) by the first byte of This patch creates one function for each of these steps. Signed-off-by: Daniel Bristot de Oliveira Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: Greg Kroah-Hartman Cc: Pavel Tatashin Cc: Masami Hiramatsu Cc: "Steven Rostedt (VMware)" Cc: Zhou Chengming Cc: Jiri Kosina Cc: Josh Poimboeuf Cc: "Peter Zijlstra (Intel)" Cc: Chris von Recklinghausen Cc: Jason Baron Cc: Scott Wood Cc: Marcelo Tosatti Cc: Clark Williams Cc: x86@kernel.org Cc: linux-kernel@vger.kernel.org --- arch/x86/kernel/alternative.c | 38 +++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index b9d5e7c9ef43..a4c83cb49cd0 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -763,6 +763,29 @@ int poke_int3_handler(struct pt_regs *regs) } +static void text_poke_bp_set_handler(void *addr, void *handler, + unsigned char int3) +{ + bp_int3_handler = handler; + bp_int3_addr = (u8 *)addr + sizeof(int3); + text_poke(addr, &int3, sizeof(int3)); +} + +static void patch_all_but_first_byte(void *addr, const void *opcode, + size_t len, unsigned char int3) +{ + /* patch all but the first byte */ + text_poke((char *)addr + sizeof(int3), + (const char *) opcode + sizeof(int3), + len - sizeof(int3)); +} + +static void patch_first_byte(void *addr, const void *opcode, unsigned char int3) +{ + /* patch the first byte */ + text_poke(addr, opcode, sizeof(int3)); +} + /** * text_poke_bp() -- update instructions on live kernel on SMP * @addr: address to patch @@ -787,27 +810,21 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) { unsigned char int3 = 0xcc; - bp_int3_handler = handler; - bp_int3_addr = (u8 *)addr + sizeof(int3); - bp_patching_in_progress = true; - lockdep_assert_held(&text_mutex); + bp_patching_in_progress = true; /* * Corresponding read barrier in int3 notifier for making sure the * in_progress and handler are correctly ordered wrt. patching. */ smp_wmb(); - text_poke(addr, &int3, sizeof(int3)); + text_poke_bp_set_handler(addr, handler, int3); on_each_cpu(do_sync_core, NULL, 1); if (len - sizeof(int3) > 0) { - /* patch all but the first byte */ - text_poke((char *)addr + sizeof(int3), - (const char *) opcode + sizeof(int3), - len - sizeof(int3)); + patch_all_but_first_byte(addr, opcode, len, int3); /* * According to Intel, this core syncing is very likely * not necessary and we'd be safe even without it. But @@ -816,8 +833,7 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) on_each_cpu(do_sync_core, NULL, 1); } - /* patch the first byte */ - text_poke(addr, opcode, sizeof(int3)); + patch_first_byte(addr, opcode, int3); on_each_cpu(do_sync_core, NULL, 1); /* -- 2.17.1