Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=44.246.68.102 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795196944; b=5XQ0g+vPeKaxHc7yI+fQUSXfpU+D4HhKiIq1G3UDKzm/Yl3Yv32TcH7Bp1HCkMBpaIAH 5yjLkHhEz8uZJQt3U6lqc7QS31bKZoGZap3Kq/pul4lbZxG7TNZH3THsVBvPD1+i/ZLis lDSCubbiovUHgVwYfqJy0UIEjEvc8DyP8ztqOVUutxQbfOL6h08Mp1BpnnXNylMrl80QD eZ7VBFZ+JV+krjAQZg28/t4sImLtEhNN8siuENSB2j0asWg6sE0oDbv8oc44zGwOQeoS1 dD9tqS6DZobAD3TNahSrNMQHo3DIyjGypNs1rGCjorEPzc48bDD+sREu7CNRi43O1xQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795196944; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:Received:Received:X-Farcaster-Flow-ID:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy; bh=demAIIHasGPlnHaxgm+fqtcdUykhBuanFKIB1V/U/sg=; b=zAmFkqqqgSIjLYj/hisSSBmPGo633kKF9Pq6RTUVtqfBkqWG4aarghDuN7LSetcflr// 34ytlAr51CrfiLba+clvETXJHDAso0Gt1TL/s+yGmmQ80twfNzkl8OZAULkGrPhSGXJY+ sdzipA5FAom9NqPJc44N25ZJJhkIKEGo9aeOKjMT1PoNQ6LvqhiznY+XW27prWuwEwOHF uq+1Wa2hUQCs9YFJ2GaSrqoMct7idK8YB71+CkJcqRt/Q/Lk3aOlaUsNbL0OtPE5KoHfG wKk1TEWdFT4BNZ4ajislR9WMyT4Wue8hnhS+iK/uyll4VmF+AQyYt/R1J3ckiRSfBTQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=amazon.de; spf=pass smtp.mailfrom=amazon.de; dkim=pass header.d=amazon.de header.i=@amazon.de header.a=rsa-sha256 header.s=amazoncorp2 header.b="Oa2N/cSm"; arc=none smtp.remote-ip=44.246.68.102 Received: from pdx-out-003.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-003.esa.us-west-2.outbound.mail-perimeter.amazon.com [44.246.68.102]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8A74606A325 for ; Fri, 20 Nov 2026 17:49:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A8A74606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=amazon.de header.i=@amazon.de header.a=rsa-sha256 header.s=amazoncorp2 header.b=Oa2N/cSm DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazoncorp2; t=1795196943; x=1826732943; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=demAIIHasGPlnHaxgm+fqtcdUykhBuanFKIB1V/U/sg=; b=Oa2N/cSmQ9aqBMjmqrByq9PRi9BAi3BEP+8gI6aM3yuS/D4v8zQxZLLr z4bmF0FVkqWMNh9cyCmfmB92YvTQMrHIWTpe8E8jsoL/K/jVJ8VVRei27 wFq0+5naeh5e9fxWB+HMSXp28xo/33bk2OOwwZtLOpr42FGyGY0DtTA97 SuZc/xR9zRl4PiA8Xdnm2txU1vp+a7TcGpHkuP9sMj6RMXX8PfiBJUP7m i90yNkeR/Q/IPPVv6AtV3ZMuJP/lg18ka4FZpu1XCXFme31QwK/ZBa8no Vxllm7qFaWM7jspMak7w4gruLP279/GGX3rqSs4YpLDm1sryGHO+mJ2Dh w==; X-CSE-ConnectionGUID: eAxuHap4Tkm7SBrmE2II/A== X-CSE-MsgGUID: 8j1pk7PXR5qxJHDk8AADmA== X-IronPort-AV: E=Sophos;i="6.27,223,1787011200"; d="scan'208";a="34618378" Received: from ip-10-5-9-48.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.9.48]) by internal-pdx-out-003.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Nov 2026 17:49:00 +0000 Received: from EX19MTAUWA002.ant.amazon.com [205.251.233.178:31864] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.36.204:2525] with esmtp (Farcaster) id e678e01d-2578-4512-a989-ba10ff20f44c; Fri, 20 Nov 2026 17:49:00 +0000 (UTC) X-Farcaster-Flow-ID: e678e01d-2578-4512-a989-ba10ff20f44c Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Fri, 20 Nov 2026 17:49:00 +0000 Received: from dev-dsk-gyokhan-1a-87ab81cc.eu-west-1.amazon.com (10.253.85.128) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Fri, 20 Nov 2026 17:48:58 +0000 From: Gyokhan Kochmarla To: CC: Mike Rapoport , Andrew Morton , Uladzislau Rezki , , Benjamin Tissoires , Jiri Kosina , Luis Chamberalin Subject: [PATCH 7.3.y 2/2] mm/execmem: make sure ROX cache always contains multiples of PMD_SIZE Date: Fri, 20 Nov 2026 17:47:45 +0000 Message-ID: <20261120174745.3785179-3-gyokhan@amazon.de> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20261120174745.3785179-1-gyokhan@amazon.de> References: <20261120174745.3785179-1-gyokhan@amazon.de> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D031UWC002.ant.amazon.com (10.13.139.212) To EX19D001UWA001.ant.amazon.com (10.13.138.214) From: "Mike Rapoport (Microsoft)" commit 569db54d8b7a4ec463a62c444de0d6f5f10e9dc1 upstream. The ROX cache relies on its chunks being PMD mapped. For a PMD mapped chunk, set_memory_rox() updates the direct map alias one PMD at a time and the large mappings there survive. When execmem refills the cache, it rounds up the requested allocation size to PMD_SIZE and tries to allocate that with vmalloc(VM_ALLOW_HUGE_VMAP). If that allocation fails, execmem falls back to vmalloc() of the original size. There are two issues with this approach: * If huge pages are not available, __vmalloc_node_range() silently falls back to base pages. The area execmem gets is virtually contiguous, but it is backed by 512 scattered base pages. Permission updates on such areas split large mappings in the direct map that contain those base pages, up to 512 PMD splits in the worst case. * execmem's own fallback adds a small base page mapped area to the cache. This adds the overhead of cache management to these allocations with no benefit of reducing fragmentation either in the vmalloc/modules address space or in the direct map. Worse, these areas are never freed from the cache, because execmem_cache_clean() releases only chunks that are a multiple of PMD_SIZE and aligned to PMD_SIZE, exactly to minimize the number of base page mappings. Add VM_REQUIRE_HUGE_VMAP option to vmalloc that fails if the allocation of huge pages fails or if such an allocation is not possible because huge page allocations in vmalloc were disabled or the architecture does not support them. Use this option when populating the ROX cache. If vmalloc(VM_REQUIRE_HUGE_VMAP) fails or vmalloc of huge pages is unavailable, handle the memory allocation outside the ROX cache with plain vmalloc(). Since the fallback allocation has to return ROX memory, add an execmem_alloc_rox() helper and use it for both populating the ROX cache and dealing with a fallback allocation in a ROX execmem_range. With that, the cache only ever contains PMD aligned chunks sized as a multiple of PMD_SIZE, and the PMD checks in execmem_cache_clean() become a VM_WARN_ON_ONCE() to ensure that the PMD mapping invariant does not change. Link: https://lore.kernel.org/20260903-execmem-rox-cache-pmd-v1-v1-3-11beb2a3d249@kernel.org Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: Andrew Morton Assisted-by: copilot:claude-opus-5 Cc: Benjamin Tissoires Cc: Jiri Kosina Cc: Luis Chamberalin Cc: "Uladzislau Rezki (Sony)" [ 7.3: 4c4b2ed5b783 is not in 7.3.y, so cache chunks keep the execmem_set_direct_map_valid() handling: execmem_vmalloc_rox() does not call set_vm_flush_reset_perms(), and the populate error path restores the direct map before vfree(). The uncached fallback is freed with plain vfree(), so execmem_alloc_rox() passes VM_FLUSH_RESET_PERMS. ] Signed-off-by: Gyokhan Kochmarla --- include/linux/vmalloc.h | 1 + mm/execmem.c | 77 +++++++++++++++++++++++++---------------- mm/vmalloc.c | 15 +++++++- 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index aed121d729b0..6e555e31e622 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -38,6 +38,7 @@ struct iov_iter; /* in uio.h */ #define VM_DEFER_KMEMLEAK 0 #endif #define VM_SPARSE 0x00001000 /* sparse vm_area. not all pages are present. */ +#define VM_REQUIRE_HUGE_VMAP 0x00002000 /* huge page mapping or nothing */ /* bits [20..32] reserved for arch specific ioremap internals */ diff --git a/mm/execmem.c b/mm/execmem.c index 3857955e701b..02c061375000 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -51,7 +51,8 @@ static void *execmem_vmalloc(struct execmem_range *range, size_t size, } if (!p) { - pr_warn_ratelimited("unable to allocate memory\n"); + if (!(vm_flags & VM_REQUIRE_HUGE_VMAP)) + pr_warn_ratelimited("unable to allocate memory\n"); return NULL; } @@ -168,10 +169,9 @@ static void execmem_cache_clean(struct work_struct *work) struct vm_struct *vm = find_vm_area(area); size_t size = mas_range_len(&mas); - if (vm && get_vm_area_size(vm) == size && - IS_ALIGNED(size, PMD_SIZE) && - IS_ALIGNED(mas.index, PMD_SIZE)) { - struct vm_struct *vm = find_vm_area(area); + if (vm && get_vm_area_size(vm) == size) { + VM_WARN_ON_ONCE(!IS_ALIGNED(mas.index, PMD_SIZE) || + !IS_ALIGNED(size, PMD_SIZE)); /* * Preallocate to ensure mas_store does not fail @@ -289,36 +289,41 @@ static void *__execmem_cache_alloc(struct execmem_range *range, size_t size) return execmem_cache_alloc_locked(range, size); } -static void *execmem_cache_populate_alloc(struct execmem_range *range, size_t size) +static void *execmem_vmalloc_rox(struct execmem_range *range, size_t size, + unsigned long vm_flags) { - unsigned long vm_flags = VM_ALLOW_HUGE_VMAP; - struct mutex *mutex = &execmem_cache.mutex; - struct vm_struct *vm; - size_t alloc_size; - int err = -ENOMEM; - void *p; - - alloc_size = round_up(size, PMD_SIZE); - p = execmem_vmalloc(range, alloc_size, PAGE_KERNEL, vm_flags); - if (!p) { - alloc_size = size; - p = execmem_vmalloc(range, alloc_size, PAGE_KERNEL, vm_flags); - } + void *p = execmem_vmalloc(range, size, PAGE_KERNEL, vm_flags); + int err; if (!p) return NULL; - vm = find_vm_area(p); - if (!vm) - goto err_free_mem; - /* fill memory with instructions that will trap */ - execmem_fill_trapping_insns(p, alloc_size); + execmem_fill_trapping_insns(p, size); - err = set_memory_rox((unsigned long)p, vm->nr_pages); + err = set_memory_rox((unsigned long)p, size >> PAGE_SHIFT); if (err) goto err_free_mem; + return p; + +err_free_mem: + vfree(p); + return NULL; +} + +static void *execmem_cache_populate_alloc(struct execmem_range *range, size_t size) +{ + unsigned long vm_flags = VM_REQUIRE_HUGE_VMAP; + size_t alloc_size = round_up(size, PMD_SIZE); + struct mutex *mutex = &execmem_cache.mutex; + int err; + void *p; + + p = execmem_vmalloc_rox(range, alloc_size, vm_flags); + if (!p) + return NULL; + /* * New memory blocks must be allocated and added to the cache * as an atomic operation, otherwise they may be consumed @@ -337,12 +342,16 @@ static void *execmem_cache_populate_alloc(struct execmem_range *range, size_t si err_reset_direct_map: mutex_unlock(mutex); - execmem_set_direct_map_valid(vm, true); -err_free_mem: + execmem_set_direct_map_valid(find_vm_area(p), true); vfree(p); return NULL; } +static void *execmem_alloc_rox(struct execmem_range *range, size_t size) +{ + return execmem_vmalloc_rox(range, size, VM_FLUSH_RESET_PERMS); +} + static void *execmem_cache_alloc(struct execmem_range *range, size_t size) { void *p; @@ -470,6 +479,11 @@ static void *execmem_cache_alloc(struct execmem_range *range, size_t size) return NULL; } +static void *execmem_alloc_rox(struct execmem_range *range, size_t size) +{ + return NULL; +} + static bool execmem_cache_free(void *ptr) { return false; @@ -479,17 +493,20 @@ static bool execmem_cache_free(void *ptr) void *execmem_alloc(enum execmem_type type, size_t size) { struct execmem_range *range = &execmem_info->ranges[type]; - bool use_cache = range->flags & EXECMEM_ROX_CACHE; + bool use_rox_cache = range->flags & EXECMEM_ROX_CACHE; unsigned long vm_flags = VM_FLUSH_RESET_PERMS; pgprot_t pgprot = range->pgprot; void *p = NULL; size = PAGE_ALIGN(size); - if (use_cache) + if (use_rox_cache) { p = execmem_cache_alloc(range, size); - else + if (!p) + p = execmem_alloc_rox(range, size); + } else { p = execmem_vmalloc(range, size, pgprot, vm_flags); + } return kasan_reset_tag(p); } diff --git a/mm/vmalloc.c b/mm/vmalloc.c index bea9f76ed7e7..3ab7eff25565 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4019,6 +4019,12 @@ static gfp_t vmalloc_fix_flags(gfp_t flags) * %__GFP_SKIP_KASAN can be used to skip unpoisoning of mapped pages * (when prot=%PAGE_KERNEL). * + * %VM_ALLOW_HUGE_VMAP allocates huge pages when possible and falls back to + * base pages if huge page allocation fails. + * + * %VM_REQUIRE_HUGE_VMAP implies %VM_ALLOW_HUGE_VMAP and fails instead of + * silently falling back to base pages. + * * Can not be called from interrupt nor NMI contexts. * Return: the address of the area or %NULL on failure */ @@ -4044,6 +4050,10 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align, return NULL; } + /* VM_REQUIRE_HUGE_VMAP implies VM_ALLOW_HUGE_VMAP */ + if (vm_flags & VM_REQUIRE_HUGE_VMAP) + vm_flags |= VM_ALLOW_HUGE_VMAP; + if (vmap_allow_huge && (vm_flags & VM_ALLOW_HUGE_VMAP)) { /* * Try huge pages. Only try for PAGE_KERNEL allocations, @@ -4060,6 +4070,9 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align, align = max(original_align, 1UL << shift); } + if ((vm_flags & VM_REQUIRE_HUGE_VMAP) && shift == PAGE_SHIFT) + return NULL; + again: area = __get_vm_area_node(size, align, shift, VM_ALLOC | VM_UNINITIALIZED | vm_flags, start, end, node, @@ -4134,7 +4147,7 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align, return area->addr; fail: - if (shift > PAGE_SHIFT) { + if (shift > PAGE_SHIFT && !(vm_flags & VM_REQUIRE_HUGE_VMAP)) { shift = PAGE_SHIFT; align = original_align; goto again; -- 2.50.1