From: Nicolas Schier <n.schier@avm.de>
To: Masahiro Yamada <masahiroy@kernel.org>
Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
Boqun Feng <boqun.feng@gmail.com>,
Miguel Ojeda <ojeda@kernel.org>
Subject: Re: [PATCH] scripts/kallsyms: remove KSYM_NAME_LEN_BUFFER
Date: Tue, 6 Jun 2023 09:59:34 +0200 [thread overview]
Message-ID: <ZH7nZpukq8lstUIh@buildd.core.avm.de> (raw)
In-Reply-To: <20230605122604.1826856-1-masahiroy@kernel.org>
On Mon, Jun 05, 2023 at 09:26:04PM +0900, Masahiro Yamada wrote:
> You do not need to decide the buffer size statically.
>
> Use getline() to grow the line buffer as needed.
>
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
>
> scripts/kallsyms.c | 61 ++++++++++++++++++++++++----------------------
> 1 file changed, 32 insertions(+), 29 deletions(-)
>
> diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
> index 0d2db41177b2..fc9709839b19 100644
> --- a/scripts/kallsyms.c
> +++ b/scripts/kallsyms.c
> @@ -19,6 +19,7 @@
> *
> */
>
> +#include <errno.h>
> #include <getopt.h>
> #include <stdbool.h>
> #include <stdio.h>
> @@ -29,24 +30,8 @@
>
> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
>
> -#define _stringify_1(x) #x
> -#define _stringify(x) _stringify_1(x)
> -
> #define KSYM_NAME_LEN 512
>
> -/*
> - * A substantially bigger size than the current maximum.
> - *
> - * It cannot be defined as an expression because it gets stringified
> - * for the fscanf() format string. Therefore, a _Static_assert() is
> - * used instead to maintain the relationship with KSYM_NAME_LEN.
> - */
> -#define KSYM_NAME_LEN_BUFFER 2048
> -_Static_assert(
> - KSYM_NAME_LEN_BUFFER == KSYM_NAME_LEN * 4,
> - "Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN"
> -);
> -
> struct sym_entry {
> unsigned long long addr;
> unsigned int len;
> @@ -136,24 +121,40 @@ static void check_symbol_range(const char *sym, unsigned long long addr,
> }
> }
>
> -static struct sym_entry *read_symbol(FILE *in)
> +static struct sym_entry *read_symbol(FILE *in, char **buf, size_t *buf_len)
> {
> - char name[KSYM_NAME_LEN_BUFFER+1], type;
> + char *name, type, *p;
> unsigned long long addr;
> - unsigned int len;
> + size_t len;
> + ssize_t readlen;
> struct sym_entry *sym;
> - int rc;
>
> - rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name);
> - if (rc != 3) {
> - if (rc != EOF && fgets(name, ARRAY_SIZE(name), in) == NULL)
> - fprintf(stderr, "Read error or end of file.\n");
> + readlen = getline(buf, buf_len, in);
> + if (readlen < 0) {
> + if (errno) {
> + perror("read_symbol");
> + exit(EXIT_FAILURE);
> + }
> return NULL;
> }
> - if (strlen(name) >= KSYM_NAME_LEN) {
> +
> + if ((*buf)[readlen - 1] == '\n')
> + (*buf)[readlen - 1] = 0;
> +
> + addr = strtoull(*buf, &p, 16);
> +
> + if (*buf == p || *p++ != ' ' || !isascii((type = *p++)) || *p++ != ' ') {
For me, this is not as easy to read as the previous fscanf(), but I like
the switch to getline().
Reviewed-by: Nicolas Schier <n.schier@avm.de>
> + fprintf(stderr, "line format error\n");
> + exit(EXIT_FAILURE);
> + }
> +
> + name = p;
> + len = strlen(name);
> +
> + if (len >= KSYM_NAME_LEN) {
> fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
> "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
> - name, strlen(name), KSYM_NAME_LEN);
> + name, len, KSYM_NAME_LEN);
> return NULL;
> }
>
> @@ -169,8 +170,7 @@ static struct sym_entry *read_symbol(FILE *in)
>
> /* include the type field in the symbol name, so that it gets
> * compressed together */
> -
> - len = strlen(name) + 1;
> + len++;
>
> sym = malloc(sizeof(*sym) + len + 1);
> if (!sym) {
> @@ -257,6 +257,8 @@ static void read_map(const char *in)
> {
> FILE *fp;
> struct sym_entry *sym;
> + char *buf = NULL;
> + size_t buflen = 0;
>
> fp = fopen(in, "r");
> if (!fp) {
> @@ -265,7 +267,7 @@ static void read_map(const char *in)
> }
>
> while (!feof(fp)) {
> - sym = read_symbol(fp);
> + sym = read_symbol(fp, &buf, &buflen);
> if (!sym)
> continue;
>
> @@ -284,6 +286,7 @@ static void read_map(const char *in)
> table[table_cnt++] = sym;
> }
>
> + free(buf);
> fclose(fp);
> }
>
> --
> 2.39.2
>
prev parent reply other threads:[~2023-06-06 8:02 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-05 12:26 [PATCH] scripts/kallsyms: remove KSYM_NAME_LEN_BUFFER Masahiro Yamada
2023-06-06 7:59 ` Nicolas Schier [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZH7nZpukq8lstUIh@buildd.core.avm.de \
--to=n.schier@avm.de \
--cc=boqun.feng@gmail.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=masahiroy@kernel.org \
--cc=ojeda@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox