Commit 36f354ec authored by Krzysztof Wilczyński's avatar Krzysztof Wilczyński Committed by Bjorn Helgaas

PCI/sysfs: Return -EINVAL consistently from "store" functions

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.comSigned-off-by: default avatarKrzysztof Wilczyński <kw@linux.com>
Signed-off-by: default avatarBjorn Helgaas <bhelgaas@google.com>
parent 95e83e21
...@@ -1947,11 +1947,9 @@ static ssize_t epf_ntb_##_name##_store(struct config_item *item, \ ...@@ -1947,11 +1947,9 @@ static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
struct config_group *group = to_config_group(item); \ struct config_group *group = to_config_group(item); \
struct epf_ntb *ntb = to_epf_ntb(group); \ struct epf_ntb *ntb = to_epf_ntb(group); \
u32 val; \ u32 val; \
int ret; \
\ \
ret = kstrtou32(page, 0, &val); \ if (kstrtou32(page, 0, &val) < 0) \
if (ret) \ return -EINVAL; \
return ret; \
\ \
ntb->_name = val; \ ntb->_name = val; \
\ \
...@@ -1980,11 +1978,9 @@ static ssize_t epf_ntb_##_name##_store(struct config_item *item, \ ...@@ -1980,11 +1978,9 @@ static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
struct device *dev = &ntb->epf->dev; \ struct device *dev = &ntb->epf->dev; \
int win_no; \ int win_no; \
u64 val; \ u64 val; \
int ret; \
\ \
ret = kstrtou64(page, 0, &val); \ if (kstrtou64(page, 0, &val) < 0) \
if (ret) \ return -EINVAL; \
return ret; \
\ \
if (sscanf(#_name, "mw%d", &win_no) != 1) \ if (sscanf(#_name, "mw%d", &win_no) != 1) \
return -EINVAL; \ return -EINVAL; \
...@@ -2005,11 +2001,9 @@ static ssize_t epf_ntb_num_mws_store(struct config_item *item, ...@@ -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 config_group *group = to_config_group(item);
struct epf_ntb *ntb = to_epf_ntb(group); struct epf_ntb *ntb = to_epf_ntb(group);
u32 val; u32 val;
int ret;
ret = kstrtou32(page, 0, &val); if (kstrtou32(page, 0, &val) < 0)
if (ret) return -EINVAL;
return ret;
if (val > MAX_MW) if (val > MAX_MW)
return -EINVAL; return -EINVAL;
......
...@@ -175,9 +175,8 @@ static ssize_t pci_epc_start_store(struct config_item *item, const char *page, ...@@ -175,9 +175,8 @@ static ssize_t pci_epc_start_store(struct config_item *item, const char *page,
epc = epc_group->epc; epc = epc_group->epc;
ret = kstrtobool(page, &start); if (kstrtobool(page, &start) < 0)
if (ret) return -EINVAL;
return ret;
if (!start) { if (!start) {
pci_epc_stop(epc); pci_epc_stop(epc);
...@@ -329,13 +328,11 @@ static ssize_t pci_epf_##_name##_store(struct config_item *item, \ ...@@ -329,13 +328,11 @@ static ssize_t pci_epf_##_name##_store(struct config_item *item, \
const char *page, size_t len) \ const char *page, size_t len) \
{ \ { \
u32 val; \ u32 val; \
int ret; \
struct pci_epf *epf = to_pci_epf_group(item)->epf; \ struct pci_epf *epf = to_pci_epf_group(item)->epf; \
if (WARN_ON_ONCE(!epf->header)) \ if (WARN_ON_ONCE(!epf->header)) \
return -EINVAL; \ return -EINVAL; \
ret = kstrtou32(page, 0, &val); \ if (kstrtou32(page, 0, &val) < 0) \
if (ret) \ return -EINVAL; \
return ret; \
epf->header->_name = val; \ epf->header->_name = val; \
return len; \ return len; \
} }
...@@ -345,13 +342,11 @@ static ssize_t pci_epf_##_name##_store(struct config_item *item, \ ...@@ -345,13 +342,11 @@ static ssize_t pci_epf_##_name##_store(struct config_item *item, \
const char *page, size_t len) \ const char *page, size_t len) \
{ \ { \
u16 val; \ u16 val; \
int ret; \
struct pci_epf *epf = to_pci_epf_group(item)->epf; \ struct pci_epf *epf = to_pci_epf_group(item)->epf; \
if (WARN_ON_ONCE(!epf->header)) \ if (WARN_ON_ONCE(!epf->header)) \
return -EINVAL; \ return -EINVAL; \
ret = kstrtou16(page, 0, &val); \ if (kstrtou16(page, 0, &val) < 0) \
if (ret) \ return -EINVAL; \
return ret; \
epf->header->_name = val; \ epf->header->_name = val; \
return len; \ return len; \
} }
...@@ -361,13 +356,11 @@ static ssize_t pci_epf_##_name##_store(struct config_item *item, \ ...@@ -361,13 +356,11 @@ static ssize_t pci_epf_##_name##_store(struct config_item *item, \
const char *page, size_t len) \ const char *page, size_t len) \
{ \ { \
u8 val; \ u8 val; \
int ret; \
struct pci_epf *epf = to_pci_epf_group(item)->epf; \ struct pci_epf *epf = to_pci_epf_group(item)->epf; \
if (WARN_ON_ONCE(!epf->header)) \ if (WARN_ON_ONCE(!epf->header)) \
return -EINVAL; \ return -EINVAL; \
ret = kstrtou8(page, 0, &val); \ if (kstrtou8(page, 0, &val) < 0) \
if (ret) \ return -EINVAL; \
return ret; \
epf->header->_name = val; \ epf->header->_name = val; \
return len; \ return len; \
} }
...@@ -376,11 +369,9 @@ static ssize_t pci_epf_msi_interrupts_store(struct config_item *item, ...@@ -376,11 +369,9 @@ static ssize_t pci_epf_msi_interrupts_store(struct config_item *item,
const char *page, size_t len) const char *page, size_t len)
{ {
u8 val; u8 val;
int ret;
ret = kstrtou8(page, 0, &val); if (kstrtou8(page, 0, &val) < 0)
if (ret) return -EINVAL;
return ret;
to_pci_epf_group(item)->epf->msi_interrupts = val; 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, ...@@ -398,11 +389,9 @@ static ssize_t pci_epf_msix_interrupts_store(struct config_item *item,
const char *page, size_t len) const char *page, size_t len)
{ {
u16 val; u16 val;
int ret;
ret = kstrtou16(page, 0, &val); if (kstrtou16(page, 0, &val) < 0)
if (ret) return -EINVAL;
return ret;
to_pci_epf_group(item)->epf->msix_interrupts = val; to_pci_epf_group(item)->epf->msix_interrupts = val;
......
...@@ -183,11 +183,10 @@ static ssize_t sriov_vf_msix_count_store(struct device *dev, ...@@ -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 *vf_dev = to_pci_dev(dev);
struct pci_dev *pdev = pci_physfn(vf_dev); struct pci_dev *pdev = pci_physfn(vf_dev);
int val, ret; int val, ret = 0;
ret = kstrtoint(buf, 0, &val); if (kstrtoint(buf, 0, &val) < 0)
if (ret) return -EINVAL;
return ret;
if (val < 0) if (val < 0)
return -EINVAL; return -EINVAL;
...@@ -376,12 +375,11 @@ static ssize_t sriov_numvfs_store(struct device *dev, ...@@ -376,12 +375,11 @@ static ssize_t sriov_numvfs_store(struct device *dev,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
int ret; int ret = 0;
u16 num_vfs; u16 num_vfs;
ret = kstrtou16(buf, 0, &num_vfs); if (kstrtou16(buf, 0, &num_vfs) < 0)
if (ret < 0) return -EINVAL;
return ret;
if (num_vfs > pci_sriov_get_totalvfs(pdev)) if (num_vfs > pci_sriov_get_totalvfs(pdev))
return -ERANGE; return -ERANGE;
......
...@@ -273,15 +273,14 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr, ...@@ -273,15 +273,14 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
unsigned long val; unsigned long val;
ssize_t result; ssize_t result = 0;
/* this can crash the machine when done on the "wrong" device */ /* this can crash the machine when done on the "wrong" device */
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
return -EPERM; return -EPERM;
result = kstrtoul(buf, 0, &val); if (kstrtoul(buf, 0, &val) < 0)
if (result < 0) return -EINVAL;
return result;
device_lock(dev); device_lock(dev);
if (dev->driver) if (dev->driver)
...@@ -313,14 +312,13 @@ static ssize_t numa_node_store(struct device *dev, ...@@ -313,14 +312,13 @@ static ssize_t numa_node_store(struct device *dev,
size_t count) size_t count)
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
int node, ret; int node;
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
return -EPERM; return -EPERM;
ret = kstrtoint(buf, 0, &node); if (kstrtoint(buf, 0, &node) < 0)
if (ret) return -EINVAL;
return ret;
if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
return -EINVAL; return -EINVAL;
...@@ -1340,10 +1338,10 @@ static ssize_t reset_store(struct device *dev, struct device_attribute *attr, ...@@ -1340,10 +1338,10 @@ static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
unsigned long val; unsigned long val;
ssize_t result = kstrtoul(buf, 0, &val); ssize_t result;
if (result < 0) if (kstrtoul(buf, 0, &val) < 0)
return result; return -EINVAL;
if (val != 1) if (val != 1)
return -EINVAL; return -EINVAL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment