From patchwork Tue Jul 6 01:06:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12359669 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10AF9C07E99 for ; Tue, 6 Jul 2021 01:06:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E4A306196C for ; Tue, 6 Jul 2021 01:06:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229773AbhGFBJD (ORCPT ); Mon, 5 Jul 2021 21:09:03 -0400 Received: from mail-wr1-f54.google.com ([209.85.221.54]:40884 "EHLO mail-wr1-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229722AbhGFBJC (ORCPT ); Mon, 5 Jul 2021 21:09:02 -0400 Received: by mail-wr1-f54.google.com with SMTP id l5so7238307wrv.7 for ; Mon, 05 Jul 2021 18:06:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=bGeSwqA1PGXcdIOF0QAiLqEJhbJ0OkQQxp/nfQHOIqY=; b=OYP3oaakbLGt5d96vuoLiXNSjgL9UUXm57ghMj3KUUYOw8v56NyOsvVa3u6UNF5Mt8 AlbRD3mQ9A16jrRqkAKGjxUmCww+Xo5rCC1reH9y9gsL+1xTlbo2PJozad3XAHZuvmBW Z0HccCl85AY2wG+0R4R0uYqES1EFqpLmFUnb6hTAatq7Akh9x0CaAOFIm8dbbegaV8dd na6Zc3LmPXrMcjpAMY5l5m8yO58pBXKuuD7stTEyCw+yafpa6HBKfYgkK1YkcPmgvM+K 64Kb7no/i5PaBowsG0M01LBWGtotJSzGFEYMjWyqucaF/61l6aXZ/OzNT6iqhMfU5iNl nerA== X-Gm-Message-State: AOAM531knCy05JRz7OvpsU21PwjBhGMK1CXa1EZ+1fswNyk8+pkIsW9z Qh4iMwYkQcpEVTPArEdYRLU= X-Google-Smtp-Source: ABdhPJzu+bvTv2ETAmmA8H/698dPtkwc+wD9tZ9AfXts1Srn6oiDegxVlnRzKu4dhvG2ov+0cXFpWw== X-Received: by 2002:a5d:638b:: with SMTP id p11mr18656371wru.380.1625533583857; Mon, 05 Jul 2021 18:06:23 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id m12sm13379418wms.24.2021.07.05.18.06.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jul 2021 18:06:23 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: Lorenzo Pieralisi , Kishon Vijay Abraham I , linux-pci@vger.kernel.org Subject: [PATCH v2 1/4] PCI/sysfs: Move to kstrtobool() to handle user input Date: Tue, 6 Jul 2021 01:06:19 +0000 Message-Id: <20210706010622.3058968-1-kw@linux.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org A common use case for many PCI sysfs objects is to either enable some functionality or trigger an action following a write to a given attribute where the value is written would be simply either "1" or "0" synonymous with either disabling or enabling something. Parsing and validation of the input values are currently done using the kstrtoul() function to convert anything in the string buffer into an integer value - anything non-zero would be accepted as "enable" and zero simply as "disable". For a while now, the kernel offers another function called kstrtobool() which was created to parse common user inputs into a boolean value, so that a range of values such as "y", "n", "1", "0", "on" and "off" handled in a case-insensitive manner would yield a boolean true or false result accordingly after the input string has been parsed. Thus, move to kstrtobool() over kstrtoul() as it's a better fit for parsing user input, and it also implicitly offers a range check as only a finite amount of possible input values will be considered as valid. Related: commit d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents") commit ef951599074b ("lib: move strtobool() to kstrtobool()") commit a81a5a17d44b ("lib: add "on"/"off" support to kstrtobool") commit 1404297ebf76 ("lib: update single-char callers of strtobool()") Signed-off-by: Krzysztof WilczyƄski --- drivers/pci/pci-sysfs.c | 97 ++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index beb8d1f4fafe..0f98c4843764 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -63,13 +63,13 @@ static ssize_t broken_parity_status_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { + bool enable; struct pci_dev *pdev = to_pci_dev(dev); - unsigned long val; - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &enable) < 0) return -EINVAL; - pdev->broken_parity_status = !!val; + pdev->broken_parity_status = enable; return count; } @@ -271,12 +271,12 @@ static DEVICE_ATTR_RO(modalias); static ssize_t enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { + bool enable; struct pci_dev *pdev = to_pci_dev(dev); - unsigned long val; - ssize_t result = kstrtoul(buf, 0, &val); + ssize_t result = 0; - if (result < 0) - return result; + if (kstrtobool(buf, &enable) < 0) + return -EINVAL; /* this can crash the machine when done on the "wrong" device */ if (!capable(CAP_SYS_ADMIN)) @@ -285,7 +285,7 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr, device_lock(dev); if (dev->driver) result = -EBUSY; - else if (val) + else if (enable) result = pci_enable_device(pdev); else if (pci_is_enabled(pdev)) pci_disable_device(pdev); @@ -311,15 +311,14 @@ static ssize_t numa_node_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { + int node; struct pci_dev *pdev = to_pci_dev(dev); - int node, ret; if (!capable(CAP_SYS_ADMIN)) return -EPERM; - ret = kstrtoint(buf, 0, &node); - if (ret) - return ret; + if (kstrtoint(buf, 0, &node) < 0) + return -EINVAL; if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) return -EINVAL; @@ -374,11 +373,11 @@ static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr, static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { + bool enable; struct pci_dev *pdev = to_pci_dev(dev); struct pci_bus *subordinate = pdev->subordinate; - unsigned long val; - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &enable) < 0) return -EINVAL; if (!capable(CAP_SYS_ADMIN)) @@ -390,32 +389,32 @@ static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, * already requested MSI or MSI-X. */ if (!subordinate) { - pdev->no_msi = !val; + pdev->no_msi = !enable; pci_info(pdev, "MSI/MSI-X %s for future drivers\n", - val ? "allowed" : "disallowed"); + enable ? "allowed" : "disallowed"); return count; } - if (val) + if (enable) subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; else subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", - val ? "allowed" : "disallowed"); + enable ? "allowed" : "disallowed"); return count; } static DEVICE_ATTR_RW(msi_bus); static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count) { - unsigned long val; + bool rescan; struct pci_bus *b = NULL; - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &rescan) < 0) return -EINVAL; - if (val) { + if (rescan) { pci_lock_rescan_remove(); while ((b = pci_find_next_bus(b)) != NULL) pci_rescan_bus(b); @@ -443,13 +442,13 @@ static ssize_t dev_rescan_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - unsigned long val; + bool rescan; struct pci_dev *pdev = to_pci_dev(dev); - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &rescan) < 0) return -EINVAL; - if (val) { + if (rescan) { pci_lock_rescan_remove(); pci_rescan_bus(pdev->bus); pci_unlock_rescan_remove(); @@ -462,12 +461,12 @@ static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL, static ssize_t remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - unsigned long val; + bool remove; - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &remove) < 0) return -EINVAL; - if (val && device_remove_file_self(dev, attr)) + if (remove && device_remove_file_self(dev, attr)) pci_stop_and_remove_bus_device_locked(to_pci_dev(dev)); return count; } @@ -478,13 +477,13 @@ static ssize_t bus_rescan_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - unsigned long val; + bool rescan; struct pci_bus *bus = to_pci_bus(dev); - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &rescan) < 0) return -EINVAL; - if (val) { + if (rescan) { pci_lock_rescan_remove(); if (!pci_is_root_bus(bus) && list_empty(&bus->devices)) pci_rescan_bus_bridge_resize(bus->self); @@ -502,14 +501,14 @@ static ssize_t d3cold_allowed_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { + bool allowed; struct pci_dev *pdev = to_pci_dev(dev); - unsigned long val; - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &allowed) < 0) return -EINVAL; - pdev->d3cold_allowed = !!val; - if (pdev->d3cold_allowed) + pdev->d3cold_allowed = allowed; + if (allowed) pci_d3cold_enable(pdev); else pci_d3cold_disable(pdev); @@ -1257,12 +1256,13 @@ static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) { + bool enable; struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - if ((off == 0) && (*buf == '0') && (count == 2)) - pdev->rom_attr_enabled = 0; - else - pdev->rom_attr_enabled = 1; + if (kstrtobool(buf, &enable) < 0) + return -EINVAL; + + pdev->rom_attr_enabled = enable; return count; } @@ -1337,23 +1337,20 @@ static const struct attribute_group pci_dev_rom_attr_group = { static ssize_t reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { + bool reset; struct pci_dev *pdev = to_pci_dev(dev); - unsigned long val; - ssize_t result = kstrtoul(buf, 0, &val); - - if (result < 0) - return result; + ssize_t result = 0; - if (val != 1) + if (kstrtobool(buf, &reset) < 0) return -EINVAL; - pm_runtime_get_sync(dev); - result = pci_reset_function(pdev); - pm_runtime_put(dev); - if (result < 0) - return result; + if (reset) { + pm_runtime_get_sync(dev); + result = pci_reset_function(pdev); + pm_runtime_put(dev); + } - return count; + return result < 0 ? result : count; } static DEVICE_ATTR_WO(reset);