From patchwork Tue Dec 6 19:12:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 30476 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3003862wrr; Tue, 6 Dec 2022 11:14:36 -0800 (PST) X-Google-Smtp-Source: AA0mqf4t4s/aTWofnofDVaiHqgnrarrDcU3A/tBe+sN5TbuGIGeKL6fj0Sq5P9lfD8W/y37AT96j X-Received: by 2002:a63:c14e:0:b0:470:4f30:6d80 with SMTP id p14-20020a63c14e000000b004704f306d80mr68808328pgi.285.1670354075854; Tue, 06 Dec 2022 11:14:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670354075; cv=none; d=google.com; s=arc-20160816; b=cQTJWeKSVuC8S+a+oRNuQ2DWmEQfDfac4TBJJxd2qo0+RbmYqDneyCI5yIiUJR9TlJ WQllplZciToURQOBCfjDlsjxHYSUK4pfvBAfPJQNIw2qwjltVYO+TO7vn0VDEo9uj/WX AhXZ/vN8o6wxiBOZJD3JM4dyTn/jKxHF5AUARBco3otycacXAX5mX7Gfmyjfl8nmzEpN /X8ihQixXKou+VmN4BWM3nlDj0LxMdtqbwNfHp+VliQZZAmtaMKwr8vh4CejbsCEqZwE OqUgEVcCcHhEoTEx8XaPDb+TuOzXYjkBmqbTXMLHx8rota7FAA5QvKXw3Yb5tSs0y7u6 AN7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id; bh=kCa0unQynhfNI1NbbXG+3Au5Tp12Fc4qWnXjUrAC1wA=; b=bl6+2vKFqVObFsL9XoTymEHNuHWgFyTJeGmxsXTlicMf6HHZYDeu84Abd+6mwTxe33 T1ZXDUjn31uypuj6magMGH4Sr93jizqbP/bnTeJYEaRFR+a+QNRB2YQ1YtqnLiaU+V3b NztVPA+xQP9Fy+hrmiV5nwcyYozrPRhKlyf1eSFDy6UxaAq5pvqgVRmu9cciow5im+qM YXRkgTxNfC8J9EVKRh9ld7LHP/4evrEvCg16OICwB9vyK6s18Fw5p+zj/lE/hVbmCvcQ 38RjfJO4IxQYY26m4Acc5DGOpH9D3ICy8HQ6WWAFPawQsYx2VNxZDjTtDxRNLr183yEP 0h1w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jw10-20020a170903278a00b001871e838ba9si17034452plb.344.2022.12.06.11.14.22; Tue, 06 Dec 2022 11:14:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229575AbiLFTMk (ORCPT + 99 others); Tue, 6 Dec 2022 14:12:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229595AbiLFTMe (ORCPT ); Tue, 6 Dec 2022 14:12:34 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C43AC2D1C3; Tue, 6 Dec 2022 11:12:31 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 58CEE616C6; Tue, 6 Dec 2022 19:12:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF729C433D6; Tue, 6 Dec 2022 19:12:30 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.96) (envelope-from ) id 1p2dMj-0004Qr-2Z; Tue, 06 Dec 2022 14:12:29 -0500 Message-ID: <20221206191229.656244029@goodmis.org> User-Agent: quilt/0.66 Date: Tue, 06 Dec 2022 14:12:02 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Andrew Morton , Karol Herbst , Pekka Paalanen , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org Subject: [PATCH 1/2] x86/mm/kmmio: Switch to arch_spin_lock() References: <20221206191201.217838841@goodmis.org> MIME-Version: 1.0 X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751493195455288490?= X-GMAIL-MSGID: =?utf-8?q?1751493195455288490?= From: Steven Rostedt The mmiotrace tracer is "special". The purpose is to help reverse engineer binary drivers by removing the memory allocated by the driver and when the driver goes to access it, a fault occurs, the mmiotracer will record what the driver was doing and then do the work on its behalf by single stepping through the process. But to achieve this ability, it must do some special things. One is it needs to grab a lock while in the breakpoint handler. This is considered an NMI state, and then lockdep warns that the lock is being held in both an NMI state (really a breakpoint handler) and also in normal context. As the breakpoint/NMI state only happens when the driver is accessing memory, there's no concern of a race condition against the setup and tear-down of mmiotracer. To make lockdep and mmiotrace work together, convert the locks used in the breakpoint handler into arch_spin_lock(). Link: https://lore.kernel.org/lkml/20221201213126.620b7dd3@gandalf.local.home/ Suggested-by: Thomas Gleixner Signed-off-by: Steven Rostedt --- arch/x86/mm/kmmio.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/arch/x86/mm/kmmio.c b/arch/x86/mm/kmmio.c index d3efbc5b3449..edb486450158 100644 --- a/arch/x86/mm/kmmio.c +++ b/arch/x86/mm/kmmio.c @@ -62,7 +62,13 @@ struct kmmio_context { int active; }; -static DEFINE_SPINLOCK(kmmio_lock); +/* + * The kmmio_lock is taken in int3 context, which is treated as NMI context. + * This causes lockdep to complain about it bein in both NMI and normal + * context. Hide it from lockdep, as it should not have any other locks + * taken under it, and this is only enabled for debugging mmio anyway. + */ +static arch_spinlock_t kmmio_lock = __ARCH_SPIN_LOCK_UNLOCKED; /* Protected by kmmio_lock */ unsigned int kmmio_count; @@ -346,10 +352,10 @@ static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs) ctx->probe->post_handler(ctx->probe, condition, regs); /* Prevent racing against release_kmmio_fault_page(). */ - spin_lock(&kmmio_lock); + arch_spin_lock(&kmmio_lock); if (ctx->fpage->count) arm_kmmio_fault_page(ctx->fpage); - spin_unlock(&kmmio_lock); + arch_spin_unlock(&kmmio_lock); regs->flags &= ~X86_EFLAGS_TF; regs->flags |= ctx->saved_flags; @@ -440,7 +446,8 @@ int register_kmmio_probe(struct kmmio_probe *p) unsigned int l; pte_t *pte; - spin_lock_irqsave(&kmmio_lock, flags); + local_irq_save(flags); + arch_spin_lock(&kmmio_lock); if (get_kmmio_probe(addr)) { ret = -EEXIST; goto out; @@ -460,7 +467,9 @@ int register_kmmio_probe(struct kmmio_probe *p) size += page_level_size(l); } out: - spin_unlock_irqrestore(&kmmio_lock, flags); + arch_spin_unlock(&kmmio_lock); + local_irq_restore(flags); + /* * XXX: What should I do here? * Here was a call to global_flush_tlb(), but it does not exist @@ -494,7 +503,8 @@ static void remove_kmmio_fault_pages(struct rcu_head *head) struct kmmio_fault_page **prevp = &dr->release_list; unsigned long flags; - spin_lock_irqsave(&kmmio_lock, flags); + local_irq_save(flags); + arch_spin_lock(&kmmio_lock); while (f) { if (!f->count) { list_del_rcu(&f->list); @@ -506,7 +516,8 @@ static void remove_kmmio_fault_pages(struct rcu_head *head) } f = *prevp; } - spin_unlock_irqrestore(&kmmio_lock, flags); + arch_spin_unlock(&kmmio_lock); + local_irq_restore(flags); /* This is the real RCU destroy call. */ call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages); @@ -540,14 +551,16 @@ void unregister_kmmio_probe(struct kmmio_probe *p) if (!pte) return; - spin_lock_irqsave(&kmmio_lock, flags); + local_irq_save(flags); + arch_spin_lock(&kmmio_lock); while (size < size_lim) { release_kmmio_fault_page(addr + size, &release_list); size += page_level_size(l); } list_del_rcu(&p->list); kmmio_count--; - spin_unlock_irqrestore(&kmmio_lock, flags); + arch_spin_unlock(&kmmio_lock); + local_irq_restore(flags); if (!release_list) return; From patchwork Tue Dec 6 19:12:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 30477 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3007727wrr; Tue, 6 Dec 2022 11:24:10 -0800 (PST) X-Google-Smtp-Source: AA0mqf7icMJfBzFg1cy9Np0gfFvJaV40MEoS52NRSkJuq9HZJdcI3VNNQHV+6SdSFzrmqNBX5kbh X-Received: by 2002:a17:906:7d50:b0:79e:4880:dd83 with SMTP id l16-20020a1709067d5000b0079e4880dd83mr64242321ejp.166.1670354650778; Tue, 06 Dec 2022 11:24:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670354650; cv=none; d=google.com; s=arc-20160816; b=rLzWBJlBlKNA5xO7koadHReU+bdJFO9XgRFXW7dFUAuqXivYlXDuVv/QFnGS558FGh Oo9jkCwN3wit3D4PiZ203c461GTPvmqYM1zwUn66kkIdUmfvpCyUyDtrl6YRV4XA30aK alrXZ467+E63x9GCweFn2R7CrmWVjZvRDhUADU2mLduJSXgRqE8OXiSBV4sbACnJwZyd KOKGZCtQGUDTovkfmFtlDJX63nwK1YLq3OSt8hx7byDNhTwmaRPwdzUOBDQwurAMjq1l Ig8Tz/f/Mm06Fqrx/WHfyOcT+6R7NU+vH2ZU8/vsOT+UkPGJ3dowNjkMN14Y74DTJ+Qe /qaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id; bh=HujnPq1cy0LQHkzAMg8HiYjgoJ2sh2DK4FtK2D0dGS8=; b=RmUS4Onp+onButhq7WSZqNUCnZ7YlrCC6e8bgWY/UOJZ5wd71v/P0Nwj1joeCwWiY4 UuV17Pdd+zOWI+93H8wRqy7jRH2mlnrRnt1bR/8tchDcZexkJSM7PRdyvhA/BqsMyJZe x6oM30M5f//CjlWVrEiH6t9K2uCqNrtR4zs7jqM4/2YVrn9zj/lqCfyc9F4IceZsD70K /Lym0Gajomxxkg7isl1vqCwP2fygzluPTGjO3Ln0mQipcYoSVtyewAn4kkNmYVqbVVhy BUc0fFgeRjzXxdUmfh6urWc0GaRGvGIhy7utI7ctaB2/J3lrb8JxVjh6oEtC3Q4gesKr YqtQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s11-20020a170906a18b00b007adef1fc90fsi11579154ejy.304.2022.12.06.11.23.46; Tue, 06 Dec 2022 11:24:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229668AbiLFTMq (ORCPT + 99 others); Tue, 6 Dec 2022 14:12:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229616AbiLFTMj (ORCPT ); Tue, 6 Dec 2022 14:12:39 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0C73B1F; Tue, 6 Dec 2022 11:12:34 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 56EB9B81B2A; Tue, 6 Dec 2022 19:12:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03B3FC433D6; Tue, 6 Dec 2022 19:12:32 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.96) (envelope-from ) id 1p2dMj-0004RL-33; Tue, 06 Dec 2022 14:12:29 -0500 Message-ID: <20221206191229.813199661@goodmis.org> User-Agent: quilt/0.66 Date: Tue, 06 Dec 2022 14:12:03 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Andrew Morton , Karol Herbst , Pekka Paalanen , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "Paul E. McKenney" Subject: [PATCH 2/2] x86/mm/kmmio: Remove rcu_read_lock() References: <20221206191201.217838841@goodmis.org> MIME-Version: 1.0 X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751493798263436321?= X-GMAIL-MSGID: =?utf-8?q?1751493798263436321?= From: Steven Rostedt The mmiotrace tracer is "special". The purpose is to help reverse engineer binary drivers by removing the memory allocated by the driver and when the driver goes to access it, a fault occurs, the mmiotracer will record what the driver was doing and then do the work on its behalf by single stepping through the process. But to achieve this ability, it must do some special things. One is it take the rcu_read_lock() when the fault occurs, and then release it in the breakpoint that in the single stepping. This makes lockdep unhappy, as it changes the state of RCU from within an exception that is not contained in that exception, and we get a nasty splat from lockdep. As it also disables preemption everywhere rcu_read_lock() is taken, and enables preemption everywhere rcu_read_unlock(), and does not enable preemption in between, it is the same as synchronize_rcu_sched(). But as the RCU sched variant has the same grace period as normal RCU, there's no reason to take the rcu_read_lock(). Simply remove it. Cc: "Paul E. McKenney" Signed-off-by: Steven Rostedt Acked-by: Paul E. McKenney --- arch/x86/mm/kmmio.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/arch/x86/mm/kmmio.c b/arch/x86/mm/kmmio.c index edb486450158..e15e3aaaf94c 100644 --- a/arch/x86/mm/kmmio.c +++ b/arch/x86/mm/kmmio.c @@ -254,7 +254,6 @@ int kmmio_handler(struct pt_regs *regs, unsigned long addr) * again. */ preempt_disable(); - rcu_read_lock(); faultpage = get_kmmio_fault_page(page_base); if (!faultpage) { @@ -323,7 +322,6 @@ int kmmio_handler(struct pt_regs *regs, unsigned long addr) return 1; /* fault handled */ no_kmmio: - rcu_read_unlock(); preempt_enable_no_resched(); return ret; } @@ -363,7 +361,6 @@ static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs) /* These were acquired in kmmio_handler(). */ ctx->active--; BUG_ON(ctx->active); - rcu_read_unlock(); preempt_enable_no_resched(); /*