Skip to content

Commit a3079e4

Browse files
htejungregkh
authored andcommitted
percpu: fix pcpu_alloc_pages() failure path
commit f0d2796 upstream. When pcpu_alloc_pages() fails midway, pcpu_free_pages() is invoked to free what has already been allocated. The invocation is across the whole requested range and pcpu_free_pages() will try to free all non-NULL pages; unfortunately, this is incorrect as pcpu_get_pages_and_bitmap(), unlike what its comment suggests, doesn't clear the pages array and thus the array may have entries from the previous invocations making the partial failure path free incorrect pages. Fix it by open-coding the partial freeing of the already allocated pages. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6baa29b commit a3079e4

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

‎mm/percpu-vm.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,31 @@ static int pcpu_alloc_pages(struct pcpu_chunk *chunk,
108108
int page_start, int page_end)
109109
{
110110
const gfp_t gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
111-
unsigned int cpu;
111+
unsigned int cpu, tcpu;
112112
int i;
113113

114114
for_each_possible_cpu(cpu) {
115115
for (i = page_start; i < page_end; i++) {
116116
struct page **pagep = &pages[pcpu_page_idx(cpu, i)];
117117

118118
*pagep = alloc_pages_node(cpu_to_node(cpu), gfp, 0);
119-
if (!*pagep) {
120-
pcpu_free_pages(chunk, pages, populated,
121-
page_start, page_end);
122-
return -ENOMEM;
123-
}
119+
if (!*pagep)
120+
goto err;
124121
}
125122
}
126123
return 0;
124+
125+
err:
126+
while (--i >= page_start)
127+
__free_page(pages[pcpu_page_idx(cpu, i)]);
128+
129+
for_each_possible_cpu(tcpu) {
130+
if (tcpu == cpu)
131+
break;
132+
for (i = page_start; i < page_end; i++)
133+
__free_page(pages[pcpu_page_idx(tcpu, i)]);
134+
}
135+
return -ENOMEM;
127136
}
128137

129138
/**

0 commit comments

Comments
 (0)