2023-07-10 09:40:45

by Zheng Yejian

[permalink] [raw]
Subject: [PATCH] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()

As comments in ftrace_process_locs(), there may be NULL pointers in
mcount_loc section:
> Some architecture linkers will pad between
> the different mcount_loc sections of different
> object files to satisfy alignments.
> Skip any NULL pointers.

After 20e5227e9f55 ("ftrace: allow NULL pointers in mcount_loc"),
NULL pointers will be accounted when allocating ftrace pages but
skipped before adding into ftrace pages, this may result in some
pages not being used. Then after 706c81f87f84 ("ftrace: Remove extra
helper functions"), warning may occur at:
WARN_ON(pg->next);

So we may need to skip NULL pointers before allocating ftrace pages.

Fixes: 706c81f87f84 ("ftrace: Remove extra helper functions")
Signed-off-by: Zheng Yejian <[email protected]>
---
kernel/trace/ftrace.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 3740aca79fe7..5b474165df31 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -6485,6 +6485,16 @@ static int ftrace_process_locs(struct module *mod,
if (!count)
return 0;

+ p = start;
+ while (p < end) {
+ /*
+ * Refer to conments below, there may be NULL pointers,
+ * skip them before allocating pages
+ */
+ addr = ftrace_call_adjust(*p++);
+ if (!addr)
+ count--;
+ }
/*
* Sorting mcount in vmlinux at build time depend on
* CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
--
2.25.1



2023-07-10 15:00:47

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()

On Tue, 11 Jul 2023 05:29:58 +0800
Zheng Yejian <[email protected]> wrote:

> As comments in ftrace_process_locs(), there may be NULL pointers in
> mcount_loc section:
> > Some architecture linkers will pad between
> > the different mcount_loc sections of different
> > object files to satisfy alignments.
> > Skip any NULL pointers.
>
> After 20e5227e9f55 ("ftrace: allow NULL pointers in mcount_loc"),
> NULL pointers will be accounted when allocating ftrace pages but
> skipped before adding into ftrace pages, this may result in some
> pages not being used. Then after 706c81f87f84 ("ftrace: Remove extra
> helper functions"), warning may occur at:
> WARN_ON(pg->next);
>
> So we may need to skip NULL pointers before allocating ftrace pages.
>
> Fixes: 706c81f87f84 ("ftrace: Remove extra helper functions")
> Signed-off-by: Zheng Yejian <[email protected]>
> ---
> kernel/trace/ftrace.c | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
> diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
> index 3740aca79fe7..5b474165df31 100644
> --- a/kernel/trace/ftrace.c
> +++ b/kernel/trace/ftrace.c
> @@ -6485,6 +6485,16 @@ static int ftrace_process_locs(struct module *mod,
> if (!count)
> return 0;
>
> + p = start;
> + while (p < end) {
> + /*
> + * Refer to conments below, there may be NULL pointers,
> + * skip them before allocating pages
> + */
> + addr = ftrace_call_adjust(*p++);
> + if (!addr)
> + count--;
> + }

My main concern about this is the added overhead during boot to process
this. There's 10s of thousands of functions, so this loop will be 10s of
thousands. I also don't like that this is an unconditional loop (meaning it
executes even when it is unnecessary to do so).


> /*
> * Sorting mcount in vmlinux at build time depend on
> * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in

How about something like this?

-- Steve

diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index b24c573934af..acd033371721 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -6474,6 +6474,7 @@ static int ftrace_process_locs(struct module *mod,
struct ftrace_page *start_pg;
struct ftrace_page *pg;
struct dyn_ftrace *rec;
+ unsigned long skipped = 0;
unsigned long count;
unsigned long *p;
unsigned long addr;
@@ -6536,8 +6537,10 @@ static int ftrace_process_locs(struct module *mod,
* object files to satisfy alignments.
* Skip any NULL pointers.
*/
- if (!addr)
+ if (!addr) {
+ skipped++;
continue;
+ }

