Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp1218168iol; Sun, 12 Jun 2022 09:18:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiAlPkNrCreVwk/6RNhveXtw0ci4SIfKw+cAFTEYr9rLcgtyZmXT9JNEvq3vrgRSGaQ4w3 X-Received: by 2002:a63:f415:0:b0:408:808b:238f with SMTP id g21-20020a63f415000000b00408808b238fmr483104pgi.469.1655050701041; Sun, 12 Jun 2022 09:18:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655050701; cv=none; d=google.com; s=arc-20160816; b=NPwsqHx+Ujn6hhxh62INsUne6lrcKsLee1rWjOyJ6wtaL4IMeXGv8aDio/yzgYwuxc 4Y/CNrfyzDuTLlSEtEHyHvRWPR2q3e/JeKFE+HgA1pAX7Xr+G5FGyIIncnuPa4FIB9QD XRB3k4Awb5W/7fQyqX0lzQ32BmB5NJVYYKcDgn4Tgr3YzhqkmuZOa3feIOg30VHZ2vO9 8EcErbkcaRpuC7+zRtGB2JpU9wdxQ9LNxFJUV9d1NyRrtbUVVB9kq6AT24u3YeNjKrC9 JFz4RSXFWUt/7oktxZXMfRudRGO3q0oOFkG+4DR7hjjF9GAZwflx389nXpREfuUbutV8 7fiA== 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=1/+B57+0D7leMr3JSbLoGbJSdZzSfeuRP8vjT9RWeAU=; b=AXFV6mG9WsDCkplHAiAlJCAOdSP4MhuIIgUSnYnlXurYow8aHCJsTWsNrtKIL0ZM6Z DbvH39u+Z4+4whnUs5ehHZFFVEOLjA6Q465DEz2UIIUYl25AHWHpWQ5gqf8kDimZTScX klvzx9SUfiSy2qQrL9OyPHFeYBmPcn5FAVaXJKtAYCj1MpPz1NElJtR7cy3tQTLuRQbl vYobGh93+OomiZEuF4jssBKAm7a2eWsm0og8dN+LPN1i75zJiq/N1nPmfNvBhNBGH071 jenXol/jntqdMH9+FSMwLWegcYPQSDFH7pEmppD+3LDQztKfKFCBjgIFh77UOGoZUjrI jRpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ihwcLZes; 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 bm7-20020a656e87000000b003fdf4a109f2si7468573pgb.260.2022.06.12.09.18.09; Sun, 12 Jun 2022 09:18:21 -0700 (PDT) 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=ihwcLZes; 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 S236695AbiFLMbH (ORCPT + 99 others); Sun, 12 Jun 2022 08:31:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231135AbiFLMbF (ORCPT ); Sun, 12 Jun 2022 08:31:05 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B75F84EA1B; Sun, 12 Jun 2022 05:31:03 -0700 (PDT) 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 5144360EBA; Sun, 12 Jun 2022 12:31:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D957FC34115; Sun, 12 Jun 2022 12:30:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1655037062; bh=75vg2kGshxM5YQM0eTP5ZRYR8Fcv4/li4Jk7t1TdZ38=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=ihwcLZes9inwOPuBDeaJye7gcke1UBLU+1VBGf4DvKCK8cnpB6u1SzgyKO4ftcfZG ErQikmY7mpilG3tu8wVrlaHYSKH7Nu92Hyqnw97kzveTBBIVOS3uQ3Jqgu9GFW4w0/ jmS6W7bFFcvW2dOW/LuS2HXtqiKHxLtKSxRKUtWOPvP35PudVpnCQhmoWF/hc9228f gVQ4oyLRAILBu9zA0K+lA3/gxYbMJXzy+bnGsqhl3RfaVq5/VfkBb87Ki9hv8Dkl4s ONjTLXhsGE6D3ntAiXzcwMS2P7ueWXHWUvDCIewbY4eDMY//09jGQh9ejxWsMYhY9O DS4tlNoS30DUQ== Date: Sun, 12 Jun 2022 21:30:41 +0900 From: Masami Hiramatsu (Google) To: Song Liu Cc: Ard Biesheuvel , Jarkko Sakkinen , Linux Kernel Mailing List , Nathaniel McCallum , Jarkko Sakkinen , Russell King , Catalin Marinas , Will Deacon , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "David S. Miller" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , X86 ML , "H. Peter Anvin" , "Naveen N. Rao" , Anil S Keshavamurthy , Masami Hiramatsu , Luis Chamberlain , Steven Rostedt , Kees Cook , "Peter Zijlstra (Intel)" , Nathan Chancellor , Josh Poimboeuf , Mark Rutland , "Eric W. Biederman" , Marco Elver , Dan Li , Sami Tolvanen , "Russell King (Oracle)" , Nick Desaulniers , Linus Walleij , Chen Zhongjin , Nicolas Pitre , Mark Brown , Luis Machado , Geert Uytterhoeven , Joey Gouly , Masahiro Yamada , Andrew Morton , Andrey Konovalov , Kefeng Wang , Atsushi Nemoto , Guenter Roeck , Dave Anglin , Christophe Leroy , Alexei Starovoitov , Nicholas Piggin , Daniel Axtens , "Aneesh Kumar K.V" , Jordan Niethe , Guo Ren , Anup Patel , Atish Patra , Changbin Du , Heiko Stuebner , Liao Chang , Philipp Tomsich , Wu Caize , Emil Renner Berthing , Alexander Egorenkov , Thomas Richter , Tobias Huschle , Ilya Leoshkevich , Tom Lendacky , Daniel Bristot de Oliveira , Michael Roth , "Kirill A. Shutemov" , Javier Martinez Canillas , Miroslav Benes , =?UTF-8?B?QW5kcsOp?= Almeida , Tiezhu Yang , Dmitry Torokhov , Aaron Tomlin , linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linux-modules@vger.kernel.org Subject: Re: [PATCH] kprobes: Enable tracing for mololithic kernel images Message-Id: <20220612213041.b1ec5d1ec3426e90e669c495@kernel.org> In-Reply-To: References: <20220608000014.3054333-1-jarkko@profian.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; 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=-9.5 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,T_SCC_BODY_TEXT_LINE 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 On Wed, 8 Jun 2022 11:19:19 -0700 Song Liu wrote: > On Wed, Jun 8, 2022 at 9:28 AM Ard Biesheuvel wrote: > > > > Hello Jarkko, > > > > On Wed, 8 Jun 2022 at 02:02, Jarkko Sakkinen wrote: > > > > > > Tracing with kprobes while running a monolithic kernel is currently > > > impossible because CONFIG_KPROBES is dependent of CONFIG_MODULES. This > > > dependency is a result of kprobes code using the module allocator for the > > > trampoline code. > > > > > > Detaching kprobes from modules helps to squeeze down the user space, > > > e.g. when developing new core kernel features, while still having all > > > the nice tracing capabilities. > > > > > > For kernel/ and arch/*, move module_alloc() and module_memfree() to > > > module_alloc.c, and compile as part of vmlinux when either CONFIG_MODULES > > > or CONFIG_KPROBES is enabled. In addition, flag kernel module specific > > > code with CONFIG_MODULES. > > > > > > As the result, kprobes can be used with a monolithic kernel. > > > > I think I may have mentioned this the previous time as well, but I > > don't think this is the right approach. > > > > Kprobes uses alloc_insn_page() to allocate executable memory, but the > > requirements for this memory are radically different compared to > > loadable modules, which need to be within an arch-specific distance of > > the core kernel, need KASAN backing etc etc. > > I think the distance of core kernel requirement is the same for kprobe > alloc_insn_page and modules, no? This strongly depends on how kprobes (software breakpoint and single-step) is implemented on the arch. For example, x86 implements the so-called "kprobe-booster" which jumps back from the single stepping trampoline buffer. Then the buffer address must be within the range where it can jump to the original address. However, if the arch implements single-step as an instruction emulation, it has no such limitation. As far as I know, arm64 will do emulation for the instructions which change PC register and will do direct execution with another software breakpoint for other instructions. Why I'm using module_alloc() for a generic function, is that can cover the limitation most widely. Thus, if we have CONFIG_ARCH_HAVE_ALLOC_INSN_PAGE flag and kprobes can check it instead of using __weak function, the kprobes may not need to depend on module_alloc() in general. Thank you, > > Thanks, > Song > > > > > This is why arm64, for instance, does not implement alloc_insn_page() > > in terms of module_alloc() [and likely does not belong in this patch > > for that reason] > > > > > > > Is there any reason kprobes cannot simply use vmalloc()? > > -- Masami Hiramatsu (Google)