Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3483295lqp; Tue, 26 Mar 2024 10:14:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXeivpeVOE/H5qJmORZCAJWPM6no28LaouVswlpN7Zf9fMaumvAetqexCGtpZ0C5mvHqD+zKZe5cx6BEbZv6Mu39hzN1qxNWtf0PCV+KA== X-Google-Smtp-Source: AGHT+IHJrA7EWxra5UytIpHjrdskczL1cfAfmLaE1s8KCEtOKls2JgHp99AuGswPazgwtJ0Ef6HO X-Received: by 2002:ac8:5f12:0:b0:430:a037:db36 with SMTP id x18-20020ac85f12000000b00430a037db36mr283282qta.32.1711473264281; Tue, 26 Mar 2024 10:14:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711473264; cv=pass; d=google.com; s=arc-20160816; b=IGPNWbXS2ATxmt6xRq8/R0l2/xO3ymX7prRsLXSduRQVFlN5L8HBwj9NLS6YM9/Myn INa6Fsyz4pmvOGox3qZlICXdYS0/b5hHqucG0F2iI8NELE+xVB9XFnNrGZqcJTyeKfWY JF8iPlbK1QshNkNbMPj9oB/MI76wazOl5IwUYjR8bzYqlgpRV0sH6gxlJY6qPCs/7MEN YbnGXyHQPXC483dCAef8YMczJYy9H7eNN7Xjs7DQLgiS+PjR1YMzcqro3ISFw6EG0aIB nn3OWMtLjsLACAxVYH/UtmsYWp7b9Z60qdMuTb6//MlykNqV01A9483l/OsjGGrOmfU/ D1FQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=YJu2crzQ59e+9V//zWgUywuzjgCKsufX8ys2K+BdJiU=; fh=1aWX4RRS5CjZep4bcBRWxLuRek/SeH4MBLZtFrQeaxU=; b=UEmrjNe/wH7CEdWJIpS1SLPE+xzR8GodpmptKCh8h9J++c4IyuhRHY84K7v0weLX1R KHGs7vWaX5kunYvRHokPFhdRfgxG7cCGniHWMOVSu6n3g22bN648xoiYB+b5DrOgJ4m3 DMTNI3cIIJAfRavsIfWUBjDxRMDft64KvNugBHTQR407QQMbUBBJacp9mvetWDEoipJO 4gwwlZ05+fOXDew5+YOHks9IgKleviSqa01WBaVSIU8nANlU8+UZI9zHFlemPwlGR0Za Q4gO8u7XOmWkENV1SR9hC7deGg2IsDQmkIKhPkFw1woewNZm7JnqqF4ZZrF5m70v7MG6 Qk+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="NlHYV/Pf"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-119567-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119567-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d16-20020a05622a05d000b004314946f2f3si6875065qtb.90.2024.03.26.10.14.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 10:14:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-119567-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="NlHYV/Pf"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-119567-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119567-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E9DA91C65F31 for ; Tue, 26 Mar 2024 17:14:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0273D1CD30; Tue, 26 Mar 2024 17:13:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NlHYV/Pf" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F34191C69C for ; Tue, 26 Mar 2024 17:13:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711473238; cv=none; b=Mg9UMePwUDrKdY1cxWrIdDazMWSmBDt3vSsPZmn+C6C/1RU5c9XosY5DqNLb/o3RmSLrZLUXFrAc0pIUCkPcc/eYyCvX7HziS2iaxH+KaPmmgsuga1o/FER+5YrvwePUeVJKh1u1D41Y97AtSNB5WcsxA35zIJ7quLT2lcefIzc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711473238; c=relaxed/simple; bh=AJR5USqJiMDQOJPpOHN5Bf+9uA64buMtfFzhzaUh3jg=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=TPQ1ERm0pHEnvar6Tc//Q4lP2bwxu9Na1GFMVfRc9YDgrGaLXL2beoK5h6RYRssUcBg8TwzNSWoxbrivruIUUwBhAu0MD/EnBMDRkFxixrdjbYz9dBIiZtPKUIAabJ8rUlpivET77oS3xGZeEFwt+9InTu2c5+QxqSFLrNm2tGs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NlHYV/Pf; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C4F4C433F1; Tue, 26 Mar 2024 17:13:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711473237; bh=AJR5USqJiMDQOJPpOHN5Bf+9uA64buMtfFzhzaUh3jg=; h=Date:Cc:Subject:From:To:References:In-Reply-To:From; b=NlHYV/PfVN+fR+oUL/bR8wrrYkHVzVrwwurW1v0qiQp4Pl3WuqaQJa3gkjg0ZjopU O59LhWiDSS2m1V/OQcpDwvQx7rEAVoANBoyRk9ymFhu0dnjHxnj5FjHPTARuknaKrk tJgqzCgaJuw9AlGJkq0Fm1BLH1b9psVm+WZSJctymRcP3LEDkGgOXAFg0lEBpvQ9sW 4cPgMw57vLzHiwappHT/o3l4dUMrbXqNHgHcH4teUtFD8LP/DcAAOhNT+Z9C7ZuMN6 gw8iDk1b8rQI4DSwjPcU/RauzGlztanbOCMy2JhrfUw5xFCJ64gsLeTch+PNdCuvvI f2tMhXwszhqgw== Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 26 Mar 2024 19:13:51 +0200 Message-Id: Cc: , , , , , , , , , , , , , , , , , , Subject: Re: [PATCH 4/4] kprobes: Remove core dependency on modules From: "Jarkko Sakkinen" To: "Mark Rutland" , X-Mailer: aerc 0.17.0 References: <20240326163624.3253157-1-mark.rutland@arm.com> <20240326163624.3253157-5-mark.rutland@arm.com> In-Reply-To: <20240326163624.3253157-5-mark.rutland@arm.com> On Tue Mar 26, 2024 at 6:36 PM EET, Mark Rutland wrote: > From: Jarkko Sakkinen > > Tracing with kprobes while running a monolithic kernel is currently > impossible because KPROBES depends on MODULES. While this dependency is > necessary when KPROBES_USE_MODULE_ALLOC=3Dy, all the other module-specifi= c > code only exist to handle the case when MODULES=3Dy, and can be hidden > behind ifdeffery. > > Add the necessary ifdeffery, and remove the dependency on MODULES=3DN whe= n > KPROBES_USE_MODULE_ALLOC=3Dn. > > Currently this allows kprobes to be used when CONFIG_MODULES=3Dn on arm64 > and riscv, and other architectures can enable support by implementing > their own kprobes_alloc_insn_page() and kprobes_free_insn_page() which > do not depend on MODULES. > > Signed-off-by: Jarkko Sakkinen > Link: https://lore.kernel.org/all/20240326012102.27438-1-jarkko@kernel.or= g/ > [Mark: Remove execmem changes, depend on !KPROBES_USE_MODULE_ALLOC] > Signed-off-by: Mark Rutland > Cc: Anil S Keshavamurthy > Cc: David S. Miller > Cc: Jarkko Sakkinen > Cc: Masami Hiramatsu > Cc: Naveen N. Rao > --- > arch/Kconfig | 2 +- > kernel/kprobes.c | 12 +++++++++++- > kernel/trace/trace_kprobe.c | 15 +++++++++++++-- > 3 files changed, 25 insertions(+), 4 deletions(-) > > diff --git a/arch/Kconfig b/arch/Kconfig > index 85bb59f7b8c07..cf43de9ffb5b9 100644 > --- a/arch/Kconfig > +++ b/arch/Kconfig > @@ -52,7 +52,7 @@ config GENERIC_ENTRY > =20 > config KPROBES > bool "Kprobes" > - depends on MODULES > + depends on MODULES || !KPROBES_USE_MODULE_ALLOC > depends on HAVE_KPROBES > select KALLSYMS > select TASKS_RCU if PREEMPTION > diff --git a/kernel/kprobes.c b/kernel/kprobes.c > index fa2ee4e59eca2..7c2f0b504cdcb 100644 > --- a/kernel/kprobes.c > +++ b/kernel/kprobes.c > @@ -1582,6 +1582,7 @@ static int check_kprobe_address_safe(struct kprobe = *p, > goto out; > } > =20 > +#ifdef CONFIG_MODULES > /* Check if 'p' is probing a module. */ > *probed_mod =3D __module_text_address((unsigned long) p->addr); > if (*probed_mod) { > @@ -1605,6 +1606,8 @@ static int check_kprobe_address_safe(struct kprobe = *p, > ret =3D -ENOENT; > } > } > +#endif This can be scoped a bit more (see v7 of my patch set). > + > out: > preempt_enable(); > jump_label_unlock(); > @@ -2484,6 +2487,7 @@ int kprobe_add_area_blacklist(unsigned long start, = unsigned long end) > return 0; > } > =20 > +#ifdef CONFIG_MODULES > /* Remove all symbols in given area from kprobe blacklist */ > static void kprobe_remove_area_blacklist(unsigned long start, unsigned l= ong end) > { > @@ -2501,6 +2505,7 @@ static void kprobe_remove_ksym_blacklist(unsigned l= ong entry) > { > kprobe_remove_area_blacklist(entry, entry + 1); > } > +#endif /* CONFIG_MODULES */ > =20 > int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *= value, > char *type, char *sym) > @@ -2566,6 +2571,7 @@ static int __init populate_kprobe_blacklist(unsigne= d long *start, > return ret ? : arch_populate_kprobe_blacklist(); > } > =20 > +#ifdef CONFIG_MODULES > static void add_module_kprobe_blacklist(struct module *mod) > { > unsigned long start, end; > @@ -2662,6 +2668,9 @@ static int kprobes_module_callback(struct notifier_= block *nb, > mutex_unlock(&kprobe_mutex); > return NOTIFY_DONE; > } > +#else > +#define kprobes_module_callback (NULL) > +#endif /* CONFIG_MODULES */ > =20 > static struct notifier_block kprobe_module_nb =3D { > .notifier_call =3D kprobes_module_callback, > @@ -2726,7 +2735,8 @@ static int __init init_kprobes(void) > err =3D arch_init_kprobes(); > if (!err) > err =3D register_die_notifier(&kprobe_exceptions_nb); > - if (!err) > + > + if (!err && IS_ENABLED(CONFIG_MODULES)) > err =3D register_module_notifier(&kprobe_module_nb); > =20 > kprobes_initialized =3D (err =3D=3D 0); > diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c > index 14099cc17fc9e..c509ba776e679 100644 > --- a/kernel/trace/trace_kprobe.c > +++ b/kernel/trace/trace_kprobe.c > @@ -111,6 +111,7 @@ static nokprobe_inline bool trace_kprobe_within_modul= e(struct trace_kprobe *tk, > return strncmp(module_name(mod), name, len) =3D=3D 0 && name[len] =3D= =3D ':'; > } > =20 > +#ifdef CONFIG_MODULES > static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprob= e *tk) > { > char *p; > @@ -129,6 +130,9 @@ static nokprobe_inline bool trace_kprobe_module_exist= (struct trace_kprobe *tk) > =20 > return ret; > } > +#else > +#define trace_kprobe_module_exist(tk) false /* aka a module never exists= */ > +#endif /* CONFIG_MODULES */ > =20 > static bool trace_kprobe_is_busy(struct dyn_event *ev) > { > @@ -670,6 +674,7 @@ static int register_trace_kprobe(struct trace_kprobe = *tk) > return ret; > } > =20 > +#ifdef CONFIG_MODULES > /* Module notifier call back, checking event on the module */ > static int trace_kprobe_module_callback(struct notifier_block *nb, > unsigned long val, void *data) > @@ -699,6 +704,9 @@ static int trace_kprobe_module_callback(struct notifi= er_block *nb, > =20 > return NOTIFY_DONE; > } > +#else > +#define trace_kprobe_module_callback (NULL) > +#endif /* CONFIG_MODULES */ The last two CONFIG_MODULES sections could be combined. This was also in v7. > =20 > static struct notifier_block trace_kprobe_module_nb =3D { > .notifier_call =3D trace_kprobe_module_callback, > @@ -1933,8 +1941,11 @@ static __init int init_kprobe_trace_early(void) > if (ret) > return ret; > =20 > - if (register_module_notifier(&trace_kprobe_module_nb)) > - return -EINVAL; > + if (IS_ENABLED(CONFIG_MODULES)) { > + ret =3D register_module_notifier(&trace_kprobe_module_nb); > + if (ret) > + return -EINVAL; > + } > =20 > return 0; > } Other than lgtm. BR, Jarkko