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=-8.4 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,USER_AGENT_SANE_1 autolearn=ham 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 4D73CC4332B for ; Fri, 20 Mar 2020 07:37:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 05BCE20724 for ; Fri, 20 Mar 2020 07:37:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iUNo0Jte" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 05BCE20724 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A42916B0003; Fri, 20 Mar 2020 03:37:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9F3596B0006; Fri, 20 Mar 2020 03:37:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 908146B0007; Fri, 20 Mar 2020 03:37:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0228.hostedemail.com [216.40.44.228]) by kanga.kvack.org (Postfix) with ESMTP id 769766B0003 for ; Fri, 20 Mar 2020 03:37:06 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 2E041180AD815 for ; Fri, 20 Mar 2020 07:37:06 +0000 (UTC) X-FDA: 76614934452.10.sugar88_783081fed9415 X-HE-Tag: sugar88_783081fed9415 X-Filterd-Recvd-Size: 5391 Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [63.128.21.74]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Mar 2020 07:37:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584689825; 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: in-reply-to:in-reply-to:references:references; bh=W1gum1vAgnk9ohf+zv7pJnapCaZq6wcMIaHYE+lE4nY=; b=iUNo0JteJhKFb0p4t4j42vWN2UDQGJsXrF/ySt2PmzID4XoSGoQ4KKALGLwdhJzSQM2oyt N6TjNdN/W2jD1f2HSB6HEAkeabO3BO8awRaYx3nEv399x7pRrBGEh01aquVFdmCX0LUQ+W 0rVAIDo6EV16W3xLG33OYQ3+FufWcX4= 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-354-3-PS3H37ND6CvYBdStjOow-1; Fri, 20 Mar 2020 03:37:03 -0400 X-MC-Unique: 3-PS3H37ND6CvYBdStjOow-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 120D88010D9; Fri, 20 Mar 2020 07:37:00 +0000 (UTC) Received: from localhost (ovpn-13-97.pek2.redhat.com [10.72.13.97]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 00D1460BF1; Fri, 20 Mar 2020 07:36:56 +0000 (UTC) Date: Fri, 20 Mar 2020 15:36:53 +0800 From: Baoquan He To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-hyperv@vger.kernel.org, Wei Yang , Michal Hocko , Greg Kroah-Hartman , Andrew Morton , Michal Hocko , Oscar Salvador , "Rafael J. Wysocki" Subject: Re: [PATCH v3 3/8] drivers/base/memory: store mapping between MMOP_* and string in an array Message-ID: <20200320073653.GE2987@MiWiFi-R3L-srv> References: <20200319131221.14044-1-david@redhat.com> <20200319131221.14044-4-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200319131221.14044-4-david@redhat.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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: On 03/19/20 at 02:12pm, David Hildenbrand wrote: > 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 @@ > > #define MEMORY_CLASS_NAME "memory" > > +static const char *const online_type_to_str[] = { > + [MMOP_OFFLINE] = "offline", > + [MMOP_ONLINE] = "online", > + [MMOP_ONLINE_KERNEL] = "online_kernel", > + [MMOP_ONLINE_MOVABLE] = "online_movable", > +}; > + > +static int memhp_online_type_from_str(const char *str) > +{ > + int i; I would change it as: for (int i = 0; i < ARRAY_SIZE(online_type_to_str); i++) { > + > + for (i = 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) > > 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 = memhp_online_type_from_str(buf); > struct memory_block *mem = to_memory_block(dev); > - int ret, online_type; > + int ret; > + > + if (online_type < 0) > + return -EINVAL; > > ret = lock_device_hotplug_sysfs(); > if (ret) > return ret; > > - if (sysfs_streq(buf, "online_kernel")) > - online_type = MMOP_ONLINE_KERNEL; > - else if (sysfs_streq(buf, "online_movable")) > - online_type = MMOP_ONLINE_MOVABLE; > - else if (sysfs_streq(buf, "online")) > - online_type = MMOP_ONLINE; > - else if (sysfs_streq(buf, "offline")) > - online_type = MMOP_OFFLINE; > - else { > - ret = -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 = -EINVAL; /* should never happen */ > } > > -err: > unlock_device_hotplug(); > > if (ret < 0) > -- > 2.24.1 >