Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp3060733ybk; Mon, 18 May 2020 16:49:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZJkbEE3/jtyCfQwfihl4zRIh9cFD9l1HxlT1Of0TKXKpKcSVZfV/nwl2Biedp+C2M3hCv X-Received: by 2002:a17:906:c7cc:: with SMTP id dc12mr17498949ejb.263.1589845759335; Mon, 18 May 2020 16:49:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589845759; cv=none; d=google.com; s=arc-20160816; b=FSmAyRcx/nqF5Hh3WEKkrzde4BQYRK+4nnrYck/HxsEureH5VnzqKq4qN1sXVx3DNw iy+oIGrI/mlY5qSRhonT0NiZafFGVeM4lg04ribnf7iXsm4fpd5F+bC2BOzamLlcEuhi ks58+VwZlmyQhozkjb3+VX2xmMYLX0HQZc7rnd4VSv0iKFjMyiTB05OhFzX1q76TlHv+ yn19juajSZ4fvi5Nb5fXni5K0LVlazA4ZtEIO6+uP/JXAsF13qvWJwQMp7oimt55ePwI 9BGsNQ9LR+Gp3ozE2IPalq0lfI9nYOgq7431f1W7hrTdniP/4a0SHwBFumx3OBeOdbf5 4giA== 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=gmezQkypMR40C9XWUz1rlFBw124Lm6BIK7vrNYP6X+U=; b=QIvrCS5gqtA2bQHRxonzwC0g/vVZgAJ1nbvNqKOjlcemc/KZqoTbov3K5MWRl4bhGn ha8kOZNwSr2VKheqi4EekwPkDwEZVADLyGnFeJjwP90Dr8L8Xd9TRbAYAKWUfcbtqM8m or+lz/j0iAUxkewxsEFc573o8690HIx+xcuZ4YlazeE1iqxrG4YEn+1bfvuyl7j7H0pj 985p1bv5ftugpyJTPpJAxm2xfOlfz2d7awrZDb47N/Pw75A4KwtN2i9Uu5Kahnhi/xGn BtnrCmsRevWQGrAETCRtjQSsujiDuEBHl41P4HIWc/Nc/CxiILON8rinAN+kchd1wXA9 68hw== 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 c21si7675607edx.105.2020.05.18.16.48.57; Mon, 18 May 2020 16:49:19 -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 S1728403AbgERXrV (ORCPT + 99 others); Mon, 18 May 2020 19:47:21 -0400 Received: from mga02.intel.com ([134.134.136.20]:65333 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728258AbgERXrS (ORCPT ); Mon, 18 May 2020 19:47:18 -0400 IronPort-SDR: a+0TBaaoglEYnYmqeMxm9Jb6YycpMTDeBn+QW8qMM67GfcwiYDawOb2rKSD41w4y1klPYOxzDw YJbRTwAk+QXw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2020 16:47:12 -0700 IronPort-SDR: 1c5lss4U7G9EI9oBxLXuQZxRq6qrLODLeOzUOYw74fwFvC5AFjjrAnTvIHITJmliDAa4UYRMy8 7Pypk+RPMjFg== X-IronPort-AV: E=Sophos;i="5.73,408,1583222400"; d="scan'208";a="288757573" Received: from rchatre-s.jf.intel.com ([10.54.70.76]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2020 16:47:11 -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 V5 4/4] x86/resctrl: Use appropriate API for strings terminated by newline Date: Mon, 18 May 2020 16:46:49 -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 --- Changes since V4: - Remove "mode" local variable from rdtgroup_mode_write(). This variable was previously used to create shorter lines with the original strcmp() code that was removed in patch 4/4. - Andy pointed out that the repeated assignment to rdtgrp->mode could be replaced by a single assignment after all the checks. This was initially rejected because it would let the "RDT_MODE_PSEUDO_LOCKED" assignment slip through. Even so, Andy's feedback revealed that the new changes unintentionally let a user's attempt at setting the mode to pseudo-locked be silently ignored where it previously reported an error. Restore original user space behavior by returning success when user attempts to change any mode when it is already the current mode (including pseudo-locked) and returning failure when user attempts to set the mode to pseudo-locked. After this change it is possible to follow Andy's original suggestion of using a single assignment. (Andy) arch/x86/kernel/cpu/resctrl/rdtgroup.c | 40 ++++++++++++++------------ 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c index 088a1536bccc..31f1bacaa288 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -1414,13 +1414,11 @@ static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) { 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) { @@ -1430,29 +1428,33 @@ static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of, rdt_last_cmd_clear(); - mode = rdtgrp->mode; + ret = sysfs_match_string(rdt_mode_str, buf); + if (ret < 0) { + rdt_last_cmd_puts("Unknown or unsupported mode\n"); + ret = -EINVAL; + goto out; + } + + user_m = ret; + ret = 0; - 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)) + /* Do nothing and return success if user asks for current mode */ + if (user_m == rdtgrp->mode) goto out; - if (mode == RDT_MODE_PSEUDO_LOCKED) { + if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) { rdt_last_cmd_puts("Cannot change pseudo-locked group\n"); ret = -EINVAL; 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; @@ -1462,16 +1464,16 @@ static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of, if (ret) 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 { + } else if (user_m == RDT_MODE_PSEUDO_LOCKED) { rdt_last_cmd_puts("Unknown or unsupported mode\n"); ret = -EINVAL; + goto out; } + rdtgrp->mode = user_m; out: rdtgroup_kn_unlock(of->kn); -- 2.21.0