Skip to content

Commit 7844c01

Browse files
osalvadorvilardagaakpm00
authored andcommitted
mm,page_owner: fix recursion
Prior to 217b211 ("mm,page_owner: implement the tracking of the stacks count") the only place where page_owner could potentially go into recursion due to its need of allocating more memory was in save_stack(), which ends up calling into stackdepot code with the possibility of allocating memory. We made sure to guard against that by signaling that the current task was already in page_owner code, so in case a recursion attempt was made, we could catch that and return dummy_handle. After above commit, a new place in page_owner code was introduced where we could allocate memory, meaning we could go into recursion would we take that path. Make sure to signal that we are in page_owner in that codepath as well. Move the guard code into two helpers {un}set_current_in_page_owner() and use them prior to calling in the two functions that might allocate memory. Link: https://lkml.kernel.org/r/20240315222610.6870-1-osalvador@suse.de Signed-off-by: Oscar Salvador <osalvador@suse.de> Fixes: 217b211 ("mm,page_owner: implement the tracking of the stacks count") Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Cc: Alexander Potapenko <glider@google.com> Cc: Andrey Konovalov <andreyknvl@gmail.com> Cc: Marco Elver <elver@google.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Oscar Salvador <osalvador@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 3290032 commit 7844c01

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

mm/page_owner.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ static depot_stack_handle_t early_handle;
5454

5555
static void init_early_allocated_pages(void);
5656

57+
static inline void set_current_in_page_owner(void)
58+
{
59+
/*
60+
* Avoid recursion.
61+
*
62+
* We might need to allocate more memory from page_owner code, so make
63+
* sure to signal it in order to avoid recursion.
64+
*/
65+
current->in_page_owner = 1;
66+
}
67+
68+
static inline void unset_current_in_page_owner(void)
69+
{
70+
current->in_page_owner = 0;
71+
}
72+
5773
static int __init early_page_owner_param(char *buf)
5874
{
5975
int ret = kstrtobool(buf, &page_owner_enabled);
@@ -133,23 +149,16 @@ static noinline depot_stack_handle_t save_stack(gfp_t flags)
133149
depot_stack_handle_t handle;
134150
unsigned int nr_entries;
135151

136-
/*
137-
* Avoid recursion.
138-
*
139-
* Sometimes page metadata allocation tracking requires more
140-
* memory to be allocated:
141-
* - when new stack trace is saved to stack depot
142-
*/
143152
if (current->in_page_owner)
144153
return dummy_handle;
145-
current->in_page_owner = 1;
146154

155+
set_current_in_page_owner();
147156
nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
148157
handle = stack_depot_save(entries, nr_entries, flags);
149158
if (!handle)
150159
handle = failure_handle;
160+
unset_current_in_page_owner();
151161

152-
current->in_page_owner = 0;
153162
return handle;
154163
}
155164

@@ -164,9 +173,13 @@ static void add_stack_record_to_list(struct stack_record *stack_record,
164173
gfp_mask &= (GFP_ATOMIC | GFP_KERNEL);
165174
gfp_mask |= __GFP_NOWARN;
166175

176+
set_current_in_page_owner();
167177
stack = kmalloc(sizeof(*stack), gfp_mask);
168-
if (!stack)
178+
if (!stack) {
179+
unset_current_in_page_owner();
169180
return;
181+
}
182+
unset_current_in_page_owner();
170183

171184
stack->stack_record = stack_record;
172185
stack->next = NULL;

0 commit comments

Comments
 (0)