PCI/sysfs: Return -EINVAL consistently from "store" functions
authorKrzysztof Wilczyński <kw@linux.com>
Wed, 15 Sep 2021 23:01:26 +0000 (23:01 +0000)
committerBjorn Helgaas <bhelgaas@google.com>
Tue, 28 Sep 2021 22:47:04 +0000 (17:47 -0500)
Most of the "store" functions that handle userspace input via sysfs return
-EINVAL should the value fail validation and/or type conversion.  This
error code is a clear message to userspace that the value is not a valid
input.

However, some of the "show" functions return input parsing error codes
as-is, which may be either -EINVAL or -ERANGE.  The former would often be
from kstrtobool(), and the latter typically from other kstr*() functions
such as kstrtou8(), kstrtou32(), kstrtoint(), etc.

-EINVAL is commonly returned as the error code to indicate that the value
provided is invalid, but -ERANGE is not very useful in userspace.

Therefore, normalize the return error code to be -EINVAL for when the
validation and/or type conversion fails.

Link: https://lore.kernel.org/r/20210915230127.2495723-2-kw@linux.com
Signed-off-by: Krzysztof Wilczyński <kw@linux.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
drivers/pci/endpoint/functions/pci-epf-ntb.c
drivers/pci/endpoint/pci-ep-cfs.c
drivers/pci/iov.c
drivers/pci/pci-sysfs.c

index 8b47561..e674891 100644 (file)
@@ -1947,11 +1947,9 @@ static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
        struct config_group *group = to_config_group(item);             \
        struct epf_ntb *ntb = to_epf_ntb(group);                        \
        u32 val;                                                        \
-       int ret;                                                        \
                                                                        \
-       ret = kstrtou32(page, 0, &val);                                 \
-       if (ret)                                                        \
-               return ret;                                             \
+       if (kstrtou32(page, 0, &val) < 0)                               \
+               return -EINVAL;                                         \
                                                                        \
        ntb->_name = val;                                               \
                                                                        \
@@ -1980,11 +1978,9 @@ static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
        struct device *dev = &ntb->epf->dev;                            \
        int win_no;                                                     \
        u64 val;                                                        \
-       int ret;                                                        \
                                                                        \
