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.5 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 E476CC43332 for ; Thu, 19 Mar 2020 13:34:41 +0000 (UTC) Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9449F20663 for ; Thu, 19 Mar 2020 13:34:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="BsuE0ZBS" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9449F20663 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 48jnvL6dldzDrC0 for ; Fri, 20 Mar 2020 00:34:38 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=redhat.com (client-ip=216.205.24.74; helo=us-smtp-delivery-74.mimecast.com; envelope-from=david@redhat.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BsuE0ZBS; dkim-atps=neutral Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [216.205.24.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 48jnQF0yK2zDrLb for ; Fri, 20 Mar 2020 00:12:51 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584623568; 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=BsuE0ZBS3WYk/My+WNaMrESrNuiM4kdwReL/2qoi7/C4mcrJzCrh5dtSSRcH5o4eVsDTce PDMginzv23Ld4wBeznV//pfqNATmX/cgyvo0n3uDIh7j/NiDFEB5PP5JV6KhGwyE0hXhDF tdtnpbyxRnFUzkbguJVvndqD+zMr1Vw= 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-467-49WSjLvPPsmX0RwL0MkBSw-1; Thu, 19 Mar 2020 09:12:45 -0400 X-MC-Unique: 49WSjLvPPsmX0RwL0MkBSw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2D540DBA3; Thu, 19 Mar 2020 13:12:43 +0000 (UTC) Received: from t480s.redhat.com (ovpn-114-197.ams2.redhat.com [10.36.114.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id A276D60BF7; Thu, 19 Mar 2020 13:12:40 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Subject: [PATCH v3 3/8] drivers/base/memory: store mapping between MMOP_* and string in an array Date: Thu, 19 Mar 2020 14:12:16 +0100 Message-Id: <20200319131221.14044-4-david@redhat.com> In-Reply-To: <20200319131221.14044-1-david@redhat.com> References: <20200319131221.14044-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Content-Transfer-Encoding: quoted-printable X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-hyperv@vger.kernel.org, Michal Hocko , Baoquan He , David Hildenbrand , Greg Kroah-Hartman , "Rafael J. Wysocki" , Wei Yang , linux-mm@kvack.org, Andrew Morton , Michal Hocko , linuxppc-dev@lists.ozlabs.org, Oscar Salvador Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" 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