* [PATCH v3 0/2] mm/memory: improve insert_pages() error handling
@ 2026-08-29 17:11 Avi Weiss
2026-08-29 17:11 ` [PATCH v3 1/2] mm/memory: simplify error handling in insert_pages() Avi Weiss
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Avi Weiss @ 2026-08-29 17:11 UTC (permalink / raw)
To: Andrew Morton
Cc: David Hildenbrand, Lorenzo Stoakes, Liam R . Howlett,
Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
linux-mm, linux-kernel, Avi Weiss
Improve insert_pages() error handling.
The first patch simplifies error handling by initializing the error
status to zero and assigning error codes at their respective failure
sites.
The second patch returns -ENOMEM when walk_to_pmd() fails. A NULL
return from walk_to_pmd() indicates failure to allocate an upper
page-table level, so -ENOMEM is more appropriate than -EFAULT and is
consistent with the subsequent pte_alloc() failure.
Changes in v3:
* drop the walk_to_pmd() rename following review
* use the mm/memory: prefix consistently across the series
* carry review tags from v2
* rebase onto current mm-new
Link: https://lore.kernel.org/r/cover.1785847364.git.thnkslprpt@gmail.com/
Avi Weiss (2):
mm/memory: simplify error handling in insert_pages()
mm/memory: return -ENOMEM for page-table allocation failure in
insert_pages()
mm/memory.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
base-commit: d2aad7fdcda7ae8a726926f2d6de7fe9e8ee7563
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v3 1/2] mm/memory: simplify error handling in insert_pages() 2026-08-29 17:11 [PATCH v3 0/2] mm/memory: improve insert_pages() error handling Avi Weiss @ 2026-08-29 17:11 ` Avi Weiss 2026-09-10 15:33 ` Lorenzo Stoakes (ARM) 2026-08-29 17:11 ` [PATCH v3 2/2] mm/memory: return -ENOMEM for page-table allocation failure " Avi Weiss 2026-09-10 10:06 ` [PATCH v3 0/2] mm/memory: improve insert_pages() error handling David Hildenbrand (Arm) 2 siblings, 1 reply; 5+ messages in thread From: Avi Weiss @ 2026-08-29 17:11 UTC (permalink / raw) To: Andrew Morton Cc: David Hildenbrand, Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko, linux-mm, linux-kernel, Avi Weiss Initialize error return status to zero and then set it as needed at each point of failure. Assign -ENOMEM explicitly when pte_alloc() fails as the pte_alloc() macro returns a boolean. Signed-off-by: Avi Weiss <thnkslprpt@gmail.com> Acked-by: David Hildenbrand (Arm) <david@kernel.org> --- mm/memory.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 9cbce5c90bff..2561dc6bdde6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2565,20 +2565,22 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr, unsigned long curr_page_idx = 0; unsigned long remaining_pages_total = *num; unsigned long pages_to_write_in_pmd; - int ret; + int err = 0; more: - ret = -EFAULT; pmd = walk_to_pmd(mm, addr); - if (!pmd) + if (!pmd) { + err = -EFAULT; goto out; + } pages_to_write_in_pmd = min_t(unsigned long, remaining_pages_total, PTRS_PER_PTE - pte_index(addr)); /* Allocate the PTE if necessary; takes PMD lock once only. */ - ret = -ENOMEM; - if (pte_alloc(mm, pmd)) + if (pte_alloc(mm, pmd)) { + err = -ENOMEM; goto out; + } while (pages_to_write_in_pmd) { int pte_idx = 0; @@ -2586,15 +2588,14 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr, start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock); if (!start_pte) { - ret = -EFAULT; + err = -EFAULT; goto out; } for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) { - int err = insert_page_in_batch_locked(vma, pte, - addr, pages[curr_page_idx], prot); + err = insert_page_in_batch_locked(vma, pte, addr, + pages[curr_page_idx], prot); if (unlikely(err)) { pte_unmap_unlock(start_pte, pte_lock); - ret = err; remaining_pages_total -= pte_idx; goto out; } @@ -2607,10 +2608,9 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr, } if (remaining_pages_total) goto more; - ret = 0; out: *num = remaining_pages_total; - return ret; + return err; } /** -- 2.43.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3 1/2] mm/memory: simplify error handling in insert_pages() 2026-08-29 17:11 ` [PATCH v3 1/2] mm/memory: simplify error handling in insert_pages() Avi Weiss @ 2026-09-10 15:33 ` Lorenzo Stoakes (ARM) 0 siblings, 0 replies; 5+ messages in thread From: Lorenzo Stoakes (ARM) @ 2026-09-10 15:33 UTC (permalink / raw) To: Avi Weiss Cc: Andrew Morton, David Hildenbrand, Liam R . Howlett, Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko, linux-mm, linux-kernel On Sat, Aug 29, 2026 at 08:11:12PM +0300, Avi Weiss wrote: > Initialize error return status to zero and then set it as needed at each > point of failure. > > Assign -ENOMEM explicitly when pte_alloc() fails as the pte_alloc() > macro returns a boolean. > > Signed-off-by: Avi Weiss <thnkslprpt@gmail.com> > Acked-by: David Hildenbrand (Arm) <david@kernel.org> LGTM so: Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org> > --- > mm/memory.c | 22 +++++++++++----------- > 1 file changed, 11 insertions(+), 11 deletions(-) > > diff --git a/mm/memory.c b/mm/memory.c > index 9cbce5c90bff..2561dc6bdde6 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -2565,20 +2565,22 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr, > unsigned long curr_page_idx = 0; > unsigned long remaining_pages_total = *num; > unsigned long pages_to_write_in_pmd; > - int ret; > + int err = 0; > more: > - ret = -EFAULT; > pmd = walk_to_pmd(mm, addr); > - if (!pmd) > + if (!pmd) { > + err = -EFAULT; > goto out; > + } > > pages_to_write_in_pmd = min_t(unsigned long, > remaining_pages_total, PTRS_PER_PTE - pte_index(addr)); > > /* Allocate the PTE if necessary; takes PMD lock once only. */ > - ret = -ENOMEM; > - if (pte_alloc(mm, pmd)) > + if (pte_alloc(mm, pmd)) { > + err = -ENOMEM; > goto out; > + } > > while (pages_to_write_in_pmd) { > int pte_idx = 0; > @@ -2586,15 +2588,14 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr, > > start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock); > if (!start_pte) { > - ret = -EFAULT; > + err = -EFAULT; > goto out; > } > for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) { > - int err = insert_page_in_batch_locked(vma, pte, > - addr, pages[curr_page_idx], prot); > + err = insert_page_in_batch_locked(vma, pte, addr, > + pages[curr_page_idx], prot); > if (unlikely(err)) { > pte_unmap_unlock(start_pte, pte_lock); > - ret = err; > remaining_pages_total -= pte_idx; > goto out; > } > @@ -2607,10 +2608,9 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr, > } > if (remaining_pages_total) > goto more; > - ret = 0; > out: > *num = remaining_pages_total; > - return ret; > + return err; > } > > /** > -- > 2.43.0 > -- Cheers, Lorenzo ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v3 2/2] mm/memory: return -ENOMEM for page-table allocation failure in insert_pages() 2026-08-29 17:11 [PATCH v3 0/2] mm/memory: improve insert_pages() error handling Avi Weiss 2026-08-29 17:11 ` [PATCH v3 1/2] mm/memory: simplify error handling in insert_pages() Avi Weiss @ 2026-08-29 17:11 ` Avi Weiss 2026-09-10 10:06 ` [PATCH v3 0/2] mm/memory: improve insert_pages() error handling David Hildenbrand (Arm) 2 siblings, 0 replies; 5+ messages in thread From: Avi Weiss @ 2026-08-29 17:11 UTC (permalink / raw) To: Andrew Morton Cc: David Hildenbrand, Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko, linux-mm, linux-kernel, Avi Weiss walk_to_pmd() returns NULL only when p4d_alloc(), pud_alloc(), or pmd_alloc() fails. These are page-table allocation failures, but insert_pages() currently reports them as -EFAULT. Return -ENOMEM instead, consistent with the subsequent pte_alloc() failure and with the single-page insert_page() path, which reports failure of the same page-table allocation chain as -ENOMEM. Address and range validation failures in vm_insert_pages() continue to return -EFAULT. Keep the later -EFAULT return for pte_offset_map_lock(), which is not an allocation failure. Signed-off-by: Avi Weiss <thnkslprpt@gmail.com> Acked-by: David Hildenbrand (Arm) <david@kernel.org> Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org> --- mm/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/memory.c b/mm/memory.c index 2561dc6bdde6..27ffe1a99a08 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2569,7 +2569,7 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr, more: pmd = walk_to_pmd(mm, addr); if (!pmd) { - err = -EFAULT; + err = -ENOMEM; goto out; } -- 2.43.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3 0/2] mm/memory: improve insert_pages() error handling 2026-08-29 17:11 [PATCH v3 0/2] mm/memory: improve insert_pages() error handling Avi Weiss 2026-08-29 17:11 ` [PATCH v3 1/2] mm/memory: simplify error handling in insert_pages() Avi Weiss 2026-08-29 17:11 ` [PATCH v3 2/2] mm/memory: return -ENOMEM for page-table allocation failure " Avi Weiss @ 2026-09-10 10:06 ` David Hildenbrand (Arm) 2 siblings, 0 replies; 5+ messages in thread From: David Hildenbrand (Arm) @ 2026-09-10 10:06 UTC (permalink / raw) To: Avi Weiss, Andrew Morton Cc: Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko, linux-mm, linux-kernel On 8/29/26 19:11, Avi Weiss wrote: > Improve insert_pages() error handling. > > The first patch simplifies error handling by initializing the error > status to zero and assigning error codes at their respective failure > sites. > > The second patch returns -ENOMEM when walk_to_pmd() fails. A NULL > return from walk_to_pmd() indicates failure to allocate an upper > page-table level, so -ENOMEM is more appropriate than -EFAULT and is > consistent with the subsequent pte_alloc() failure. > > Changes in v3: > > * drop the walk_to_pmd() rename following review > * use the mm/memory: prefix consistently across the series > * carry review tags from v2 > * rebase onto current mm-new Double-checked, LGTM. -- Cheers, David ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-09-10 15:33 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-08-29 17:11 [PATCH v3 0/2] mm/memory: improve insert_pages() error handling Avi Weiss 2026-08-29 17:11 ` [PATCH v3 1/2] mm/memory: simplify error handling in insert_pages() Avi Weiss 2026-09-10 15:33 ` Lorenzo Stoakes (ARM) 2026-08-29 17:11 ` [PATCH v3 2/2] mm/memory: return -ENOMEM for page-table allocation failure " Avi Weiss 2026-09-10 10:06 ` [PATCH v3 0/2] mm/memory: improve insert_pages() error handling David Hildenbrand (Arm)
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®