-       ret = kstrtou64(page, 0, &val);                                 \
-       if (ret)                                                        \
-               return ret;                                             \
+       if (kstrtou64(page, 0, &val) < 0)                               \
+               return -EINVAL;                                         \
                                                                        \
        if (sscanf(#_name, "mw%d", &win_no) != 1)                       \
                return -EINVAL;                                         \
@@ -2005,11 +2001,9 @@ static ssize_t epf_ntb_num_mws_store(struct config_item *item,
        struct config_group *group = to_config_group(item);
        struct epf_ntb *ntb = to_epf_ntb(group);
        u32 val;
-       int ret;
 
-       ret = kstrtou32(page, 0, &val);
-       if (ret)
-               return ret;
+       if (kstrtou32(page, 0, &val) < 0)
+               return -EINVAL;
 
        if (val > MAX_MW)
                return -EINVAL;
index 9999118..19bc0e8 100644 (file)
@@ -175,9 +175,8 @@ static ssize_t pci_epc_start_store(struct config_item *item, const char *page,
 
        epc = epc_group->epc;
 
-       ret = kstrtobool(page, &start);
-       if (ret)
-               return ret;
+       if (kstrtobool(page, &start) < 0)
+               return -EINVAL;
 
        if (!start) {
                pci_epc_stop(epc);
@@ -329,13 +328,11 @@ static ssize_t pci_epf_##_name##_store(struct config_item *item,         \
                                       const char *page, size_t len)           \
 {                                                                             \
        u32 val;                                                               \
-       int ret;                                                               \
        struct pci_epf *epf = to_pci_epf_group(item)->epf;                     \
        if (WARN_ON_ONCE(!epf->header))                                        \
                return -EINVAL;                                                \
-       ret = kstrtou32(page, 0, &val);                                        \
-       if (ret)                                                               \
-               return ret;                                                    \
+       if (kstrtou32(page, 0, &val) < 0)                                      \
+               return -EINVAL;                                                \
        epf->header->_name = val;                                              \
        return len;                                                            \
 }
@@ -345,13 +342,11 @@ static ssize_t pci_epf_##_name##_store(struct config_item *item,         \
                                       const char *page, size_t len)           \
 {                                                                             \
        u16 val;                                                               \
-       int ret;                                                               \
        struct pci_epf *epf = to_pci_epf_group(item)->epf;                     \
        if (WARN_ON_ONCE(!epf->header))                                        \
                return -EINVAL;                                                \
-       ret = kstrtou16(page, 0, &val);                                        \
-       if (ret)                                                               \
-               return ret;                                                    \
+       if (kstrtou16(page, 0, &val) < 0)                                      \
+               return -EINVAL;                                                \
        epf->header->_name = val;                                              \
        return len;                                                            \
 }
@@ -361,13 +356,11 @@ static ssize_t pci_epf_##_name##_store(struct config_item *item,         \
                                       const char *page, size_t len)           \
 {                                                                             \
        u8 val;                                                                \
-       int ret;                                                               \
        struct pci_epf *epf = to_pci_epf_group(item)->epf;                     \
        if (WARN_ON_ONCE(!epf->header))                                        \
                return -EINVAL;                                                \
-       ret = kstrtou8(page, 0, &val);                                         \
-       if (ret)                                                               \
-               return ret;                                                    \
+       if (kstrtou8(page, 0, &val) < 0)                                       \
+               return -EINVAL;                                                \
        epf->header->_name = val;                                              \
        return len;                                                            \
 }
@@ -376,11 +369,9 @@ static ssize_t pci_epf_msi_interrupts_store(struct config_item *item,
                                            const char *page, size_t len)
 {
        u8 val;
-       int ret;
 
-       ret = kstrtou8(page, 0, &val);
-       if (ret)
-               return ret;
+       if (kstrtou8(page, 0, &val) < 0)
+               return -EINVAL;
 
        to_pci_epf_group(item)->epf->msi_interrupts = val;
 
@@ -398,11 +389,9 @@ static ssize_t pci_epf_msix_interrupts_store(struct config_item *item,
                                             const char *page, size_t len)
 {
        u16 val;
-       int ret;
 
-       ret = kstrtou16(page, 0, &val);
-       if (ret)
-               return ret;
+       if (kstrtou16(page, 0, &val) < 0)
+               return -EINVAL;
 
        to_pci_epf_group(item)->epf->msix_interrupts = val;
 
index dafdc65..0267977 100644 (file)
@@ -183,11 +183,10 @@ static ssize_t sriov_vf_msix_count_store(struct device *dev,
 {
        struct pci_dev *vf_dev = to_pci_dev(dev);
        struct pci_dev *pdev = pci_physfn(vf_dev);
-       int val, ret;
+       int val, ret = 0;
 
-       ret = kstrtoint(buf, 0, &val);
-       if (ret)
-               return ret;
+       if (kstrtoint(buf, 0, &val) < 0)
+               return -EINVAL;
 
        if (val < 0)
                return -EINVAL;
@@ -376,12 +375,11 @@ static ssize_t sriov_numvfs_store(struct device *dev,
                                  const char *buf, size_t count)
 {
        struct pci_dev *pdev = to_pci_dev(dev);
-       int ret;
+       int ret = 0;
        u16 num_vfs;
 
-       ret = kstrtou16(buf, 0, &num_vfs);
-       if (ret < 0)
-               return ret;
+       if (kstrtou16(buf, 0, &num_vfs) < 0)
+               return -EINVAL;
 
        if (num_vfs > pci_sriov_get_totalvfs(pdev))
                return -ERANGE;
index 6832e16..a092fc0 100644 (file)
@@ -273,15 +273,14 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
 {
        struct pci_dev *pdev = to_pci_dev(dev);
        unsigned long val;
-       ssize_t result;
+       ssize_t result = 0;
 
        /* this can crash the machine when done on the "wrong" device */
        if (!capable(CAP_SYS_ADMIN))
                return -EPERM;
 
-       result = kstrtoul(buf, 0, &val);
-       if (result < 0)
-               return result;
+       if (kstrtoul(buf, 0, &val) < 0)
+               return -EINVAL;
 
        device_lock(dev);
        if (dev->driver)
@@ -313,14 +312,13 @@ static ssize_t numa_node_store(struct device *dev,
                               size_t count)
 {
        struct pci_dev *pdev = to_pci_dev(dev);
-       int node, ret;
+       int node;
 
        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;
@@ -1340,10 +1338,10 @@ static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
 {
        struct pci_dev *pdev = to_pci_dev(dev);
        unsigned long val;
-       ssize_t result = kstrtoul(buf, 0, &val);
+       ssize_t result;
 
-       if (result < 0)
-               return result;
+       if (kstrtoul(buf, 0, &val) < 0)
+               return -EINVAL;
 
        if (val != 1)
                return -EINVAL;