Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp382155rwb; Thu, 12 Jan 2023 07:22:56 -0800 (PST) X-Google-Smtp-Source: AMrXdXsV8ECw0I1ecm3IOXcSKvsoOeHyMRddrxQmrHoV2vdBitDvcAEm2GNbluVl3eYEGh0mawrt X-Received: by 2002:a62:bd19:0:b0:566:900d:a1de with SMTP id a25-20020a62bd19000000b00566900da1demr75563080pff.26.1673536976243; Thu, 12 Jan 2023 07:22:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673536976; cv=none; d=google.com; s=arc-20160816; b=tJ74pzUY3loTmuEXoE1gwvKj8T8lp/O36Z8B9c/s2xXJxhoq/79m9reaRJ99RTXE7O an8GKgPJ8ETpZA2FMm3rv/Yf+UV114lcjmbxGEF74RzF2lK92/tVWGRiw+qP7W3e3jlH tJ/Xg+macKAmGARspNQ9zLcXszOezsb4XTpy+9LDtFJLUwu++zwRteNNULfznBuvkkeG aWMni1x0IU1OdPYQLeSKYEIPAhaDHtSRE52W+H2aeDn8mJIm+qRb7SLZMlZ3CKxWEhU7 NVCpNX39exqs1yfRDOwGwNIb83LYm95OvejkAiPuLA7v+1xQqOBj5toicOFzjjtNMZky F1ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=OJJ+dfvgUCUBqWKi0v7Tetb4IO7EtJMZWResf+c++Sw=; b=lFpGLd9/Gn8Ekj9rr/IZcdENRzPOpjR/+JqiFSZ3KG+aTd+aq1CFd3p1Px/yNJNReZ inPlGnLO28LHWKjvqXBx4pkQ8Y8TL7hWInmoW6GyrZOPv6AV5OAd0uRjb8iCSvi99Ph4 XljDuAg5Y29r4vE3dVzAGEpB6f4F1oadeLcDKNszKE2Kv839YVLuBG5WNSblIcr0h8CM B3XRZV2yq79FzAp1xFYKLMydPkRyF4yNB7DeiMGEKB9ak08PdyA9k5M+p0ZMtlco8cfn 76jxszsSQDM94oJArG5Q60hO2fnOYBdCz5B+pwFGFLx8W1wFAP8WVu9zqYq20c+rfDXq 9H8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lIZz+Qk2; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z24-20020a056a001d9800b005879278c7fesi12711237pfw.317.2023.01.12.07.22.48; Thu, 12 Jan 2023 07:22:56 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lIZz+Qk2; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240068AbjALOuP (ORCPT + 50 others); Thu, 12 Jan 2023 09:50:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239946AbjALOtt (ORCPT ); Thu, 12 Jan 2023 09:49:49 -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 9C14811801; Thu, 12 Jan 2023 06:36: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 dfw.source.kernel.org (Postfix) with ESMTPS id EDDDF62026; Thu, 12 Jan 2023 14:36:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F03A8C433D2; Thu, 12 Jan 2023 14:36:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1673534193; bh=9nDQCiy4ZhBYJG7V8fAABcpXbz/cL2INS6RnNTWpn5c=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=lIZz+Qk2uhEmNImMBalk6jfCkyNFYOU2j7KgJZiyd7J4KZdb8Q6kep+cMExbNqLBS WzD41YJiJE0EwV8JTbfn1OvywR7eYFmvVN2Y6HHX4L+JFnhmXQyodBTI0xn2mZc1Fa 89ZkX0W8VnZ7ccWoRv1VkOauwq1UT25Zbi28lMNhaX40Zt32KTZz/VA7DoTXuFuM/G VyZFrylrtkkYkrbyue6jV9N3smPm4POG7PePSVsMS+/8izNdhpWz+Vw75WRd8t2KA6 PnN565yEY+H6dySMGTQtQMc8j05MSwWlr/UwuWhxYEn6Lm4Qt5YTI7iJb8HID3c5jV yFXK44DuowCPA== Date: Thu, 12 Jan 2023 23:36:29 +0900 From: Masami Hiramatsu (Google) To: Tiezhu Yang Cc: "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: kernel hangs when kprobe memcpy Message-Id: <20230112233629.fafdbbe07dddf364f8078df6@kernel.org> In-Reply-To: <19666c03-4bf6-7aac-3f1d-cd31ab7de2d5@loongson.cn> References: <19666c03-4bf6-7aac-3f1d-cd31ab7de2d5@loongson.cn> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, 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 Hi Tiezhu, On Thu, 12 Jan 2023 21:32:51 +0800 Tiezhu Yang wrote: > > > On 01/11/2023 07:38 PM, Tiezhu Yang wrote: > > Hi all, > > > > (1) I have the following test environment, kernel hangs when kprobe memcpy: > > > > system: x86_64 fedora 36 > > kernel version: Linux 5.7 (compile and update) > > test case: modprobe kprobe_example symbol="memcpy" > > (CONFIG_SAMPLE_KPROBES=m) > > > > In order to fix build errors, it needs to unset CONFIG_NFP and do the > > following changes: > > commit 52a9dab6d892 ("libsubcmd: Fix use-after-free for realloc(..., 0)") > > commit de979c83574a ("x86/entry: Build thunk_$(BITS) only if > > CONFIG_PREEMPTION=y") > > > > (2) Using the latest upstream mainline kernel, no hang problem due to the > > commit e3a9e681adb7 ("x86/entry: Fixup bad_iret vs noinstr") to prohibit > > probing memcpy which is put into the .noinstr.text section. > > > > # modprobe kprobe_example symbol="memcpy" > > modprobe: ERROR: could not insert 'kprobe_example': Invalid argument > > > > In my opinion, according to the commit message, the above commit is not > > intended to fix the memcpy hang problem, the problem was fixed by accident. > > > > (3) If make handler_pre() and handler_post() as empty functions in the 5.7 > > kernel code, the above hang problem does not exist. > > > > diff --git a/samples/kprobes/kprobe_example.c > > b/samples/kprobes/kprobe_example.c > > index fd346f58ddba..c194171d8a46 100644 > > --- a/samples/kprobes/kprobe_example.c > > +++ b/samples/kprobes/kprobe_example.c > > @@ -28,8 +28,6 @@ static struct kprobe kp = { > > static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs) > > { > > #ifdef CONFIG_X86 > > - pr_info("<%s> p->addr = 0x%p, ip = %lx, flags = 0x%lx\n", > > - p->symbol_name, p->addr, regs->ip, regs->flags); > > #endif > > #ifdef CONFIG_PPC > > pr_info("<%s> p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n", > > @@ -65,8 +63,6 @@ static void __kprobes handler_post(struct kprobe *p, > > struct pt_regs *regs, > > unsigned long flags) > > { > > #ifdef CONFIG_X86 > > - pr_info("<%s> p->addr = 0x%p, flags = 0x%lx\n", > > - p->symbol_name, p->addr, regs->flags); > > #endif > > #ifdef CONFIG_PPC > > pr_info("<%s> p->addr = 0x%p, msr = 0x%lx\n", > > > > I want to know what is the real reason of the hang problem when kprobe > > memcpy, > > I guess it may be kprobe recursion, what do you think? Thank you. > > > > By the way, kprobe memset has no problem whether or not handler_pre() and > > handler_post() are empty functions. > > > > Thanks, > > Tiezhu > > I find out the following call trace: > > handler_pre() > pr_info() > printk() > _printk() > vprintk() > vprintk_store() > memcpy() > > I think it may cause recursive exceptions, so we should > mark memcpy as non-kprobe-able, right? Yes, and the .noinstr.text (noinstr function attribute) is including non-kprobe-able (nokprobe function attribute). I a function is nokprobe and notrace, it should be noinstr. "NOKPROBE_SYMBOL" is used for the symbol which is called in the kprobe processing path (e.g. x86 int3 handler etc.). BTW, that the bug you reported is interesting. Even if another kprobe is called inside kprobe pre/post handler, it must be skipped. If you can share your kconfig, I can try to reproduce it. Thank you, -- Masami Hiramatsu (Google)