Skip to contentSkip to conversation

kallsyms: Optimizes the performance of lookup symbols

PATCH Superseded Zhen Lei 2 participants 25 messages Sep 20, 2022 – Sep 30, 2022

Subsystems (2)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Zhen Lei

author
#0 lore raw

v4 cover kallsyms: Optimizes the performance of lookup symbols

v3 --> v4:
1. Move the declaration of function kallsyms_sym_address() to linux/kallsyms.h, fix a build warning.

v2 --> v3:
1. Improve test cases, perform complete functional tests on functions kallsyms_lookup_name(), kallsyms_on_each_symbol() and kallsyms_on_each_match_symbol().
2. Add patch [PATCH v3 2/8] scripts/kallsyms: ensure that all possible combinations are compressed.
3. The symbol type is not compressed regardless of whether CONFIG_KALLSYMS_ALL is set or not. The memory overhead is increased by less than 20KiB if CONFIG_KALLSYMS_ALL=n.
4. Discard [PATCH v2 3/8] kallsyms: Adjust the types of some local variables

v1 --> v2:
Add self-test facility

v1:
Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This is very slow.

In fact, we can first compress the name being looked up and then use it for comparison when traversing 'kallsyms_names'.

This patch series optimizes the performance of function kallsyms_lookup_name(), and function klp_find_object_symbol() in the livepatch module. Based on the test results, the performance overhead is reduced to 5%. That is, the performance of these functions is improved by 20 times.

To avoid increasing the kernel size in non-debug mode, the optimization is only for the case CONFIG_KALLSYMS_ALL=y.

Zhen Lei (8):
scripts/kallsyms: rename build_initial_tok_table() scripts/kallsyms: ensure that all possible combinations are compressed scripts/kallsyms: don't compress symbol types
kallsyms: Improve the performance of kallsyms_lookup_name()
kallsyms: Add helper kallsyms_on_each_match_symbol()
livepatch: Use kallsyms_on_each_match_symbol() to improve performance
livepatch: Improve the search performance of
module_kallsyms_on_each_symbol()
kallsyms: Add self-test facility

 include/linux/kallsyms.h   |   9 +
 init/Kconfig               |  13 ++
 kernel/Makefile            |   1 +
 kernel/kallsyms.c          | 106 +++++++++-
 kernel/kallsyms_selftest.c | 423 +++++++++++++++++++++++++++++++++++++
 kernel/livepatch/core.c    |  25 ++-
 kernel/module/kallsyms.c   |  13 +-
 scripts/kallsyms.c         |  24 ++-
 8 files changed, 598 insertions(+), 16 deletions(-)
 create mode 100644 kernel/kallsyms_selftest.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v4 1/8 scripts/kallsyms: rename build_initial_tok_table()

Except for the function build_initial_tok_table(), no token abbreviation is used elsewhere.

$ cat scripts/kallsyms.c | grep tok | wc -l
33
$ cat scripts/kallsyms.c | grep token | wc -l
31

Here, it would be clearer to use the full name.

---
 scripts/kallsyms.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/kallsyms.c +2 -2
index f18e6dfc68c5839..8caccc8f4a23703 100644
@@ -525,7 +525,7 @@ static void forget_symbol(const unsigned char *symbol, int len)
 }
 
 /* do the initial token count */
-static void build_initial_tok_table(void)
+static void build_initial_token_table(void)
 {
 	unsigned int i;
 
@@ -650,7 +650,7 @@ static void insert_real_symbols_in_table(void)
 
 static void optimize_token_table(void)
 {
-	build_initial_tok_table();
+	build_initial_token_table();
 
 	insert_real_symbols_in_table();
 
Signature · 1 line
2.25.1
maintainer Reviewed-by on v4 1/8
#2 lore raw

Re: [PATCH v4 1/8] scripts/kallsyms: rename build_initial_tok_table()

Quoted 11 lines · Zhen Lei

Except for the function build_initial_tok_table(), no token abbreviation is used elsewhere.

$ cat scripts/kallsyms.c | grep tok | wc -l
33
$ cat scripts/kallsyms.c | grep token | wc -l
31

Here, it would be clearer to use the full name.

Best Regards,
Petr

author
#3 lore raw

PATCH v4 2/8 scripts/kallsyms: ensure that all possible combinations are compressed

In reply to Zhen Lei

For a symbol, there may be more than one place that can be merged. For
example: nfs_fs_proc_net_init, there are two "f"+"s_" combinations. And we're only compressing the first combination at the moment. Let's compress all possible combinations.

---
 scripts/kallsyms.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/kallsyms.c +4 -1
index 8caccc8f4a23703..3319d9f38d7a5f2 100644
@@ -553,7 +553,7 @@ static void compress_symbols(const unsigned char *str, int idx)
 	unsigned char *p1, *p2;
 
 	for (i = 0; i < table_cnt; i++) {
-
+retry:
 		len = table[i]->len;
 		p1 = table[i]->sym;
 
@@ -585,6 +585,9 @@ static void compress_symbols(const unsigned char *str, int idx)
 
 		/* increase the counts for this symbol's new tokens */
 		learn_symbol(table[i]->sym, len);
+
+		/* May be more than one place that can be merged, try again */
+		goto retry;
 	}
 }
 
Signature · 1 line
2.25.1
maintainer
#4 lore raw

Re: [PATCH v4 2/8] scripts/kallsyms: ensure that all possible combinations are compressed

Quoted 3 lines · Zhen Lei

For a symbol, there may be more than one place that can be merged. For
example: nfs_fs_proc_net_init, there are two "f"+"s_" combinations. And we're only compressing the first combination at the moment.

Really?

Quoted 14 lines
scripts/kallsyms.c +1 -1
index 8caccc8f4a23703..3319d9f38d7a5f2 100644
@@ -553,7 +553,7 @@ static void compress_symbols(const unsigned char *str, int idx)
 	unsigned char *p1, *p2;
 
 	for (i = 0; i < table_cnt; i++) {
-
+retry:
 		len = table[i]->len;
 		p1 = table[i]->sym;
 

scripts/kallsyms.c static void compress_symbols(const unsigned char *str, int idx) · line 592

Quoted 8 lines
scripts/kallsyms.c +3 -0
@@ -585,6 +585,9 @@ static void compress_symbols(const unsigned char *str, int idx)
 
 		/* increase the counts for this symbol's new tokens */
 		learn_symbol(table[i]->sym, len);
+
+		/* May be more than one place that can be merged, try again */
+		goto retry;
 	}
 }

My understanding is that the code already tries to find the same token several times. Here are the important parts of the existing code:

static void compress_symbols(const unsigned char *str, int idx) {

		p2 = find_token(p1, len, str);

		do {
			/* replace the found token with idx */
			*p2 = idx;
			[...]

			/* find the token on the symbol */
			p2 = find_token(p1, size, str);

		} while (p2);

Best Regards,
Petr

#5 lore raw

Re: [PATCH v4 2/8] scripts/kallsyms: ensure that all possible combinations are compressed

Quoted 6 lines · Petr Mladek
Quoted 3 lines · Zhen Lei

For a symbol, there may be more than one place that can be merged. For
example: nfs_fs_proc_net_init, there are two "f"+"s_" combinations. And we're only compressing the first combination at the moment.

Really?

Yes, there are about 200 such functions.

Quoted 31 lines
Quoted 15 lines
scripts/kallsyms.c +1 -1
index 8caccc8f4a23703..3319d9f38d7a5f2 100644
@@ -553,7 +553,7 @@ static void compress_symbols(const unsigned char *str, int idx)
 	unsigned char *p1, *p2;
 
 	for (i = 0; i < table_cnt; i++) {
-
+retry:
 		len = table[i]->len;
 		p1 = table[i]->sym;
 

scripts/kallsyms.c static void compress_symbols(const unsigned char *str, int idx) · line 592

Quoted 8 lines
scripts/kallsyms.c +3 -0
@@ -585,6 +585,9 @@ static void compress_symbols(const unsigned char *str, int idx)
 
 		/* increase the counts for this symbol's new tokens */
 		learn_symbol(table[i]->sym, len);
+
+		/* May be more than one place that can be merged, try again */
+		goto retry;
 	}
 }

My understanding is that the code already tries to find the same token several times. Here are the important parts of the existing code:

static void compress_symbols(const unsigned char *str, int idx) {

Quoted 9 lines
		p2 = find_token(p1, len, str);

		do {
			/* replace the found token with idx */
			*p2 = idx;
			[...]

			/* find the token on the symbol */
			p2 = find_token(p1, size, str);

Oh, yes, it retries. Let me reanalyze it. However, the problem is real, and there may be a problem somewhere in the loop.

Quoted 7 lines
		} while (p2);

Best Regards,
Petr
.

Signature · 2 lines
Regards,
  Zhen Lei
#6 lore raw

Re: [PATCH v4 2/8] scripts/kallsyms: ensure that all possible combinations are compressed

Quoted 50 lines · Leizhen (ThunderTown)
Quoted 6 lines · Petr Mladek
Quoted 3 lines · Zhen Lei

For a symbol, there may be more than one place that can be merged. For
example: nfs_fs_proc_net_init, there are two "f"+"s_" combinations. And we're only compressing the first combination at the moment.

Really?

Yes, there are about 200 such functions.

Quoted 38 lines
Quoted 15 lines
scripts/kallsyms.c +1 -1
index 8caccc8f4a23703..3319d9f38d7a5f2 100644
@@ -553,7 +553,7 @@ static void compress_symbols(const unsigned char *str, int idx)
 	unsigned char *p1, *p2;
 
 	for (i = 0; i < table_cnt; i++) {
-
+retry:
 		len = table[i]->len;
 		p1 = table[i]->sym;
 

scripts/kallsyms.c static void compress_symbols(const unsigned char *str, int idx) · line 592

Quoted 8 lines
scripts/kallsyms.c +3 -0
@@ -585,6 +585,9 @@ static void compress_symbols(const unsigned char *str, int idx)
 
 		/* increase the counts for this symbol's new tokens */
 		learn_symbol(table[i]->sym, len);
+
+		/* May be more than one place that can be merged, try again */
+		goto retry;
 	}
 }

My understanding is that the code already tries to find the same token several times. Here are the important parts of the existing code:

