From patchwork Fri Jan 20 23:38:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gavin Shan X-Patchwork-Id: 13110700 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 9CE1BC05027 for ; Fri, 20 Jan 2023 23:38:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D10E76B0071; Fri, 20 Jan 2023 18:38:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CC1206B0073; Fri, 20 Jan 2023 18:38:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B888E6B007D; Fri, 20 Jan 2023 18:38:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A939B6B0071 for ; Fri, 20 Jan 2023 18:38:36 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4CFF81C60D4 for ; Fri, 20 Jan 2023 23:38:36 +0000 (UTC) X-FDA: 80376794232.23.A445B48 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 0132220009 for ; Fri, 20 Jan 2023 23:38:33 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="hdt/8ADh"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf03.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=1674257914; 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: references:dkim-signature; bh=eQABDFnHgbtrqYImAs8JujABVEskMqaC41YW4ab4b4o=; b=8N6eTWLGNAnOZdNWwFDd+1Cx/w7givMzp9u+wBqSDb5+BDvdkNOOY0KWEVX2llJnzYNI3d EGbs46znlitX4N3iflI25WKSZaxpTa9Iy/w1tgH7BYToSXPfu3ugCwClcTsehjGBNqwgL3 bPZ3Y6tE6+Qn5+dSCQV/nZGBFW6ajWI= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="hdt/8ADh"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf03.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=1674257914; a=rsa-sha256; cv=none; b=RMOIe5oXNneAVfmBE+X2TnyXu3+FNkaaf39GZ5YGFmENb9ET9xlynXylTrzwnjve43zf/S Oyv7M46DzdQUJryqljvAwAdPtiEfRFHVK/kUQaywSFoLSRdOFkD4PEmgWl1AlTB/rzGs3V dGxpr4MacGaQzsUtwJW909O3n7u4UqQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674257913; 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; bh=eQABDFnHgbtrqYImAs8JujABVEskMqaC41YW4ab4b4o=; b=hdt/8ADhmxMjQfHjXH7cn7VObeJS7eWFc5qOTG3NE9vDEExKzmjVHbraY0BSOXqdw3yb3E e8UGGDfiLfsCDUveLyFzy2roDi2uWYTFxFfE/YDI08pxBHaMf3thVT42NTOaKdGFxadUWt 6PjnBnlhPG1pPul3E47I7V9fxDyq2Eg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-450-1HqXXzKmNvmnGNSzx2WArA-1; Fri, 20 Jan 2023 18:38:29 -0500 X-MC-Unique: 1HqXXzKmNvmnGNSzx2WArA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4F376101A521; Fri, 20 Jan 2023 23:38:29 +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 6AF1140C2064; Fri, 20 Jan 2023 23:38:26 +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 v2] drivers/base/memory: Use array to show memory block state Date: Sat, 21 Jan 2023 07:38:14 +0800 Message-Id: <20230120233814.368803-1-gshan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Rspamd-Queue-Id: 0132220009 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: ewiwmuy1dfmtp1o35u9igbzpr7ihdq7z X-HE-Tag: 1674257913-753175 X-HE-Meta: U2FsdGVkX1/3Wka7MQCOXEXovmr73uOJPqwD7fykZl/pyYT6ldCfkwtgY6+Qr6Mct7z+b8Q6N/0YyAamDZi7zjo5hKJDZbZG0oGr6Q4IFvui1gmQxj0fbuDg9s4gf5Jqtfr+ze0hcN3WCG+hFMmDI4I6c5yOP726zJ4+ZHOnhqQyT04uxs3k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EaqDgQ= 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. Besides, WARN_ON() is removed since the warning can be caught by the return value, which is "ERROR-UNKNOWN-%ld\n". A system reboot caused by WARN_ON() is definitely unexpected as Greg mentioned. No functional change intended. Signed-off-by: Gavin Shan Reviewed-by: David Hildenbrand --- v2: Drop WARN_ON() (Greg) --- 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..0fdacdc79806 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 (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)