From patchwork Thu Jun 1 10:12:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 101853 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp198140vqr; Thu, 1 Jun 2023 03:27:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6MEJv42ZT79eqa1Q5NxGYpTdKollRnefG6aSU0iLTOt0RVbTtJ+pGtj8HGpva+fo+wKcwj X-Received: by 2002:a05:6a00:802:b0:647:776c:d19c with SMTP id m2-20020a056a00080200b00647776cd19cmr6859014pfk.13.1685615248059; Thu, 01 Jun 2023 03:27:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685615248; cv=none; d=google.com; s=arc-20160816; b=I4W3sAiMbUIB2z06gKQ6aA2c1vEo8SMGajDaw7VnbWPQE8JOivs39cmVkvxV+LFbcn QTHWiY1FACXdGXsGYMXPqbltgtJ5hdxGZngD4cPOMofNvrYxMtlPyNnvVimygHYeH8qu KhDCpVxcwqMBB4rwAWeh+ci6O3Hd5fFgZvoe7wjHOFS+gGNM3orUrzUHqxBmfcUBY8So T00DG6e3c8N/Gwhk+E47LU1uk/DNyaqftYWym19KxHqN8kWZTfwHRfD4ugHDZ+XjnuI2 R2OrqtQOOS6JlzHJc4d3eZM2XET5FWIdePyV4rV8UGXx/6JJ/N8vr4ttTEIGJu/ZK+Gt aS5A== 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=05HDVXLEJHOC+5nXP44eXKnWed4lAfRsKGVHkCVpKWA=; b=JLX2sHaHg8lw9Sk1vuUb+HJUrb63IaQREscBW/DJK8RN+cKjFStcTCkZJw8DrinQwZ MTIlgrSMUICNZc7n1zHrKoR57TAEov1V/NeJ2PEK5N4DyC29PKhgsl0WWJTscPF/HSg4 tOQZrpO6OgvhJ2AwnRr3sBZwiCrmpR+vXf9zSeNW+M/bK53sVBTb7dYmOIq2dDgLNxXo 0kCeMfFAMc5axyjoAjjw3DmKojF+QFJWC8Nb3Fu7zV42b7R/itwA6MeEDq5QVX7PD9qC b06ullYXRAqA/sD/jxzqBCInvneWaYBn+cN80I34A2MT3e9BwKQPMih+o5fCRluQqaR8 4y7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=O7CZPc6M; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f14-20020aa7968e000000b0063f032d78efsi5284430pfk.269.2023.06.01.03.27.04; Thu, 01 Jun 2023 03:27:28 -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=O7CZPc6M; 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 S233480AbjFAKRZ (ORCPT + 99 others); Thu, 1 Jun 2023 06:17:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233136AbjFAKQy (ORCPT ); Thu, 1 Jun 2023 06:16:54 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 291DB186; Thu, 1 Jun 2023 03:14:18 -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 791F86103C; Thu, 1 Jun 2023 10:14:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FAFFC433A4; Thu, 1 Jun 2023 10:14:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685614450; bh=uo4TstuV9eOgMvWG2HMh3EopMTe0NSijQ6dCLHFkzlY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O7CZPc6MlEZU2IHEZeVkwJcIwQJEEB5iuuRczkSp9DPkVEs2CTsE2n+UlMJ78bjRb bHx43pbJtvyCqQB2ppqa+4mtw9WtDOO7shncdZkW4UoVJqIpyddfjlp+zv8jSwtp42 olPg+ALemFonlyR2mBbGi7ynfKHEuoMygAQ14CkN2gqUPLF0e9eotDdkWwf3CJpO4P MtlH9YB92zpcMsMjb4wZzxxVIBXMaAbZ3tiBdQ1y1vwxIU/L1EiwcIpjq0Q8AbeucT fOc120em6tsqLyl1NTJ6DptdtOaFAD2VpQyQwJufWbj6SPI/CAMk57eeZGlrQb5uR+ zovU4757P3Cow== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Michael Ellerman , Mike Rapoport , "Naveen N. Rao" , Palmer Dabbelt , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH 06/13] mm/jitalloc: introduce jit_data_alloc() Date: Thu, 1 Jun 2023 13:12:50 +0300 Message-Id: <20230601101257.530867-7-rppt@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230601101257.530867-1-rppt@kernel.org> References: <20230601101257.530867-1-rppt@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767495694368041454?= X-GMAIL-MSGID: =?utf-8?q?1767495694368041454?= From: "Mike Rapoport (IBM)" Data related to code allocations, such as module data section, need to comply with architecture constraints for its placement and its allocation right now was done using jit_text_alloc(). Create a dedicated API for allocating data related to code allocations and allow architectures to define address ranges for data allocations. Since currently this is only relevant for powerpc variants that use the VMALLOC address space for module data allocations, automatically reuse address ranges defined for text unless address range for data is explicitly defined by an architecture. With separation of code and data allocations, data sections of the modules are now mapped as PAGE_KERNEL rather than PAGE_KERNEL_EXEC which was a default on many architectures. Signed-off-by: Mike Rapoport (IBM) --- arch/powerpc/kernel/module.c | 8 ++++++++ include/linux/jitalloc.h | 2 ++ kernel/module/main.c | 15 +++------------ mm/jitalloc.c | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/arch/powerpc/kernel/module.c b/arch/powerpc/kernel/module.c index 83bdedc7eba0..b58af61e90c0 100644 --- a/arch/powerpc/kernel/module.c +++ b/arch/powerpc/kernel/module.c @@ -96,6 +96,10 @@ static struct jit_alloc_params jit_alloc_params = { struct jit_alloc_params *jit_alloc_arch_params(void) { + /* + * BOOK3S_32 and 8xx define MODULES_VADDR for text allocations and + * allow allocating data in the entire vmalloc space + */ #ifdef MODULES_VADDR pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC; unsigned long limit = (unsigned long)_etext - SZ_32M; @@ -112,6 +116,10 @@ struct jit_alloc_params *jit_alloc_arch_params(void) jit_alloc_params.text.start = MODULES_VADDR; jit_alloc_params.text.end = MODULES_END; } + + jit_alloc_params.data.pgprot = PAGE_KERNEL; + jit_alloc_params.data.start = VMALLOC_START; + jit_alloc_params.data.end = VMALLOC_END; #else jit_alloc_params.text.start = VMALLOC_START; jit_alloc_params.text.end = VMALLOC_END; diff --git a/include/linux/jitalloc.h b/include/linux/jitalloc.h index 823b13706a90..7f8cafb3cfe9 100644 --- a/include/linux/jitalloc.h +++ b/include/linux/jitalloc.h @@ -45,6 +45,7 @@ struct jit_address_space { */ struct jit_alloc_params { struct jit_address_space text; + struct jit_address_space data; enum jit_alloc_flags flags; unsigned int alignment; }; @@ -53,6 +54,7 @@ struct jit_alloc_params *jit_alloc_arch_params(void); void jit_free(void *buf); void *jit_text_alloc(size_t len); +void *jit_data_alloc(size_t len); #ifdef CONFIG_JIT_ALLOC void jit_alloc_init(void); diff --git a/kernel/module/main.c b/kernel/module/main.c index dfb7fa109f1a..91477aa5f671 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1195,25 +1195,16 @@ void __weak module_arch_freeing_init(struct module *mod) { } -static bool mod_mem_use_vmalloc(enum mod_mem_type type) -{ - return IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC) && - mod_mem_type_is_core_data(type); -} - static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) { - if (mod_mem_use_vmalloc(type)) - return vzalloc(size); + if (mod_mem_type_is_data(type)) + return jit_data_alloc(size); return jit_text_alloc(size); } static void module_memory_free(void *ptr, enum mod_mem_type type) { - if (mod_mem_use_vmalloc(type)) - vfree(ptr); - else - jit_free(ptr); + jit_free(ptr); } static void free_mod_mem(struct module *mod) diff --git a/mm/jitalloc.c b/mm/jitalloc.c index 221940e36b46..16fd715d501a 100644 --- a/mm/jitalloc.c +++ b/mm/jitalloc.c @@ -72,6 +72,20 @@ void *jit_text_alloc(size_t len) fallback_start, fallback_end, kasan); } +void *jit_data_alloc(size_t len) +{ + unsigned int align = jit_alloc_params.alignment; + pgprot_t pgprot = jit_alloc_params.data.pgprot; + unsigned long start = jit_alloc_params.data.start; + unsigned long end = jit_alloc_params.data.end; + unsigned long fallback_start = jit_alloc_params.data.fallback_start; + unsigned long fallback_end = jit_alloc_params.data.fallback_end; + bool kasan = jit_alloc_params.flags & JIT_ALLOC_KASAN_SHADOW; + + return jit_alloc(len, align, pgprot, start, end, + fallback_start, fallback_end, kasan); +} + struct jit_alloc_params * __weak jit_alloc_arch_params(void) { return NULL; @@ -88,6 +102,23 @@ static bool jit_alloc_validate_params(struct jit_alloc_params *p) return true; } +static void jit_alloc_init_missing(struct jit_alloc_params *p) +{ + if (!pgprot_val(jit_alloc_params.data.pgprot)) + jit_alloc_params.data.pgprot = PAGE_KERNEL; + + if (!jit_alloc_params.data.start) { + jit_alloc_params.data.start = p->text.start; + jit_alloc_params.data.end = p->text.end; + } + + if (!jit_alloc_params.data.fallback_start && + jit_alloc_params.text.fallback_start) { + jit_alloc_params.data.fallback_start = p->text.fallback_start; + jit_alloc_params.data.fallback_end = p->text.fallback_end; + } +} + void jit_alloc_init(void) { struct jit_alloc_params *p = jit_alloc_arch_params(); @@ -97,6 +128,8 @@ void jit_alloc_init(void) return; jit_alloc_params = *p; + jit_alloc_init_missing(p); + return; } @@ -105,4 +138,7 @@ void jit_alloc_init(void) jit_alloc_params.text.pgprot = PAGE_KERNEL_EXEC; jit_alloc_params.text.start = VMALLOC_START; jit_alloc_params.text.end = VMALLOC_END; + jit_alloc_params.data.pgprot = PAGE_KERNEL; + jit_alloc_params.data.start = VMALLOC_START; + jit_alloc_params.data.end = VMALLOC_END; }