Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7272235pxb; Thu, 18 Feb 2021 06:07:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJyP4hHhIwQoReAPnf2cb11nYQdFI1pMofShf6e76RZ+PbpG/CixuKJZxjS6tt4vxdYpPtHa X-Received: by 2002:a17:907:770d:: with SMTP id kw13mr4214027ejc.219.1613657263257; Thu, 18 Feb 2021 06:07:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613657263; cv=none; d=google.com; s=arc-20160816; b=ymAxPR0QiqtqK5VjlKp/WunAO6OW69z/UV3+QE/LFDM9uW3b6TWxVO8y7oG2fF3eho YfApg1OzMjUzdkXVGszdtXSNa+vLCK+qtCsYt1xPmLsLBD9pLGB0ObFvZmxV7bDD8tPN lAemg+vpw9uVuZ03GGbmx4SiEtSjBPskAuJXlVxWk7vKxG4TKOPcS+Pgp9a3sVp9gf4T /UiqgEDDE+IWogH1pJtXJEEzU/H8896o5MbWoBrk7S32Qg4Sj9FguWGFK+hV1cbdlias dZCENuPGPLQibwIZ3b99fzC1rGBkH2Ej+P9gIvD5cNB1NF2FdRPM6YHJtNVc/BIKhqtl NTZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=xyXPn6tY0vq5dxBTZ3O8QAETE7cABhqacxx+g75wB8w=; b=h7yjVgyPgElu1qClPZUKEjGoDY30uW+4Vj0lx0rEuOFKmCFewnFvVsKF+PnoJqMq3d 5kfwBMqht40P4eAzNr1bkIYAqu/aXeALrbgQd9Unhzk7qzLkvXnmYEcRMPK92z10UMUX egzU+4axgE2jc3b8MKhq5vNRjZmnyZPXm3xqCgQDw9C+qvgDOcJJ8RBrC82X/vLaefvW wC/ycPbaaHRrvTPRovlmGbIqPzFp0+LEfiTKtp/tjuRJEcXz3l5xNuRIgU3PggFuIsX+ 1EaFst9+/50zgQUIY7XB4Lyl6qrtfJbqNjjTA6sRMqEQE320fVFNXZLQWAHtyKiUA/4Q hTVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CutVTw7u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f20si3575743ejk.711.2021.02.18.06.07.17; Thu, 18 Feb 2021 06:07:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CutVTw7u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233016AbhBROD1 (ORCPT + 99 others); Thu, 18 Feb 2021 09:03:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232500AbhBRMLD (ORCPT ); Thu, 18 Feb 2021 07:11:03 -0500 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F31CCC061574 for ; Thu, 18 Feb 2021 04:10:22 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id u143so1155557pfc.7 for ; Thu, 18 Feb 2021 04:10:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=xyXPn6tY0vq5dxBTZ3O8QAETE7cABhqacxx+g75wB8w=; b=CutVTw7uYhIpp5cmmTIC1QFr6S3NW0wxXqNOqnLDnxROFygpNA9XxZj8+urr8mtD6q bDewKsleQo2S3Gd+PUg5EhYxtToNofrAT7cI/XtOtdfHzAq9SEY0vnUecpgQ1Z1uAUVM ppiH7mpZ8MEcpqZ/AzX3hr2pAPSyU47g1C0LT436IsKiONJXR2ujyiyd1sBy97c4f6Sw kavYG8BoT34WxR2noDnT4QETpps1oQMEijTXl77EnHmK9Vl8IRRdRWaj6l1E4PI0OcLB oZ6IvjzlBNy4O17dl42ujCFD6/l7/qWcQe7JGdALtszUqDago4/fSJ0bt2izYL+yFdMN MunQ== 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=xyXPn6tY0vq5dxBTZ3O8QAETE7cABhqacxx+g75wB8w=; b=RtBKgkkA54bZrIJmMrBHgx4nZazEliBNsmBlpoArhmWh5jJ182oKYx9uailSLbszaY cElcMyktr4Gc/oWMX1LhaRiHMbrbKPtOkpz3XSInjt9qgWRUaTEJ+qC2B5dppIrPMjhS yQw0zZEjqarZK7vx9/xl0Wev2QAjfKA/kAl5giiibpf6rvgC8tCh3rqZr7WgudkiogeV jUp1OoZc84F+CJuSB8jvCQO0GEyLvwjEEHLRTKQXzMD1xoDAcgE4aI14De7sm63B5rE5 OumeH3TTs4rEQQ5glkYhypry6mgD0lWfnEwGE3Y7t/rb6fDhs00bJrcuVC3qJ2xc56gT /p/w== X-Gm-Message-State: AOAM5318DoMcCUSMooo3UtquVehJ6UcAB7bVEVpUUew/NRxG7KeJmR7l NzHA0xLN7SGGp6m+SaBOo68hEA== X-Received: by 2002:a63:3404:: with SMTP id b4mr1146124pga.105.1613650221684; Thu, 18 Feb 2021 04:10:21 -0800 (PST) Received: from localhost.localdomain ([122.173.196.104]) by smtp.gmail.com with ESMTPSA id w196sm6124277pfd.23.2021.02.18.04.10.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Feb 2021 04:10:20 -0800 (PST) From: Sumit Garg To: kgdb-bugreport@lists.sourceforge.net Cc: jason.wessel@windriver.com, daniel.thompson@linaro.org, dianders@chromium.org, linux-kernel@vger.kernel.org, Sumit Garg Subject: [PATCH v4] kdb: Simplify kdb commands registration Date: Thu, 18 Feb 2021 17:39:58 +0530 Message-Id: <1613650198-27437-1-git-send-email-sumit.garg@linaro.org> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Simplify kdb commands registration via using linked list instead of static array for commands storage. Signed-off-by: Sumit Garg --- Changes in v4: - Fix kdb commands memory allocation issue prior to slab being available with an array of statically allocated commands. Now it works fine with kgdbwait. - Fix a misc checkpatch warning. - I have dropped Doug's review tag as I think this version includes a major fix that should be reviewed again. Changes in v3: - Remove redundant "if" check. - Pick up review tag from Doug. Changes in v2: - Remove redundant NULL check for "cmd_name". - Incorporate misc. comment. kernel/debug/kdb/kdb_main.c | 129 ++++++++++++++--------------------------- kernel/debug/kdb/kdb_private.h | 2 + 2 files changed, 47 insertions(+), 84 deletions(-) diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c index 930ac1b..5215e04 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,12 @@ 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); + +#define KDB_CMD_INIT_MAX 50 +static int kdb_cmd_init_idx; +static kdbtab_t kdb_commands_init[KDB_CMD_INIT_MAX]; typedef struct _kdbmsg { int km_diag; /* kdb diagnostic */ @@ -921,7 +919,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,25 +1009,17 @@ int kdb_parse(const char *cmdstr) ++argv[0]; } - for_each_kdbcmd(tp, i) { - if (tp->cmd_name) { - /* - * If this command is allowed to be abbreviated, - * check to see if this is it. - */ - - if (tp->cmd_minlen - && (strlen(argv[0]) <= tp->cmd_minlen)) { - if (strncmp(argv[0], - tp->cmd_name, - tp->cmd_minlen) == 0) { - break; - } - } + list_for_each_entry(tp, &kdb_cmds_head, list_node) { + /* + * If this command is allowed to be abbreviated, + * check to see if this is it. + */ + if (tp->cmd_minlen && (strlen(argv[0]) <= tp->cmd_minlen) && + (strncmp(argv[0], tp->cmd_name, tp->cmd_minlen) == 0)) + break; - if (strcmp(argv[0], tp->cmd_name) == 0) - break; - } + if (strcmp(argv[0], tp->cmd_name) == 0) + break; } /* @@ -1037,19 +1027,15 @@ 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 (tp->cmd_name) { - if (strncmp(argv[0], - tp->cmd_name, - strlen(tp->cmd_name)) == 0) { - break; - } - } + if (list_entry_is_head(tp, &kdb_cmds_head, list_node)) { + list_for_each_entry(tp, &kdb_cmds_head, list_node) { + if (strncmp(argv[0], tp->cmd_name, + strlen(tp->cmd_name)) == 0) + break; } } - 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,17 +2414,14 @@ 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; - if (!kt->cmd_name) - continue; if (!kdb_check_flags(kt->cmd_flags, kdb_cmd_enabled, true)) continue; if (strlen(kt->cmd_usage) > 20) @@ -2667,49 +2650,30 @@ 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) { - if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) { + list_for_each_entry(kp, &kdb_cmds_head, list_node) { + if (strcmp(kp->cmd_name, cmd) == 0) { kdb_printf("Duplicate kdb command registered: " "%s, func %px help %s\n", cmd, func, help); return 1; } } - /* - * 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"); + if (slab_is_available()) { + kp = kmalloc(sizeof(*kp), GFP_KDB); + if (!kp) { + 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); + kp->is_dynamic = true; + } else { + if (kdb_cmd_init_idx >= KDB_CMD_INIT_MAX) { + kdb_printf("Could not allocate init kdb_command table\n"); + return 1; } - 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 = &kdb_commands_init[kdb_cmd_init_idx]; + kdb_cmd_init_idx++; } 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,16 @@ EXPORT_SYMBOL_GPL(kdb_register); */ int kdb_unregister(char *cmd) { - int i; kdbtab_t *kp; /* * find the command. */ - for_each_kdbcmd(kp, i) { - if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) { - kp->cmd_name = NULL; + list_for_each_entry(kp, &kdb_cmds_head, list_node) { + if (strcmp(kp->cmd_name, cmd) == 0) { + list_del(&kp->list_node); + if (kp->is_dynamic) + kfree(kp); return 0; } } @@ -2778,12 +2745,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..f969a6a 100644 --- a/kernel/debug/kdb/kdb_private.h +++ b/kernel/debug/kdb/kdb_private.h @@ -174,6 +174,8 @@ typedef struct _kdbtab { short cmd_minlen; /* Minimum legal # command * chars required */ kdb_cmdflags_t cmd_flags; /* Command behaviour flags */ + struct list_head list_node; /* Command list */ + bool is_dynamic; /* Command table allocation type */ } kdbtab_t; extern int kdb_bt(int, const char **); /* KDB display back trace */ -- 2.7.4