From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7AEB8C2BB1D for ; Tue, 17 Mar 2020 10:50:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5178520658 for ; Tue, 17 Mar 2020 10:50:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="e831wjD0" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726294AbgCQKuS (ORCPT ); Tue, 17 Mar 2020 06:50:18 -0400 Received: from us-smtp-delivery-74.mimecast.com ([216.205.24.74]:29754 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726467AbgCQKuR (ORCPT ); Tue, 17 Mar 2020 06:50:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584442216; 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=88iyfgf6uemMR/FRnlDjgZQv5nsyNNsQWtkQXwJKijk=; b=e831wjD0jjn1ouBEIUa4RbcBSYRcaKDNR0M5b2DZYVSvs2UMxfHwmR4H0oD5mm7TOq1iVd AW/aHH4t5n1RXsQ/pMdEbhb6Y/W75Mp4UJq9YJD9YMY2PgBiCs1ksODcVq9EN8CgCBmrNK USM89s9e5OK9DeeUI9QMhv5+WuD82+o= 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-315-TcWPR2udNh2bRBvAarTUzQ-1; Tue, 17 Mar 2020 06:50:13 -0400 X-MC-Unique: TcWPR2udNh2bRBvAarTUzQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1AB28801A06; Tue, 17 Mar 2020 10:50:11 +0000 (UTC) Received: from t480s.redhat.com (ovpn-112-136.ams2.redhat.com [10.36.112.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6E12073865; Tue, 17 Mar 2020 10:50:03 +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 , Wei Yang , Michal Hocko , Greg Kroah-Hartman , Andrew Morton , Michal Hocko , Oscar Salvador , "Rafael J. Wysocki" , Baoquan He Subject: [PATCH v2 3/8] drivers/base/memory: store mapping between MMOP_* and string in an array Date: Tue, 17 Mar 2020 11:49:37 +0100 Message-Id: <20200317104942.11178-4-david@redhat.com> In-Reply-To: <20200317104942.11178-1-david@redhat.com> References: <20200317104942.11178-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Content-Transfer-Encoding: quoted-printable Sender: linux-hyperv-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hyperv@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. Reviewed-by: Wei Yang Acked-by: Michal Hocko 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