Commit 1d801741 authored by Zheng Yejian's avatar Zheng Yejian Committed by Yongqiang Liu
Browse files

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

mainline inclusion
from mainline-v6.5-rc2
commit 26efd79c
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I7KYPM
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=26efd79c4624294e553aeaa3439c646729bad084

--------------------------------

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 20e5227e ("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 706c81f8 ("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.

Link: https://lore.kernel.org/linux-trace-kernel/20230712060452.3175675-1-zhengyejian1@huawei.com



Cc: stable@vger.kernel.org
Fixes: 706c81f8 ("ftrace: Remove extra helper functions")
Suggested-by: default avatarSteven Rostedt <rostedt@goodmis.org>
Signed-off-by: default avatarZheng Yejian <zhengyejian1@huawei.com>
Signed-off-by: default avatarSteven Rostedt (Google) <rostedt@goodmis.org>
Reviewed-by: default avatarXu Kuohai <xukuohai@huawei.com>
Signed-off-by: default avatarYongqiang Liu <liuyongqiang13@huawei.com>
parent e4403a7d
Loading
Loading
Loading
Loading
+29 −12
Original line number Diff line number Diff line
@@ -3048,12 +3048,25 @@ 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;
	int order;

	while (pg) {
		order = get_count_order(pg->size / ENTRIES_PER_PAGE);
		free_pages((unsigned long)pg->records, order);
		pages = pg->next;
		kfree(pg);
		pg = pages;
	}
}

static struct ftrace_page *
ftrace_allocate_pages(unsigned long num_to_init)
{
	struct ftrace_page *start_pg;
	struct ftrace_page *pg;
	int order;
	int cnt;

	if (!num_to_init)
@@ -3087,14 +3100,7 @@ ftrace_allocate_pages(unsigned long num_to_init)
	return start_pg;

 free_pages:
	pg = start_pg;
	while (pg) {
		order = get_count_order(pg->size / ENTRIES_PER_PAGE);
		free_pages((unsigned long)pg->records, order);
		start_pg = pg->next;
		kfree(pg);
		pg = start_pg;
	}
	ftrace_free_pages(start_pg);
	pr_info("ftrace: FAILED to allocate memory for functions\n");
	return NULL;
}
@@ -5575,9 +5581,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;
@@ -5630,8 +5638,10 @@ static int ftrace_process_locs(struct module *mod,
		 * object files to satisfy alignments.
		 * Skip any NULL pointers.
		 */
		if (!addr)
		if (!addr) {
			skipped++;
			continue;
		}

		if (pg->index == pg->size) {
			/* We should have allocated enough */
@@ -5644,8 +5654,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;
@@ -5667,6 +5679,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;
}