Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2663297ybb; Sun, 5 Apr 2020 13:15:04 -0700 (PDT) X-Google-Smtp-Source: APiQypIFLVhYL7lk0vFDFsDhs37AdpLanQdMNx7ALx8g4AebujJIiGdXt0Pi8UU2Yyx+IzXzEws2 X-Received: by 2002:a9d:69d5:: with SMTP id v21mr15366075oto.197.1586117704326; Sun, 05 Apr 2020 13:15:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586117704; cv=none; d=google.com; s=arc-20160816; b=leu22Y3fkotO3Uh34M4v+g+iyu/vZcYoSNiLvr2m3UfNNDkQ+NUM4xnmy1ikcxvjpP vm1a9umpv0PgcHytWnfISn6Jbj4RryBkhfdUoXG4da7txEBcUrshtIVA+SxEuMVH09D8 4Ed7dVmu7ixp9/JLxc/GHKf7svUVSrKuh1COw3+RDRNNC3+i0aHzSIjL9r+5p00jHXuW pqDNCYuaW7oo0HUO4bqTnbYXz/H5kZlE2E//Pt97vCbvDcrbB8v/Db+zO1LEGLVhTwTx LGrejulpQaJFpkS9dF8206LZHC1H+fA2BbvIUo5eKl4B3nlGLa2O1vRXBxZMlUZrzkjl DCGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:organization:references:in-reply-to :message-id:date:subject:cc:to:from:ironport-sdr:ironport-sdr; bh=KFVPsYkAdjxYP6k9sWevE82fZWoTfsEYm+mK8V/ds0c=; b=Z9ZeutYkVQscWXq2wYve3+rJDfha5NZjVp0/W2jnMFc+rLx+Q4/vwB1cda4etTrkon CQERuX0SwFw3+b+QNtc4rXIiDizKYYDIzw5ZOdWNG5H0r/3uOc/sWGvDVm195C1xdrP6 v2TeY6UaWKUS4NFEVgV1xc6ZLlxBxj87FIcL0wliQ2UOhBcV1yfyKF1pJiungHCG3TYV jwz5aHltCt8j3GVIthZHUtIn+yHgWvuo1ezLem8GfrCoWaK3hjx41u+ysZR/wHbwBwgA Cy99qyOT2kNDpUpk5oWuHnSWPX1ONc07XUM4L+xtGNPpp17SkD4nWvsWu3mTWiMbREYa 0yHQ== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o5si6556532oig.147.2020.04.05.13.14.52; Sun, 05 Apr 2020 13:15:04 -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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727806AbgDEUO0 (ORCPT + 99 others); Sun, 5 Apr 2020 16:14:26 -0400 Received: from mga01.intel.com ([192.55.52.88]:62225 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726887AbgDEUOZ (ORCPT ); Sun, 5 Apr 2020 16:14:25 -0400 IronPort-SDR: KhOi43E007TxgpGAcYkZPZ5kpBQhmWEyVyb/9zraGh1KsQnApW1yMp4g8cR5EXVFn0VJxTxax4 iNpMSHV29OEQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Apr 2020 13:14:25 -0700 IronPort-SDR: JpaVrJWetPt9eNI+Fc5lTAqJM/Vu1lLuI7gaveF0Aovyq8Fo3GAs6y3kfRcMlz/hUJOSErmEsI 0Jx3Lkb5gJ4Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,348,1580803200"; d="scan'208";a="329688361" Received: from ahunter-desktop.fi.intel.com ([10.237.72.87]) by orsmga001.jf.intel.com with ESMTP; 05 Apr 2020 13:14:21 -0700 From: Adrian Hunter To: Peter Zijlstra Cc: Ingo Molnar , Masami Hiramatsu , Steven Rostedt , Borislav Petkov , "H . Peter Anvin" , x86@kernel.org, Mark Rutland , Alexander Shishkin , Mathieu Poirier , Leo Yan , Arnaldo Carvalho de Melo , Jiri Olsa , linux-kernel@vger.kernel.org Subject: [PATCH V6 02/15] perf/x86: Add support for perf text poke event for text_poke_bp_batch() callers Date: Sun, 5 Apr 2020 23:13:14 +0300 Message-Id: <20200405201327.7332-3-adrian.hunter@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200405201327.7332-1-adrian.hunter@intel.com> References: <20200405201327.7332-1-adrian.hunter@intel.com> Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add support for perf text poke event for text_poke_bp_batch() callers. That includes jump labels. See comments for more details. Signed-off-by: Adrian Hunter --- arch/x86/kernel/alternative.c | 37 ++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 15ac0d5f4b40..69b49521bc4b 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -947,6 +948,7 @@ struct text_poke_loc { s32 rel32; u8 opcode; const u8 text[POKE_MAX_OPCODE_SIZE]; + u8 old; }; struct bp_patching_desc { @@ -1115,8 +1117,10 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries /* * First step: add a int3 trap to the address that will be patched. */ - for (i = 0; i < nr_entries; i++) + for (i = 0; i < nr_entries; i++) { + tp[i].old = *(u8 *)text_poke_addr(&tp[i]); text_poke(text_poke_addr(&tp[i]), &int3, INT3_INSN_SIZE); + } text_poke_sync(); @@ -1124,14 +1128,45 @@ static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries * Second step: update all but the first byte of the patched range. */ for (do_sync = 0, i = 0; i < nr_entries; i++) { + u8 old[POKE_MAX_OPCODE_SIZE] = { tp[i].old, }; int len = text_opcode_size(tp[i].opcode); if (len - INT3_INSN_SIZE > 0) { + memcpy(old + INT3_INSN_SIZE, + text_poke_addr(&tp[i]) + INT3_INSN_SIZE, + len - INT3_INSN_SIZE); text_poke(text_poke_addr(&tp[i]) + INT3_INSN_SIZE, (const char *)tp[i].text + INT3_INSN_SIZE, len - INT3_INSN_SIZE); do_sync++; } + + /* + * Emit a perf event to record the text poke, primarily to + * support Intel PT decoding which must walk the executable code + * to reconstruct the trace. The flow up to here is: + * - write INT3 byte + * - IPI-SYNC + * - write instruction tail + * At this point the actual control flow will be through the + * INT3 and handler and not hit the old or new instruction. + * Intel PT outputs FUP/TIP packets for the INT3, so the flow + * can still be decoded. Subsequently: + * - emit RECORD_TEXT_POKE with the new instruction + * - IPI-SYNC + * - write first byte + * - IPI-SYNC + * So before the text poke event timestamp, the decoder will see + * either the old instruction flow or FUP/TIP of INT3. After the + * text poke event timestamp, the decoder will see either the + * new instruction flow or FUP/TIP of INT3. Thus decoders can + * use the timestamp as the point at which to modify the + * executable code. + * The old instruction is recorded so that the event can be + * processed forwards or backwards. + */ + perf_event_text_poke(text_poke_addr(&tp[i]), old, len, + tp[i].text, len); } if (do_sync) { -- 2.17.1