Skip to content

Commit

Permalink
PCI/sysfs: Return -EINVAL consistently from "store" functions
Browse files Browse the repository at this point in the history
At the moment, most of the "store" functions that handle input from the
userspace via the sysfs objects commonly returns the -EINVAL error code
should the value provided fail validation and/or type conversion.  This
error code is a clear message to the userspace that the provided value
is not a valid input.

However, some of the "show" functions return the error code as-is as
returned from the helper functions used to parse the input value, and in
which case the error code can be either -EINVAL or -ERANGE.  The former
would often be a return from the kstrtobool() function and the latter
will most likely be originating from other kstr*() functions family such
as e.g., kstrtou8(), kstrtou32(), etc.

The -EINVAL is commonly returned as the error code to indicate that the
value provided is invalid, and the -ERANGE code is not very useful in
the userspace, thus normalize the return error code to be -EINVAL for
when the validation and/or type conversion fails.

Signed-off-by: Krzysztof Wilczyński <kw@linux.com>
  • Loading branch information
kwilczynski authored and intel-lab-lkp committed Jul 5, 2021
1 parent 7123f48 commit f58c20f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 41 deletions.
18 changes: 6 additions & 12 deletions drivers/pci/endpoint/functions/pci-epf-ntb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1928,11 +1928,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; \
\
Expand Down Expand Up @@ -1961,11 +1959,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; \
Expand All @@ -1986,11 +1982,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;
Expand Down
35 changes: 12 additions & 23 deletions drivers/pci/endpoint/pci-ep-cfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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; \
}
Expand All @@ -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; \
}
Expand All @@ -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; \
}
Expand All @@ -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;

Expand All @@ -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;

Expand Down
10 changes: 4 additions & 6 deletions drivers/pci/iov.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ static ssize_t sriov_vf_msix_count_store(struct device *dev,
struct pci_dev *pdev = pci_physfn(vf_dev);
int val, ret;

ret = kstrtoint(buf, 0, &val);
if (ret)
return ret;
if (kstrtoint(buf, 0, &val) < 0)
return -EINVAL;

if (val < 0)
return -EINVAL;
Expand Down Expand Up @@ -379,9 +378,8 @@ static ssize_t sriov_numvfs_store(struct device *dev,
int ret;
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;
Expand Down

0 comments on commit f58c20f

Please sign in to comment.