From patchwork Fri Jan 20 05:57:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gavin Shan X-Patchwork-Id: 13109194 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E88DC46467 for ; Fri, 20 Jan 2023 05:57:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 185EF6B007E; Fri, 20 Jan 2023 00:57:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 135BA6B0080; Fri, 20 Jan 2023 00:57:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EF2BE6B0081; Fri, 20 Jan 2023 00:57:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id DE71D6B007E for ; Fri, 20 Jan 2023 00:57:47 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A8F04160DE0 for ; Fri, 20 Jan 2023 05:57:47 +0000 (UTC) X-FDA: 80374120974.26.07F94E6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf07.hostedemail.com (Postfix) with ESMTP id F13CE40009 for ; Fri, 20 Jan 2023 05:57:45 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Q4lgTuYD; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf07.hostedemail.com: domain of gshan@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=gshan@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674194266; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KjmOoqTkye3UNXuCyk++rWgNtMqygxPkyWDL4dhhiqM=; b=JY1Pdphc5/ASJVvy+zUkCtnp777tTC0RnrPUMSH99g+rpa7Iw6mn4ywToJHc4M2w33iaA0 sJl+pcfJNVyLCxqMO9kqfuKIlZcvkeNqezFSGUxZBqaWZRCNi1efXmLdpYPNA7wPY5RrVU vn7nWRLb8GX+nQmCKXUuCf8LXfNrlSY= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Q4lgTuYD; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf07.hostedemail.com: domain of gshan@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=gshan@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674194266; a=rsa-sha256; cv=none; b=L7qPNAWIQg8Hwq0HbQDGHYMa39kRuHFApXBYEJZmrHGNPnah2kK3UoW8SGozG+crtohwrk TIXb0XzT8pJd6vty0o/VIrcpaiEU/DDhxMcUldJFnVJpDIssnjl1PQ/M7xFq79R3GogcCz dxCJdur3jzUrXcwXhhh/3eqU4mqpU8g= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674194265; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KjmOoqTkye3UNXuCyk++rWgNtMqygxPkyWDL4dhhiqM=; b=Q4lgTuYDpqxk5bvhaGV/rgoqpV8vwq0SPXd8vlDXh8pQQDnlRYalp1nMiaxn7hRmiHlHn0 Lkx9mf0qbGOcE2WgjSG+jfcDZEJGRTNLfb0hUUjwK9DL2fOij2ck9zw5zPEEpO1eeoTEE+ b2P1TOQl7QBZ6S4yvYdi2Dps4QxUJNc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-98-kgi0r40PNt-4Eh0V51Y9Ag-1; Fri, 20 Jan 2023 00:57:41 -0500 X-MC-Unique: kgi0r40PNt-4Eh0V51Y9Ag-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 66E4F2A5954E; Fri, 20 Jan 2023 05:57:41 +0000 (UTC) Received: from gshan.redhat.com (vpn2-54-98.bne.redhat.com [10.64.54.98]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BE79640C6EC4; Fri, 20 Jan 2023 05:57:38 +0000 (UTC) From: Gavin Shan To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, david@redhat.com, osalvador@suse.de, gregkh@linuxfoundation.org, rafael@kernel.org, shan.gavin@gmail.com Subject: [PATCH 2/2] drivers/base/memory: Use array to show memory block state Date: Fri, 20 Jan 2023 13:57:27 +0800 Message-Id: <20230120055727.355483-3-gshan@redhat.com> In-Reply-To: <20230120055727.355483-1-gshan@redhat.com> References: <20230120055727.355483-1-gshan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspamd-Queue-Id: F13CE40009 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: gx9xf36u1ugrwdmyiy9eg1nxypy1jt5d X-HE-Tag: 1674194265-568669 X-HE-Meta: U2FsdGVkX19EA+7Oord5xRy+OYpklJFJl2HP7GevhujVffMboB34NV37ClF8kM1MLnR1r+ExWPn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/EziMGIF5PMcDxZjSvkk1pRkJUpJg2hVM9GA0OyrPDUBsqStW79LNRmgFaIH+MnKDctldZ9BsqJHNmFNgrXfU6IJeqxXfwwy5BXEj08M/tGo95TKt11Q= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Use an array to show memory block state from '/sys/devices/system/ memory/memoryX/state', to simplify the code. No functional change intended. Signed-off-by: Gavin Shan --- drivers/base/memory.c | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/drivers/base/memory.c b/drivers/base/memory.c index b456ac213610..9474f25c452c 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -141,28 +141,15 @@ static ssize_t state_show(struct device *dev, struct device_attribute *attr, char *buf) { struct memory_block *mem = to_memory_block(dev); - const char *output; + static const char *const mem_state_str[] = { + NULL, "online", "going-offline", NULL, "offline", + }; - /* - * We can probably put these states in a nice little array - * so that they're not open-coded - */ - switch (mem->state) { - case MEM_ONLINE: - output = "online"; - break; - case MEM_OFFLINE: - output = "offline"; - break; - case MEM_GOING_OFFLINE: - output = "going-offline"; - break; - default: - WARN_ON(1); + if (WARN_ON(mem->state >= ARRAY_SIZE(mem_state_str) || + !mem_state_str[mem->state])) return sysfs_emit(buf, "ERROR-UNKNOWN-%ld\n", mem->state); - } - return sysfs_emit(buf, "%s\n", output); + return sysfs_emit(buf, "%s\n", mem_state_str[mem->state]); } int memory_notify(unsigned long val, void *v)