static void compress_symbols(const unsigned char *str, int idx) {

		p2 = find_token(p1, len, str);

		do {
			/* replace the found token with idx */
			*p2 = idx;
			[...]
Quoted 5 lines
Quoted 2 lines
			/* find the token on the symbol */
			p2 = find_token(p1, size, str);

Oh, yes, it retries. Let me reanalyze it. However, the problem is real, and there may be a problem somewhere in the loop.

Hi, Petr:
Thanks. I found that it's my fault. The first round skip the type
character. But the next round will incorrectly skip one character, so for nfs_fs_proc_net_init, the next round start from s, and using

            ^

the proposed "unsigned char type" in your next reply should solve the problem. Thank you very much.

- for (i = 0; i < len - 1; i++)
+ for (i = sym_start_idx; i < len - 1; i++)

Quoted 9 lines
Quoted 8 lines

		} while (p2);

Best Regards,
Petr
.

Signature · 2 lines
Regards,
  Zhen Lei
author
#7 lore raw

PATCH v4 3/8 scripts/kallsyms: don't compress symbol types

In reply to Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. Because we do not know the symbol type, and the symbol type may be combined with the following characters to form a token.

So if we don't compress the symbol type, we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

Of course, because the symbol type is forcibly not compressed, the compression rate also decreases. Here are the test results with defconfig:

arm64: <<<<<<

        ---------------------------------------------------------------
       | ALL | nr_symbols | compressed size | original size | ratio(%) |
        -----|---------------------------------------------------------|
Before |  Y  |     174094 |       1884938   |      3750653  |  50.25   |
After  |  Y  |     174099 |       1960154   |      3750756  |  52.26   |
Before |  N  |      61744 |        725507   |      1222737  |  59.33   |
After  |  N  |      61747 |        745733   |      1222801  |  60.98   |
        ---------------------------------------------------------------

The memory overhead is increased by:
73.5KiB and 4.0% if CONFIG_KALLSYMS_ALL=y.
19.8KiB and 2.8% if CONFIG_KALLSYMS_ALL=n.

x86: <<<<<<<<

       ---------------------------------------------------------------
       | ALL | nr_symbols | compressed size | original size | ratio(%) |
        -----|---------------------------------------------------------|
Before |  Y  |     131415 |       1697542   |      3161216  |  53.69   |
After  |  Y  |     131540 |       1747769   |      3163933  |  55.24   |
Before |  N  |      60695 |        737627   |      1283046  |  57.49   |
After  |  N  |      60699 |        754797   |      1283149  |  58.82   |
        ---------------------------------------------------------------

The memory overhead is increased by:
49.0KiB and 3.0% if CONFIG_KALLSYMS_ALL=y.
16.8KiB and 2.3% if CONFIG_KALLSYMS_ALL=n.

This additional memory overhead is worth it compared to the performance improvement, I think.

---
 scripts/kallsyms.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
scripts/kallsyms.c +12 -3
index 3319d9f38d7a5f2..1ae9ce773d2a31d 100644
@@ -61,6 +61,15 @@ static int all_symbols;
 static int absolute_percpu;
 static int base_relative;
 
+/*
+ * Each entry in the symbol table consists of the symbol type and the symbol
+ * itself. To optimize the performance of finding or traversing symbols in
+ * kernel, do not compress the symbol type. In this way, when looking for a
+ * symbol of unknown type, we can first compress the searched symbol and then
+ * make a quick comparison based on the compressed length and content.
+ */
+static int sym_start_idx = 1;
+
 static int token_profit[0x10000];
 
 /* the table that holds the result of the compression */
@@ -511,7 +520,7 @@ static void learn_symbol(const unsigned char *symbol, int len)
 {
 	int i;
 
-	for (i = 0; i < len - 1; i++)
+	for (i = sym_start_idx; i < len - 1; i++)
 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
 }
 
@@ -520,7 +529,7 @@ static void forget_symbol(const unsigned char *symbol, int len)
 {
 	int i;
 
-	for (i = 0; i < len - 1; i++)
+	for (i = sym_start_idx; i < len - 1; i++)
 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
 }
 
@@ -538,7 +547,7 @@ static unsigned char *find_token(unsigned char *str, int len,
 {
 	int i;
 
-	for (i = 0; i < len - 1; i++) {
+	for (i = sym_start_idx; i < len - 1; i++) {
 		if (str[i] == token[0] && str[i+1] == token[1])
 			return &str[i];
 	}
Signature · 1 line
2.25.1
maintainer
#8 lore raw

Re: [PATCH v4 3/8] scripts/kallsyms: don't compress symbol types

Quoted 39 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. Because we do not know the symbol type, and the symbol type may be combined with the following characters to form a token.

So if we don't compress the symbol type, we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

Of course, because the symbol type is forcibly not compressed, the compression rate also decreases. Here are the test results with defconfig:

arm64: <<<<<<

        ---------------------------------------------------------------
       | ALL | nr_symbols | compressed size | original size | ratio(%) |
        -----|---------------------------------------------------------|
Before |  Y  |     174094 |       1884938   |      3750653  |  50.25   |
After  |  Y  |     174099 |       1960154   |      3750756  |  52.26   |
Before |  N  |      61744 |        725507   |      1222737  |  59.33   |
After  |  N  |      61747 |        745733   |      1222801  |  60.98   |
        ---------------------------------------------------------------

The memory overhead is increased by:
73.5KiB and 4.0% if CONFIG_KALLSYMS_ALL=y.
19.8KiB and 2.8% if CONFIG_KALLSYMS_ALL=n.

x86: <<<<<<<<

       ---------------------------------------------------------------
       | ALL | nr_symbols | compressed size | original size | ratio(%) |
        -----|---------------------------------------------------------|
Before |  Y  |     131415 |       1697542   |      3161216  |  53.69   |
After  |  Y  |     131540 |       1747769   |      3163933  |  55.24   |
Before |  N  |      60695 |        737627   |      1283046  |  57.49   |
After  |  N  |      60699 |        754797   |      1283149  |  58.82   |
        ---------------------------------------------------------------
Quoted 6 lines

The memory overhead is increased by:
49.0KiB and 3.0% if CONFIG_KALLSYMS_ALL=y.
16.8KiB and 2.3% if CONFIG_KALLSYMS_ALL=n.

This additional memory overhead is worth it compared to the performance improvement, I think.

I agree. The speedup mentioned in the followup patches looks big. I just suggest to do this change a cleaner way, see below.

Quoted 26 lines
---
 scripts/kallsyms.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
scripts/kallsyms.c +9 -0
index 3319d9f38d7a5f2..1ae9ce773d2a31d 100644
@@ -61,6 +61,15 @@ static int all_symbols;
 static int absolute_percpu;
 static int base_relative;
 
+/*
+ * Each entry in the symbol table consists of the symbol type and the symbol
+ * itself. To optimize the performance of finding or traversing symbols in
+ * kernel, do not compress the symbol type. In this way, when looking for a
+ * symbol of unknown type, we can first compress the searched symbol and then
+ * make a quick comparison based on the compressed length and content.
+ */
+static int sym_start_idx = 1;
+
 static int token_profit[0x10000];
 
 /* the table that holds the result of the compression */

scripts/kallsyms.c static void learn_symbol(const unsigned char *symbol, int len) · line 523

Quoted 5 lines
scripts/kallsyms.c +1 -1
@@ -511,7 +520,7 @@ static void learn_symbol(const unsigned char *symbol, int len)
 {
 	int i;
 
-	for (i = 0; i < len - 1; i++)
+	for (i = sym_start_idx; i < len - 1; i++)

It creates yet another twists in scripts/kallsyms.c. read_symbol() explicitely adds the type as the first character so that it can be compressed. And this patch adds a hack to skip it.

Let's do it a clean way and store the type serarately:

struct sym_entry {
	unsigned long long addr;
	unsigned int len;
	unsigned int start_pos;
	unsigned int percpu_absolute;
	unsigned char type;
	unsigned char name[];
};

static struct sym_entry *read_symbol(FILE *in)
{
[...]

	name_len = strlen(name);

	sym = malloc(sizeof(*sym) + name_len);
	if (!sym) {
		fprintf(stderr, "kallsyms failure: "
			"unable to allocate required amount of memory\n");
		exit(EXIT_FAILURE);
	}
	sym->addr = addr;
	sym->len = name_len;
	sym->type = type;
	strcpy(sys->name, name);
	sym->percpu_absolute = 0;
}

It would allow to remove the tricky:

	static char *sym_name(const struct sym_entry *s)
	{
		return (char *)s->sym + 1;
	}

and access s->name directly.

OK, the problem is how to store the type. The clean way would be to put it into a separate section, for example:

static void write_src(void)
{
[...]

	output_label("kallsyms_types");
	off = 0;
	for (i = 0; i < table_cnt; i++) {
		printf("\t.byte 0x%02x\n", table[i]->type);
	}
	printf("\n");

[...]
}

It would probably increase the size even more. Another problem is that it would need changes in the crash dump tools, see:

static int __init crash_save_vmcoreinfo_init(void) {
[...]

	VMCOREINFO_SYMBOL(kallsyms_names);

[...]
}

A solution would be to store it the old way:

static void write_src(void)
{
[...]

	output_label("kallsyms_names");
	off = 0;
	for (i = 0; i < table_cnt; i++) {
		if ((i & 0xFF) == 0)
			markers[i >> 8] = off;

		/*
		 * Store the symbol type togerher with symbol name.
		 * It helps to reduce the size.
		 */
		printf("\t.byte 0x%02x", table[i]->len + 1);
		printf(", 0x%02x", table[i]->type);
		for (k = 0; k < table[i]->len; k++)
			printf(", 0x%02x", table[i]->sym[k]);
		printf("\n");

		/* symbol name lenght + type + "\n" */
		off += table[i]->len + 2;
	}
	printf("\n");

[...]
}

The result would be the same as with your patch. But the code would be even cleaner than before.

Best Regards,
Petr

#9 lore raw

Re: [PATCH v4 3/8] scripts/kallsyms: don't compress symbol types

Quoted 86 lines · Petr Mladek
Quoted 39 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. Because we do not know the symbol type, and the symbol type may be combined with the following characters to form a token.

So if we don't compress the symbol type, we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

Of course, because the symbol type is forcibly not compressed, the compression rate also decreases. Here are the test results with defconfig:

arm64: <<<<<<

        ---------------------------------------------------------------
       | ALL | nr_symbols | compressed size | original size | ratio(%) |
        -----|---------------------------------------------------------|
Before |  Y  |     174094 |       1884938   |      3750653  |  50.25   |
After  |  Y  |     174099 |       1960154   |      3750756  |  52.26   |
Before |  N  |      61744 |        725507   |      1222737  |  59.33   |
After  |  N  |      61747 |        745733   |      1222801  |  60.98   |
        ---------------------------------------------------------------

The memory overhead is increased by:
73.5KiB and 4.0% if CONFIG_KALLSYMS_ALL=y.
19.8KiB and 2.8% if CONFIG_KALLSYMS_ALL=n.

x86: <<<<<<<<

       ---------------------------------------------------------------
       | ALL | nr_symbols | compressed size | original size | ratio(%) |
        -----|---------------------------------------------------------|
Before |  Y  |     131415 |       1697542   |      3161216  |  53.69   |
After  |  Y  |     131540 |       1747769   |      3163933  |  55.24   |
Before |  N  |      60695 |        737627   |      1283046  |  57.49   |
After  |  N  |      60699 |        754797   |      1283149  |  58.82   |
        ---------------------------------------------------------------
Quoted 6 lines

The memory overhead is increased by:
49.0KiB and 3.0% if CONFIG_KALLSYMS_ALL=y.
16.8KiB and 2.3% if CONFIG_KALLSYMS_ALL=n.

This additional memory overhead is worth it compared to the performance improvement, I think.

I agree. The speedup mentioned in the followup patches looks big. I just suggest to do this change a cleaner way, see below.

Quoted 26 lines
---
 scripts/kallsyms.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
scripts/kallsyms.c +9 -0
index 3319d9f38d7a5f2..1ae9ce773d2a31d 100644
@@ -61,6 +61,15 @@ static int all_symbols;
 static int absolute_percpu;
 static int base_relative;
 
+/*
+ * Each entry in the symbol table consists of the symbol type and the symbol
+ * itself. To optimize the performance of finding or traversing symbols in
+ * kernel, do not compress the symbol type. In this way, when looking for a
+ * symbol of unknown type, we can first compress the searched symbol and then
+ * make a quick comparison based on the compressed length and content.
+ */
+static int sym_start_idx = 1;
+
 static int token_profit[0x10000];
 
 /* the table that holds the result of the compression */

scripts/kallsyms.c static void learn_symbol(const unsigned char *symbol, int len) · line 523

Quoted 5 lines
scripts/kallsyms.c +1 -1
@@ -511,7 +520,7 @@ static void learn_symbol(const unsigned char *symbol, int len)
 {
 	int i;
 
-	for (i = 0; i < len - 1; i++)
+	for (i = sym_start_idx; i < len - 1; i++)

It creates yet another twists in scripts/kallsyms.c. read_symbol() explicitely adds the type as the first character so that it can be compressed. And this patch adds a hack to skip it.

Quoted 8 lines

Let's do it a clean way and store the type serarately:

struct sym_entry {
	unsigned long long addr;
	unsigned int len;
	unsigned int start_pos;
	unsigned int percpu_absolute;
	unsigned char type;

Yes, it's very necessary. Thanks.

Quoted 1 line
	unsigned char name[];

Yes, using "name[]" will be clearer than using "sym[]"

Quoted 20 lines

};

static struct sym_entry *read_symbol(FILE *in)
{
[...]

	name_len = strlen(name);

	sym = malloc(sizeof(*sym) + name_len);
	if (!sym) {
		fprintf(stderr, "kallsyms failure: "
			"unable to allocate required amount of memory\n");
		exit(EXIT_FAILURE);
	}
	sym->addr = addr;
	sym->len = name_len;
	sym->type = type;
	strcpy(sys->name, name);
	sym->percpu_absolute = 0;
}
Quoted 8 lines

It would allow to remove the tricky:

	static char *sym_name(const struct sym_entry *s)
	{
		return (char *)s->sym + 1;
	}

and access s->name directly.

OK

Quoted 19 lines

OK, the problem is how to store the type. The clean way would be to put it into a separate section, for example:

static void write_src(void)
{
[...]

	output_label("kallsyms_types");
	off = 0;
	for (i = 0; i < table_cnt; i++) {
		printf("\t.byte 0x%02x\n", table[i]->type);
	}
	printf("\n");

[...]
}

It would probably increase the size even more. Another problem is that it would need changes in the crash dump tools, see:

Quoted 8 lines

static int __init crash_save_vmcoreinfo_init(void) {
[...]

	VMCOREINFO_SYMBOL(kallsyms_names);

[...]
}

A solution would be to store it the old way:

I'll use this compatibility mode first. Because I'm going to compress the type later.

Quoted 24 lines

static void write_src(void)
{
[...]

	output_label("kallsyms_names");
	off = 0;
	for (i = 0; i < table_cnt; i++) {
		if ((i & 0xFF) == 0)
			markers[i >> 8] = off;

		/*
		 * Store the symbol type togerher with symbol name.
		 * It helps to reduce the size.
		 */
		printf("\t.byte 0x%02x", table[i]->len + 1);
		printf(", 0x%02x", table[i]->type);
		for (k = 0; k < table[i]->len; k++)
			printf(", 0x%02x", table[i]->sym[k]);
		printf("\n");

		/* symbol name lenght + type + "\n" */
		off += table[i]->len + 2;
	}
	printf("\n");
Quoted 5 lines

[...]
}

The result would be the same as with your patch. But the code would be even cleaner than before.

Yes,Thank you for your valuable comments.

Quoted 5 lines

Best Regards,
Petr
.

Signature · 2 lines
Regards,
  Zhen Lei
author
#10 lore raw

PATCH v4 4/8 kallsyms: Improve the performance of kallsyms_lookup_name()

In reply to Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This process can be optimized.

And now scripts/kallsyms no longer compresses the symbol types, each symbol type always occupies one byte. So we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

The pseudo code of the test case is as follows:
static int stat_find_name(...)

{
	start = sched_clock();
	(void)kallsyms_lookup_name(name);
	end = sched_clock();
	//Update min, max, cnt, sum
}

/*
* Traverse all symbols in sequence and collect statistics on the time
* taken by kallsyms_lookup_name() to lookup each symbol. */
kallsyms_on_each_symbol(stat_find_name, NULL);

The test results are as follows (twice):
After : min=5250, max= 726560, avg= 302132
After : min=5320, max= 726850, avg= 301978
Before: min=170, max=15949190, avg=7553906
Before: min=160, max=15877280, avg=7517784

The average time consumed is only 4.01% and the maximum time consumed is only 4.57% of the time consumed before optimization.

---
 kernel/kallsyms.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
kernel/kallsyms.c +76 -3
index 3e7e2c2ad2f75ef..2d76196cfe89f34 100644
@@ -87,6 +87,71 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
 	return off;
 }
 
+static int kallsyms_name_to_tokens(const char *name, char *buf)
+{
+	int i, j, k, n;
+	int len, token_len;
+	const char *token;
+	unsigned char token_idx[KSYM_NAME_LEN];
+	unsigned char token_bak[KSYM_NAME_LEN];
+
+	/*
+	 * n, number of tokens in the string name.
+	 * token_idx[i], the start index of the ith token.
+	 * token_idx[n] is used to calculate the length of the last token.
+	 */
+	n = strlen(name);
+	if (n >= KSYM_NAME_LEN) {
+		buf[0] = 0;
+		return 0;
+	}
+	for (i = 0; i <= n; i++)
+		token_idx[i] = (unsigned char)i;
+
+	/*
+	 * For tokens whose token_len >= 2, a larger index value indicates
+	 * a higher occurrence frequency. See scripts/kallsyms.c
+	 */
+	for (i = 255; i >= 0; i--) {
+		token = &kallsyms_token_table[kallsyms_token_index[i]];
+		token_len = strlen(token);
+		if (token_len <= 1)
+			continue;
+
+		/*
+		 * Find and merge two tokens into one.
+		 *
+		 *                |<-- new_token -->|
+		 *                | token1 | token2 |
+		 * token_idx[]:   j       j+1      j+2
+		 *
+		 */
+		for (j = 0; j < n - 1; j++) {
+			len = token_idx[j + 2] - token_idx[j];
+			if (len == token_len &&
+			    !strncmp(name + token_idx[j], token, len)) {
+				token_bak[token_idx[j]] = (unsigned char)i;
+				for (k = j + 1; k < n; k++)
+					token_idx[k] = token_idx[k + 1];
+				n--;
+			}
+		}
+	}
+
+	for (j = 0; j < n; j++) {
+		len = token_idx[j + 1] - token_idx[j];
+		if (len <= 1) {
+			buf[j] = name[token_idx[j]];
+			continue;
+		}
+
+		buf[j] = token_bak[token_idx[j]];
+	}
+	buf[n] = 0;
+
+	return n;
+}
+
 /*
  * Get symbol type information. This is encoded as a single char at the
  * beginning of the symbol name.
@@ -192,20 +257,28 @@ unsigned long kallsyms_lookup_name(const char *name)
 	char namebuf[KSYM_NAME_LEN];
 	unsigned long i;
 	unsigned int off;
+	int len;
 
 	/* Skip the search for empty string. */
 	if (!*name)
 		return 0;
 
+	len = kallsyms_name_to_tokens(name, namebuf);
+	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
+		if (kallsyms_names[off] == len + 1 &&
+		    !memcmp(&kallsyms_names[off + 2], namebuf, len))
+			return kallsyms_sym_address(i);
+
+		off += kallsyms_names[off] + 1;
+	}
+
 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
 
-		if (strcmp(namebuf, name) == 0)
-			return kallsyms_sym_address(i);
-
 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
 			return kallsyms_sym_address(i);
 	}
+
 	return module_kallsyms_lookup_name(name);
 }
 
Signature · 1 line
2.25.1
maintainer
#11 lore raw

Re: [PATCH v4 4/8] kallsyms: Improve the performance of kallsyms_lookup_name()

Quoted 47 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This process can be optimized.

And now scripts/kallsyms no longer compresses the symbol types, each symbol type always occupies one byte. So we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

The pseudo code of the test case is as follows:
static int stat_find_name(...)

{
	start = sched_clock();
	(void)kallsyms_lookup_name(name);
	end = sched_clock();
	//Update min, max, cnt, sum
}

/*
* Traverse all symbols in sequence and collect statistics on the time
* taken by kallsyms_lookup_name() to lookup each symbol. */
kallsyms_on_each_symbol(stat_find_name, NULL);

The test results are as follows (twice):
After : min=5250, max= 726560, avg= 302132
After : min=5320, max= 726850, avg= 301978
Before: min=170, max=15949190, avg=7553906
Before: min=160, max=15877280, avg=7517784

The average time consumed is only 4.01% and the maximum time consumed is only 4.57% of the time consumed before optimization.

---
 kernel/kallsyms.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
kernel/kallsyms.c +0 -0
index 3e7e2c2ad2f75ef..2d76196cfe89f34 100644

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 90

Quoted 4 lines
kernel/kallsyms.c +1 -0
@@ -87,6 +87,71 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
 	return off;
 }
 
+static int kallsyms_name_to_tokens(const char *name, char *buf)

This is not safe API. It is always needed to pass the size of the buffer.

Also it should be called "compress". "token" is just an implementation detail.

I would do:

static int kallsyms_compress_symbol_name(const char *name,
char *buf, size_t size)

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 96

Quoted 6 lines
kernel/kallsyms.c +6 -0
+{
+	int i, j, k, n;
+	int len, token_len;
+	const char *token;
+	unsigned char token_idx[KSYM_NAME_LEN];
+	unsigned char token_bak[KSYM_NAME_LEN];

Why do we need two buffers? It should be possible to compress the name in the same buffer as it is done in compress_symbols() in scripts/callsyms.c.

Quoted 41 lines
kernel/kallsyms.c +41 -0
+
+	/*
+	 * n, number of tokens in the string name.
+	 * token_idx[i], the start index of the ith token.
+	 * token_idx[n] is used to calculate the length of the last token.
+	 */
+	n = strlen(name);
+	if (n >= KSYM_NAME_LEN) {
+		buf[0] = 0;
+		return 0;
+	}
+	for (i = 0; i <= n; i++)
+		token_idx[i] = (unsigned char)i;
+
+	/*
+	 * For tokens whose token_len >= 2, a larger index value indicates
+	 * a higher occurrence frequency. See scripts/kallsyms.c
+	 */
+	for (i = 255; i >= 0; i--) {
+		token = &kallsyms_token_table[kallsyms_token_index[i]];
+		token_len = strlen(token);
+		if (token_len <= 1)
+			continue;
+
+		/*
+		 * Find and merge two tokens into one.
+		 *
+		 *                |<-- new_token -->|
+		 *                | token1 | token2 |
+		 * token_idx[]:   j       j+1      j+2
+		 */
+		for (j = 0; j < n - 1; j++) {
+			len = token_idx[j + 2] - token_idx[j];
+			if (len == token_len &&
+			    !strncmp(name + token_idx[j], token, len)) {
+				token_bak[token_idx[j]] = (unsigned char)i;
+				for (k = j + 1; k < n; k++)
+					token_idx[k] = token_idx[k + 1];
+				n--;
+			}
+		}

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 148

Quoted 10 lines
kernel/kallsyms.c +10 -0
+	}
+
+	for (j = 0; j < n; j++) {
+		len = token_idx[j + 1] - token_idx[j];
+		if (len <= 1) {
+			buf[j] = name[token_idx[j]];
+			continue;
+		}
+
+		buf[j] = token_bak[token_idx[j]];

Maybe, I do not understand the compression format correctly but this code looks too complicated. Honestly, I even did not try to understand it.

My understanding is the we just need to find all tokens and replace them with index.

It should be even easier than compress_symbols() in scripts/callsyms.c. The token_table already exists and we do not need to handle the token_profit...

The following looks more strigtforward (not even compile tested):

	ssize_t len, size;

	len = strscpy(buf, symname, size);
	if (WARN_ON_ONCE(len < 0))
		return -EINVAL;

	/* the tokens with higher index are used first */
	for (idx = 255; idx >= 0; idx--) {
		token =	&kallsyms_token_table[kallsyms_token_index[i]];
		token_len = strlen(token);

		p1 = buf;
		/* length of the remaining symname including the trailing '\0' */
		remaining = len + 1;

		/* find the token in the symbol name */
		p2 = strstr(token, p1);

		while (p2) {
			/* replace token with index */
			*p2 = idx;
			remaining -= ((p2 - p1) + token_len);
			memmove(p2 + 1, p2 + token_len, remaining);
			len -= (token_len - 1);
			p1 = p2;

			/* find the token in the rest of the symbol name */
			p2 = strstr(token, p1);
		}
	}

	return len;
Quoted 16 lines
kernel/kallsyms.c +7 -0
+	}
+	buf[n] = 0;
+
+	return n;
+}
+
 /*
  * Get symbol type information. This is encoded as a single char at the
  * beginning of the symbol name.
@@ -192,20 +257,28 @@ unsigned long kallsyms_lookup_name(const char *name)
 	char namebuf[KSYM_NAME_LEN];
 	unsigned long i;
 	unsigned int off;
+	int len;
 
 	/* Skip the search for empty string. */

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 272

Quoted 10 lines
kernel/kallsyms.c +7 -0
 	if (!*name)
 		return 0;
 
+	len = kallsyms_name_to_tokens(name, namebuf);
+	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
+		if (kallsyms_names[off] == len + 1 &&
+		    !memcmp(&kallsyms_names[off + 2], namebuf, len))
+			return kallsyms_sym_address(i);
+
+		off += kallsyms_names[off] + 1;

These complicated checks are hard to review. The following looks much more readable to me:

	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
		/* the stored symbol name is prefixed by symbol type */
		name_len = kallsyms_names[off] - 1;
		name = &kallsyms_names[off + 2];
		off += name_len + 2;

		if (name_len != len)
			continue;

		if (!memcmp(name, namebuf, len))
			return kallsyms_sym_address(i);
	}

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 279

