Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1444955ybk; Sat, 16 May 2020 11:31:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZG9Ul/nRfVtvcjUT2Bj5VgBCHDxJ2X8YVAUrSNgaAUJ9qdsvItrWjjXCI6Sy1I2uaH0h8 X-Received: by 2002:a05:6402:2d4:: with SMTP id b20mr7967269edx.118.1589653888117; Sat, 16 May 2020 11:31:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589653888; cv=none; d=google.com; s=arc-20160816; b=qX+vOrzPVVJUJMhkZVT/uoLbnEEi/544iQrsz6ijMMNjKcEcqdnYdzSYXI9LB35lv2 pnfPHsbhZ2KmqSV1qm5lJ0i9EM9GjdhXfn3/PfaEjV1IUT9A5DihC4hF+fYJ0zhRz8hG temLa3ZRagjO2q8aRoYGCAWg1l0jzKsdYPyJRQXqq+Y+ntjeShKXWh9e464ou71caiQR rtZL6x/v23VB+ggNx7F1ELjApI82cC34Niq2xgLtCNm9CX0MP9y910xDv5nxj4QmXQ4n dC1wsgiDPpo4lOccW1uj/jdjTXH8CZqv6cXFRSvP8eslbrKeARTKFWEbu9D4X6Xx62B5 EdjQ== 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=WAMmBPFcl2aqNYvbCyY1Q6Bv5yBUbh3Bi74j+DC6b7w=; b=tu7x7BZfTH4a+dkDVBYSIFU2Of9akxMDyIjJgwKaoFZyZVrilfRWgzbw95phNtwsxT Xgj+RyE3/Y1Yn54I0n4qZ3vxiCm+wFsmalf3AEoIxUUH+SPNAdF9dtCRquByKAtF+9qd V84b6jXahuccKhqALGg7alxRmCJUHx28oxDvAinlv2xFLBFnAmXg0yBM7XKZdMwvmhH1 67J3OE/tvJ4V8AHXHSpRr0G3CfLsKT7wkoIndYlTvfjdlU0p/vMu3mCfickMJQbAgW8t VQa5E+jBvZp8qf3nogW0jRruny00mPAoz/43JM+6dcbhN1t6+paur6Oz6UeaKUQ8XD3I gHGg== 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 f13si3887131edc.577.2020.05.16.11.31.05; Sat, 16 May 2020 11:31:28 -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 S1726710AbgEPS33 (ORCPT + 99 others); Sat, 16 May 2020 14:29:29 -0400 Received: from mga05.intel.com ([192.55.52.43]:56285 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726298AbgEPS3X (ORCPT ); Sat, 16 May 2020 14:29:23 -0400 IronPort-SDR: TfY2BZCu06WlrN0Ey/UH/24aXYdrDOoWG064xZFZ79eIrDdOvky63XJOMgpCz+WYYue/JllW7c jCbnCf6H11iA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2020 11:29:20 -0700 IronPort-SDR: MzVdcs248i3MjTjkHHeBODWbsp/ohHaSFgOKTXHnytAbE8rkyOilMYMQhn6jxTB7Ul/+eFihfw hCmnew2PG4/A== X-IronPort-AV: E=Sophos;i="5.73,400,1583222400"; d="scan'208";a="281563333" Received: from rchatre-s.jf.intel.com ([10.54.70.76]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2020 11:29:19 -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 V4 4/4] x86/resctrl: Use appropriate API for strings terminated by newline Date: Sat, 16 May 2020 11:28:41 -0700 Message-Id: <0c04b58148f181ba182b9a487465e4853e4eadbb.1589652468.git.reinette.chatre@intel.com> 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 088a1536bccc..0e302d3c44e6 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -1415,12 +1415,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) { @@ -1432,11 +1431,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) { @@ -1445,14 +1450,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; @@ -1463,14 +1468,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