Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2164331ybb; Thu, 2 Apr 2020 14:32:56 -0700 (PDT) X-Google-Smtp-Source: APiQypJYceW+/93EmiGornqVPJBf1I3jE2vMzFwCjjUPFTX5xBSrYW7855K/3Vq9lw7VLTprExr9 X-Received: by 2002:a05:6830:3109:: with SMTP id b9mr4198786ots.369.1585863176171; Thu, 02 Apr 2020 14:32:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585863176; cv=none; d=google.com; s=arc-20160816; b=AaIZTz5S1lh7fNBBttcvfvOoQQJEq86K9rWfmhQ9I58KX/gCeWoGDOkJDRuqa5lOGA wj798fVXMRr+pPdxiW7UNQr/pRSJyokzBVFsUysFMGOpgahc1TT9IPDqPBrc3tWfHCk9 Taf63WK6De41UWYuq+pADrhMHb/SctCz8Ze8qvSUFjQlrxZUmtSEtkDIUW9u9R1efXw+ vnMvchInGER7WzBDPzosuXDtE9R6TZKRZiPcWNHqSMRp/AAs+W/GgaW2m9ul8Nbh7b6G me9JcbjXRefE42X4OvqtV9XCl82c2M0bbS5rk5DqmdiY5JrQ8HEftg8VKTd2W6Ya1Cve GwGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:ironport-sdr:ironport-sdr; bh=rjLPj3ABswWrkoewmaU+8GyielgfkAyGjumXdLwLrmc=; b=wGdH8J6AggpP1QpyH388oNqWYvf63u3el7PjNe0KjXcf7QaNcz185WuqDgViz+Tbz9 iSYeAkYfx14FhObIN5F3bz6/SZlsqVNpB1XQOr0i7SyYj63By7WHORWVLt9HUZ7EKnD9 lnXuZPAK6C+/dr/vN5D4ZaSehgv6UkTN5VqFQezySFMm2jJ2ADZA3bD3j6ptd7dDaAwz lxyz+uuPgI2I4cs0mRTat1JIQwPBZNubfFZiq6UWKl3TeBaqtLyoa9VJJ+P9pIxgF1Rh TGKJ+7W/HnPwqoLO1EARxYVbcAVKWPy2clpgpYfFtu4nc7jSzWnhuo7wDDxB84BNLNaR 8A/w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r21si2813885otp.320.2020.04.02.14.32.43; Thu, 02 Apr 2020 14:32:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388951AbgDBVTL (ORCPT + 99 others); Thu, 2 Apr 2020 17:19:11 -0400 Received: from mga01.intel.com ([192.55.52.88]:49032 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732970AbgDBVTL (ORCPT ); Thu, 2 Apr 2020 17:19:11 -0400 IronPort-SDR: iGyh8JElyw2af+Xe3jBy2BLkISCB3p9knontwnd6oCgqYZ8QWmh+8lSAjk05XRf2wQxs0UOpOn eFKhfHXjz6bQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2020 14:19:10 -0700 IronPort-SDR: ElJsfs2xsoC8GvmhzJDVLvxw+RK8t6eHJy2WYVJoZ3H8QS+d0UpDaVMMYn4f+dt4T7K65KWjMx UX3888N5RLow== X-IronPort-AV: E=Sophos;i="5.72,336,1580803200"; d="scan'208";a="396509021" Received: from rchatre-mobl.amr.corp.intel.com (HELO [10.251.27.29]) ([10.251.27.29]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2020 14:19:09 -0700 Subject: Re: [PATCH 2/2] x86/resctrl: Use appropriate API for strings terminated by newline To: Andy Shevchenko Cc: tglx@linutronix.de, fenghua.yu@intel.com, bp@alien8.de, tony.luck@intel.com, kuo-lang.tseng@intel.com, mingo@redhat.com, babu.moger@amd.com, hpa@zytor.com, x86@kernel.org, linux-kernel@vger.kernel.org References: <2a51c327497738ad7012e4f185046c530dba4594.1585765499.git.reinette.chatre@intel.com> <20200402130625.GA1922688@smile.fi.intel.com> From: Reinette Chatre Message-ID: <720ab442-3e2e-bd6d-d35c-5cc5de1d9824@intel.com> Date: Thu, 2 Apr 2020 14:19:07 -0700 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <20200402130625.GA1922688@smile.fi.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Andy, (Your two responses have been merged) On 4/2/2020 6:06 AM, Andy Shevchenko wrote: > On Wed, Apr 01, 2020 at 11:30:48AM -0700, Reinette Chatre wrote: >> 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 | 25 +++++++++++++------------ >> 1 file changed, 13 insertions(+), 12 deletions(-) >> >> diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c >> index fbee891a7d6e..623e33c0a290 100644 >> --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c >> +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c >> @@ -1412,11 +1412,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; >> > > ...and forgot to mention this... > > int user_m; > int ret; > > >> /* Valid input requires a trailing newline */ >> if (nbytes == 0 || buf[nbytes - 1] != '\n') >> return -EINVAL; >> - buf[nbytes - 1] = '\0'; > > The above test is not needed and comment now is misleading. > WRT nbytes I believe that kernel fs code checks for that. If nbytes is 0 it is still passed to this function. You are correct that those tests are not needed though (if nbytes is 0 then sysfs_match_string() will not find a match and return EINVAL via that path). Thank you for catching this. I'll remove those unnecessary checks. > >> rdtgrp = rdtgroup_kn_lock_live(of->kn); >> if (!rdtgrp) { >> @@ -1428,11 +1428,15 @@ 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)) >> + user_m = sysfs_match_string(rdt_mode_str, buf); >> + if (user_m < 0) { >> + rdt_last_cmd_puts("Unknown or unsupported mode\n"); >> + ret = user_m; >> + goto out; >> + } > > You can do it the way > > ret = sysfs_match_string(...); > if (ret < 0) { > ... > } > user_m = ret; > > ...and this changes > > ret = 0; > > ok, I'll do it this way in the next version. Thank you Reinette