end_offset = (pg->index+1) * sizeof(pg->records[0]);
if (end_offset > PAGE_SIZE << pg->order) {
@@ -6551,12 +6554,24 @@ static int ftrace_process_locs(struct module *mod,
rec->ip = addr;
}

- /* We should have used all pages */
- WARN_ON(pg->next);
-
/* Assign the last page to ftrace_pages */
ftrace_pages = pg;

+ /* We should have used all pages unless we skipped some */
+ if (pg->next) {
+ WARN_ON(!skipped);
+ while (ftrace_pages->next) {
+ pg = ftrace_pages->next;
+ ftrace_pages->next = pg->next;
+ if (pg->records) {
+ free_pages((unsigned long)pg->records, pg->order);
+ ftrace_number_of_pages -= 1 << pg->order;
+ }
+ kfree(pg);
+ ftrace_number_of_groups--;
+ }
+ }
+
/*
* We only need to disable interrupts on start up
* because we are modifying code that an interrupt

2023-07-11 06:58:02

by Zheng Yejian

[permalink] [raw]
Subject: Re: [PATCH] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()

On 2023/7/10 22:46, Steven Rostedt wrote:
> On Tue, 11 Jul 2023 05:29:58 +0800
> Zheng Yejian <[email protected]> wrote:
>
>> As comments in ftrace_process_locs(), there may be NULL pointers in
>> mcount_loc section:
>> > Some architecture linkers will pad between
>> > the different mcount_loc sections of different
>> > object files to satisfy alignments.
>> > Skip any NULL pointers.
>>
>> After 20e5227e9f55 ("ftrace: allow NULL pointers in mcount_loc"),
>> NULL pointers will be accounted when allocating ftrace pages but
>> skipped before adding into ftrace pages, this may result in some
>> pages not being used. Then after 706c81f87f84 ("ftrace: Remove extra
>> helper functions"), warning may occur at:
>> WARN_ON(pg->next);
>>
>> So we may need to skip NULL pointers before allocating ftrace pages.
>>
>> Fixes: 706c81f87f84 ("ftrace: Remove extra helper functions")
>> Signed-off-by: Zheng Yejian <[email protected]>
>> ---
>> kernel/trace/ftrace.c | 10 ++++++++++
>> 1 file changed, 10 insertions(+)
>>
>> diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
>> index 3740aca79fe7..5b474165df31 100644
>> --- a/kernel/trace/ftrace.c
>> +++ b/kernel/trace/ftrace.c
>> @@ -6485,6 +6485,16 @@ static int ftrace_process_locs(struct module *mod,
>> if (!count)
>> return 0;
>>
>> + p = start;
>> + while (p < end) {
>> + /*
>> + * Refer to conments below, there may be NULL pointers,
>> + * skip them before allocating pages
>> + */
>> + addr = ftrace_call_adjust(*p++);
>> + if (!addr)
>> + count--;
>> + }
>
> My main concern about this is the added overhead during boot to process
> this. There's 10s of thousands of functions, so this loop will be 10s of
> thousands. I also don't like that this is an unconditional loop (meaning it
> executes even when it is unnecessary to do so).
>

Agreed! The added overhead probably superfluousin in most cases.