Quoted 10 lines
kernel/kallsyms.c +2 -3
+	}
+
 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
 
-		if (strcmp(namebuf, name) == 0)
-			return kallsyms_sym_address(i);
-
 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
 			return kallsyms_sym_address(i);

Hmm, it means that the speedup is not usable when kernel is compiled LLVM? It might actually slow down the search because we would need to use both fast and slow search?

Best Regards,
Petr

#12 lore raw

Re: [PATCH v4 4/8] kallsyms: Improve the performance of kallsyms_lookup_name()

Quoted 51 lines · Petr Mladek
Quoted 50 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This process can be optimized.

And now scripts/kallsyms no longer compresses the symbol types, each symbol type always occupies one byte. So we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

The pseudo code of the test case is as follows:
static int stat_find_name(...)

{
	start = sched_clock();
	(void)kallsyms_lookup_name(name);
	end = sched_clock();
	//Update min, max, cnt, sum
}

/*
* Traverse all symbols in sequence and collect statistics on the time
* taken by kallsyms_lookup_name() to lookup each symbol. */
kallsyms_on_each_symbol(stat_find_name, NULL);

The test results are as follows (twice):
After : min=5250, max= 726560, avg= 302132
After : min=5320, max= 726850, avg= 301978
Before: min=170, max=15949190, avg=7553906
Before: min=160, max=15877280, avg=7517784

