Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp272796ybj; Wed, 6 May 2020 17:18:24 -0700 (PDT) X-Google-Smtp-Source: APiQypKOvEuxVgHhOnI7+b5n8Iy7js7bY0ozHm9wZkT18si9RItyXjw6WZl8sRl0ujpErn8FJV1/ X-Received: by 2002:a50:951c:: with SMTP id u28mr9276719eda.310.1588810704618; Wed, 06 May 2020 17:18:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588810704; cv=none; d=google.com; s=arc-20160816; b=KkG/bn+aycqpAxnvMEkOTsxRpEpz/StsAl9iFlHxtBm9lKABMjhMJETKjxZcbdb4aW EzlVP63i4DXezge6w2ZAYGpyIsOdOd5wCa/VrgAob6bYNNZoFlaLsZ04+DxI5XbH2Ear n2VRChY71UGqH8rLoJOe8d2IH0QJMpH2GVGxBl3g1TBqqTEtc3higdaZem0hw7O6etaW SHe4p2t5apirn6cS9dbe4Qqwurm/j8auBOlUABdrGa83dJkAduDr340ksO45gKw5T9v1 amNBw/KXlPQP1YKzluS0yFz+s23GF3jZMuGplJqFe+FWMraQBxN5i4uKSLU55QkbWqWk uZsQ== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=489PdTXgmaoBCz0Ah+WxLS9K1d/BIV6Sz6Zy54GJI3s=; b=BZjKxazR1ixEHmjZCTdVZtRVD6Wf1aCFzQmACL3lf+o+TBPIIW+mpVEQjD4hROxDsX mTvuxW8+tVNCx93ZcqoYe2vls30+Y0J3IStnXxG85G9vOzofCIvM43WLRG5uDVtrz/CG w7IgsSmQxPuDO3KVHcKKUlHBCT1N5TOxHSm0aCqdnX5WdfZyRUWp1/5qlf5RLI/oAZex GT/xHppsp+MC1NhO/ClvjbI3rnypfWXDeAoDHmQtBp3x0wOIoJpIF98WR9QWbjHLpG0W qHrJ6AG2ZaOpCgDw9SWcUI9p3qRzWe1eCjWggbancSuvZOaBW2u7N9O5N4s5GBfspA8R hpBQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p16si2247009ejd.448.2020.05.06.17.18.01; Wed, 06 May 2020 17:18:24 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728385AbgEFXun (ORCPT + 99 others); Wed, 6 May 2020 19:50:43 -0400 Received: from mga11.intel.com ([192.55.52.93]:22516 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725887AbgEFXue (ORCPT ); Wed, 6 May 2020 19:50:34 -0400 IronPort-SDR: jof/3rPbb9FPWE/FstfGpnNpuwzobHtLALOg7vafv7QI7Z8pxXuhCclVww84s2Ode2qtmzgsBw jDhidq+dZx9A== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 May 2020 16:50:31 -0700 IronPort-SDR: 9sNPWl6NHJRwt/Qx2aLUX8d+XLEBJrueDqYI9OOBKgEeH5SRLqpbLmf1UkHa85o4etq+9abTUJ mRgHZlshZ+5A== X-IronPort-AV: E=Sophos;i="5.73,361,1583222400"; d="scan'208";a="435084104" Received: from rchatre-s.jf.intel.com ([10.54.70.76]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 May 2020 16:50:30 -0700 From: Reinette Chatre 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, babu.moger@amd.com, 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 Date: Wed, 6 May 2020 16:49:55 -0700 Message-Id: X-Mailer: git-send-email 2.21.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; - /* 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