From patchwork Mon Jan 18 13:00:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sumit Garg X-Patchwork-Id: 365531 Delivered-To: patches@linaro.org Received: by 2002:a02:ccad:0:0:0:0:0 with SMTP id t13csp2237257jap; Mon, 18 Jan 2021 05:00:44 -0800 (PST) X-Received: by 2002:a17:90a:6c90:: with SMTP id y16mr26945237pjj.129.1610974843930; Mon, 18 Jan 2021 05:00:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610974843; cv=none; d=google.com; s=arc-20160816; b=URhlE5tikjJifpziMvzh5L9d37pjchhBuEsgdmzgtTCq9LZTacggeHW1K7z2wvr70Q wBCk+8icbPq2OOzyF8ev+q3pP8c/VPU4RZ+z43oNNeKwd8KKvUTI2GYLnKK9e/RzFkg2 xFicHmib/D8lTGlLQeoXngqyhH9F6lIrDqj5dffHxyLXrCRguWvomV5Rvp8DHuuHpy9c ua59sXMQnljECujqesDHu/OaIbCzJnM6uYJw+4ErZH+Z7tHPH7y66UNZSxr+OK0J5dka 2VNnQa6T22SapWnpilc9XO6/htnjn0zy1YAcvJY4m+76qNg69YmRonicIkxGQ0CjcBv+ JX6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature; bh=gTVBmM4UNHvaEJIDwReFHkZoSU7qflf4NSZK9lOnjg4=; b=edkN0x0oBzL16i6dx39UHeTCheJTAbhLZWekjk1oWIhNhnczJu7/heN4g30Gz0uS+h AFDY7PVScwrsxY3hzazcTOMfdZxcBuHBkIkY9Z4Fi78HlQNKrn0tgQBicYq4J8i2YZtA Mk6nhK15lK7D5N4K3ylmvUNYbesBVEgtZMaRGB/s96ZGG95wItoh9j1DmIndPyET9+D8 X+F9Y50P0nxflfHZOSScZaKhsAbWEyjtDe0UubEVg61SlK0XZzcjVSJ3769BHck8q1La K94xKb7TCUUOyd3Q6crBH5bOTaBVudLRWOuxFoN1nGRr89Jf2FqaFb/yOsZiWCBNDBDi f3kA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JCAaMyym; spf=pass (google.com: domain of sumit.garg@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=sumit.garg@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id 2sor5763794plj.11.2021.01.18.05.00.43 for (Google Transport Security); Mon, 18 Jan 2021 05:00:43 -0800 (PST) Received-SPF: pass (google.com: domain of sumit.garg@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JCAaMyym; spf=pass (google.com: domain of sumit.garg@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=sumit.garg@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=gTVBmM4UNHvaEJIDwReFHkZoSU7qflf4NSZK9lOnjg4=; b=JCAaMyymeEv6y5ERLnpX56fQQoHg9K106E5iSk1mmHzzKSpfW6k/LckW/ENdyuP1Xh lAn6xvZZVSWYMH+S4jQuWMVAADDm3iEX+JyoFbwf0PtNnMYSZNFIJ9UOJm6D5VuoJmNm akUK0f+9EiUaRu0r2yjxkJkY8wzD4xgT/rkGJVRdD0F+kL1bJb4OLS2hFivYLQbQOuPP MoMTHVWKv+FdRs5iwpmRUjy+iseJsT6z5bHYwBPu01g6gOrgwY/1vIrZQUqO8h/3I54w wmIpcy8j3zHE+5BQQPpAdqvP9RelWTcGUAR+8R3xQpQg3yVXHt+Jz3Qy5iHzQ5GEoWER amtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=gTVBmM4UNHvaEJIDwReFHkZoSU7qflf4NSZK9lOnjg4=; b=QCGWiSimpL88rtJSz5vMfqGm8J2OqSdJKzEd4Fb5/Vz9D8maGfKIByVd7b/JFWbUWv vT6uGNAgy/tXRYa9wsZtTI9XRZSPdkj3w0lQqZ53+3OHb4MI8FiNmPiiqfxOKppPv73m GLezFRvxRkarvUgtIWdWwrXH1sYjPT0+1GumNcLdyNNlOJncPWlQc4YZsIbDAAfmnYBZ 8qmb0ljqo/wKeELxTYm+CtoQKPxf17bfnJeG2Izzr5f87M3x9jbpRNdiTzGupC4JjrOt zG48YVheZAbEBbqLFajECNXv3n4KsWWXuwz8tDj7hOvmf08F4WrkAjAFxljbOXTzRxDO RC1g== X-Gm-Message-State: AOAM530kwddhGXPfuVjsryPCdpB2aSpnu9G+SAlOTpT6SRVUrUpeLICa 5s/RMFYAb1coWWK23sJjN/Hr9QRz X-Google-Smtp-Source: ABdhPJy68Foti0KdcCKdswLThXW+SEwUiteH13rG7AWsSlQYDzmnmQUwuYkwxg3nKPBFInlozLKS5g== X-Received: by 2002:a17:902:dc83:b029:de:8484:7de with SMTP id n3-20020a170902dc83b02900de848407demr13571102pld.11.1610974843114; Mon, 18 Jan 2021 05:00:43 -0800 (PST) Return-Path: Received: from localhost.localdomain ([122.181.110.213]) by smtp.gmail.com with ESMTPSA id e72sm8106050pfh.199.2021.01.18.05.00.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Jan 2021 05:00:42 -0800 (PST) From: Sumit Garg To: daniel.thompson@linaro.org Cc: patches@linaro.org, Sumit Garg Subject: [RFC INTERNAL] kdb: Use linked list instead of static array for commands Date: Mon, 18 Jan 2021 18:30:13 +0530 Message-Id: <1610974813-22022-1-git-send-email-sumit.garg@linaro.org> X-Mailer: git-send-email 2.7.4 Simplify kdb commands registration via using linked list instead of static array for commands storage. Signed-off-by: Sumit Garg --- Hi Daniel, This is my intial step towards kdb code refactoring. Please do let me know if I am headed in the right direction. -Sumit kernel/debug/kdb/kdb_main.c | 78 ++++++++++-------------------------------- kernel/debug/kdb/kdb_private.h | 1 + 2 files changed, 20 insertions(+), 59 deletions(-) -- 2.7.4 diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c index 930ac1b..93ac0f5 100644 --- a/kernel/debug/kdb/kdb_main.c +++ b/kernel/debug/kdb/kdb_main.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -84,15 +85,8 @@ static unsigned int kdb_continue_catastrophic = static unsigned int kdb_continue_catastrophic; #endif -/* kdb_commands describes the available commands. */ -static kdbtab_t *kdb_commands; -#define KDB_BASE_CMD_MAX 50 -static int kdb_max_commands = KDB_BASE_CMD_MAX; -static kdbtab_t kdb_base_commands[KDB_BASE_CMD_MAX]; -#define for_each_kdbcmd(cmd, num) \ - for ((cmd) = kdb_base_commands, (num) = 0; \ - num < kdb_max_commands; \ - num++, num == KDB_BASE_CMD_MAX ? cmd = kdb_commands : cmd++) +/* kdb_cmds_head describes the available commands. */ +static LIST_HEAD(kdb_cmds_head); typedef struct _kdbmsg { int km_diag; /* kdb diagnostic */ @@ -921,7 +915,7 @@ int kdb_parse(const char *cmdstr) char *cp; char *cpp, quoted; kdbtab_t *tp; - int i, escaped, ignore_errors = 0, check_grep = 0; + int escaped, ignore_errors = 0, check_grep = 0; /* * First tokenize the command string. @@ -1011,7 +1005,7 @@ int kdb_parse(const char *cmdstr) ++argv[0]; } - for_each_kdbcmd(tp, i) { + list_for_each_entry(tp, &kdb_cmds_head, list_node) { if (tp->cmd_name) { /* * If this command is allowed to be abbreviated, @@ -1037,8 +1031,8 @@ int kdb_parse(const char *cmdstr) * few characters of this match any of the known commands. * e.g., md1c20 should match md. */ - if (i == kdb_max_commands) { - for_each_kdbcmd(tp, i) { + if (list_entry_is_head(tp, &kdb_cmds_head, list_node)) { + list_for_each_entry(tp, &kdb_cmds_head, list_node) { if (tp->cmd_name) { if (strncmp(argv[0], tp->cmd_name, @@ -1049,7 +1043,7 @@ int kdb_parse(const char *cmdstr) } } - if (i < kdb_max_commands) { + if (!list_entry_is_head(tp, &kdb_cmds_head, list_node)) { int result; if (!kdb_check_flags(tp->cmd_flags, kdb_cmd_enabled, argc <= 1)) @@ -2428,12 +2422,11 @@ static int kdb_kgdb(int argc, const char **argv) static int kdb_help(int argc, const char **argv) { kdbtab_t *kt; - int i; kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description"); kdb_printf("-----------------------------" "-----------------------------\n"); - for_each_kdbcmd(kt, i) { + list_for_each_entry(kt, &kdb_cmds_head, list_node) { char *space = ""; if (KDB_FLAG(CMD_INTERRUPT)) return 0; @@ -2667,13 +2660,9 @@ int kdb_register_flags(char *cmd, short minlen, kdb_cmdflags_t flags) { - int i; kdbtab_t *kp; - /* - * Brute force method to determine duplicates - */ - for_each_kdbcmd(kp, i) { + list_for_each_entry(kp, &kdb_cmds_head, list_node) { if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) { kdb_printf("Duplicate kdb command registered: " "%s, func %px help %s\n", cmd, func, help); @@ -2681,35 +2670,10 @@ int kdb_register_flags(char *cmd, } } - /* - * Insert command into first available location in table - */ - for_each_kdbcmd(kp, i) { - if (kp->cmd_name == NULL) - break; - } - - if (i >= kdb_max_commands) { - kdbtab_t *new = kmalloc_array(kdb_max_commands - - KDB_BASE_CMD_MAX + - kdb_command_extend, - sizeof(*new), - GFP_KDB); - if (!new) { - kdb_printf("Could not allocate new kdb_command " - "table\n"); - return 1; - } - if (kdb_commands) { - memcpy(new, kdb_commands, - (kdb_max_commands - KDB_BASE_CMD_MAX) * sizeof(*new)); - kfree(kdb_commands); - } - memset(new + kdb_max_commands - KDB_BASE_CMD_MAX, 0, - kdb_command_extend * sizeof(*new)); - kdb_commands = new; - kp = kdb_commands + kdb_max_commands - KDB_BASE_CMD_MAX; - kdb_max_commands += kdb_command_extend; + kp = kmalloc(sizeof(*kp), GFP_KDB); + if (!kp) { + kdb_printf("Could not allocate new kdb_command table\n"); + return 1; } kp->cmd_name = cmd; @@ -2719,6 +2683,8 @@ int kdb_register_flags(char *cmd, kp->cmd_minlen = minlen; kp->cmd_flags = flags; + list_add_tail(&kp->list_node, &kdb_cmds_head); + return 0; } EXPORT_SYMBOL_GPL(kdb_register_flags); @@ -2757,15 +2723,15 @@ EXPORT_SYMBOL_GPL(kdb_register); */ int kdb_unregister(char *cmd) { - int i; kdbtab_t *kp; /* * find the command. */ - for_each_kdbcmd(kp, i) { + list_for_each_entry(kp, &kdb_cmds_head, list_node) { if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) { - kp->cmd_name = NULL; + list_del(&kp->list_node); + kfree(kp); return 0; } } @@ -2778,12 +2744,6 @@ EXPORT_SYMBOL_GPL(kdb_unregister); /* Initialize the kdb command table. */ static void __init kdb_inittab(void) { - int i; - kdbtab_t *kp; - - for_each_kdbcmd(kp, i) - kp->cmd_name = NULL; - kdb_register_flags("md", kdb_md, "", "Display Memory Contents, also mdWcN, e.g. md8c1", 1, KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS); diff --git a/kernel/debug/kdb/kdb_private.h b/kernel/debug/kdb/kdb_private.h index a4281fb..7a4a181 100644 --- a/kernel/debug/kdb/kdb_private.h +++ b/kernel/debug/kdb/kdb_private.h @@ -174,6 +174,7 @@ typedef struct _kdbtab { short cmd_minlen; /* Minimum legal # command * chars required */ kdb_cmdflags_t cmd_flags; /* Command behaviour flags */ + struct list_head list_node; } kdbtab_t; extern int kdb_bt(int, const char **); /* KDB display back trace */