The average time consumed is only 4.01% and the maximum time consumed is only 4.57% of the time consumed before optimization.

---
 kernel/kallsyms.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
kernel/kallsyms.c +0 -0
index 3e7e2c2ad2f75ef..2d76196cfe89f34 100644
@@ -87,6 +87,71 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
 	return off;
 }
 
Quoted 4 lines

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 90

Quoted 1 line
kernel/kallsyms.c +1 -0
+static int kallsyms_name_to_tokens(const char *name, char *buf)

This is not safe API. It is always needed to pass the size of the buffer.

OK

Quoted 8 lines

Also it should be called "compress". "token" is just an implementation detail.

I would do:

static int kallsyms_compress_symbol_name(const char *name,
char *buf, size_t size)

This's a wonderful name. Thanks.

Quoted 11 lines

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 96

Quoted 8 lines
kernel/kallsyms.c +6 -0
+{
+	int i, j, k, n;
+	int len, token_len;
+	const char *token;
+	unsigned char token_idx[KSYM_NAME_LEN];
+	unsigned char token_bak[KSYM_NAME_LEN];

Why do we need two buffers? It should be possible to compress the name in the same buffer as it is done in compress_symbols() in scripts/callsyms.c.

Because the performance would be a little better. Now this function takes just over a microsecond. Currently, it takes about 250 microseconds on average to lookup a symbol, so adding a little more time to this function doesn't affect the overall picture. I'll modify and test it as you suggest below.

Quoted 57 lines
Quoted 42 lines
kernel/kallsyms.c +41 -0
+
+	/*
+	 * n, number of tokens in the string name.
+	 * token_idx[i], the start index of the ith token.
+	 * token_idx[n] is used to calculate the length of the last token.
+	 */
+	n = strlen(name);
+	if (n >= KSYM_NAME_LEN) {
+		buf[0] = 0;
+		return 0;
+	}
+	for (i = 0; i <= n; i++)
+		token_idx[i] = (unsigned char)i;
+
+	/*
+	 * For tokens whose token_len >= 2, a larger index value indicates
+	 * a higher occurrence frequency. See scripts/kallsyms.c
+	 */
+	for (i = 255; i >= 0; i--) {
+		token = &kallsyms_token_table[kallsyms_token_index[i]];
+		token_len = strlen(token);
+		if (token_len <= 1)
+			continue;
+
+		/*
+		 * Find and merge two tokens into one.
+		 *
+		 *                |<-- new_token -->|
+		 *                | token1 | token2 |
+		 * token_idx[]:   j       j+1      j+2
+		 */
+		for (j = 0; j < n - 1; j++) {
+			len = token_idx[j + 2] - token_idx[j];
+			if (len == token_len &&
+			    !strncmp(name + token_idx[j], token, len)) {
+				token_bak[token_idx[j]] = (unsigned char)i;
+				for (k = j + 1; k < n; k++)
+					token_idx[k] = token_idx[k + 1];
+				n--;
+			}
+		}

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 148

Quoted 10 lines
kernel/kallsyms.c +10 -0
+	}
+
+	for (j = 0; j < n; j++) {
+		len = token_idx[j + 1] - token_idx[j];
+		if (len <= 1) {
+			buf[j] = name[token_idx[j]];
+			continue;
+		}
+
+		buf[j] = token_bak[token_idx[j]];

Maybe, I do not understand the compression format correctly but this code looks too complicated. Honestly, I even did not try to understand it.

Quoted 7 lines

My understanding is the we just need to find all tokens and replace them with index.

It should be even easier than compress_symbols() in scripts/callsyms.c. The token_table already exists and we do not need to handle the token_profit...

The following looks more strigtforward (not even compile tested):

OK, I will try this one. Or refer to compress_symbols() in scripts/callsyms.c.

Quoted 53 lines
	ssize_t len, size;

	len = strscpy(buf, symname, size);
	if (WARN_ON_ONCE(len < 0))
		return -EINVAL;

	/* the tokens with higher index are used first */
	for (idx = 255; idx >= 0; idx--) {
		token =	&kallsyms_token_table[kallsyms_token_index[i]];
		token_len = strlen(token);

		p1 = buf;
		/* length of the remaining symname including the trailing '\0' */
		remaining = len + 1;

		/* find the token in the symbol name */
		p2 = strstr(token, p1);

		while (p2) {
			/* replace token with index */
			*p2 = idx;
			remaining -= ((p2 - p1) + token_len);
			memmove(p2 + 1, p2 + token_len, remaining);
			len -= (token_len - 1);
			p1 = p2;

			/* find the token in the rest of the symbol name */
			p2 = strstr(token, p1);
		}
	}

	return len;
Quoted 19 lines
kernel/kallsyms.c +7 -0
+	}
+	buf[n] = 0;
+
+	return n;
+}
+
 /*
  * Get symbol type information. This is encoded as a single char at the
  * beginning of the symbol name.
@@ -192,20 +257,28 @@ unsigned long kallsyms_lookup_name(const char *name)
 	char namebuf[KSYM_NAME_LEN];
 	unsigned long i;
 	unsigned int off;
+	int len;
 
 	/* Skip the search for empty string. */
 	if (!*name)
 		return 0;
 
Quoted 10 lines

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 272

Quoted 7 lines
kernel/kallsyms.c +7 -0
+	len = kallsyms_name_to_tokens(name, namebuf);
+	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
+		if (kallsyms_names[off] == len + 1 &&
+		    !memcmp(&kallsyms_names[off + 2], namebuf, len))
+			return kallsyms_sym_address(i);
+
+		off += kallsyms_names[off] + 1;

These complicated checks are hard to review. The following looks much more readable to me:

Yes, it looks well.

Quoted 20 lines
	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
		/* the stored symbol name is prefixed by symbol type */
		name_len = kallsyms_names[off] - 1;
		name = &kallsyms_names[off + 2];
		off += name_len + 2;

		if (name_len != len)
			continue;

		if (!memcmp(name, namebuf, len))
			return kallsyms_sym_address(i);
	}
