From patchwork Wed Jul 17 08:27:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhu Hengbo X-Patchwork-Id: 13735212 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B75CBC3DA4B for ; Wed, 17 Jul 2024 08:28:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ZEBtEF9dXfLtAizGYB6BBCrV3PKP0E3IKbyoL9VtQt0=; b=nLjX6ohGORCwI/ fHKZhEezsgcm1b3XbTR34HsWHOZOgSJGdazAy/UAMKSKjc5PYO1F+CU8dUKtDlyw/qpZ8Bd5PJ7JN wdxNEHQEXEDSGu1ovLHoHNEaX8PSvfMnKGFXUNBn34cc62KsMXPSr/oFisEkwUHCQE/TNTLO6fZYi ss4bMIEQ3VZ/48MX4VIu74+4frvIXWkAq08qse9T7PiSkg34WIrdAupblcYOGRc1LtfdfJtf2Jyt6 7r51VKEwKgAcTR3zdyKWH824J2aFkMvsaZdT/TbYtczBR3mUwmZfeLRNV0M458jVodZlWHoJK+j/Y AYRgtzmA475zghPiDdpA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sU01G-0000000D8bb-2tw9; Wed, 17 Jul 2024 08:28:14 +0000 Received: from smtp21.cstnet.cn ([159.226.251.21] helo=cstnet.cn) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sU01D-0000000D8b1-0p4I for linux-riscv@lists.infradead.org; Wed, 17 Jul 2024 08:28:12 +0000 Received: from ubuntu-2204-dev.. (unknown [180.110.112.93]) by APP-01 (Coremail) with SMTP id qwCowABnbsp1gJdmjKDXAw--.5352S2; Wed, 17 Jul 2024 16:27:34 +0800 (CST) From: Zhu Hengbo To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Zhu Hengbo , Andrew Morton , Suren Baghdasaryan , Alexandre Ghiti , Kefeng Wang , "Matthew Wilcox (Oracle)" , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] riscv: add tracepoints for page fault Date: Wed, 17 Jul 2024 08:27:19 +0000 Message-Id: <20240717082728.84401-1-zhuhengbo@iscas.ac.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240715101400.39103-1-zhuhengbo@iscas.ac.cn> References: <20240715101400.39103-1-zhuhengbo@iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: qwCowABnbsp1gJdmjKDXAw--.5352S2 X-Coremail-Antispam: 1UD129KBjvJXoWxWryrtw4xXr1DGry7Xw4DXFb_yoWrKrWkpF nFkr95Gr47Xwsa93yxZwn2vF15CanYkayjgry3C3yYvw4IqryUA392q3yqyr17Ar1kGFyI 9F4Yyrya9w45uaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvY14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r4j6ryUM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6xkF7I0E14v26r xl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj 6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWxJVW8Jr1lOx8S6xCaFVCjc4AY6r 1j6r4UM4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02 628vn2kIc2xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c 02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_ GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7 CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v2 6r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0J jUTmhUUUUU= X-Originating-IP: [180.110.112.93] X-CM-SenderInfo: x2kxxvpqje0q5lvft2wodfhubq/1tbiBwkBDGaXTZmt1QABsf X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240717_012811_625206_0A190CD4 X-CRM114-Status: GOOD ( 14.06 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Introduce page_fault_user and page_fault_kernel for riscv page fault. Help to get more detail information when page fault happen. Signed-off-by: Zhu Hengbo --- Changes in v2: - Add print instruction point info Simple test go below: root@riscv-ubuntu2204 ~ # bin/perf list | grep exceptions exceptions:page_fault_kernel [Tracepoint event] exceptions:page_fault_user [Tracepoint event] root@riscv-ubuntu2204 ~ # bin/perf record -e exceptions:page_fault_kernel -e exceptions:page_fault_user [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.091 MB perf.data (19 samples) ] perf report tracepoint: perf 563 [007] 115.824363: exceptions:page_fault_user: user page fault, address=0x7fff94cf6400 epc=0x55558632808e cause=0xd perf 563 [007] 115.824441: exceptions:page_fault_user: user page fault, address=0x7fff94c75400 epc=0x55558632808e cause=0xd perf 563 [007] 115.824518: exceptions:page_fault_user: user page fault, address=0x7fff94bf4400 epc=0x55558632808e cause=0xd perf 563 [007] 115.824907: exceptions:page_fault_kernel: kernel page fault, address=0x7fff94bf5000 epc=fault_in_readable cause=0xd perf 563 [007] 115.825238: exceptions:page_fault_user: user page fault, address=0x7fff94bf4408 epc=0x5555863281bc cause=0xf perf 564 [000] 116.247999: exceptions:page_fault_user: user page fault, address=0x7fff94b73400 epc=0x55558632808e cause=0xd perf 564 [000] 116.248558: exceptions:page_fault_user: user page fault, address=0x7fff94af2400 epc=0x55558632808e cause=0xd --- arch/riscv/include/asm/trace/exceptions.h | 66 +++++++++++++++++++++++ arch/riscv/mm/fault.c | 15 ++++++ 2 files changed, 81 insertions(+) create mode 100644 arch/riscv/include/asm/trace/exceptions.h diff --git a/arch/riscv/include/asm/trace/exceptions.h b/arch/riscv/include/asm/trace/exceptions.h new file mode 100644 index 000000000000..ff258da2f45f --- /dev/null +++ b/arch/riscv/include/asm/trace/exceptions.h @@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Tracepoints for RISC-V exceptions + * + * Copyright (C) 2024 ISCAS. All rights reserved + * + */ + +#if !defined(_TRACE_PAGE_FAULT_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_PAGE_FAULT_H + +#include + +#undef TRACE_SYSTEM +#define TRACE_SYSTEM exceptions + +TRACE_EVENT(page_fault_user, + TP_PROTO(struct pt_regs *regs), + TP_ARGS(regs), + + TP_STRUCT__entry( + __field(unsigned long, address) + __field(unsigned long, epc) + __field(unsigned long, cause) + ), + + TP_fast_assign( + __entry->address = regs->badaddr; + __entry->epc = regs->epc; + __entry->cause = regs->cause; + ), + + TP_printk("user page fault, address=%ps epc=%ps cause=0x%lx", + (void *)__entry->address, (void *)__entry->epc, + __entry->cause) +); + +TRACE_EVENT(page_fault_kernel, + TP_PROTO(struct pt_regs *regs), + TP_ARGS(regs), + + TP_STRUCT__entry( + __field(unsigned long, address) + __field(unsigned long, epc) + __field(unsigned long, cause) + ), + + TP_fast_assign( + __entry->address = regs->badaddr; + __entry->epc = regs->epc; + __entry->cause = regs->cause; + ), + + TP_printk("kernel page fault, address=%ps epc=%ps cause=0x%lx", + (void *)__entry->address, (void *)__entry->epc, + __entry->cause) +); + +#undef TRACE_INCLUDE_PATH +#undef TRACE_INCLUDE_FILE +#define TRACE_INCLUDE_PATH asm/trace/ +#define TRACE_INCLUDE_FILE exceptions +#endif /* _TRACE_PAGE_FAULT_H */ + +/* This part must be outside protection */ +#include diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c index 5224f3733802..22874074c5bc 100644 --- a/arch/riscv/mm/fault.c +++ b/arch/riscv/mm/fault.c @@ -22,6 +22,10 @@ #include "../kernel/head.h" + +#define CREATE_TRACE_POINTS +#include + static void die_kernel_fault(const char *msg, unsigned long addr, struct pt_regs *regs) { @@ -215,6 +219,15 @@ static inline bool access_error(unsigned long cause, struct vm_area_struct *vma) return false; } + +static inline void trace_page_fault(struct pt_regs *regs) +{ + if (user_mode(regs)) + trace_page_fault_user(regs); + else + trace_page_fault_kernel(regs); +} + /* * This routine handles page faults. It determines the address and the * problem, and then passes it off to one of the appropriate routines. @@ -235,6 +248,8 @@ void handle_page_fault(struct pt_regs *regs) tsk = current; mm = tsk->mm; + trace_page_fault(regs); + if (kprobe_page_fault(regs, cause)) return;