From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (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 CB114489894 for ; Thu, 30 Jul 2026 17:12:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785431531; cv=none; b=RMO3ke+EMiF4mNhuHpIHVcyG3yKvUB7pTUvVWpD+y+HYbaXtL4xxczfdEJsxEtgfF1uxyWafP7LV7FWt3tuWWJ7udWONIfjIiCths7pYELm8DUSywNtuYK2amJcLl02pnOk5OZ5ack+XClDv64bVIC/TWL5+g1NMasyZsuX5HcQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785431531; c=relaxed/simple; bh=zYAkOJaNPL4xBYnOjtYCUoUBVWTqOFSpys2Y77mBLYI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c3WEXgGThoTZKhP9f517yqi3MOtecXMuNkKdxm4LeEYtUX7NnNcaKcXNuWWBlprMnYkufw5mdsT5W1tJHQJxTd2+A/GdklQyAPpRvS3bPU9owyxo+u/LGuzjwyesNo7GxX8zJkJM67Rqf+T8ccxw0WL13mOt+y00Xauf7ANl73k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=T6cSoRlG; arc=none smtp.client-ip=209.85.167.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="T6cSoRlG" Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-5b2aa3be376so23817e87.0 for ; Thu, 30 Jul 2026 10:12:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785431528; x=1786036328; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=yDBPGVDJWrnywuKgG1Cti5x+qeb2gthsYs+EL+eew3o=; b=T6cSoRlGEXOa+m++Rj01f4GFsia9Dj17EtXMvEb5zPq+dviHhCFQQdlgl6zZjMm8uF 9txQxZqCqi2elmopJEyQhPE3q0WT2JMjnEwERAXKQ8r15EpIyCA0CpRex42TAI2TYJJC NBcJmEATOkTYws6s+H9q9ZN6eZ8AGEbDHSPKecPiIS1oLRPsZDrKb7HFq8EI+ITcEoEr 69LesSL1GG36Hjich78R/33PFr2NqJTEbFTMajh/Rb71HvT5vHaBjC03nf+0h+WKIFx3 pAobfdGWeZmE8lqBUa0vtAUburG6nJSGaN+T1GVsZn6orvecRmfZH7Eg+5PrRhfOsgkt kOrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785431528; x=1786036328; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=yDBPGVDJWrnywuKgG1Cti5x+qeb2gthsYs+EL+eew3o=; b=GlgEkoBTCK7KHtS/TjQhbu6K6B33QoakyvbEUG+5PLUa3SBv0zeI/jN1Hw0gVjTh+C d7CO+PmpgtgYl4kWdRYuIx37gs+XXgvJwrnzXQS1mHMkDgMTjN7oFCJYAAHP+1WEHbu6 hUMYirJ5aDS8U2SWR7CP7hcwtUL9KYyZaGvmfj89apTb/w/UVuv8GkVN3aVT0Jir53Dm AtPGcvHvYlQhEAYYNxsB2y+1uMLmmn56/Kwk1+dDN3y1IONZqs6hy8LfiDIIgrJlOw+s YYdBc8PMS30kdQa+pASp0Fa1FerdgM8layRJY9xBsIE1NXjmH4yxt5QFicGWLSyl6AGv kJmw== X-Forwarded-Encrypted: i=1; AHgh+RrdAZ1b5juiJ4EnoaRwfBoRfGbq8GMxcTRj9V/LcUyQmcF+MeYOPitmSjfOuknewIzx+Dc5+Co9L9QehoY=@vger.kernel.org X-Gm-Message-State: AOJu0Yw6QoJShO3z+EXW7vVWdatHTQGTzHZ+oNzR+LLze574BvAm2PbU Qpe9lLwuH2FkFY26gMfoJXFqEBZnL4SEwSeNqKrjeCLUa0AeUNHq3WST X-Gm-Gg: AR+sD128K/bUjslAMPFRrRuba5vRdb1wv1SBrQCUR6ckwL3Eh4wcmwaUDbNymXCwUM/ htpmgIAU8wThN4ASuQJK0m2E/ToqHOMdE0NLmxIZuHHneqhS/Y9MpGsxgQon16FR9aN7Uc8OBeq JI+BZcrYSXUuvG1c2hgmqQyvsUc1z06ZoLTey2L8cqwicqOQ21nWgx5xTRa7NWOsptUgM4KWvSk 8iDuOG1smhEAlr7fTyqkPnQWG8foJFDiZMQxxHvNAdALDKIQLFP1dQMg5vJ49MrcXKg47/KX5jB ytSXEyyTToyAVV5/3TALa1sgvv4iW2sKu+oumZQNLEmBySXG0KQa8FbwC2lqn5OyA+tBG2dF0YH CtzJUyy6W0+X5bBRcrYoULUYMSWYwq0gase/mvjzlJC8hgvSaMQjd+6nR7xg1Ep5JV509qr7lVH pdS1NQ2LDFcdpT7XMTJnkvJOeU42V80aKVMKNmiSCMS6y1H1G7E0XbR6vYlv4WboCthTOTb43Kd Qcp86PO/D7wA8gAPzrgTzDBV56pYxl8wK2jMvyRQ1noebknVRam1Y8YZyrQXYW4RhVa X-Received: by 2002:a05:6512:688c:10b0:5ae:c926:fc18 with SMTP id 2adb3069b0e04-5b2db36e9abmr408754e87.38.1785431527552; Thu, 30 Jul 2026 10:12:07 -0700 (PDT) Received: from localhost.localdomain (46-138-176-102.dynamic.spd-mgts.ru. [46.138.176.102]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5b2db94ff55sm469966e87.18.2026.07.30.10.12.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2026 10:12:06 -0700 (PDT) From: Artem Lytkin To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, urezki@gmail.com, willy@infradead.org, shivamkalra98@zohomail.in, linux-kernel@vger.kernel.org Subject: [PATCH v3] mm/vmalloc: make vm_struct.nr_pages an unsigned long Date: Thu, 30 Jul 2026 20:11:42 +0300 Message-ID: <20260730171142.76817-1-iprintercanon@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260730090628.65814-1-iprintercanon@gmail.com> References: <20260730090628.65814-1-iprintercanon@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit vm_struct::nr_pages is an unsigned int, and the file keeps deriving byte counts from it as nr_pages << PAGE_SHIFT. A shift is evaluated in the type of its promoted left operand, so those are 32-bit arithmetic and wrap at 4 GiB of bytes, which is 2^20 pages. Every site depends on a cast being remembered; vmap() has one, two recent commits did not. vread_iter() then computes a size of zero for a 4 GiB VM_ALLOC area and /proc/kcore returns it as zeros while reporting a successful read, which drgn, crash or gdb cannot tell from real memory, and the vrealloc() grow-in-place check declines a request that would have fit. Widen the field so the class of bug goes away instead of one site at a time. Everything feeding or consuming it widens too: vm_area_alloc_pages() and its accumulators, nr_small_pages, new_nr_pages and old_nr_pages, the index range of vm_area_free_pages(), and three page indexes that were plain int. Five casts go. Two prints needed fixing as well, %u in vmalloc_dump_obj() and %d for the unsigned field in vmalloc_info_show(). No bug report behind this, I found it reading the code. The 4 GiB wrap needs only a machine with over 4 GiB of memory. Neither larger threshold is a practical concern: 2^32 pages, where the field itself truncates, is 16 TiB and beyond what hardware can populate, and 2^31, where the plain int indexes break, is 8 TiB and larger than anything in the tree asks for. The int *nr cursor in the mapping path is unchanged and is separate work. Users outside mm/vmalloc.c need no change either; those handing the count to a narrower parameter cannot drive it near 2^31, and kexec_handover still caps its own 32-bit ABI field where it did. sizeof(struct vm_struct) on x86-64 stays 72 bytes with CONFIG_HAVE_ARCH_HUGE_VMALLOC=n and goes from 72 to 80 with it enabled, both inside the kmalloc-96 bucket it already comes from. Fixes: 0bca23804632 ("mm/vmalloc: use physical page count in vread_iter() for VM_ALLOC areas") Fixes: d57ac904ffdc ("mm/vmalloc: use physical page count for vrealloc() grow-in-place check") Suggested-by: Andrew Morton Reviewed-by: Uladzislau Rezki (Sony) Assisted-by: Claude:claude-fable-5 Signed-off-by: Artem Lytkin --- v3: - collapsed to a single patch. v2 put the two cast fixes ahead of this one so the series would apply, and this one then undid them; widening the field fixes both sites by itself (Uladzislau) - shorter changelog (Uladzislau) v2: https://lore.kernel.org/linux-mm/20260730090628.65814-1-iprintercanon@gmail.com/ v1: https://lore.kernel.org/linux-mm/20260729175708.7074-1-iprintercanon@gmail.com/ include/linux/vmalloc.h | 2 +- mm/vmalloc.c | 58 ++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index e4d8d0a9f30f9..aed121d729b01 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -62,7 +62,7 @@ struct vm_struct { #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC unsigned int page_order; #endif - unsigned int nr_pages; + unsigned long nr_pages; phys_addr_t phys_addr; const void *caller; unsigned long requested_size; diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 26f32949c2f2e..196da8738cf10 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3404,7 +3404,7 @@ struct vm_struct *remove_vm_area(const void *addr) static inline void set_area_direct_map(const struct vm_struct *area, int (*set_direct_map)(struct page *page)) { - int i; + unsigned long i; /* HUGE_VMALLOC passes small pages to set_direct_map */ for (i = 0; i < area->nr_pages; i++) @@ -3420,7 +3420,7 @@ static void vm_reset_perms(struct vm_struct *area) unsigned long start = ULONG_MAX, end = 0; unsigned int page_order = vm_area_page_order(area); int flush_dmap = 0; - int i; + unsigned long i; /* * Find the start and end range of the direct mappings to make sure that @@ -3493,10 +3493,10 @@ void vfree_atomic(const void *addr) * Caller is responsible for unmapping (vunmap_range) and KASAN * poisoning before calling this. */ -static void vm_area_free_pages(struct vm_struct *vm, unsigned int start_idx, - unsigned int end_idx) +static void vm_area_free_pages(struct vm_struct *vm, unsigned long start_idx, + unsigned long end_idx) { - unsigned int i; + unsigned long i; if (!(vm->flags & VM_MAP_PUT_PAGES)) { for (i = start_idx; i < end_idx; i++) @@ -3819,12 +3819,12 @@ static inline gfp_t vmalloc_gfp_adjust(gfp_t flags, const bool large) return flags; } -static inline unsigned int +static inline unsigned long vm_area_alloc_pages(gfp_t gfp, int nid, - unsigned int order, unsigned int nr_pages, struct page **pages) + unsigned int order, unsigned long nr_pages, struct page **pages) { - unsigned int nr_allocated = 0; - unsigned int nr_remaining = nr_pages; + unsigned long nr_allocated = 0; + unsigned long nr_remaining = nr_pages; unsigned int max_attempt_order = MAX_PAGE_ORDER; struct page *page; int i; @@ -3872,7 +3872,7 @@ vm_area_alloc_pages(gfp_t gfp, int nid, if (!order) { while (nr_allocated < nr_pages) { unsigned int nr, nr_pages_request; - int i; + unsigned long i; /* * A maximum allowed request is hard-coded and is 100 @@ -3880,7 +3880,7 @@ vm_area_alloc_pages(gfp_t gfp, int nid, * long preemption off scenario in the bulk-allocator * so the range is [1:100]. */ - nr_pages_request = min(100U, nr_pages - nr_allocated); + nr_pages_request = min(100UL, nr_pages - nr_allocated); /* memory allocation should consider mempolicy, we can't * wrongly use nearest node when nid == NUMA_NO_NODE, @@ -4026,12 +4026,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, unsigned long addr = (unsigned long)area->addr; unsigned long size = get_vm_area_size(area); unsigned long array_size; - unsigned int nr_small_pages = size >> PAGE_SHIFT; + unsigned long nr_small_pages = size >> PAGE_SHIFT; unsigned int page_order; unsigned int flags; int ret; - array_size = (unsigned long)nr_small_pages * sizeof(struct page *); + array_size = nr_small_pages * sizeof(struct page *); /* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */ if (!gfpflags_allow_blocking(gfp_mask)) @@ -4525,7 +4525,7 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align } if (size <= old_size) { - unsigned int new_nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; + unsigned long new_nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; /* Zero out "freed" memory, potentially for future realloc. */ if (want_init_on_free() || want_init_on_alloc(flags)) @@ -4554,7 +4554,7 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align !(vm->flags & (VM_FLUSH_RESET_PERMS | VM_USERMAP)) && gfp_has_io_fs(flags)) { unsigned long addr = (unsigned long)kasan_reset_tag(p); - unsigned int old_nr_pages = vm->nr_pages; + unsigned long old_nr_pages = vm->nr_pages; /* * Use the node lock to synchronize with concurrent @@ -4567,16 +4567,13 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align spin_unlock(&vn->busy.lock); /* Notify kmemleak of the reduced allocation size before unmapping. */ - kmemleak_free_part( - (void *)addr + ((unsigned long)new_nr_pages - << PAGE_SHIFT), - (unsigned long)(old_nr_pages - new_nr_pages) - << PAGE_SHIFT); + kmemleak_free_part((void *)addr + + (new_nr_pages << PAGE_SHIFT), + (old_nr_pages - new_nr_pages) + << PAGE_SHIFT); - vunmap_range(addr + ((unsigned long)new_nr_pages - << PAGE_SHIFT), - addr + ((unsigned long)old_nr_pages - << PAGE_SHIFT)); + vunmap_range(addr + (new_nr_pages << PAGE_SHIFT), + addr + (old_nr_pages << PAGE_SHIFT)); vm_area_free_pages(vm, new_nr_pages, old_nr_pages); } @@ -5400,7 +5397,7 @@ bool vmalloc_dump_obj(void *object) struct vmap_area *va; struct vmap_node *vn; unsigned long addr; - unsigned int nr_pages; + unsigned long nr_pages; addr = PAGE_ALIGN((unsigned long) object); vn = addr_to_node(addr); @@ -5420,7 +5417,7 @@ bool vmalloc_dump_obj(void *object) nr_pages = vm->nr_pages; spin_unlock(&vn->busy.lock); - pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n", + pr_cont(" %lu-page vmalloc region starting at %#lx allocated at %pS\n", nr_pages, addr, caller); return true; @@ -5438,16 +5435,17 @@ bool vmalloc_dump_obj(void *object) static void show_numa_info(struct seq_file *m, struct vm_struct *v, unsigned int *counters) { - unsigned int nr; unsigned int step = 1U << vm_area_page_order(v); + unsigned long i; + unsigned int nr; if (!counters) return; memset(counters, 0, nr_node_ids * sizeof(unsigned int)); - for (nr = 0; nr < v->nr_pages; nr += step) - counters[page_to_nid(v->pages[nr])] += step; + for (i = 0; i < v->nr_pages; i += step) + counters[page_to_nid(v->pages[i])] += step; for_each_node_state(nr, N_HIGH_MEMORY) if (counters[nr]) seq_printf(m, " N%u=%u", nr, counters[nr]); @@ -5505,7 +5503,7 @@ static int vmalloc_info_show(struct seq_file *m, void *p) seq_printf(m, " %pS", v->caller); if (v->nr_pages) - seq_printf(m, " pages=%d", v->nr_pages); + seq_printf(m, " pages=%lu", v->nr_pages); if (v->phys_addr) seq_printf(m, " phys=%pa", &v->phys_addr); base-commit: eee677bbc48890b2bcaa42ea7942478302937a09 -- 2.43.0