From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A72F13C5DA6 for ; Sat, 22 Aug 2026 13:54:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.70 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787406851; cv=none; b=f5H2TIjkvoJxt0zyAJFKGOBurGdrZbaBsJQzC2J/IT/Gauhb1EmLj9WJBmmSOnM8+3tRViq92bfrIz337OHUzyrzBDdlgrtfxFLAnPUuRl7V94+XvtNE1aI33CU9UBuW8BmQ0WkaUE2SAQ/Pi9EJJLb2diEcs3MZlJCfnwxiS5U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787406851; c=relaxed/simple; bh=yELrb/Sn4lo7UMYrT7w3YjKzJs20THaExKEmg90KpcI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=J6j2gVbjak6oj/FTaPLt52XTFPXwAQWonN0EbStEWpXzhR8toZRjVYJkdGMoGW7V1vxz+OPDp3JbzqZ8BF1EIrR3DdzNHLxhiJ0V1hxuZPAO6bHarN7DE+Vg8x5dw5KMJqoC3rjmuGWOEwLG1l29rg5pVnkzq7j6eQMs6aIMvBM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ajfLccfz; arc=none smtp.client-ip=209.85.221.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ajfLccfz" Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-47fe23b5acdso1549917f8f.0 for ; Sat, 22 Aug 2026 06:54:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787406848; x=1788011648; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=3C1MhcgCYBzARL/+z1ptrjz40O26zol1QO0uE2euMIU=; b=ajfLccfzb8aufZ93lpS12KFsEBz1a/lZsL1yqyr0/HMIvDSBamuTL+LGYhOx9pBNbk K1pirkT3tQwPO8v1WYsWEm7dw7ZfY/Of5tZkqMsz2YEVFK/rtu+9YE5wZX+17XcrfAMC MkhEQub5GCxrKCFSQ5zd/WFT0jrtfbnXOr4WhEUHWlQQ8Ns24kbKRxCxz60zlDrHPM/U 4TNgyBe12z6G4wNKMy82hpmdtl2KaKIgMWUvjGtdM8DtKYXx6hf/YVf8eQ6NNHwuk0qO 2Z2/Gv+YvtIdB8q5bVAt3ZK3CGaNbnK5D0j85Y66QKbF16QmmxJ8TcAGeZegjp4Om3YC Qd0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787406848; x=1788011648; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=3C1MhcgCYBzARL/+z1ptrjz40O26zol1QO0uE2euMIU=; b=LRmqIGIeCwT7s6FPx+v4mVcKqKKfoJJjmodN8Nl4g8c3mZj2U0fFv7P4vx0FBoorI/ vI9JzAmL3uOsrMzUAls6thGa5UYruuFb0Wae4y7Lundo+c1Ru7lAvFzDxmOL7iwYQUQF 5/1NKaZQpJ4PcYHEamBKKfUh0y1viS/AdDRU00wMVlA+1wGhXeRNUeEFgZAdWMLK1rKd pxgNGvhuXGC0AYiNEKvNEp5f9mk4Lt2C+uPwBl00A6m16pbIXCmqN9YqDUEUYaY3iBH6 eKqzN03Qaa/dSkx6nmNnAoh1eDcX/ECipQikcVzJnUAgLc18Log3YA/mFyZ6U+2O9XHA 8apA== X-Gm-Message-State: AFuF++niQuuWVfqlUG3KW8UpZzK0XQyQEaWrGCWzdiZg6DtmIu3+eRaI eoo0ia4bnYOabK7Xa8wHV99t5iCU8KeSHd96nih7l9ktnMMus2QgKPOc3S7EdW6qzy+PPNFxV5H fGsIo69EvYSJKh1Wp131az5FjTQW4MIU7x1cvd7oPrn8Tq1atbVJLueOV+6js5zV0nC2zgGbJ+6 z6B8U0Or8zDBP55V1h/9omGWQk6Zq09h341Q== X-Received: from wrnk3.prod.google.com ([2002:adf:e8c3:0:b0:481:5a0f:19b5]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:310f:b0:47f:7129:e30 with SMTP id ffacd0b85a97d-482c0b976f1mr17563678f8f.11.1787406847588; Sat, 22 Aug 2026 06:54:07 -0700 (PDT) Date: Sat, 22 Aug 2026 15:53:26 +0200 In-Reply-To: <20260822135323.795946-11-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260822135323.795946-11-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2204; i=ardb@kernel.org; h=from:subject; bh=2CJ7XIW20mRvuSe13JPdhhevFvLjPgwtnqW6fGhR0pc=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIatz5c1bi+7sDNh4oOHy8XMHks1ZWx/c9LM5qpAednXj/ pol76fv6ChlYRDjYpAVU2QRmP333c7TE6VqnWfJwsxhZQIZwsDFKQAT0dVjZGjsanuobS/bwOzn INDJt9ssjUtyEdfbFTGrcj54/RWdJsTw31v4rEPQ3sLVG+ati7dtnpxtEvm4wabQeZJex247NZX 3/AA= X-Mailer: git-send-email 2.55.0.860.g4b6b3295ed-goog Message-ID: <20260822135323.795946-15-ardb+git@google.com> Subject: [RFC PATCH 4/9] module: Allocate MOD_INIT_TEXT from the MOD_TEXT ROX allocation From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Catalin Marinas , Will Deacon , Steven Rostedt , Masami Hiramatsu , Mark Rutland , Andrew Morton , Mike Rapoport , Luis Chamberlain , Petr Pavlu , Daniel Gomez , Sami Tolvanen , Aaron Tomlin , "=?UTF-8?q?Adrian=20Barna=C5=9B?=" , Ryan Roberts , Kevin Brodsky , linux-arm-kernel@lists.infradead.org, linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org Content-Type: text/plain; charset="UTF-8" From: Ard Biesheuvel When execmem ROX caches are used for module text and inittext, place them adjacently in memory, by allocating space for both initially, and splitting off the space for MOD_INIT_TEXT as needed. This avoids the corner case on arm64, where .init.text being placed far from .text results in a lot of complexity wrt indirect branches and PLTs that we'd prefer to avoid. Signed-off-by: Ard Biesheuvel --- kernel/module/main.c | 22 ++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 46dd8d25a605..2d6213496359 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1342,7 +1342,7 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) { unsigned int size = PAGE_ALIGN(mod->mem[type].size); enum execmem_type execmem_type; - void *ptr; + void *ptr = NULL; mod->mem[type].size = size; @@ -1351,11 +1351,25 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) else execmem_type = EXECMEM_MODULE_TEXT; - ptr = execmem_alloc_rw(execmem_type, size); + bool is_rox = execmem_is_rox(execmem_type); + if (is_rox) { + /* + * Special case for MOD_TEXT / MOD_INIT_TEXT: allocate the + * latter by splitting off required space from the former + * so that they are always placed close together. + */ + if (type == MOD_TEXT) + size += PAGE_ALIGN(mod->mem[MOD_INIT_TEXT].size); + else if (type == MOD_INIT_TEXT) + ptr = execmem_split(mod->mem[MOD_TEXT].base, size); + } + + if (!ptr) + ptr = execmem_alloc_rw(execmem_type, size); if (!ptr) return -ENOMEM; - mod->mem[type].is_rox = execmem_is_rox(execmem_type); + mod->mem[type].is_rox = is_rox; /* * The pointer to these blocks of memory are stored on the module @@ -1368,7 +1382,7 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) * *do* eventually get freed, but let's just keep things simple * and avoid *any* false positives. */ - if (!mod->mem[type].is_rox) + if (!is_rox) kmemleak_not_leak(ptr); memset(ptr, 0, size); -- 2.55.0.860.g4b6b3295ed-goog