diff mbox series

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

Message ID 20230711201630.1837109-1-zhengyejian1@huawei.com (mailing list archive)
State Superseded
Headers show
Series [v2] ftrace: Fix possible warning on checking all pages used in ftrace_process_locs() | expand

Commit Message

Zheng Yejian July 11, 2023, 8:16 p.m. UTC
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 <rostedt@goodmis.org>
Signed-off-by: Zheng Yejian <zhengyejian1@huawei.com>
---
 kernel/trace/ftrace.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

Comments

Steven Rostedt July 11, 2023, 1:58 p.m. UTC | #1
On Wed, 12 Jul 2023 04:16:30 +0800
Zheng Yejian <zhengyejian1@huawei.com> 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
diff mbox series

Patch

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;
 }