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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CEE61C433F5 for ; Mon, 25 Apr 2022 22:00:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45CF16B0073; Mon, 25 Apr 2022 18:00:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E5876B0074; Mon, 25 Apr 2022 18:00:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 286E96B0075; Mon, 25 Apr 2022 18:00:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 160B36B0073 for ; Mon, 25 Apr 2022 18:00:49 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EBF252873F for ; Mon, 25 Apr 2022 22:00:48 +0000 (UTC) X-FDA: 79396771776.10.2BB2C8D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id A6B051C004E for ; Mon, 25 Apr 2022 22:00:45 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id ACAE761356; Mon, 25 Apr 2022 22:00:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CFCACC385A7; Mon, 25 Apr 2022 22:00:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1650924047; bh=D/VDL1DahBoKxuAKBJl+6cBrdh/4mvjtNLDy8kSYqLo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=OTvaaRx+WZC2FTgPWUF92ffX7vNnsCvJ6xGzm3EYYE+onLaPH8ue4af3+31PLTHFQ +8nJQqfkcm2RPP7Ibj38OVAk1WzMIa3swMC+VomMeRFckVw0CQGDAYWMBFgHxsKjB+ KKqgUwCcumKjax0obedXSG38hajcEmIeMkSJOTbE= Date: Mon, 25 Apr 2022 15:00:46 -0700 From: Andrew Morton To: Jagdish Gediya Cc: andy@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, minchan@kernel.org, ying.huang@intel.com, dave.hansen@intel.com Subject: Re: [RESEND PATCH] string_helpers: sysfs: Add helper to get bool from string Message-Id: <20220425150046.e23a0198e0076221549eb7cf@linux-foundation.org> In-Reply-To: <20220425085233.82576-1-jvgediya@linux.ibm.com> References: <20220425085233.82576-1-jvgediya@linux.ibm.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Stat-Signature: 8psw53niahigohrgw4tbz67dxxd1zm6c X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: A6B051C004E Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=OTvaaRx+; spf=pass (imf20.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Rspam-User: X-HE-Tag: 1650924045-763303 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 Mon, 25 Apr 2022 14:22:33 +0530 Jagdish Gediya wrote: > At many places in kernel, It is necessary to convert sysfs input > to corrosponding bool value e.g. "false" or "0" need to be converted > to bool false, "true" or "1" need to be converted to bool true, > places where such conversion is needed currently check the input > string manually. Also, such conversions compare sysfs input using > strncmp functions so even if certain number of character match in the > beginning, they assume the string as valid bool, which is not the > right semantic e.g. false is bool but falseX is not. > > Introduce new string helper function to convert sysfs input to > corrosponding bool value. Modify existing such conversions to use > this new function. > > logs, > $ cat /sys/kernel/mm/numa/demotion_enabled > false > $ echo true > /sys/kernel/mm/numa/demotion_enabled > $ cat demotion_enabled > true > $ echo truex > /sys/kernel/mm/numa/demotion_enabled > -bash: echo: write error: Invalid argument > $ echo 10 > /sys/kernel/mm/numa/demotion_enabled > -bash: echo: write error: Invalid argument > $ echo false > /sys/kernel/mm/numa/demotion_enabled > $ cat demotion_enabled > false > $ echo falseabc > /sys/kernel/mm/numa/demotion_enabled > -bash: echo: write error: Invalid argument > $ echo 1 > /sys/kernel/mm/numa/demotion_enabled > $ cat demotion_enabled > true > $ echo 0 > /sys/kernel/mm/numa/demotion_enabled > $ cat demotion_enabled > false > > This patch doesn't have any functionality change. > > ... > > --- a/lib/string_helpers.c > +++ b/lib/string_helpers.c > @@ -967,6 +967,26 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count, > } > EXPORT_SYMBOL(memcpy_and_pad); > > +/** > + * sysfs_strbool - Get bool value corrosponding to string > + * @s: The string to operate on. > + * @output: Pointer to fill resulting bool value > + * > + * Returns 1 if string represents bool value, 0 otherwise > + */ > +int sysfs_strbool(const char *s, bool *output) > +{ > + if (sysfs_streq(s, "1") || sysfs_streq(s, "true")) > + *output = true; > + else if (sysfs_streq(s, "0") || sysfs_streq(s, "false")) > + *output = false; > + else > + return 0; > + > + return 1; > +} > +EXPORT_SYMBOL(sysfs_strbool); > + Can we teach kstrtobool() about "true" and "false" then use that?