Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp25309ybb; Tue, 14 Apr 2020 18:04:40 -0700 (PDT) X-Google-Smtp-Source: APiQypLNsGCbAIf77VfmbHukuqQpPqiC3JuwVhCRynlQydTIJHHvVj6kTtLc0OsMt0jKPgyX9yzf X-Received: by 2002:a50:baa6:: with SMTP id x35mr8484905ede.54.1586912680389; Tue, 14 Apr 2020 18:04:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586912680; cv=none; d=google.com; s=arc-20160816; b=iMN5b/9pV16xUHnSqyRQjqjcUJGndVj2VYcQKkwsDdc3+QOQXdIfrTACdCkfJ4+BrH FDeuJL5KMKMqmK651Z6J6xfkdoBUiUts/mu9UCyfT2/i5pMOOKoNOeGranczGnj3NGnq pm/rCGtTYCo6/dPRUdtLnAGCJLN7R5mq2pc4oJDrnzmlolH1m8jStAcAJUAOEGGsfdFC 2DiTu5L2s1Ri4V0ehmPxgehFLsFdkeDfl/DZGjUX39LymlYDcofcwaZTk+BEY4Bnd4HH bledJfgaTOY186jiHpVpYFvIZJro4nxO32JWVwGJ4As7hgMnnVHDUTrcFbllnW22JHCn rouA== 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=ltX9BVK/KYYOCVfx4w4mKChYfmcN+thb6rmO2AEDwmw=; b=eafDTrHI5MFv3UrgVaeON1x8oBjMCmGNaN5Zlngzqx/VeLoOxUB8/fl1uVuNpOx91f ltkwqjmFUqsG1hfOnUKPfxyiUoMrMWsZn3ycMpn7T0uypZJhimTCV4xpkORc2JtpbLvQ gbmRNC+SU7gUXElZJDujcjnooziZrdmGSvn0+QdVENOFziwlbowe5k1NlpeFscgOb9rW 0AavMLNmJGMoLIkgGHeJb/C9aMMIUfXrGDpTX1S7tmceY9ZhUrlnbssEPHggrC+aRkvE 8BeOlF5ew3qS5F4C5iDLpm3ektaVCy/H3M/LTVEt5zdajWG/G+bdHybyvRLF+toERLjM ydUw== 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 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 d19si8395415ejj.481.2020.04.14.18.04.12; Tue, 14 Apr 2020 18:04:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for 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: best guess record for 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 S2388664AbgDMU61 (ORCPT + 99 others); Mon, 13 Apr 2020 16:58:27 -0400 Received: from mga17.intel.com ([192.55.52.151]:8591 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733299AbgDMU6Y (ORCPT ); Mon, 13 Apr 2020 16:58:24 -0400 IronPort-SDR: lfLas3BSUw/VSZ5VktiZEjQcFDAAiUvBKLjlWRs8KFEQRg0VKDLCmzgYu1G/EBE1fXbjeCEQsm G7cWtzv39bfg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2020 13:58:22 -0700 IronPort-SDR: tb7Za98YRpkT32NZhHWbbskeu9k1XsyohvCyQVkxlFJnn/WvMB1Wq6xNeC9C/0aRE9p2VsaoJ2 WIsHuxhUE3kg== X-IronPort-AV: E=Sophos;i="5.72,380,1580803200"; d="scan'208";a="453301669" Received: from rchatre-s.jf.intel.com ([10.54.70.76]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2020 13:58:21 -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 V2 4/4] x86/resctrl: Use appropriate API for strings terminated by newline Date: Mon, 13 Apr 2020 13:58:03 -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 V1: - Remove unnecessary checks on user input (Andy Shevchenko) - Change code style surrounding usage of sysfs_match_string() (Andy Shevchenko) 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 d4e9b8a204d6..a8cbec226633 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