Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp2687416pxb; Sat, 19 Feb 2022 20:22:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3fFuC3hlioIV0bTTuI79QhrrRsi6KIPYhSiYRnHuphjTbIFALhlBtiLMlKZM1ly+wLFl2 X-Received: by 2002:a17:902:9f8f:b0:14d:66b4:3e96 with SMTP id g15-20020a1709029f8f00b0014d66b43e96mr13629307plq.97.1645330977991; Sat, 19 Feb 2022 20:22:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645330977; cv=none; d=google.com; s=arc-20160816; b=hXcvbBCx1JpOfC+MypHJrIyI2Qp39vDGvHX6/TvKAKACDY/jjKxWbZHjth3yQzD7yh A6L3imfbbyBzfK/s19gO/pZ1p3WGQd35UrQsHFV461eNrIWl2TBVnnguX5T3tUuweuDh DuEhftkdXstr/lkKaCxIxDjKZ03M5+0tit0iGHCcm4rxYVYbYrAxKOpYrkHdVftM3AIQ gGy8LQicNLu7BmFiNTSTKOGT/RROAsI34QH6nQQ1/Tw77pFnYpJaKEghEstuUj+1jzwW 7JGutbtBDtekqe3z7sZmDAvxz7m2Gc10ttd0gw8ve1/9hgs2SAGzU4EbcvHNRX/S9Uuw WBbQ== 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:date:subject:cc:to:from :dkim-signature; bh=ePF02phTQNWdjP93kafJk+s6599DXNgjVbs0ZVFVdQM=; b=r/W+erFSpPekqnVt6KruwXQH0x0ouBvNPHqX9T0VJ9m9Ms/WSzgpESNczMBkkiemTA 7k8PmmUsF409Lhpiu+eHibjuouXzsGqLn/2QxC0FwFBzTju0AtXT66U9hAofOZ4yBRvS mxLIoUqLeIYPr180ifD31wKTXwOLgnfQH20ltfbBfdbMyvFxrEEac9rmjV1AtJyrCdzW X9mwHcVi7USc68Hc6CO9M0M77xBvWzKG7EMj573nCW7YiTQZpt/b5PloW0EmGpLV+b+E MeCe3nAvAdwuox0+bBgR5zahk2jBuNSlgH6ioVlSi19ILRdpzVF7DWj8968R93EHXJpr VsHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ISWieQXe; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m3si22459402plm.283.2022.02.19.20.22.42; Sat, 19 Feb 2022 20:22:57 -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=@redhat.com header.s=mimecast20190719 header.b=ISWieQXe; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239792AbiBRV1o (ORCPT + 99 others); Fri, 18 Feb 2022 16:27:44 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:51494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239796AbiBRVZm (ORCPT ); Fri, 18 Feb 2022 16:25:42 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 33C692409D8 for ; Fri, 18 Feb 2022 13:25:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645219522; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ePF02phTQNWdjP93kafJk+s6599DXNgjVbs0ZVFVdQM=; b=ISWieQXeDpCk+KmT62RPuPdKMdTqF6umgY5vikQst6ffcryEACws5EsCP+a+9yJcUp1vG+ GlgaqPeJvCo1tfVk2xDYorTLwuwJH81vvrqnmGMEOYB+uxP318o/q7QGkNYuaVoyZ03lCX exjnNHG/ZNQISk+Ysi5LdMu+kfNJ7J4= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-99-0iSSoCbYM2-XubFSDJsuJA-1; Fri, 18 Feb 2022 16:25:20 -0500 X-MC-Unique: 0iSSoCbYM2-XubFSDJsuJA-1 Received: by mail-wm1-f72.google.com with SMTP id c7-20020a1c3507000000b0034a0dfc86aaso6464808wma.6 for ; Fri, 18 Feb 2022 13:25:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ePF02phTQNWdjP93kafJk+s6599DXNgjVbs0ZVFVdQM=; b=OZazsI7YZNoVEM3EaVK4059NBwFiVdSGsm/xazDElWwP1v/ebgLn09eFTteF4qb7oY ByvGIhwTuW61gLu1jAoXqxj2btM0vyNYjZbNGkdRiucphlaby6Yi7F+4VGC8FAEz8lZf 3Ohs5Xs15TaCCJaku/ztIvM97bj6gCR6ZDIy998s+okZzxEUm03DTooujTCXRDcwmpJK MJBwQmllARydSlSZEs6nkneQ8bxqBOkDHlAcx1JF6fRcgoa9X/1J7/Yv5aoAow4S3Vyf uALc/w4StwcYI3eB8WdXf9aPwOTmet6wEGWi64oUWBqp3REoQUONbMX6E0XktOvpHu0v KhLA== X-Gm-Message-State: AOAM533EigrV973B+huKEJGz5ijLC32UVZ/B47psOtoN/TrRHmlz0Jka uul90RT95t9/RMpurToHRrCJU8zlhyyBrp72ldqhkw1JH3OzR3HbeSSrdElAw7q2/Ve4ttphavD yj0cBX+1Vwb/cCglIzEGA/nI= X-Received: by 2002:a5d:4242:0:b0:1e3:3130:5088 with SMTP id s2-20020a5d4242000000b001e331305088mr7172758wrr.540.1645219519515; Fri, 18 Feb 2022 13:25:19 -0800 (PST) X-Received: by 2002:a5d:4242:0:b0:1e3:3130:5088 with SMTP id s2-20020a5d4242000000b001e331305088mr7172738wrr.540.1645219519232; Fri, 18 Feb 2022 13:25:19 -0800 (PST) Received: from localhost (cpc111743-lutn13-2-0-cust979.9-3.cable.virginm.net. [82.17.115.212]) by smtp.gmail.com with ESMTPSA id j10sm657539wmq.20.2022.02.18.13.25.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 13:25:18 -0800 (PST) From: Aaron Tomlin To: mcgrof@kernel.org Cc: cl@linux.com, pmladek@suse.com, mbenes@suse.cz, akpm@linux-foundation.org, jeyu@kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, live-patching@vger.kernel.org, atomlin@atomlin.com, ghalat@redhat.com, allen.lkml@gmail.com, joe@perches.com, christophe.leroy@csgroup.eu, msuchanek@suse.de, oleksandr@natalenko.name Subject: [PATCH v6 05/13] module: Move latched RB-tree support to a separate file Date: Fri, 18 Feb 2022 21:25:03 +0000 Message-Id: <20220218212511.887059-6-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220218212511.887059-1-atomlin@redhat.com> References: <20220218212511.887059-1-atomlin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 No functional change. This patch migrates module latched RB-tree support (e.g. see __module_address()) from core module code into kernel/module/tree_lookup.c. Signed-off-by: Aaron Tomlin --- kernel/module/Makefile | 3 + kernel/module/internal.h | 33 +++++++++ kernel/module/main.c | 130 ++---------------------------------- kernel/module/tree_lookup.c | 109 ++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 126 deletions(-) create mode 100644 kernel/module/tree_lookup.c diff --git a/kernel/module/Makefile b/kernel/module/Makefile index ba3ebdb7055b..6fb21ebe1aa3 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -8,3 +8,6 @@ obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o obj-$(CONFIG_MODULE_SIG) += signing.o obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o obj-$(CONFIG_LIVEPATCH) += livepatch.o +ifdef CONFIG_MODULES +obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o +endif diff --git a/kernel/module/internal.h b/kernel/module/internal.h index ad7a444253ed..57a715454c9e 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -9,6 +9,7 @@ #include #include #include +#include #ifndef ARCH_SHF_SMALL #define ARCH_SHF_SMALL 0 @@ -93,3 +94,35 @@ static inline void module_decompress_cleanup(struct load_info *info) { } #endif + +#ifdef CONFIG_MODULES_TREE_LOOKUP +struct mod_tree_root { + struct latch_tree_root root; + unsigned long addr_min; + unsigned long addr_max; +}; + +extern struct mod_tree_root mod_tree; + +void mod_tree_insert(struct module *mod); +void mod_tree_remove_init(struct module *mod); +void mod_tree_remove(struct module *mod); +struct module *mod_find(unsigned long addr); +#else /* !CONFIG_MODULES_TREE_LOOKUP */ + +static void mod_tree_insert(struct module *mod) { } +static void mod_tree_remove_init(struct module *mod) { } +static void mod_tree_remove(struct module *mod) { } +static inline struct module *mod_find(unsigned long addr) +{ + struct module *mod; + + list_for_each_entry_rcu(mod, &modules, list, + lockdep_is_held(&module_mutex)) { + if (within_module(addr, mod)) + return mod; + } + + return NULL; +} +#endif /* CONFIG_MODULES_TREE_LOOKUP */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 3596ebf3a6c3..76b53880ad91 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -90,138 +90,16 @@ static DECLARE_WORK(init_free_wq, do_free_init); static LLIST_HEAD(init_free_list); #ifdef CONFIG_MODULES_TREE_LOOKUP - -/* - * Use a latched RB-tree for __module_address(); this allows us to use - * RCU-sched lookups of the address from any context. - * - * This is conditional on PERF_EVENTS || TRACING because those can really hit - * __module_address() hard by doing a lot of stack unwinding; potentially from - * NMI context. - */ - -static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n) -{ - struct module_layout *layout = container_of(n, struct module_layout, mtn.node); - - return (unsigned long)layout->base; -} - -static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n) -{ - struct module_layout *layout = container_of(n, struct module_layout, mtn.node); - - return (unsigned long)layout->size; -} - -static __always_inline bool -mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b) -{ - return __mod_tree_val(a) < __mod_tree_val(b); -} - -static __always_inline int -mod_tree_comp(void *key, struct latch_tree_node *n) -{ - unsigned long val = (unsigned long)key; - unsigned long start, end; - - start = __mod_tree_val(n); - if (val < start) - return -1; - - end = start + __mod_tree_size(n); - if (val >= end) - return 1; - - return 0; -} - -static const struct latch_tree_ops mod_tree_ops = { - .less = mod_tree_less, - .comp = mod_tree_comp, -}; - -static struct mod_tree_root { - struct latch_tree_root root; - unsigned long addr_min; - unsigned long addr_max; -} mod_tree __cacheline_aligned = { +struct mod_tree_root mod_tree __cacheline_aligned = { .addr_min = -1UL, }; #define module_addr_min mod_tree.addr_min #define module_addr_max mod_tree.addr_max -static noinline void __mod_tree_insert(struct mod_tree_node *node) -{ - latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops); -} - -static void __mod_tree_remove(struct mod_tree_node *node) -{ - latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops); -} - -/* - * These modifications: insert, remove_init and remove; are serialized by the - * module_mutex. - */ -static void mod_tree_insert(struct module *mod) -{ - mod->core_layout.mtn.mod = mod; - mod->init_layout.mtn.mod = mod; - - __mod_tree_insert(&mod->core_layout.mtn); - if (mod->init_layout.size) - __mod_tree_insert(&mod->init_layout.mtn); -} - -static void mod_tree_remove_init(struct module *mod) -{ - if (mod->init_layout.size) - __mod_tree_remove(&mod->init_layout.mtn); -} - -static void mod_tree_remove(struct module *mod) -{ - __mod_tree_remove(&mod->core_layout.mtn); - mod_tree_remove_init(mod); -} - -static struct module *mod_find(unsigned long addr) -{ - struct latch_tree_node *ltn; - - ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops); - if (!ltn) - return NULL; - - return container_of(ltn, struct mod_tree_node, node)->mod; -} - -#else /* MODULES_TREE_LOOKUP */ - -static unsigned long module_addr_min = -1UL, module_addr_max = 0; - -static void mod_tree_insert(struct module *mod) { } -static void mod_tree_remove_init(struct module *mod) { } -static void mod_tree_remove(struct module *mod) { } - -static struct module *mod_find(unsigned long addr) -{ - struct module *mod; - - list_for_each_entry_rcu(mod, &modules, list, - lockdep_is_held(&module_mutex)) { - if (within_module(addr, mod)) - return mod; - } - - return NULL; -} - -#endif /* MODULES_TREE_LOOKUP */ +#else /* !CONFIG_MODULES_TREE_LOOKUP */ +static unsigned long module_addr_min = -1UL, module_addr_max; +#endif /* CONFIG_MODULES_TREE_LOOKUP */ /* * Bounds of module text, for speeding up __module_address. diff --git a/kernel/module/tree_lookup.c b/kernel/module/tree_lookup.c new file mode 100644 index 000000000000..0bc4ec3b22ce --- /dev/null +++ b/kernel/module/tree_lookup.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Modules tree lookup + * + * Copyright (C) 2015 Peter Zijlstra + * Copyright (C) 2015 Rusty Russell + */ + +#include +#include +#include "internal.h" + +/* + * Use a latched RB-tree for __module_address(); this allows us to use + * RCU-sched lookups of the address from any context. + * + * This is conditional on PERF_EVENTS || TRACING because those can really hit + * __module_address() hard by doing a lot of stack unwinding; potentially from + * NMI context. + */ + +static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n) +{ + struct module_layout *layout = container_of(n, struct module_layout, mtn.node); + + return (unsigned long)layout->base; +} + +static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n) +{ + struct module_layout *layout = container_of(n, struct module_layout, mtn.node); + + return (unsigned long)layout->size; +} + +static __always_inline bool +mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b) +{ + return __mod_tree_val(a) < __mod_tree_val(b); +} + +static __always_inline int +mod_tree_comp(void *key, struct latch_tree_node *n) +{ + unsigned long val = (unsigned long)key; + unsigned long start, end; + + start = __mod_tree_val(n); + if (val < start) + return -1; + + end = start + __mod_tree_size(n); + if (val >= end) + return 1; + + return 0; +} + +static const struct latch_tree_ops mod_tree_ops = { + .less = mod_tree_less, + .comp = mod_tree_comp, +}; + +static noinline void __mod_tree_insert(struct mod_tree_node *node) +{ + latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops); +} + +static void __mod_tree_remove(struct mod_tree_node *node) +{ + latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops); +} + +/* + * These modifications: insert, remove_init and remove; are serialized by the + * module_mutex. + */ +void mod_tree_insert(struct module *mod) +{ + mod->core_layout.mtn.mod = mod; + mod->init_layout.mtn.mod = mod; + + __mod_tree_insert(&mod->core_layout.mtn); + if (mod->init_layout.size) + __mod_tree_insert(&mod->init_layout.mtn); +} + +void mod_tree_remove_init(struct module *mod) +{ + if (mod->init_layout.size) + __mod_tree_remove(&mod->init_layout.mtn); +} + +void mod_tree_remove(struct module *mod) +{ + __mod_tree_remove(&mod->core_layout.mtn); + mod_tree_remove_init(mod); +} + +struct module *mod_find(unsigned long addr) +{ + struct latch_tree_node *ltn; + + ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops); + if (!ltn) + return NULL; + + return container_of(ltn, struct mod_tree_node, node)->mod; +} -- 2.34.1