Quoted 5 lines
kernel/kallsyms.c +2 -0
+	}
+
 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
 
Quoted 9 lines

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 279

Quoted 5 lines
kernel/kallsyms.c +0 -3
-		if (strcmp(namebuf, name) == 0)
-			return kallsyms_sym_address(i);
-
 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
 			return kallsyms_sym_address(i);

Hmm, it means that the speedup is not usable when kernel is compiled LLVM? It might actually slow down the search because we would need to use both fast and slow search?

Theoretically, I don't think so. A string comparison was removed from the slow search. "if (name_len != len)" is faster than
"if (strcmp(namebuf, name) == 0)". Even if they're equal, kallsyms_compress_symbol_name() only takes 1-2us, it doesn't affect the overall picture. The average lookup time before optimization is millisecond-level. To allay your concerns, I can run a test.

Before: min=170, max=15949190, avg=7553906

Quoted 5 lines

Best Regards,
Petr
.

Signature · 2 lines
Regards,
  Zhen Lei
maintainer
#13 lore raw

Re: [PATCH v4 4/8] kallsyms: Improve the performance of kallsyms_lookup_name()

Quoted 61 lines · Leizhen (ThunderTown)
Quoted 43 lines · Petr Mladek
Quoted 42 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This process can be optimized.

And now scripts/kallsyms no longer compresses the symbol types, each symbol type always occupies one byte. So we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

The pseudo code of the test case is as follows:
static int stat_find_name(...)

{
	start = sched_clock();
	(void)kallsyms_lookup_name(name);
	end = sched_clock();
	//Update min, max, cnt, sum
}

/*
* Traverse all symbols in sequence and collect statistics on the time
* taken by kallsyms_lookup_name() to lookup each symbol. */
kallsyms_on_each_symbol(stat_find_name, NULL);

The test results are as follows (twice):
After : min=5250, max= 726560, avg= 302132
After : min=5320, max= 726850, avg= 301978
Before: min=170, max=15949190, avg=7553906
Before: min=160, max=15877280, avg=7517784

The average time consumed is only 4.01% and the maximum time consumed is only 4.57% of the time consumed before optimization.

---
 kernel/kallsyms.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
Quoted 14 lines

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 92

