mm: page_owner: detect page_owner recursion via task_struct
Subsystems (3)
PATCH 1/1 mm: page_owner: detect page_owner recursion via task_struct
Before the change page_owner recursion was detected via fetching backtrace and inspecting it for current instruction pointer. It has a few problems:
- it is slightly slow as it requires extra backtrace and a linear stack scan of the result
- it is too late to check if backtrace fetching required memory allocation itself (ia64's unwinder requires it).
To simplify recursion tracking let's use page_owner recursion depth as a counter in 'struct task_struct'.
The change make page_owner=on work on ia64 bu avoiding infinite recursion in:
kmalloc()
-> __set_page_owner()
-> save_stack()
-> unwind() [ia64-specific]
-> build_script()
-> kmalloc()
-> __set_page_owner() [we short-circuit here]
-> save_stack()
-> unwind() [recursion]
--- include/linux/sched.h | 9 +++++++++ init/init_task.c | 3 +++ mm/page_owner.c | 41 +++++++++++++++++------------------------ 3 files changed, 29 insertions(+), 24 deletions(-)
include/linux/sched.h +9 -0
@@ -1371,6 +1371,15 @@ struct task_struct {struct llist_head kretprobe_instances;#endif +#ifdef CONFIG_PAGE_OWNER +/* +* Used by page_owner=on to detect recursion in page tracking. +* Is it fine to have non-atomic ops here if we ever access +* this variable via current->page_owner_depth? +*/ +unsigned int page_owner_depth; +#endif +/* * New fields for task_struct should be added above here, so that * they are included in the randomized portion of task_struct.
init/init_task.c +3 -0
@@ -213,6 +213,9 @@ struct task_struct init_task#ifdef CONFIG_SECCOMP .seccomp = { .filter_count = ATOMIC_INIT(0) },#endif +#ifdef CONFIG_PAGE_OWNER + .page_owner_depth = 0, +#endif };EXPORT_SYMBOL (init_task);
mm/page_owner.c +17 -24
65 lines not on this page · raw message
Signature · 1 line
2.31.1
Andrew Morton
maintainerQuoted 10 lines · On Thu, 1 Apr 2021 23:30:10 +0100 Sergei Trofimovich <[email protected]> wrote:
Before the change
page_ownerrecursion was detected via fetching backtrace and inspecting it for current instruction pointer. It has a few problems:
- it is slightly slow as it requires extra backtrace and a linear stack scan of the result
- it is too late to check if backtrace fetching required memory allocation itself (ia64's unwinder requires it).To simplify recursion tracking let's use
page_ownerrecursion depth as a counter in 'structtask_struct'.
Seems like a better approach.
Quoted 17 lines
The change make page_owner=on work on ia64 bu avoiding infinite recursion in:
kmalloc()
->__set_page_owner()
->save_stack()
->unwind()[ia64-specific]
->build_script()
->kmalloc()
->__set_page_owner()[we short-circuit here]
->save_stack()
->unwind()[recursion]...
include/linux/sched.h struct task_struct { · line 1378
Quoted 8 lines
include/linux/sched.h +5
-0@@ -1371,6 +1371,15 @@ struct task_struct {struct llist_head kretprobe_instances;#endif +#ifdef CONFIG_PAGE_OWNER +/* +* Used by page_owner=on to detect recursion in page tracking. +* Is it fine to have non-atomic ops here if we ever access +* this variable via current->page_owner_depth?
Yes, it is fine. This part of the comment can be removed.
include/linux/sched.h struct task_struct { · line 1381
Quoted 3 lines
include/linux/sched.h +3
-0+*/ +unsigned int page_owner_depth; +#endif
Adding to the task_struct has a cost. But I don't expect that PAGE_OWNER is commonly used in prodction builds (correct?).
init/init_task.c struct task_struct init_task · line 220
Quoted 11 lines
init/init_task.c +3
-0@@ -213,6 +213,9 @@ struct task_struct init_task#ifdef CONFIG_SECCOMP .seccomp = { .filter_count = ATOMIC_INIT(0) },#endif +#ifdef CONFIG_PAGE_OWNER + .page_owner_depth = 0, +#endif };EXPORT_SYMBOL (init_task);
It will be initialized to zero by the compiler. We can omit this hunk entirely.
Quoted 5 lines
mm/page_owner.c +0
-0@@ -20,6 +20,16 @@*/ #define PAGE_OWNER_STACK_DEPTH (16)
mm/page_owner.c line 31
Quoted 10 lines
mm/page_owner.c +9
-0+/* +* How many reenters we allow to page_owner. +* +* Sometimes metadata allocation tracking requires more memory to be allocated: +* - when new stack trace is saved to stack depot +* - when backtrace itself is calculated (ia64) +* Instead of falling to infinite recursion give it a chance to recover. +*/ +#define PAGE_OWNER_MAX_RECURSION_DEPTH (1)
So this is presently a boolean. Is there any expectation that PAGE_OWNER_MAX_RECURSION_DEPTH will ever be greater than 1? If not, we could use a single bit in the task_struct. Add it to the "Unserialized, strictly 'current'" bitfields. Could make it a 2-bit field if we want to permit PAGE_OWNER_MAX_RECURSION_DEPTH=larger.
Quoted 35 lines · On Thu, 1 Apr 2021 17:05:19 -0700 Andrew Morton <[email protected]>…
Quoted 10 lines · On Thu, 1 Apr 2021 23:30:10 +0100 Sergei Trofimovich <[email protected]> wrote:
Before the change
page_ownerrecursion was detected via fetching backtrace and inspecting it for current instruction pointer. It has a few problems:
- it is slightly slow as it requires extra backtrace and a linear stack scan of the result
- it is too late to check if backtrace fetching required memory allocation itself (ia64's unwinder requires it).To simplify recursion tracking let's use
page_ownerrecursion depth as a counter in 'structtask_struct'.Seems like a better approach.
Quoted 20 lines
The change make page_owner=on work on ia64 bu avoiding infinite recursion in:
kmalloc()
->__set_page_owner()
->save_stack()
->unwind()[ia64-specific]
->build_script()
->kmalloc()
->__set_page_owner()[we short-circuit here]
->save_stack()
->unwind()[recursion]...
include/linux/sched.h +0
-0@@ -1371,6 +1371,15 @@ struct task_struct {struct llist_head kretprobe_instances;#endif
Quoted 7 lines
include/linux/sched.h struct task_struct { · line 1378
Quoted 5 lines
include/linux/sched.h +5
-0+#ifdef CONFIG_PAGE_OWNER +/* +* Used by page_owner=on to detect recursion in page tracking. +* Is it fine to have non-atomic ops here if we ever access +* this variable via current->page_owner_depth? Yes, it is fine. This part of the comment can be removed.
Cool! Will do.
Quoted 6 lines
include/linux/sched.h struct task_struct { · line 1381
Quoted 3 lines
include/linux/sched.h +3
-0+*/ +unsigned int page_owner_depth; +#endif Adding to the
task_structhas a cost. But I don't expect thatPAGE_OWNERis commonly used in prodction builds (correct?).
Yeah, PAGE_OWNER should not be enabled for production kernels.
Not having extra memory overhead (or layout disruption) is a nice benefit though. I'll switch to "Unserialized, strictly 'current'" bitfield.
Quoted 21 lines
init/init_task.c struct task_struct init_task · line 220
Quoted 11 lines
init/init_task.c +3
-0@@ -213,6 +213,9 @@ struct task_struct init_task#ifdef CONFIG_SECCOMP .seccomp = { .filter_count = ATOMIC_INIT(0) },#endif +#ifdef CONFIG_PAGE_OWNER + .page_owner_depth = 0, +#endif };EXPORT_SYMBOL (init_task);It will be initialized to zero by the compiler. We can omit this hunk entirely.
Quoted 6 lines
mm/page_owner.c +0
-0@@ -20,6 +20,16 @@*/ #define PAGE_OWNER_STACK_DEPTH (16)
Quoted 15 lines
mm/page_owner.c line 31
Quoted 9 lines
mm/page_owner.c +9
-0+/* +* How many reenters we allow to page_owner. +* +* Sometimes metadata allocation tracking requires more memory to be allocated: +* - when new stack trace is saved to stack depot +* - when backtrace itself is calculated (ia64) +* Instead of falling to infinite recursion give it a chance to recover. +*/ +#define PAGE_OWNER_MAX_RECURSION_DEPTH (1)So this is presently a boolean. Is there any expectation that
PAGE_OWNER_MAX_RECURSION_DEPTHwill ever be greater than 1? If not, we could use a single bit in thetask_struct. Add it to the "Unserialized, strictly 'current'" bitfields. Could make it a 2-bit field if we want to permit PAGE_OWNER_MAX_RECURSION_DEPTH=larger.
Let's settle on depth=1. depth>1 is not trivial for other reasons I don't completely understand.
Follow-up patch incoming.
Signature · 1 line
Sergei
PATCH v2 1/1 mm: page_owner: detect page_owner recursion via task_struct
Before the change page_owner recursion was detected via fetching backtrace and inspecting it for current instruction pointer. It has a few problems:
- it is slightly slow as it requires extra backtrace and a linear stack scan of the result
- it is too late to check if backtrace fetching required memory allocation itself (ia64's unwinder requires it).
To simplify recursion tracking let's use page_owner recursion flag in 'struct task_struct'.
The change make page_owner=on work on ia64 by avoiding infinite recursion in:
kmalloc()
-> __set_page_owner()
-> save_stack()
-> unwind() [ia64-specific]
-> build_script()
-> kmalloc()
-> __set_page_owner() [we short-circuit here]
-> save_stack()
-> unwind() [recursion]
---
Change since v1:
- use bit from task_struct instead of a new field
- track only one recursion depth level so far
include/linux/sched.h | 4 ++++ mm/page_owner.c | 32 ++++++++++---------------------- 2 files changed, 14 insertions(+), 22 deletions(-)
include/linux/sched.h +4 -0
@@ -841,6 +841,10 @@ struct task_struct {/* Stalled due to lack of memory */ unsigned in_memstall:1;#endif +#ifdef CONFIG_PAGE_OWNER +/* Used by page_owner=on to detect recursion in page tracking. */ + unsigned in_page_owner:1; +#endif unsigned long atomic_flags;/* Flags requiring atomic access. */
mm/page_owner.c +10 -22
@@ -97,42 +97,30 @@ static inline struct page_owner *get_page_owner(struct page_ext *page_ext)return (void *)page_ext + page_owner_ops.offset; } -static inline bool check_recursive_alloc (unsigned long *entries, -unsigned int nr_entries, -unsigned long ip) -{ -unsigned int i; - -for (i =0 ; i < nr_entries; i++) { -if (entries[i] == ip) -return true; - } -return false; -} - static noinline depot_stack_handle_t save_stack(gfp_t flags) {unsigned long entries[PAGE_OWNER_STACK_DEPTH ];depot_stack_handle_t handle;unsigned int nr_entries; - nr_entries =stack_trace_save (entries,ARRAY_SIZE (entries),2 ); -/* -* We need to check recursion here because our request to -* stackdepot could trigger memory allocation to save new -* entry. New memory allocation would reach here and call -* stack_depot_save_entries() again if we don't catch it. There is -* still not enough memory in stackdepot so it would try to -* allocate memory again and loop forever. +* Avoid recursion. +* +* Sometimes page metadata allocation tracking requires more +* memory to be allocated: +* - when new stack trace is saved to stack depot +* - when backtrace itself is calculated (ia64) */ -if (check_recursive_alloc (entries, nr_entries, _RET_IP_)) +if (current->in_page_owner)return dummy_handle; + current->in_page_owner =1 ; + nr_entries =stack_trace_save (entries,ARRAY_SIZE (entries),2 ); handle =stack_depot_save (entries, nr_entries, flags);if (!handle) handle = failure_handle; + current->in_page_owner =0 ;return handle; }
Signature · 1 line
2.31.1
Quoted 23 lines · Sergei Trofimovich
Before the change
page_ownerrecursion was detected via fetching backtrace and inspecting it for current instruction pointer. It has a few problems:
- it is slightly slow as it requires extra backtrace and a linear stack scan of the result
- it is too late to check if backtrace fetching required memory allocation itself (ia64's unwinder requires it).To simplify recursion tracking let's use
page_ownerrecursion flag in 'structtask_struct'.The change make page_owner=on work on ia64 by avoiding infinite recursion in:
kmalloc()
->__set_page_owner()
->save_stack()
->unwind()[ia64-specific]
->build_script()
->kmalloc()
->__set_page_owner()[we short-circuit here]
->save_stack()
->unwind()[recursion]
Quoted 12 lines
CC Ingo Molnar <[email protected]>CC Peter Zijlstra <[email protected]>CC Juri Lelli <[email protected]>CC Vincent Guittot <[email protected]>CC Dietmar Eggemann <[email protected]>CC Steven Rostedt <[email protected]>CC Ben Segall <[email protected]>CC Mel Gorman <[email protected]>CC Daniel Bristot de Oliveira <[email protected]>CC Andrew Morton <[email protected]>Signed-off-by Sergei Trofimovich <[email protected]>
Much better indeed, thanks.
Quoted 82 lines
---
Change since v1:
- use bit fromtask_structinstead of a new field
- track only one recursion depth level so farinclude/linux/sched.h | 4 ++++ mm/page_owner.c | 32 ++++++++++---------------------- 2 files changed, 14 insertions(+), 22 deletions(-)include/linux/sched.h +4
-0@@ -841,6 +841,10 @@ struct task_struct {/* Stalled due to lack of memory */ unsigned in_memstall:1;#endif +#ifdef CONFIG_PAGE_OWNER +/* Used by page_owner=on to detect recursion in page tracking. */ + unsigned in_page_owner:1; +#endif unsigned long atomic_flags;/* Flags requiring atomic access. */ mm/page_owner.c +10
-22@@ -97,42 +97,30 @@ static inline struct page_owner *get_page_owner(struct page_ext *page_ext)return (void *)page_ext + page_owner_ops.offset; } -static inline bool check_recursive_alloc (unsigned long *entries, -unsigned int nr_entries, -unsigned long ip) -{ -unsigned int i; - -for (i =0 ; i < nr_entries; i++) { -if (entries[i] == ip) -return true; - } -return false; -} - static noinline depot_stack_handle_t save_stack(gfp_t flags) {unsigned long entries[PAGE_OWNER_STACK_DEPTH ];depot_stack_handle_t handle;unsigned int nr_entries; - nr_entries =stack_trace_save (entries,ARRAY_SIZE (entries),2 ); -/* -* We need to check recursion here because our request to -* stackdepot could trigger memory allocation to save new -* entry. New memory allocation would reach here and call -* stack_depot_save_entries() again if we don't catch it. There is -* still not enough memory in stackdepot so it would try to -* allocate memory again and loop forever. +* Avoid recursion. +* +* Sometimes page metadata allocation tracking requires more +* memory to be allocated: +* - when new stack trace is saved to stack depot +* - when backtrace itself is calculated (ia64) */ -if (check_recursive_alloc (entries, nr_entries, _RET_IP_)) +if (current->in_page_owner)return dummy_handle; + current->in_page_owner =1 ; + nr_entries =stack_trace_save (entries,ARRAY_SIZE (entries),2 ); handle =stack_depot_save (entries, nr_entries, flags);if (!handle) handle = failure_handle; + current->in_page_owner =0 ;return handle; }
Quoted 48 lines · Sergei Trofimovich
Quoted 35 lines · On Thu, 1 Apr 2021 17:05:19 -0700 Andrew Morton <[email protected]>…
Quoted 10 lines · On Thu, 1 Apr 2021 23:30:10 +0100 Sergei Trofimovich <[email protected]> wrote:
Before the change
page_ownerrecursion was detected via fetching backtrace and inspecting it for current instruction pointer. It has a few problems:
- it is slightly slow as it requires extra backtrace and a linear stack scan of the result
- it is too late to check if backtrace fetching required memory allocation itself (ia64's unwinder requires it).To simplify recursion tracking let's use
page_ownerrecursion depth as a counter in 'structtask_struct'.Seems like a better approach.
Quoted 20 lines
The change make page_owner=on work on ia64 bu avoiding infinite recursion in:
kmalloc()
->__set_page_owner()
->save_stack()
->unwind()[ia64-specific]
->build_script()
->kmalloc()
->__set_page_owner()[we short-circuit here]
->save_stack()
->unwind()[recursion]...
include/linux/sched.h +0
-0@@ -1371,6 +1371,15 @@ struct task_struct {struct llist_head kretprobe_instances;#endif Quoted 7 lines
include/linux/sched.h struct task_struct { · line 1378
Quoted 5 lines
include/linux/sched.h +5
-0+#ifdef CONFIG_PAGE_OWNER +/* +* Used by page_owner=on to detect recursion in page tracking. +* Is it fine to have non-atomic ops here if we ever access +* this variable via current->page_owner_depth? Yes, it is fine. This part of the comment can be removed.
Cool! Will do.
Quoted 8 lines
Quoted 6 lines
include/linux/sched.h struct task_struct { · line 1381
Quoted 3 lines
include/linux/sched.h +3
-0+*/ +unsigned int page_owner_depth; +#endif Adding to the
task_structhas a cost. But I don't expect thatPAGE_OWNERis commonly used in prodction builds (correct?).Yeah,
PAGE_OWNERshould not be enabled for production kernels.
Note that it was converted to use a static key exactly so that it can be always built in production kernels, and simply enabled on boot when needed. Our kernels have it enabled.
Quoted 40 lines
Not having extra memory overhead (or layout disruption) is a nice benefit though. I'll switch to "Unserialized, strictly 'current'" bitfield.
Quoted 21 lines
init/init_task.c struct task_struct init_task · line 220
Quoted 11 lines
init/init_task.c +3
-0@@ -213,6 +213,9 @@ struct task_struct init_task#ifdef CONFIG_SECCOMP .seccomp = { .filter_count = ATOMIC_INIT(0) },#endif +#ifdef CONFIG_PAGE_OWNER + .page_owner_depth = 0, +#endif };EXPORT_SYMBOL (init_task);It will be initialized to zero by the compiler. We can omit this hunk entirely.
Quoted 6 lines
mm/page_owner.c +0
-0@@ -20,6 +20,16 @@*/ #define PAGE_OWNER_STACK_DEPTH (16)Quoted 15 lines
mm/page_owner.c line 31
Quoted 9 lines
mm/page_owner.c +9
-0+/* +* How many reenters we allow to page_owner. +* +* Sometimes metadata allocation tracking requires more memory to be allocated: +* - when new stack trace is saved to stack depot +* - when backtrace itself is calculated (ia64) +* Instead of falling to infinite recursion give it a chance to recover. +*/ +#define PAGE_OWNER_MAX_RECURSION_DEPTH (1)So this is presently a boolean. Is there any expectation that
PAGE_OWNER_MAX_RECURSION_DEPTHwill ever be greater than 1? If not, we could use a single bit in thetask_struct. Add it to the "Unserialized, strictly 'current'" bitfields. Could make it a 2-bit field if we want to permit PAGE_OWNER_MAX_RECURSION_DEPTH=larger.
Quoted 2 lines
Let's settle on depth=1. depth>1 is not trivial for other reasons I don't completely understand.
That's fine, I don't think depth>1 would bring us much benefit anyway.
Quoted 2 lines
Follow-up patch incoming.