>
>> /*
>> * Sorting mcount in vmlinux at build time depend on
>> * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
>
> How about something like this?
>
> -- Steve
>
> diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
> index b24c573934af..acd033371721 100644
> --- a/kernel/trace/ftrace.c
> +++ b/kernel/trace/ftrace.c
> @@ -6474,6 +6474,7 @@ static int ftrace_process_locs(struct module *mod,
> struct ftrace_page *start_pg;
> struct ftrace_page *pg;
> struct dyn_ftrace *rec;
> + unsigned long skipped = 0;
> unsigned long count;
> unsigned long *p;
> unsigned long addr;
> @@ -6536,8 +6537,10 @@ static int ftrace_process_locs(struct module *mod,
> * object files to satisfy alignments.
> * Skip any NULL pointers.
> */
> - if (!addr)
> + if (!addr) {
> + skipped++;
> continue;
> + }
>
> end_offset = (pg->index+1) * sizeof(pg->records[0]);
> if (end_offset > PAGE_SIZE << pg->order) {
> @@ -6551,12 +6554,24 @@ static int ftrace_process_locs(struct module *mod,
> rec->ip = addr;
> }
>
> - /* We should have used all pages */
> - WARN_ON(pg->next);
> -
> /* Assign the last page to ftrace_pages */
> ftrace_pages = pg;
>
> + /* We should have used all pages unless we skipped some */
> + if (pg->next) {
> + WARN_ON(!skipped);
> + while (ftrace_pages->next) {
> + pg = ftrace_pages->next;
> + ftrace_pages->next = pg->next;
> + if (pg->records) {
> + free_pages((unsigned long)pg->records, pg->order);
> + ftrace_number_of_pages -= 1 << pg->order;
> + }
> + kfree(pg);
> + ftrace_number_of_groups--;
> + }

Do we only need to free the pages that not being used?

> + }
> +
> /*
> * We only need to disable interrupts on start up
> * because we are modifying code that an interrupt
>


2023-07-11 08:55:10

by Zheng Yejian

[permalink] [raw]
Subject: [PATCH v2] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()

As comments in ftrace_process_locs(), there may be NULL pointers in
mcount_loc section:
> Some architecture linkers will pad between
> the different mcount_loc sections of different
> object files to satisfy alignments.
> Skip any NULL pointers.

After commit 20e5227e9f55 ("ftrace: allow NULL pointers in mcount_loc"),
NULL pointers will be accounted when allocating ftrace pages but skipped
before adding into ftrace pages, this may result in some pages not being
used. Then after commit 706c81f87f84 ("ftrace: Remove extra helper
functions"), warning may occur at:
WARN_ON(pg->next);

To fix it, only warn for case that no pointers skipped but pages not used
up, then free those unused pages after releasing ftrace_lock.

Fixes: 706c81f87f84 ("ftrace: Remove extra helper functions")
Suggested-by: Steven Rostedt <[email protected]>
Signed-off-by: Zheng Yejian <[email protected]>
---
kernel/trace/ftrace.c | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 3740aca79fe7..b46e539dc085 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -6473,7 +6473,9 @@ static int ftrace_process_locs(struct module *mod,
{
struct ftrace_page *start_pg;
struct ftrace_page *pg;
+ struct ftrace_page *pg_unuse = NULL;
struct dyn_ftrace *rec;
+ unsigned long skipped = 0;
unsigned long count;
unsigned long *p;
unsigned long addr;
@@ -6536,8 +6538,10 @@ static int ftrace_process_locs(struct module *mod,
* object files to satisfy alignments.
* Skip any NULL pointers.
*/
- if (!addr)
+ if (!addr) {
+ skipped++;
continue;
+ }

end_offset = (pg->index+1) * sizeof(pg->records[0]);
if (end_offset > PAGE_SIZE << pg->order) {
@@ -6551,9 +6555,8 @@ static int ftrace_process_locs(struct module *mod,
rec->ip = addr;
}

- /* We should have used all pages */
- WARN_ON(pg->next);
-
+ pg_unuse = pg->next;
+ pg->next = NULL;
/* Assign the last page to ftrace_pages */
ftrace_pages = pg;

@@ -6574,6 +6577,20 @@ static int ftrace_process_locs(struct module *mod,
out:
mutex_unlock(&ftrace_lock);

+ /* We should have used all pages unless we skipped some */
+ if (pg_unuse) {
+ WARN_ON(!skipped);
+ while (pg_unuse) {
+ pg = pg_unuse;
+ pg_unuse = pg->next;
+ if (pg->records) {
+ free_pages((unsigned long)pg->records, pg->order);
+ ftrace_number_of_pages -= 1 << pg->order;
+ }
+ kfree(pg);
+ ftrace_number_of_groups--;
+ }
+ }
return ret;
}

--
2.25.1


2023-07-11 11:34:49

by Zheng Yejian

[permalink] [raw]
Subject: [PATCH v3] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()

As comments in ftrace_process_locs(), there may be NULL pointers in
mcount_loc section:
> Some architecture linkers will pad between
> the different mcount_loc sections of different
> object files to satisfy alignments.
> Skip any NULL pointers.

After commit 20e5227e9f55 ("ftrace: allow NULL pointers in mcount_loc"),
NULL pointers will be accounted when allocating ftrace pages but skipped
before adding into ftrace pages, this may result in some pages not being
used. Then after commit 706c81f87f84 ("ftrace: Remove extra helper
functions"), warning may occur at:
WARN_ON(pg->next);

To fix it, only warn for case that no pointers skipped but pages not used
up, then free those unused pages after releasing ftrace_lock.

Fixes: 706c81f87f84 ("ftrace: Remove extra helper functions")
Suggested-by: Steven Rostedt <[email protected]>
Signed-off-by: Zheng Yejian <[email protected]>
---
Changes v2 -> v3: https://lore.kernel.org/all/[email protected]/
- Check NULL for 'pg->next' before assigning it to variable 'pg_unuse'

Changes v1 -> v2: https://lore.kernel.org/all/[email protected]/
- As suggested by Steve, only warn for case that no pointers skipped
but pages not used up then free those unused pages. But I move the
free process after releasing ftrace_lock.

kernel/trace/ftrace.c | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 3740aca79fe7..e43e21a3d5fd 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -6473,7 +6473,9 @@ static int ftrace_process_locs(struct module *mod,
{
struct ftrace_page *start_pg;
struct ftrace_page *pg;
+ struct ftrace_page *pg_unuse = NULL;
struct dyn_ftrace *rec;
+ unsigned long skipped = 0;
unsigned long count;
unsigned long *p;
unsigned long addr;
@@ -6536,8 +6538,10 @@ static int ftrace_process_locs(struct module *mod,
* object files to satisfy alignments.
* Skip any NULL pointers.
*/
- if (!addr)
+ if (!addr) {
+ skipped++;
continue;
+ }

end_offset = (pg->index+1) * sizeof(pg->records[0]);
if (end_offset > PAGE_SIZE << pg->order) {
@@ -6551,8 +6555,10 @@ static int ftrace_process_locs(struct module *mod,
rec->ip = addr;
}

- /* We should have used all pages */
- WARN_ON(pg->next);
+ if (pg->next) {
+ pg_unuse = pg->next;
+ pg->next = NULL;
+ }

/* Assign the last page to ftrace_pages */
ftrace_pages = pg;
@@ -6574,6 +6580,20 @@ static int ftrace_process_locs(struct module *mod,
out:
mutex_unlock(&ftrace_lock);

+ /* We should have used all pages unless we skipped some */
+ if (pg_unuse) {
+ WARN_ON(!skipped);
+ while (pg_unuse) {
+ pg = pg_unuse;
+ pg_unuse = pg->next;
+ if (pg->records) {
+ free_pages((unsigned long)pg->records, pg->order);
+ ftrace_number_of_pages -= 1 << pg->order;
+ }
+ kfree(pg);
+ ftrace_number_of_groups--;
+ }
+ }
return ret;
}

--
2.25.1


2023-07-11 14:14:39

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v2] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()

On Wed, 12 Jul 2023 04:16:30 +0800
Zheng Yejian <[email protected]> wrote:

> diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
> index 3740aca79fe7..b46e539dc085 100644
> --- a/kernel/trace/ftrace.c
> +++ b/kernel/trace/ftrace.c
> @@ -6473,7 +6473,9 @@ static int ftrace_process_locs(struct module *mod,
> {
> struct ftrace_page *start_pg;
> struct ftrace_page *pg;
> + struct ftrace_page *pg_unuse = NULL;
> struct dyn_ftrace *rec;
> + unsigned long skipped = 0;
> unsigned long count;
> unsigned long *p;
> unsigned long addr;

Nit, to keep the upside-down-xmas-tree format:

struct ftrace_page *pg_unuse = NULL;
struct ftrace_page *start_pg;
struct ftrace_page *pg;
struct dyn_ftrace *rec;
unsigned long skipped = 0;
unsigned long count;
unsigned long *p;
unsigned long addr;

-- Steve

2023-07-12 01:57:55

by Zheng Yejian

[permalink] [raw]
Subject: [PATCH v4] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()

As comments in ftrace_process_locs(), there may be NULL pointers in
mcount_loc section:
> Some architecture linkers will pad between
> the different mcount_loc sections of different
> object files to satisfy alignments.
> Skip any NULL pointers.

After commit 20e5227e9f55 ("ftrace: allow NULL pointers in mcount_loc"),
NULL pointers will be accounted when allocating ftrace pages but skipped
before adding into ftrace pages, this may result in some pages not being
used. Then after commit 706c81f87f84 ("ftrace: Remove extra helper
functions"), warning may occur at:
WARN_ON(pg->next);

To fix it, only warn for case that no pointers skipped but pages not used
up, then free those unused pages after releasing ftrace_lock.

Fixes: 706c81f87f84 ("ftrace: Remove extra helper functions")
Suggested-by: Steven Rostedt <[email protected]>
Signed-off-by: Zheng Yejian <[email protected]>
---
Changes v3 [3] => v4:
- Keep the upside-down-xmas-tree format as Steve suggested.
Link: https://lore.kernel.org/all/[email protected]/

Changes v2 [2] => v3:
- Check NULL for 'pg->next' before assigning it to variable 'pg_unuse'.

Changes v1 [1] => v2:
- As Steve suggested, only warn for case that no pointers skipped
but pages not used up then free those unused pages. But I move
the free process after releasing ftrace_lock.
Link: https://lore.kernel.org/all/[email protected]/
- Update commit messages about the new solution.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/

kernel/trace/ftrace.c | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 3740aca79fe7..6fc238f6ef3e 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -6471,9 +6471,11 @@ static int ftrace_process_locs(struct module *mod,
unsigned long *start,
unsigned long *end)
{
+ struct ftrace_page *pg_unuse = NULL;
struct ftrace_page *start_pg;
struct ftrace_page *pg;
struct dyn_ftrace *rec;
+ unsigned long skipped = 0;
unsigned long count;
unsigned long *p;
unsigned long addr;
@@ -6536,8 +6538,10 @@ static int ftrace_process_locs(struct module *mod,
* object files to satisfy alignments.
* Skip any NULL pointers.
*/
- if (!addr)
+ if (!addr) {
+ skipped++;
continue;
+ }

end_offset = (pg->index+1) * sizeof(pg->records[0]);
if (end_offset > PAGE_SIZE << pg->order) {
@@ -6551,8 +6555,10 @@ static int ftrace_process_locs(struct module *mod,
rec->ip = addr;
}

- /* We should have used all pages */
- WARN_ON(pg->next);
+ if (pg->next) {
+ pg_unuse = pg->next;
+ pg->next = NULL;
+ }

/* Assign the last page to ftrace_pages */
ftrace_pages = pg;
@@ -6574,6 +6580,20 @@ static int ftrace_process_locs(struct module *mod,
out:
mutex_unlock(&ftrace_lock);

+ /* We should have used all pages unless we skipped some */
+ if (pg_unuse) {
+ WARN_ON(!skipped);
+ while (pg_unuse) {
+ pg = pg_unuse;
+ pg_unuse = pg->next;
+ if (pg->records) {
+ free_pages((unsigned long)pg->records, pg->order);
+ ftrace_number_of_pages -= 1 << pg->order;
+ }
+ kfree(pg);
+ ftrace_number_of_groups--;
+ }
+ }
return ret;
}

--
2.25.1


2023-07-12 06:30:53

by Zheng Yejian

[permalink] [raw]
Subject: [PATCH v5] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()

As comments in ftrace_process_locs(), there may be NULL pointers in
mcount_loc section:
> Some architecture linkers will pad between
> the different mcount_loc sections of different
> object files to satisfy alignments.
> Skip any NULL pointers.

After commit 20e5227e9f55 ("ftrace: allow NULL pointers in mcount_loc"),
NULL pointers will be accounted when allocating ftrace pages but skipped
before adding into ftrace pages, this may result in some pages not being
used. Then after commit 706c81f87f84 ("ftrace: Remove extra helper
functions"), warning may occur at:
WARN_ON(pg->next);

To fix it, only warn for case that no pointers skipped but pages not used
up, then free those unused pages after releasing ftrace_lock.

Fixes: 706c81f87f84 ("ftrace: Remove extra helper functions")
Suggested-by: Steven Rostedt <[email protected]>
Signed-off-by: Zheng Yejian <[email protected]>
---
Changes v4 [6] => v5:
- Extract page free logic in ftrace_allocate_pages() as ftrace_free_pages(),
then call it to free unused pages in ftrace_process_locs().

Changes v3 [3] => v4:
- Keep the upside-down-xmas-tree format as Steve suggested [5].

Changes v2 [2] => v3:
- Check NULL for 'pg->next' before assigning it to variable 'pg_unuse'.

Changes v1 [1] => v2:
- As Steve suggested [4], only warn for case that no pointers skipped
but pages not used up then free those unused pages. But I move
the free process after releasing ftrace_lock.
- Update commit messages about the new solution.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/all/[email protected]/

kernel/trace/ftrace.c | 45 +++++++++++++++++++++++++++++--------------
1 file changed, 31 insertions(+), 14 deletions(-)

diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 3740aca79fe7..05c0024815bf 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -3305,6 +3305,22 @@ static int ftrace_allocate_records(struct ftrace_page *pg, int count)
return cnt;
}

+static void ftrace_free_pages(struct ftrace_page *pages)
+{
+ struct ftrace_page *pg = pages;
+
+ while (pg) {
+ if (pg->records) {
+ free_pages((unsigned long)pg->records, pg->order);
+ ftrace_number_of_pages -= 1 << pg->order;
+ }
+ pages = pg->next;
+ kfree(pg);
+ pg = pages;
+ ftrace_number_of_groups--;
+ }
+}
+
static struct ftrace_page *
ftrace_allocate_pages(unsigned long num_to_init)
{
@@ -3343,17 +3359,7 @@ ftrace_allocate_pages(unsigned long num_to_init)
return start_pg;

free_pages:
- pg = start_pg;
- while (pg) {
- if (pg->records) {
- free_pages((unsigned long)pg->records, pg->order);
- ftrace_number_of_pages -= 1 << pg->order;
- }
- start_pg = pg->next;
- kfree(pg);
- pg = start_pg;
- ftrace_number_of_groups--;
- }
+ ftrace_free_pages(start_pg);
pr_info("ftrace: FAILED to allocate memory for functions\n");
return NULL;
}
@@ -6471,9 +6477,11 @@ static int ftrace_process_locs(struct module *mod,
unsigned long *start,
unsigned long *end)
{
+ struct ftrace_page *pg_unuse = NULL;
struct ftrace_page *start_pg;
struct ftrace_page *pg;
struct dyn_ftrace *rec;
+ unsigned long skipped = 0;
unsigned long count;
unsigned long *p;
unsigned long addr;
@@ -6536,8 +6544,10 @@ static int ftrace_process_locs(struct module *mod,
* object files to satisfy alignments.
* Skip any NULL pointers.
*/
- if (!addr)
+ if (!addr) {
+ skipped++;
continue;
+ }

end_offset = (pg->index+1) * sizeof(pg->records[0]);
if (end_offset > PAGE_SIZE << pg->order) {
@@ -6551,8 +6561,10 @@ static int ftrace_process_locs(struct module *mod,
rec->ip = addr;
}

- /* We should have used all pages */
- WARN_ON(pg->next);
+ if (pg->next) {
+ pg_unuse = pg->next;
+ pg->next = NULL;
+ }

/* Assign the last page to ftrace_pages */
ftrace_pages = pg;
@@ -6574,6 +6586,11 @@ static int ftrace_process_locs(struct module *mod,
out:
mutex_unlock(&ftrace_lock);

+ /* We should have used all pages unless we skipped some */
+ if (pg_unuse) {
+ WARN_ON(!skipped);
+ ftrace_free_pages(pg_unuse);
+ }
return ret;
}

--
2.25.1


2023-07-12 13:32:26

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v5] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()

On Wed, 12 Jul 2023 14:04:52 +0800
Zheng Yejian <[email protected]> wrote:

> --- a/kernel/trace/ftrace.c
> +++ b/kernel/trace/ftrace.c
> @@ -3305,6 +3305,22 @@ static int ftrace_allocate_records(struct ftrace_page *pg, int count)
> return cnt;
> }
>
> +static void ftrace_free_pages(struct ftrace_page *pages)
> +{
> + struct ftrace_page *pg = pages;
> +
> + while (pg) {
> + if (pg->records) {
> + free_pages((unsigned long)pg->records, pg->order);
> + ftrace_number_of_pages -= 1 << pg->order;
> + }
> + pages = pg->next;
> + kfree(pg);
> + pg = pages;
> + ftrace_number_of_groups--;
> + }
> +}
> +
> static struct ftrace_page *
> ftrace_allocate_pages(unsigned long num_to_init)
> {
> @@ -3343,17 +3359,7 @@ ftrace_allocate_pages(unsigned long num_to_init)
> return start_pg;
>
> free_pages:
> - pg = start_pg;
> - while (pg) {
> - if (pg->records) {
> - free_pages((unsigned long)pg->records, pg->order);
> - ftrace_number_of_pages -= 1 << pg->order;
> - }
> - start_pg = pg->next;
> - kfree(pg);
> - pg = start_pg;
> - ftrace_number_of_groups--;
> - }
> + ftrace_free_pages(start_pg);
> pr_info("ftrace: FAILED to allocate memory for functions\n");
> return NULL;
> }

Nice little clean up. I had already started testing your previous patch,
but due to my test machine running out of disk space (perf doesn't clean up
its .debug directory :-p), I have to rerun it.

I'll apply this one for the new testing.

Thanks!

-- Steve