Quoted 11 lines
kernel/kallsyms.c +6 -0
index 3e7e2c2ad2f75ef..2d76196cfe89f34 100644
@@ -87,6 +87,71 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
+{
+	int i, j, k, n;
+	int len, token_len;
+	const char *token;
+	unsigned char token_idx[KSYM_NAME_LEN];
+	unsigned char token_bak[KSYM_NAME_LEN];

Why do we need two buffers? It should be possible to compress the name in the same buffer as it is done in compress_symbols() in scripts/callsyms.c.

Quoted 5 lines

Because the performance would be a little better. Now this function takes just over a microsecond. Currently, it takes about 250 microseconds on average to lookup a symbol, so adding a little more time to this function doesn't affect the overall picture. I'll modify and test it as you suggest below.

We need to be careful about a stack overflow. I have seen that KSYM_NAME_LEN might need to be increased to 512 because of Rust support, see
https://lore.kernel.org/r/[email protected]

Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
kernel/kallsyms.c +0 -0
@@ -192,20 +257,28 @@ unsigned long kallsyms_lookup_name(const char *name)
 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
 
Quoted 9 lines

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 279

Quoted 5 lines
kernel/kallsyms.c +0 -3
-		if (strcmp(namebuf, name) == 0)
-			return kallsyms_sym_address(i);
-
 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
 			return kallsyms_sym_address(i);

Hmm, it means that the speedup is not usable when kernel is compiled LLVM? It might actually slow down the search because we would need to use both fast and slow search?

Quoted 8 lines

Theoretically, I don't think so. A string comparison was removed from the slow search. "if (name_len != len)" is faster than
"if (strcmp(namebuf, name) == 0)". Even if they're equal, kallsyms_compress_symbol_name() only takes 1-2us, it doesn't affect the overall picture. The average lookup time before optimization is millisecond-level.

Before: min=170, max=15949190, avg=7553906

Good point! I agree that the potential extra overhead is negligible when using the old code as a fallback.

Best Regards,
Petr

#14 lore raw

Re: [PATCH v4 4/8] kallsyms: Improve the performance of kallsyms_lookup_name()

Quoted 68 lines · Petr Mladek
Quoted 61 lines · Leizhen (ThunderTown)
Quoted 43 lines · Petr Mladek
Quoted 42 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This process can be optimized.

And now scripts/kallsyms no longer compresses the symbol types, each symbol type always occupies one byte. So we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

The pseudo code of the test case is as follows:
static int stat_find_name(...)

{
	start = sched_clock();
	(void)kallsyms_lookup_name(name);
	end = sched_clock();
	//Update min, max, cnt, sum
}

/*
* Traverse all symbols in sequence and collect statistics on the time
* taken by kallsyms_lookup_name() to lookup each symbol. */
kallsyms_on_each_symbol(stat_find_name, NULL);

The test results are as follows (twice):
After : min=5250, max= 726560, avg= 302132
After : min=5320, max= 726850, avg= 301978
Before: min=170, max=15949190, avg=7553906
Before: min=160, max=15877280, avg=7517784

The average time consumed is only 4.01% and the maximum time consumed is only 4.57% of the time consumed before optimization.

---
 kernel/kallsyms.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
Quoted 14 lines

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 92

Quoted 11 lines
kernel/kallsyms.c +6 -0
index 3e7e2c2ad2f75ef..2d76196cfe89f34 100644
@@ -87,6 +87,71 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
+{
+	int i, j, k, n;
+	int len, token_len;
+	const char *token;
+	unsigned char token_idx[KSYM_NAME_LEN];
+	unsigned char token_bak[KSYM_NAME_LEN];

Why do we need two buffers? It should be possible to compress the name in the same buffer as it is done in compress_symbols() in scripts/callsyms.c.

Quoted 5 lines

Because the performance would be a little better. Now this function takes just over a microsecond. Currently, it takes about 250 microseconds on average to lookup a symbol, so adding a little more time to this function doesn't affect the overall picture. I'll modify and test it as you suggest below.

Quoted 4 lines

We need to be careful about a stack overflow. I have seen that KSYM_NAME_LEN might need to be increased to 512 because of Rust support, see
https://lore.kernel.org/r/[email protected]

OK. Thanks for your information. I decided to add kallsyms_best_token_table[], kallsyms_best_token_table_len, so that we only need one namebuf[], like kallsyms_expand_symbol().

Quoted 31 lines
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
kernel/kallsyms.c +0 -0
@@ -192,20 +257,28 @@ unsigned long kallsyms_lookup_name(const char *name)
 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
 
Quoted 9 lines

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 279

Quoted 5 lines
kernel/kallsyms.c +0 -3
-		if (strcmp(namebuf, name) == 0)
-			return kallsyms_sym_address(i);
-
 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
 			return kallsyms_sym_address(i);

Hmm, it means that the speedup is not usable when kernel is compiled LLVM? It might actually slow down the search because we would need to use both fast and slow search?

Quoted 8 lines

Theoretically, I don't think so. A string comparison was removed from the slow search. "if (name_len != len)" is faster than
"if (strcmp(namebuf, name) == 0)". Even if they're equal, kallsyms_compress_symbol_name() only takes 1-2us, it doesn't affect the overall picture. The average lookup time before optimization is millisecond-level.

Before: min=170, max=15949190, avg=7553906

Good point! I agree that the potential extra overhead is negligible when using the old code as a fallback.

Best Regards,
Petr
.

Signature · 2 lines
Regards,
  Zhen Lei
maintainer
#15 lore raw

Re: [PATCH v4 4/8] kallsyms: Improve the performance of kallsyms_lookup_name()

Quoted 71 lines · Leizhen (ThunderTown)
Quoted 68 lines · Petr Mladek
Quoted 66 lines · Leizhen (ThunderTown)
Quoted 43 lines · Petr Mladek
Quoted 42 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This process can be optimized.

And now scripts/kallsyms no longer compresses the symbol types, each symbol type always occupies one byte. So we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

The pseudo code of the test case is as follows:
static int stat_find_name(...)

{
	start = sched_clock();
	(void)kallsyms_lookup_name(name);
	end = sched_clock();
	//Update min, max, cnt, sum
}

/*
* Traverse all symbols in sequence and collect statistics on the time
* taken by kallsyms_lookup_name() to lookup each symbol. */
kallsyms_on_each_symbol(stat_find_name, NULL);

The test results are as follows (twice):
After : min=5250, max= 726560, avg= 302132
After : min=5320, max= 726850, avg= 301978
Before: min=170, max=15949190, avg=7553906
Before: min=160, max=15877280, avg=7517784

The average time consumed is only 4.01% and the maximum time consumed is only 4.57% of the time consumed before optimization.

---
 kernel/kallsyms.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
Quoted 14 lines

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 92

Quoted 11 lines
kernel/kallsyms.c +6 -0
index 3e7e2c2ad2f75ef..2d76196cfe89f34 100644
@@ -87,6 +87,71 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
+{
+	int i, j, k, n;
+	int len, token_len;
+	const char *token;
+	unsigned char token_idx[KSYM_NAME_LEN];
+	unsigned char token_bak[KSYM_NAME_LEN];

Why do we need two buffers? It should be possible to compress the name in the same buffer as it is done in compress_symbols() in scripts/callsyms.c.

Because the performance would be a little better. Now this function takes just over a microsecond. Currently, it takes about 250 microseconds on average to lookup a symbol, so adding a little more time to this function doesn't affect the overall picture. I'll modify and test it as you suggest below.

Quoted 8 lines
Quoted 4 lines

We need to be careful about a stack overflow. I have seen that KSYM_NAME_LEN might need to be increased to 512 because of Rust support, see
https://lore.kernel.org/r/[email protected]

OK. Thanks for your information. I decided to add kallsyms_best_token_table[], kallsyms_best_token_table_len, so that we only need one namebuf[], like kallsyms_expand_symbol().

Thanks for the effort. Adding kallsyms_best_token_table[] sounds like the right solution.

Best Regards,
Petr

#16 lore raw

Re: [PATCH v4 4/8] kallsyms: Improve the performance of kallsyms_lookup_name()

In reply to Petr Mladek
Quoted 94 lines · Petr Mladek
Quoted 61 lines · Leizhen (ThunderTown)
Quoted 43 lines · Petr Mladek
Quoted 42 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This process can be optimized.

And now scripts/kallsyms no longer compresses the symbol types, each symbol type always occupies one byte. So we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

The pseudo code of the test case is as follows:
static int stat_find_name(...)

{
	start = sched_clock();
	(void)kallsyms_lookup_name(name);
	end = sched_clock();
	//Update min, max, cnt, sum
}

/*
* Traverse all symbols in sequence and collect statistics on the time
* taken by kallsyms_lookup_name() to lookup each symbol. */
kallsyms_on_each_symbol(stat_find_name, NULL);

The test results are as follows (twice):
After : min=5250, max= 726560, avg= 302132
After : min=5320, max= 726850, avg= 301978
Before: min=170, max=15949190, avg=7553906
Before: min=160, max=15877280, avg=7517784

The average time consumed is only 4.01% and the maximum time consumed is only 4.57% of the time consumed before optimization.

---
 kernel/kallsyms.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
Quoted 14 lines

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 92

Quoted 11 lines
kernel/kallsyms.c +6 -0
index 3e7e2c2ad2f75ef..2d76196cfe89f34 100644
@@ -87,6 +87,71 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
+{
+	int i, j, k, n;
+	int len, token_len;
+	const char *token;
+	unsigned char token_idx[KSYM_NAME_LEN];
+	unsigned char token_bak[KSYM_NAME_LEN];

Why do we need two buffers? It should be possible to compress the name in the same buffer as it is done in compress_symbols() in scripts/callsyms.c.

Quoted 5 lines

Because the performance would be a little better. Now this function takes just over a microsecond. Currently, it takes about 250 microseconds on average to lookup a symbol, so adding a little more time to this function doesn't affect the overall picture. I'll modify and test it as you suggest below.

We need to be careful about a stack overflow. I have seen that KSYM_NAME_LEN might need to be increased to 512 because of Rust support, see
https://lore.kernel.org/r/[email protected]

Quoted 21 lines
Quoted 4 lines
Quoted 4 lines
kernel/kallsyms.c +0 -0
@@ -192,20 +257,28 @@ unsigned long kallsyms_lookup_name(const char *name)
 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
 
Quoted 9 lines

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 279

Quoted 5 lines
kernel/kallsyms.c +0 -3
-		if (strcmp(namebuf, name) == 0)
-			return kallsyms_sym_address(i);
-
 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
 			return kallsyms_sym_address(i);

Hmm, it means that the speedup is not usable when kernel is compiled LLVM? It might actually slow down the search because we would need to use both fast and slow search?

Theoretically, I don't think so. A string comparison was removed from the slow search. "if (name_len != len)" is faster than
"if (strcmp(namebuf, name) == 0)". Even if they're equal, kallsyms_compress_symbol_name() only takes 1-2us, it doesn't affect the overall picture. The average lookup time before optimization is millisecond-level.

Quoted 4 lines
Quoted 1 line

Before: min=170, max=15949190, avg=7553906

Good point! I agree that the potential extra overhead is negligible when using the old code as a fallback.

These days sleep better. When I got up this morning, my subconscious told me that compiled LLVM could also be optimized. In fact, the method is simple, that is, check whether the next token starts with '.' or '$' after being expanded.

I will post v7 before the holidays.

Quoted 5 lines

Best Regards,
Petr
.

Signature · 2 lines
Regards,
  Zhen Lei
#17 lore raw

Re: [PATCH v4 4/8] kallsyms: Improve the performance of kallsyms_lookup_name()

Quoted 99 lines · Leizhen (ThunderTown)
Quoted 96 lines · Petr Mladek
Quoted 61 lines · Leizhen (ThunderTown)
Quoted 43 lines · Petr Mladek
Quoted 42 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This process can be optimized.

And now scripts/kallsyms no longer compresses the symbol types, each symbol type always occupies one byte. So we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

The pseudo code of the test case is as follows:
static int stat_find_name(...)

{
	start = sched_clock();
	(void)kallsyms_lookup_name(name);
	end = sched_clock();
	//Update min, max, cnt, sum
}

/*
* Traverse all symbols in sequence and collect statistics on the time
* taken by kallsyms_lookup_name() to lookup each symbol. */
kallsyms_on_each_symbol(stat_find_name, NULL);

The test results are as follows (twice):
After : min=5250, max= 726560, avg= 302132
After : min=5320, max= 726850, avg= 301978
Before: min=170, max=15949190, avg=7553906
Before: min=160, max=15877280, avg=7517784

The average time consumed is only 4.01% and the maximum time consumed is only 4.57% of the time consumed before optimization.

---
 kernel/kallsyms.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
Quoted 14 lines

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 92

Quoted 11 lines
kernel/kallsyms.c +6 -0
index 3e7e2c2ad2f75ef..2d76196cfe89f34 100644
@@ -87,6 +87,71 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
+{
+	int i, j, k, n;
+	int len, token_len;
+	const char *token;
+	unsigned char token_idx[KSYM_NAME_LEN];
+	unsigned char token_bak[KSYM_NAME_LEN];

Why do we need two buffers? It should be possible to compress the name in the same buffer as it is done in compress_symbols() in scripts/callsyms.c.

Quoted 5 lines

Because the performance would be a little better. Now this function takes just over a microsecond. Currently, it takes about 250 microseconds on average to lookup a symbol, so adding a little more time to this function doesn't affect the overall picture. I'll modify and test it as you suggest below.

We need to be careful about a stack overflow. I have seen that KSYM_NAME_LEN might need to be increased to 512 because of Rust support, see
https://lore.kernel.org/r/[email protected]

Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
kernel/kallsyms.c +0 -0
@@ -192,20 +257,28 @@ unsigned long kallsyms_lookup_name(const char *name)
 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
 
Quoted 9 lines

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 279

Quoted 5 lines
kernel/kallsyms.c +0 -3
-		if (strcmp(namebuf, name) == 0)
-			return kallsyms_sym_address(i);
-
 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
 			return kallsyms_sym_address(i);

Hmm, it means that the speedup is not usable when kernel is compiled LLVM? It might actually slow down the search because we would need to use both fast and slow search?

Theoretically, I don't think so. A string comparison was removed from the slow search. "if (name_len != len)" is faster than
"if (strcmp(namebuf, name) == 0)". Even if they're equal, kallsyms_compress_symbol_name() only takes 1-2us, it doesn't affect the overall picture. The average lookup time before optimization is millisecond-level.

Before: min=170, max=15949190, avg=7553906

Quoted 8 lines
Quoted 2 lines

Good point! I agree that the potential extra overhead is negligible when using the old code as a fallback.

These days sleep better. When I got up this morning, my subconscious told me that compiled LLVM could also be optimized. In fact, the method is simple, that is, check whether the next token starts with '.' or '$' after being expanded.

I will post v7 before the holidays.

Sorry, I'm going to break my promise. A lot of code needs to be modified on the tool side, to make sure that the first '.' or '$' will not be in the middle or end of the expanded substring. The lab is powered off, so I can only post v7 after the holidays (one week).

Quoted 7 lines
Quoted 6 lines

Best Regards,
Petr
.

Signature · 2 lines
Regards,
  Zhen Lei
#18 lore raw

Re: [PATCH v4 4/8] kallsyms: Improve the performance of kallsyms_lookup_name()

In reply to Leizhen (ThunderTown)
Quoted 150 lines · Leizhen (ThunderTown)
Quoted 51 lines · Petr Mladek
Quoted 50 lines · Zhen Lei

Currently, to search for a symbol, we need to expand the symbols in 'kallsyms_names' one by one, and then use the expanded string for comparison. This process can be optimized.

And now scripts/kallsyms no longer compresses the symbol types, each symbol type always occupies one byte. So we can first compress the searched symbol and then make a quick comparison based on the compressed length and content. In this way, for entries with mismatched lengths, there is no need to expand and compare strings. And for those matching lengths, there's no need to expand the symbol. This saves a lot of time. According to my test results, the average performance of kallsyms_lookup_name() can be improved by 20 to 30 times.

The pseudo code of the test case is as follows:
static int stat_find_name(...)

{
	start = sched_clock();
	(void)kallsyms_lookup_name(name);
	end = sched_clock();
	//Update min, max, cnt, sum
}

/*
* Traverse all symbols in sequence and collect statistics on the time
* taken by kallsyms_lookup_name() to lookup each symbol. */
kallsyms_on_each_symbol(stat_find_name, NULL);

The test results are as follows (twice):
After : min=5250, max= 726560, avg= 302132
After : min=5320, max= 726850, avg= 301978
Before: min=170, max=15949190, avg=7553906
Before: min=160, max=15877280, avg=7517784

The average time consumed is only 4.01% and the maximum time consumed is only 4.57% of the time consumed before optimization.

---
 kernel/kallsyms.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
kernel/kallsyms.c +0 -0
index 3e7e2c2ad2f75ef..2d76196cfe89f34 100644
@@ -87,6 +87,71 @@ static unsigned int kallsyms_expand_symbol(unsigned int off,
 	return off;
 }
 
Quoted 4 lines

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 90

Quoted 1 line
kernel/kallsyms.c +1 -0
+static int kallsyms_name_to_tokens(const char *name, char *buf)

This is not safe API. It is always needed to pass the size of the buffer.

OK

Quoted 8 lines

Also it should be called "compress". "token" is just an implementation detail.

I would do:

static int kallsyms_compress_symbol_name(const char *name,
char *buf, size_t size)

This's a wonderful name. Thanks.

Quoted 11 lines

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 96

Quoted 8 lines
kernel/kallsyms.c +6 -0
+{
+	int i, j, k, n;
+	int len, token_len;
+	const char *token;
+	unsigned char token_idx[KSYM_NAME_LEN];
+	unsigned char token_bak[KSYM_NAME_LEN];

Why do we need two buffers? It should be possible to compress the name in the same buffer as it is done in compress_symbols() in scripts/callsyms.c.

Because the performance would be a little better. Now this function takes just over a microsecond. Currently, it takes about 250 microseconds on average to lookup a symbol, so adding a little more time to this function doesn't affect the overall picture. I'll modify and test it as you suggest below.

Quoted 60 lines
Quoted 42 lines
kernel/kallsyms.c +41 -0
+
+	/*
+	 * n, number of tokens in the string name.
+	 * token_idx[i], the start index of the ith token.
+	 * token_idx[n] is used to calculate the length of the last token.
+	 */
+	n = strlen(name);
+	if (n >= KSYM_NAME_LEN) {
+		buf[0] = 0;
+		return 0;
+	}
+	for (i = 0; i <= n; i++)
+		token_idx[i] = (unsigned char)i;
+
+	/*
+	 * For tokens whose token_len >= 2, a larger index value indicates
+	 * a higher occurrence frequency. See scripts/kallsyms.c
+	 */
+	for (i = 255; i >= 0; i--) {
+		token = &kallsyms_token_table[kallsyms_token_index[i]];
+		token_len = strlen(token);
+		if (token_len <= 1)
+			continue;
+
+		/*
+		 * Find and merge two tokens into one.
+		 *
+		 *                |<-- new_token -->|
+		 *                | token1 | token2 |
+		 * token_idx[]:   j       j+1      j+2
+		 */
+		for (j = 0; j < n - 1; j++) {
+			len = token_idx[j + 2] - token_idx[j];
+			if (len == token_len &&
+			    !strncmp(name + token_idx[j], token, len)) {
+				token_bak[token_idx[j]] = (unsigned char)i;
+				for (k = j + 1; k < n; k++)
+					token_idx[k] = token_idx[k + 1];
+				n--;
+			}
+		}

kernel/kallsyms.c static unsigned int kallsyms_expand_symbol(unsigned int off, · line 148

Quoted 10 lines
kernel/kallsyms.c +10 -0
+	}
+
+	for (j = 0; j < n; j++) {
+		len = token_idx[j + 1] - token_idx[j];
+		if (len <= 1) {
+			buf[j] = name[token_idx[j]];
+			continue;
+		}
+
+		buf[j] = token_bak[token_idx[j]];

Maybe, I do not understand the compression format correctly but this code looks too complicated. Honestly, I even did not try to understand it.

My understanding is the we just need to find all tokens and replace them with index.

Quoted 6 lines
Quoted 4 lines

It should be even easier than compress_symbols() in scripts/callsyms.c. The token_table already exists and we do not need to handle the token_profit...

The following looks more strigtforward (not even compile tested):

OK, I will try this one. Or refer to compress_symbols() in scripts/callsyms.c.

This method won't work. Because the tokens in kallsyms_token_table[] have been expanded. For example: name = "nfs_fs_proc_net_init" kallsyms_token_table[0xf3] = "s_", raw token = 73 5f kallsyms_token_table[0x9f] = "fs_", raw token = 66 f3

After "s_" have been replaced with f3, there is no "fs_" substring in namebuf. That's why I wrote a new piece of code. Due to I didn't want to add a variable like kallsyms_token_table[].

Now, I will add kallsyms_best_token_table[], kallsyms_best_token_table_len; kallsyms_best_token_table[] does not store tokens that contain only one character. And index=0 is the token with the highest frequency.

Quoted 112 lines
Quoted 54 lines

	ssize_t len, size;

	len = strscpy(buf, symname, size);
	if (WARN_ON_ONCE(len < 0))
		return -EINVAL;

	/* the tokens with higher index are used first */
	for (idx = 255; idx >= 0; idx--) {
		token =	&kallsyms_token_table[kallsyms_token_index[i]];
		token_len = strlen(token);

		p1 = buf;
		/* length of the remaining symname including the trailing '\0' */
		remaining = len + 1;

		/* find the token in the symbol name */
		p2 = strstr(token, p1);

		while (p2) {
			/* replace token with index */
			*p2 = idx;
			remaining -= ((p2 - p1) + token_len);
			memmove(p2 + 1, p2 + token_len, remaining);
			len -= (token_len - 1);
			p1 = p2;

			/* find the token in the rest of the symbol name */
			p2 = strstr(token, p1);
		}
	}

	return len;
Quoted 19 lines
kernel/kallsyms.c +7 -0
+	}
+	buf[n] = 0;
+
+	return n;
+}
+
 /*
  * Get symbol type information. This is encoded as a single char at the
  * beginning of the symbol name.
@@ -192,20 +257,28 @@ unsigned long kallsyms_lookup_name(const char *name)
 	char namebuf[KSYM_NAME_LEN];
 	unsigned long i;
 	unsigned int off;
+	int len;
 
 	/* Skip the search for empty string. */
 	if (!*name)
 		return 0;
 
Quoted 10 lines

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 272

Quoted 7 lines
kernel/kallsyms.c +7 -0
+	len = kallsyms_name_to_tokens(name, namebuf);
+	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
+		if (kallsyms_names[off] == len + 1 &&
+		    !memcmp(&kallsyms_names[off + 2], namebuf, len))
+			return kallsyms_sym_address(i);
+
+		off += kallsyms_names[off] + 1;

These complicated checks are hard to review. The following looks much more readable to me:

Yes, it looks well.

Quoted 20 lines
	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
		/* the stored symbol name is prefixed by symbol type */
		name_len = kallsyms_names[off] - 1;
		name = &kallsyms_names[off + 2];
		off += name_len + 2;

		if (name_len != len)
			continue;

		if (!memcmp(name, namebuf, len))
			return kallsyms_sym_address(i);
	}
Quoted 5 lines
kernel/kallsyms.c +2 -0
+	}
+
 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
 
Quoted 9 lines

kernel/kallsyms.c unsigned long kallsyms_lookup_name(const char *name) · line 279

Quoted 5 lines
kernel/kallsyms.c +0 -3
-		if (strcmp(namebuf, name) == 0)
-			return kallsyms_sym_address(i);
-
 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
 			return kallsyms_sym_address(i);

Hmm, it means that the speedup is not usable when kernel is compiled LLVM? It might actually slow down the search because we would need to use both fast and slow search?

Theoretically, I don't think so. A string comparison was removed from the slow search. "if (name_len != len)" is faster than
"if (strcmp(namebuf, name) == 0)". Even if they're equal, kallsyms_compress_symbol_name() only takes 1-2us, it doesn't affect the overall picture. The average lookup time before optimization is millisecond-level. To allay your concerns, I can run a test.

Before: min=170, max=15949190, avg=7553906

Quoted 5 lines

Best Regards,
Petr
.

Signature · 2 lines
Regards,
  Zhen Lei
author
#19 lore raw

PATCH v4 5/8 kallsyms: Add helper kallsyms_on_each_match_symbol()

In reply to Zhen Lei

Function kallsyms_on_each_symbol() traverses all symbols and submits each symbol to the hook 'fn' for judgment and processing. For some cases, the hook actually only handles the matched symbol, such as livepatch.

So that, we can first compress the name being looked up and then use it for comparison when traversing 'kallsyms_names', this greatly reduces the time consumed by traversing.

The pseudo code of the test case is as follows:
static int tst_find(void *data, const char *name,
struct module *mod, unsigned long addr)

{
	if (strcmp(name, "vmap") == 0)
		*(unsigned long *)data = addr;
        return 0;
}

static int tst_match(void *data, unsigned long addr)

{
        *(unsigned long *)data = addr;
        return 0;
}

start = sched_clock(); kallsyms_on_each_match_symbol(tst_match, "vmap", &addr); end = sched_clock();

start = sched_clock();
kallsyms_on_each_symbol(tst_find, &addr);
end = sched_clock();

The test results are as follows (twice):
kallsyms_on_each_match_symbol: 557400, 583900
kallsyms_on_each_symbol : 16659500, 16113950

kallsyms_on_each_match_symbol() consumes only 3.48% of kallsyms_on_each_symbol()'s time.

---
 include/linux/kallsyms.h |  8 ++++++++
 kernel/kallsyms.c        | 25 +++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
include/linux/kallsyms.h +8 -0
index ad39636e0c3f122..f9f2cc084cab16b 100644
@@ -69,6 +69,8 @@ static inline void *dereference_symbol_descriptor(void *ptr)
 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
 				      unsigned long),
 			    void *data);
+extern int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
+					 const char *name, void *data);
 
 /* Lookup the address for a symbol. Returns 0 if not found. */
 unsigned long kallsyms_lookup_name(const char *name);
@@ -168,6 +170,12 @@ static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct
 {
 	return -EOPNOTSUPP;
 }
+
+static inline int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
+						const char *name, void *data)
+{
+	return -EOPNOTSUPP;
+}
 #endif /*CONFIG_KALLSYMS*/
 
 static inline void print_ip_sym(const char *loglvl, unsigned long ip)
