Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp509221ybh; Wed, 11 Mar 2020 05:31:46 -0700 (PDT) X-Google-Smtp-Source: ADFU+vurtKo3w86tWUNOrZa2rn6bPY0SxmThdaaRaLsCiJVqCHa88s3ppUqkJetjCOdkHjH6qfuT X-Received: by 2002:a4a:ac4c:: with SMTP id q12mr63780oon.66.1583929906458; Wed, 11 Mar 2020 05:31:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583929906; cv=none; d=google.com; s=arc-20160816; b=JAKCTBVI/SYm2LrEKI2DjNQQllc/0m2Ql8wjUm6yaydQi8aH2wkOh1aJtxumWRgcKQ UKGQv+wT56Ug68AQIeY984iQIGY+jkUZj3v1QEqUi9ZfwCR/EdTWxNHFglt7LfiOTuke h4FjNvQCklGSLRtDognnB9b4bjIynoIdZ6ryR4GNovZrRjcJAIttv35f/9Y/ejp60v9K 3s4dxnKyW7ZI8ugkDh3u3OPRQNHiwl8BbbudmKCq9/OxxsCoTEsbU2KNa4vh5UDCYmIL XGnRtHvsreV+T+a68VGsLjF5XB9EnghjXS8xmTIIc0UTeUl5BGTXfZwUddML9jmfWU0d n2hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=EdxfJCF7xzkoVVDPgtAvv+vhs38SZ2AEeiWuNCr8aIE=; b=QrzKe5ZgcXvUx24uRnwm2cddeDC5cEWJFPqLv+h1gjYHyVVxDECEFfv8MpUoYWsAUP 8Xgxeol18Uey1tg9DLCsq739RgG8XA1LN8TNf9Fk4lUG8qk/eCj2fV39GtbN68ko+LFH BH3qZyEHJiMLq9RQ00qZ5yUQCwqKEP3oC96StkzstSB6q0tPD/h8y6M21ghI18l3mpLM u9bw7TgonyCUSQd+vs6q9rqPzTrHvRHFocVbAqkEA98dB5NoJGw9/cCf0AwXXgaZ9PVZ 9iYVpFiPaLOZyk9+pZQv1ki5x80S4DpeQTf1Gp68QA0QAme+SDWavwqzxQ5b6Qcg5Wi6 AK3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Wf40d4SS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y17si1041066oto.70.2020.03.11.05.31.34; Wed, 11 Mar 2020 05:31:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Wf40d4SS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729384AbgCKMa4 (ORCPT + 99 others); Wed, 11 Mar 2020 08:30:56 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:45995 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729238AbgCKMaz (ORCPT ); Wed, 11 Mar 2020 08:30:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583929854; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EdxfJCF7xzkoVVDPgtAvv+vhs38SZ2AEeiWuNCr8aIE=; b=Wf40d4SS6rho6B3LfdycR+0q3rH3rThKeu0Rj+97X/SQqC7/fFih/sCDHjMBezavltkHEJ ZfLNpa11lzqKnwFUrDsiLBoNKbiTnKC/l1xmBS8O08wSiVfVbvWc+nu/KPNbTJCZvtwVPq m4UvqkFx1HyhxYIBeyEg9srE0b30XKE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-405-8YruXUlWMsCI7s9QgkT7jw-1; Wed, 11 Mar 2020 08:30:48 -0400 X-MC-Unique: 8YruXUlWMsCI7s9QgkT7jw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A9B211084420; Wed, 11 Mar 2020 12:30:46 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.36.118.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id 74D105C1D4; Wed, 11 Mar 2020 12:30:43 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-hyperv@vger.kernel.org, David Hildenbrand , Greg Kroah-Hartman , Andrew Morton , Michal Hocko , Oscar Salvador , "Rafael J. Wysocki" , Baoquan He , Wei Yang Subject: [PATCH v1 3/5] drivers/base/memory: store mapping between MMOP_* and string in an array Date: Wed, 11 Mar 2020 13:30:24 +0100 Message-Id: <20200311123026.16071-4-david@redhat.com> In-Reply-To: <20200311123026.16071-1-david@redhat.com> References: <20200311123026.16071-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Let's use a simple array which we can reuse soon. While at it, move the string->mmop conversion out of the device hotplug lock. Cc: Greg Kroah-Hartman Cc: Andrew Morton Cc: Michal Hocko Cc: Oscar Salvador Cc: "Rafael J. Wysocki" Cc: Baoquan He Cc: Wei Yang Signed-off-by: David Hildenbrand --- drivers/base/memory.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/drivers/base/memory.c b/drivers/base/memory.c index e7e77cafef80..8a7f29c0bf97 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -28,6 +28,24 @@ =20 #define MEMORY_CLASS_NAME "memory" =20 +static const char *const online_type_to_str[] =3D { + [MMOP_OFFLINE] =3D "offline", + [MMOP_ONLINE] =3D "online", + [MMOP_ONLINE_KERNEL] =3D "online_kernel", + [MMOP_ONLINE_MOVABLE] =3D "online_movable", +}; + +static int memhp_online_type_from_str(const char *str) +{ + int i; + + for (i =3D 0; i < ARRAY_SIZE(online_type_to_str); i++) { + if (sysfs_streq(str, online_type_to_str[i])) + return i; + } + return -EINVAL; +} + #define to_memory_block(dev) container_of(dev, struct memory_block, dev) =20 static int sections_per_block; @@ -236,26 +254,17 @@ static int memory_subsys_offline(struct device *dev= ) static ssize_t state_store(struct device *dev, struct device_attribute *= attr, const char *buf, size_t count) { + const int online_type =3D memhp_online_type_from_str(buf); struct memory_block *mem =3D to_memory_block(dev); - int ret, online_type; + int ret; + + if (online_type < 0) + return -EINVAL; =20 ret =3D lock_device_hotplug_sysfs(); if (ret) return ret; =20 - if (sysfs_streq(buf, "online_kernel")) - online_type =3D MMOP_ONLINE_KERNEL; - else if (sysfs_streq(buf, "online_movable")) - online_type =3D MMOP_ONLINE_MOVABLE; - else if (sysfs_streq(buf, "online")) - online_type =3D MMOP_ONLINE; - else if (sysfs_streq(buf, "offline")) - online_type =3D MMOP_OFFLINE; - else { - ret =3D -EINVAL; - goto err; - } - switch (online_type) { case MMOP_ONLINE_KERNEL: case MMOP_ONLINE_MOVABLE: @@ -271,7 +280,6 @@ static ssize_t state_store(struct device *dev, struct= device_attribute *attr, ret =3D -EINVAL; /* should never happen */ } =20 -err: unlock_device_hotplug(); =20 if (ret < 0) --=20 2.24.1