From patchwork Thu Jun 1 10:12:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 101860 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp199332vqr; Thu, 1 Jun 2023 03:29:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6hm9rYQxe5Ov4rBPYKN5IbVJNDaRniYWyilfUEgSerKf7yQ2orpSh4rkGNBi5iqhsxyVW6 X-Received: by 2002:a17:902:74c1:b0:1b1:8ce6:4e1a with SMTP id f1-20020a17090274c100b001b18ce64e1amr2681190plt.28.1685615365702; Thu, 01 Jun 2023 03:29:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685615365; cv=none; d=google.com; s=arc-20160816; b=zGJ3mlzmCQGsA0uCTvIAtghdcOCtVJcuLj82IueM8UPhXv33o+78UPon8s5v/+NQQ0 vaKHC8tDndg66EuBEKYTkxzK76UD0yAZks5oLIaNjXLazFR6YghlT296ml7tBbXF4S1o M414IPd1OQXw5rVxlUM4vnZ8x/0Vj01ms4kn8N3IGn/d4+AjUFcITjEzeaAWryHq3efd iOaWrBTFCvawQmLp4uN6sEGhx7L2ZFQLc/lq52Q916guTHF4B7/qBygrP3PUw9G1Qrgk 6uV0YIt4TlCJhhv1Http8kf6YhVCk8D1PD4oxuLHaYt3vvO7Gpg8eqpQ7BxPFaMpUr+T Gysw== 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=J/56DbUFdPq9RXYnB2g49SoeTK1qz0HhBBMN6HfUyZw=; b=Q6bQo1c36TlWSaqB2CfexcU4K3wl3lieagRXe2nOMtFrAydpoO+r8OyrRYK3pZZCGe M42Yf+80rSiLzSRzn7fvlNT6NepTiaCJMt9euL/DzyTFq2DrfDNe/30wnMUmT6UQ5xfA XcyYucKqrYG7zRr8xPUnGOszuWbE8jSviCIVUgc34PIm/X/Ov2xuM8otw7xNIYfKjM0n CptROunihBDe5lOMDEHB0OWa/o0w1KYkRJ/4Z10MtSeHE/MP9yZm3NBW56B2AO3RVkUb XKwTbtZaazzGzTGchSk0pbs0x8oEtmcxNHBh8WAKcbkm3lwMAet2bRL1AV9XIpDA1YGx bkBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="gpP21c/R"; 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 e3-20020a170902744300b001ac937171d7si2462030plt.278.2023.06.01.03.29.11; Thu, 01 Jun 2023 03:29:25 -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="gpP21c/R"; 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 S233602AbjFAKS7 (ORCPT + 99 others); Thu, 1 Jun 2023 06:18:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233501AbjFAKSK (ORCPT ); Thu, 1 Jun 2023 06:18:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15100E6B; Thu, 1 Jun 2023 03:15:40 -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 A08FC6431E; Thu, 1 Jun 2023 10:14:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E2E8C433A0; Thu, 1 Jun 2023 10:14:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685614487; bh=Hrx9VtVZyq4uWMBfRCekQXUO/IA4A1XOafQormtZTuY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gpP21c/RwAJYJm+U25a1tWi/38F4mEWtcst293zmlG6D0PKkBX6d3h9ATwIa7uumf xA2hJJdQXNiMtmOQ7k+n+4Eh/tb5q0l+ZhSYKzMYnVQJJBECXK0Boas7FeJCclB+qU YcYICH1vai6DP7rq0TDqg3DTbnaTA2lnglkoOvwlhDm1fQ7nyhjt7rdP88kQv/EuQI aAPjl78FETn69M4Prx6Edwv1H7cz2NpKwM5xe4vWf7aC2SYCcxtbSZA9vK0QR4+ALQ VYyMUVtF0JdrmF3hiV5k4Z6ygm8TjniyOkfbF+4LCBKjd7eHTl1itn2XLvK6KpbTcp yOKt8OfWFYJOQ== 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 10/13] modules, jitalloc: prepare to allocate executable memory as ROX Date: Thu, 1 Jun 2023 13:12:54 +0300 Message-Id: <20230601101257.530867-11-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=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767495817865485486?= X-GMAIL-MSGID: =?utf-8?q?1767495817865485486?= From: "Mike Rapoport (IBM)" When executable memory will be allocated as ROX it won't be possible to update it using memset() and memcpy(). Introduce jit_update_copy() and jit_update_set() APIs and use them in modules loading code instead of memcpy() and memset(). Signed-off-by: Mike Rapoport (IBM) --- include/linux/jitalloc.h | 2 ++ kernel/module/main.c | 19 ++++++++++++++----- mm/jitalloc.c | 20 ++++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/include/linux/jitalloc.h b/include/linux/jitalloc.h index 7f8cafb3cfe9..0ba5ef785a85 100644 --- a/include/linux/jitalloc.h +++ b/include/linux/jitalloc.h @@ -55,6 +55,8 @@ 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); +void jit_update_copy(void *buf, void *new_buf, size_t len); +void jit_update_set(void *buf, int c, 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 91477aa5f671..9f0711c42aa2 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1197,9 +1197,19 @@ void __weak module_arch_freeing_init(struct module *mod) static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) { - if (mod_mem_type_is_data(type)) - return jit_data_alloc(size); - return jit_text_alloc(size); + void *p; + + if (mod_mem_type_is_data(type)) { + p = jit_data_alloc(size); + if (p) + memset(p, 0, size); + } else { + p = jit_text_alloc(size); + if (p) + jit_update_set(p, 0, size); + } + + return p; } static void module_memory_free(void *ptr, enum mod_mem_type type) @@ -2223,7 +2233,6 @@ static int move_module(struct module *mod, struct load_info *info) t = type; goto out_enomem; } - memset(ptr, 0, mod->mem[type].size); mod->mem[type].base = ptr; } @@ -2251,7 +2260,7 @@ static int move_module(struct module *mod, struct load_info *info) ret = -ENOEXEC; goto out_enomem; } - memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size); + jit_update_copy(dest, (void *)shdr->sh_addr, shdr->sh_size); } /* * Update the userspace copy's ELF section address to point to diff --git a/mm/jitalloc.c b/mm/jitalloc.c index 16fd715d501a..a8ae64364d56 100644 --- a/mm/jitalloc.c +++ b/mm/jitalloc.c @@ -7,6 +7,16 @@ static struct jit_alloc_params jit_alloc_params; +static inline void jit_text_poke_copy(void *dst, const void *src, size_t len) +{ + memcpy(dst, src, len); +} + +static inline void jit_text_poke_set(void *addr, int c, size_t len) +{ + memset(addr, c, len); +} + static void *jit_alloc(size_t len, unsigned int alignment, pgprot_t pgprot, unsigned long start, unsigned long end, unsigned long fallback_start, unsigned long fallback_end, @@ -86,6 +96,16 @@ void *jit_data_alloc(size_t len) fallback_start, fallback_end, kasan); } +void jit_update_copy(void *buf, void *new_buf, size_t len) +{ + jit_text_poke_copy(buf, new_buf, len); +} + +void jit_update_set(void *addr, int c, size_t len) +{ + jit_text_poke_set(addr, c, len); +} + struct jit_alloc_params * __weak jit_alloc_arch_params(void) { return NULL;