Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp97121ybk; Thu, 14 May 2020 17:23:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxW/+E3Wu3T/JzKjCkSOxSC+vESlsFedhScgK0LO/KzLvBGYfnp2y1TfGK4POZGIyYfGXFC X-Received: by 2002:a17:906:2b8a:: with SMTP id m10mr542441ejg.183.1589502234137; Thu, 14 May 2020 17:23:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589502234; cv=none; d=google.com; s=arc-20160816; b=pJw1KF/H0zQMcV1HnqIeM+ifIQ5j75+ce7PbfWUcuoppyjVbRl/fIxlXjx/Knw1bhu LTlzy+4xSiHV9VR2k0uWJCG3oWvK3QJP++5HADJJeAbLyeJ/jCsOLuM4TQ1IgQNByRRJ HCACrFapXlFFVRbO4FZdWJeYMiPz/IQNwS42ZvU8ImP6H4t7tag5qbvmldcnfBgZFBVs XzUd8SR0im54GE22KpnCWblrJxaZta1PCN4Q19+8PBpMdFMm9aK41ZkjTpSNtTMLdbn+ 62kvQFL6Fha3VXB00SeD99SI2i678/gOk1tlQWJLNimMRM3X4dpr6XkQorTkmNBr/yHg xd+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=bL+5kEGRnbjLCbw4uwEC87Mowvuaf4yCMcaxogqbFfI=; b=RoIY8BnFvQHWm39tGYl9QHsvJ3mjb6vZo1qpcl7y8cVt9LDYj5dp7Ab9mVHfa9qobT rCQwcALPsvWJMidk12HQhz3nPgndNYZzh45drmwAj+TTa5Yxlaw88/80igNjw9Yl4Ran 8VKwAIWwpFoj8nD/4Y36zSQenXxhuKY6zZnpkCjrSmBq6+l2456YjCHf2FrxZH4zODXx EyEHhHB+TYvh1BKKfQqCfwEg+nvFQpMkzRQZzl8NjCgGZ7Cs4IBCnuMC7vwRThuRf23B oBbuDx2RW5rKkotv5+hXjCGjdXEzbyR272C/Rq9fuP5UlpovKuvkBAsiSgHJu8+NXHs3 bcnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="t/Yi1mfa"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a28si247658edm.536.2020.05.14.17.23.29; Thu, 14 May 2020 17:23:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="t/Yi1mfa"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728183AbgENTLn (ORCPT + 99 others); Thu, 14 May 2020 15:11:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728653AbgENTLh (ORCPT ); Thu, 14 May 2020 15:11:37 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A131C061A0C for ; Thu, 14 May 2020 12:11:37 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id z1so1778835pfn.3 for ; Thu, 14 May 2020 12:11:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=bL+5kEGRnbjLCbw4uwEC87Mowvuaf4yCMcaxogqbFfI=; b=t/Yi1mfa6GwVKuRE/g5RK/qViOZdBVoSNTRQGXQYtnmACJjQz/hngYa8oPaPm5i04H kUTGjHYv3Niltkv8XQgfA6LXzQGYmw5Y/ldWSLgqI11ZFG//CelBClfjMXZ720diwD9c kmLrrd0dKwCmKRd/YeVdDqm5YR0Jqy8Q2LvUTVityXMmTdJDKooO0TVuU2rXx8Z6DsCc tsKeYi21IdbIBItdW+J3+zHBV2JIkN/oFNWTGrJithMnRMFcAYqx4NOhTbMK9SQrP+0d 87S8xn/AA8UkNawRfSr8SfrtEqmoSj9p8AKQl9XLZ9ZNcF1uqBVFpR+Y8+6ZrNQcmy5O nogA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=bL+5kEGRnbjLCbw4uwEC87Mowvuaf4yCMcaxogqbFfI=; b=gpy4CTVaH139YnkeXlKwaZy3sKZEqUtwKaPG8bmKZTe6X/JlMLJ5ZZVaVHCdhPTc4T EgFujeaCotq7ErmS0nOTY0GQgei++TqFrM9g6SvbAZGLWol1PzYrD92kcac3YSjjk4Aa fsGQR4m+UO2ik65AuNw09ddqRvc8MBIkdYu6Hp6HwVDt8Cg3lr4jEhoWjPZtNsy2tx1q l9+mZ8xtn0DJkIVGr1+DzrLwOoEDXpXXof+punKNRBlbBi35sBoTBmLQT08Zju4dyjS+ 34MTWp79/U5t2syrXH7EjmxQv4z8t4iNWC4GWx4MnqaWaZzd7s26XDuYko0abAoCY/wN C1tw== X-Gm-Message-State: AOAM5314zytGSwI7BA8HWlaUo+sZ80bjMQxJP02u565qfWLOwthfLNBv b1n7mhHXpm/989pvTNUJHf/HK17FYrs58QO/PlA= X-Received: by 2002:a63:1c1:: with SMTP id 184mr5437931pgb.203.1589483496366; Thu, 14 May 2020 12:11:36 -0700 (PDT) MIME-Version: 1.0 References: <9c7a011c-2946-102c-5cdd-9dc5155533ec@amd.com> In-Reply-To: <9c7a011c-2946-102c-5cdd-9dc5155533ec@amd.com> From: Andy Shevchenko Date: Thu, 14 May 2020 22:11:29 +0300 Message-ID: Subject: Re: [PATCH V3 4/4] x86/resctrl: Use appropriate API for strings terminated by newline To: Babu Moger Cc: Reinette Chatre , "tglx@linutronix.de" , "fenghua.yu@intel.com" , "bp@alien8.de" , "tony.luck@intel.com" , "kuo-lang.tseng@intel.com" , "ravi.v.shankar@intel.com" , "mingo@redhat.com" , "hpa@zytor.com" , "x86@kernel.org" , "linux-kernel@vger.kernel.org" , Andy Shevchenko Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 14, 2020 at 10:08 PM Babu Moger wrote: > > -----Original Message----- > > From: Reinette Chatre > > Sent: Wednesday, May 6, 2020 6:50 PM > > To: tglx@linutronix.de; fenghua.yu@intel.com; bp@alien8.de; > > tony.luck@intel.com > > Cc: kuo-lang.tseng@intel.com; ravi.v.shankar@intel.com; mingo@redhat.com; > > Moger, Babu ; hpa@zytor.com; x86@kernel.org; > > linux-kernel@vger.kernel.org; Reinette Chatre ; > > Andy Shevchenko > > Subject: [PATCH V3 4/4] x86/resctrl: Use appropriate API for strings terminated > > by newline > > > > The user input to files in the resctrl filesystem are expected to be > > terminated with a newline. Testing the user input includes a test for > > the presence of a newline and then replacing the newline with NUL > > byte followed by comparison using strcmp(). > > > > sysfs_streq() exists to test if strings are equal, treating both NUL and > > newline-then-NUL as equivalent string terminations. Even more, > > sysfs_match_string() exists to match a given string in an array using > > sysfs_streq(). > > > > Replace existing strcmp() comparisons of strings that are terminated > > with a newline with more appropriate sysfs_streq() via the > > sysfs_match_string() API that can perform the match across the different > > mode strings that are already maintained in an array. > > > > Suggested-by: Andy Shevchenko > > Signed-off-by: Reinette Chatre > > --- > > arch/x86/kernel/cpu/resctrl/rdtgroup.c | 32 ++++++++++++++------------ > > 1 file changed, 17 insertions(+), 15 deletions(-) > > > > diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c > > b/arch/x86/kernel/cpu/resctrl/rdtgroup.c > > index c60a3b307f7d..e70694892c02 100644 > > --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c > > +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c > > @@ -1408,12 +1408,11 @@ static ssize_t rdtgroup_mode_write(struct > > kernfs_open_file *of, > > { > > struct rdtgroup *rdtgrp; > > enum rdtgrp_mode mode; > > - int ret = 0; > > + int user_m; > > + int ret; > > This is bit confusing here. Mode and user_m should have been of same type. It can't. If you compare enum to int you will need the explicit (weird) casting. > You can define user_m to user_mode to be very clear. > You can completely remove "mode" and directly use rdtgrp->mode instead. It > is left to you. > > > > - /* Valid input requires a trailing newline */ > > - if (nbytes == 0 || buf[nbytes - 1] != '\n') > > + if (nbytes == 0) > > return -EINVAL; > > - buf[nbytes - 1] = '\0'; > > > > rdtgrp = rdtgroup_kn_lock_live(of->kn); > > if (!rdtgrp) { > > @@ -1425,11 +1424,17 @@ static ssize_t rdtgroup_mode_write(struct > > kernfs_open_file *of, > > > > mode = rdtgrp->mode; > > > > - if ((!strcmp(buf, "shareable") && mode == RDT_MODE_SHAREABLE) || > > - (!strcmp(buf, "exclusive") && mode == RDT_MODE_EXCLUSIVE) || > > - (!strcmp(buf, "pseudo-locksetup") && > > - mode == RDT_MODE_PSEUDO_LOCKSETUP) || > > - (!strcmp(buf, "pseudo-locked") && mode == > > RDT_MODE_PSEUDO_LOCKED)) > > + ret = sysfs_match_string(rdt_mode_str, buf); > > + if (ret < 0) { > > + rdt_last_cmd_puts("Unknown or unsupported mode\n"); > > + goto out; > > + } > > + > > + user_m = ret; > > + ret = 0; > > + > > + /* Do nothing and return success if user asks for current mode */ > > + if (user_m == mode) > > goto out; > > > > if (mode == RDT_MODE_PSEUDO_LOCKED) { > > @@ -1438,14 +1443,14 @@ static ssize_t rdtgroup_mode_write(struct > > kernfs_open_file *of, > > goto out; > > } > > > > - if (!strcmp(buf, "shareable")) { > > + if (user_m == RDT_MODE_SHAREABLE) { > > if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) { > > ret = rdtgroup_locksetup_exit(rdtgrp); > > if (ret) > > goto out; > > } > > rdtgrp->mode = RDT_MODE_SHAREABLE; > > - } else if (!strcmp(buf, "exclusive")) { > > + } else if (user_m == RDT_MODE_EXCLUSIVE) { > > if (!rdtgroup_mode_test_exclusive(rdtgrp)) { > > ret = -EINVAL; > > goto out; > > @@ -1456,14 +1461,11 @@ static ssize_t rdtgroup_mode_write(struct > > kernfs_open_file *of, > > goto out; > > } > > rdtgrp->mode = RDT_MODE_EXCLUSIVE; > > - } else if (!strcmp(buf, "pseudo-locksetup")) { > > + } else if (user_m == RDT_MODE_PSEUDO_LOCKSETUP) { > > ret = rdtgroup_locksetup_enter(rdtgrp); > > if (ret) > > goto out; > > rdtgrp->mode = RDT_MODE_PSEUDO_LOCKSETUP; > > - } else { > > - rdt_last_cmd_puts("Unknown or unsupported mode\n"); > > - ret = -EINVAL; > > } > > > > out: > > -- > > 2.21.0 > -- With Best Regards, Andy Shevchenko