kernel/kallsyms.c +25 -0
index 2d76196cfe89f34..cbcc9c560f5c188 100644
@@ -305,6 +305,31 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
 	return 0;
 }
 
+int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
+				  const char *name, void *data)
+{
+	unsigned int i, off;
+	int len, ret;
+	char namebuf[KSYM_NAME_LEN];
+
+	len = kallsyms_name_to_tokens(name, namebuf);
+	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
+		if ((i & 0xfff) == 0)
+			cond_resched();
+
+		if ((kallsyms_names[off] == len + 1) &&
+		    !memcmp(&kallsyms_names[off + 2], namebuf, len)) {
+			ret = fn(data, kallsyms_sym_address(i));
+			if (ret != 0)
+				return ret;
+			cond_resched();
+		}
+		off += kallsyms_names[off] + 1;
+	}
+
+	return 0;
+}
+
 static unsigned long get_symbol_pos(unsigned long addr,
 				    unsigned long *symbolsize,
 				    unsigned long *offset)
Signature · 1 line
2.25.1
maintainer
#20 lore raw

Re: [PATCH v4 5/8] kallsyms: Add helper kallsyms_on_each_match_symbol()

Quoted 54 lines · Zhen Lei

Function kallsyms_on_each_symbol() traverses all symbols and submits each symbol to the hook 'fn' for judgment and processing. For some cases, the hook actually only handles the matched symbol, such as livepatch.

So that, we can first compress the name being looked up and then use it for comparison when traversing 'kallsyms_names', this greatly reduces the time consumed by traversing.

The pseudo code of the test case is as follows:
static int tst_find(void *data, const char *name,
struct module *mod, unsigned long addr)

{
	if (strcmp(name, "vmap") == 0)
		*(unsigned long *)data = addr;
        return 0;
}

static int tst_match(void *data, unsigned long addr)

{
        *(unsigned long *)data = addr;
        return 0;
}

start = sched_clock(); kallsyms_on_each_match_symbol(tst_match, "vmap", &addr); end = sched_clock();

start = sched_clock();
kallsyms_on_each_symbol(tst_find, &addr);
end = sched_clock();

The test results are as follows (twice):
kallsyms_on_each_match_symbol: 557400, 583900
kallsyms_on_each_symbol : 16659500, 16113950

kallsyms_on_each_match_symbol() consumes only 3.48% of kallsyms_on_each_symbol()'s time.

kernel/kallsyms.c +10 -0
@@ -305,6 +305,31 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
 	return 0;
 }
 
+int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
+				  const char *name, void *data)
+{
+	unsigned int i, off;
+	int len, ret;
+	char namebuf[KSYM_NAME_LEN];
+
+	len = kallsyms_name_to_tokens(name, namebuf);
+	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
+		if ((i & 0xfff) == 0)

kernel/kallsyms.c int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, · line 327

Quoted 10 lines
kernel/kallsyms.c +10 -0
+			cond_resched();
+
+		if ((kallsyms_names[off] == len + 1) &&
+		    !memcmp(&kallsyms_names[off + 2], namebuf, len)) {
+			ret = fn(data, kallsyms_sym_address(i));
+			if (ret != 0)
+				return ret;
+			cond_resched();
+		}
+		off += kallsyms_names[off] + 1;

Similar tricky code is used also in kallsyms_lookup_name(). Please, avoid code duplication and put this into a helper funtion.

Best Regards,
Petr

Quoted 10 lines
kernel/kallsyms.c +5 -0
+	}
+
+	return 0;
+}
+
 static unsigned long get_symbol_pos(unsigned long addr,
 				    unsigned long *symbolsize,
 				    unsigned long *offset)
Signature · 1 line
2.25.1
#21 lore raw

Re: [PATCH v4 5/8] kallsyms: Add helper kallsyms_on_each_match_symbol()

Quoted 60 lines · Petr Mladek
Quoted 59 lines · Zhen Lei

Function kallsyms_on_each_symbol() traverses all symbols and submits each symbol to the hook 'fn' for judgment and processing. For some cases, the hook actually only handles the matched symbol, such as livepatch.

So that, we can first compress the name being looked up and then use it for comparison when traversing 'kallsyms_names', this greatly reduces the time consumed by traversing.

The pseudo code of the test case is as follows:
static int tst_find(void *data, const char *name,
struct module *mod, unsigned long addr)

{
	if (strcmp(name, "vmap") == 0)
		*(unsigned long *)data = addr;
        return 0;
}

static int tst_match(void *data, unsigned long addr)

{
        *(unsigned long *)data = addr;
        return 0;
}

start = sched_clock(); kallsyms_on_each_match_symbol(tst_match, "vmap", &addr); end = sched_clock();

start = sched_clock();
kallsyms_on_each_symbol(tst_find, &addr);
end = sched_clock();

The test results are as follows (twice):
kallsyms_on_each_match_symbol: 557400, 583900
kallsyms_on_each_symbol : 16659500, 16113950

kallsyms_on_each_match_symbol() consumes only 3.48% of kallsyms_on_each_symbol()'s time.

kernel/kallsyms.c +15 -0
@@ -305,6 +305,31 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
 	return 0;
 }
 
+int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
+				  const char *name, void *data)
+{
+	unsigned int i, off;
+	int len, ret;
+	char namebuf[KSYM_NAME_LEN];
+
+	len = kallsyms_name_to_tokens(name, namebuf);
+	for (i = 0, off = 0; len && i < kallsyms_num_syms; i++) {
+		if ((i & 0xfff) == 0)
+			cond_resched();
+
+		if ((kallsyms_names[off] == len + 1) &&
+		    !memcmp(&kallsyms_names[off + 2], namebuf, len)) {
+			ret = fn(data, kallsyms_sym_address(i));
Quoted 8 lines

kernel/kallsyms.c int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, · line 327

Quoted 5 lines
kernel/kallsyms.c +5 -0
+			if (ret != 0)
+				return ret;
+			cond_resched();
+		}
+		off += kallsyms_names[off] + 1;

Similar tricky code is used also in kallsyms_lookup_name(). Please, avoid code duplication and put this into a helper funtion.

Okay, I'll try it.

Quoted 16 lines

Best Regards,
Petr

Quoted 10 lines
kernel/kallsyms.c +5 -0
+	}
+
+	return 0;
+}
+
 static unsigned long get_symbol_pos(unsigned long addr,
 				    unsigned long *symbolsize,
 				    unsigned long *offset)
Signature · 1 line
2.25.1

.

Signature · 2 lines
Regards,
  Zhen Lei
author
#22 lore raw

PATCH v4 6/8 livepatch: Use kallsyms_on_each_match_symbol() to improve performance

In reply to Zhen Lei

Based on the test results of kallsyms_on_each_match_symbol() and kallsyms_on_each_symbol(), the average performance can be improved by 20 to 30 times.

---
 kernel/livepatch/core.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
kernel/livepatch/core.c +19 -1
index 42f7e716d56bf72..31b57ccf908017e 100644
@@ -153,6 +153,24 @@ static int klp_find_callback(void *data, const char *name,
 	return 0;
 }
 
+static int klp_match_callback(void *data, unsigned long addr)
+{
+	struct klp_find_arg *args = data;
+
+	args->addr = addr;
+	args->count++;
+
+	/*
+	 * Finish the search when the symbol is found for the desired position
+	 * or the position is not defined for a non-unique symbol.
+	 */
+	if ((args->pos && (args->count == args->pos)) ||
+	    (!args->pos && (args->count > 1)))
+		return 1;
+
+	return 0;
+}
+
 static int klp_find_object_symbol(const char *objname, const char *name,
 				  unsigned long sympos, unsigned long *addr)
 {
@@ -167,7 +185,7 @@ static int klp_find_object_symbol(const char *objname, const char *name,
 	if (objname)
 		module_kallsyms_on_each_symbol(klp_find_callback, &args);
 	else
-		kallsyms_on_each_symbol(klp_find_callback, &args);
+		kallsyms_on_each_match_symbol(klp_match_callback, name, &args);
 
 	/*
 	 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
Signature · 1 line
2.25.1
author
#23 lore raw

PATCH v4 7/8 livepatch: Improve the search performance of module_kallsyms_on_each_symbol()

In reply to Zhen Lei

Currently we traverse all symbols of all modules to find the specified function for the specified module. But in reality, we just need to find the given module and then traverse all the symbols in it.

In order to achieve this purpose, split the call to hook 'fn' into two phases:
1. Finds the given module. Pass pointer 'mod'. Hook 'fn' directly returns the comparison result of the module name without comparing the function name.
2. Finds the given function in that module. Pass pointer 'mod = NULL'. Hook 'fn' skip the comparison of module name and directly compare function names.

Phase1: mod1-->mod2..(subsequent modules do not need to be compared)

                |

Phase2: -->f1-->f2-->f3

---
 kernel/livepatch/core.c  |  7 ++-----
 kernel/module/kallsyms.c | 13 ++++++++++++-
 2 files changed, 14 insertions(+), 6 deletions(-)
kernel/livepatch/core.c +2 -5
index 31b57ccf908017e..98e23137e4133bc 100644
@@ -130,15 +130,12 @@ static int klp_find_callback(void *data, const char *name,
 {
 	struct klp_find_arg *args = data;
 
-	if ((mod && !args->objname) || (!mod && args->objname))
-		return 0;
+	if (mod)
+		return strcmp(args->objname, mod->name);
 
 	if (strcmp(args->name, name))
 		return 0;
 
-	if (args->objname && strcmp(args->objname, mod->name))
-		return 0;
-
 	args->addr = addr;
 	args->count++;
 
kernel/module/kallsyms.c +12 -1
index f5c5c9175333df7..b033613e6c7e3bb 100644
@@ -510,6 +510,11 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
 		if (mod->state == MODULE_STATE_UNFORMED)
 			continue;
 
+		/* check mod->name first */
+		ret = fn(data, NULL, mod, 0);
+		if (ret)
+			continue;
+
 		/* Use rcu_dereference_sched() to remain compliant with the sparse tool */
 		preempt_disable();
 		kallsyms = rcu_dereference_sched(mod->kallsyms);
@@ -522,10 +527,16 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
 				continue;
 
 			ret = fn(data, kallsyms_symbol_name(kallsyms, i),
-				 mod, kallsyms_symbol_value(sym));
+				 NULL, kallsyms_symbol_value(sym));
 			if (ret != 0)
 				goto out;
 		}
+
+		/*
+		 * The given module is found, the subsequent modules do not
+		 * need to be compared.
+		 */
+		break;
 	}
 out:
 	mutex_unlock(&module_mutex);
Signature · 1 line
2.25.1
author
#24 lore raw

PATCH v4 8/8 kallsyms: Add self-test facility

In reply to Zhen Lei

Added test cases for basic functions and performance of functions kallsyms_lookup_name(), kallsyms_on_each_symbol() and kallsyms_on_each_match_symbol(). It also calculates the compression rate of the kallsyms compression algorithm for the current symbol set.

The basic functions test begins by testing a set of symbols whose address values are known. Then, traverse all symbol addresses and find the corresponding symbol name based on the address. It's impossible to determine whether these addresses are correct, but we can use the above three functions along with the addresses to test each other. Due to the traversal operation of kallsyms_on_each_symbol() is too slow, only 60 symbols can be tested in one second, so let it test on average once every 128 symbols. The other two functions validate all symbols.

If the basic functions test is passed, print only performance test results. If the test fails, print error information, but do not perform subsequent performance tests.

Start self-test automatically after system startup if CONFIG_KALLSYMS_SELFTEST=y.

Example of output content: (prefix 'kallsyms_selftest:' is omitted)
start
---------------------------------------------------------

 | nr_symbols | compressed size | original size | ratio(%) |
 |---------------------------------------------------------|
 |     174099 |       1960154   |      3750756  |  52.26   |

---------------------------------------------------------
kallsyms_lookup_name() looked up 174099 symbols
The time spent on each symbol is (ns): min=5250, max=726560, avg=302132 kallsyms_on_each_symbol() traverse all: 16659500 ns kallsyms_on_each_match_symbol() traverse all: 557400 ns finish

---
 include/linux/kallsyms.h   |   1 +
 init/Kconfig               |  13 ++
 kernel/Makefile            |   1 +
 kernel/kallsyms.c          |   2 +-
 kernel/kallsyms_selftest.c | 423 +++++++++++++++++++++++++++++++++++++
 5 files changed, 439 insertions(+), 1 deletion(-)
 create mode 100644 kernel/kallsyms_selftest.c
include/linux/kallsyms.h +1 -0
index f9f2cc084cab16b..b46e1d8b0c18316 100644
@@ -66,6 +66,7 @@ static inline void *dereference_symbol_descriptor(void *ptr)
 }
 
 #ifdef CONFIG_KALLSYMS
+unsigned long kallsyms_sym_address(int idx);
 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
 				      unsigned long),
 			    void *data);
init/Kconfig +13 -0
index 532362fcfe31fd3..60193fd185fb6e6 100644
@@ -1716,6 +1716,19 @@ config KALLSYMS
 	  symbolic stack backtraces. This increases the size of the kernel
 	  somewhat, as all symbols have to be loaded into the kernel image.
 
+config KALLSYMS_SELFTEST
+	bool "Test the basic functions and performance of kallsyms"
+	depends on KALLSYMS
+	default n
+	help
+	  Test the basic functions and performance of some interfaces, such as
+	  kallsyms_lookup_name. It also calculates the compression rate of the
+	  kallsyms compression algorithm for the current symbol set.
+
+	  Start self-test automatically after system startup. Suggest executing
+	  "dmesg | grep kallsyms_selftest" to collect test results. "finish" is
+	  displayed in the last line, indicating that the test is complete.
+
 config KALLSYMS_ALL
 	bool "Include all symbols in kallsyms"
 	depends on DEBUG_KERNEL && KALLSYMS
kernel/Makefile +1 -0
index 318789c728d3290..122a5fed457bd98 100644
@@ -68,6 +68,7 @@ endif
 obj-$(CONFIG_UID16) += uid16.o
 obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
+obj-$(CONFIG_KALLSYMS_SELFTEST) += kallsyms_selftest.o
 obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
 obj-$(CONFIG_CRASH_CORE) += crash_core.o
 obj-$(CONFIG_KEXEC_CORE) += kexec_core.o
kernel/kallsyms.c +1 -1
index cbcc9c560f5c188..34e306eecbb60c8 100644
@@ -193,7 +193,7 @@ static unsigned int get_symbol_offset(unsigned long pos)
 	return name - kallsyms_names;
 }
 
-static unsigned long kallsyms_sym_address(int idx)
+unsigned long kallsyms_sym_address(int idx)
 {
 	if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
 		return kallsyms_addresses[idx];
kernel/kallsyms_selftest.c new file +423 -0

423 lines not on this page · raw message

Signature · 1 line
2.25.1