From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Bjorn Helgaas <bhelgaas@google.com>,
linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Subject: [PATCH 2/3] PCI/sysfs: Use __free() in reset_method_store()
Date: Mon, 28 Oct 2024 19:40:45 +0200 [thread overview]
Message-ID: <20241028174046.1736-3-ilpo.jarvinen@linux.intel.com> (raw)
In-Reply-To: <20241028174046.1736-1-ilpo.jarvinen@linux.intel.com>
Use __free() from cleanup.h to handle freeing options in
reset_method_store() as it simplifies the code flow.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
drivers/pci/pci-sysfs.c | 18 +++++++-----------
1 file changed, 7 insertions(+), 11 deletions(-)
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 507478082454..74e4e0917898 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -13,6 +13,7 @@
*/
#include <linux/bitfield.h>
+#include <linux/cleanup.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/pci.h>
@@ -1430,7 +1431,7 @@ static ssize_t reset_method_store(struct device *dev,
const char *buf, size_t count)
{
struct pci_dev *pdev = to_pci_dev(dev);
- char *options, *tmp_options, *name;
+ char *tmp_options, *name;
int m, n;
u8 reset_methods[PCI_NUM_RESET_METHODS] = { 0 };
@@ -1445,7 +1446,7 @@ static ssize_t reset_method_store(struct device *dev,
return count;
}
- options = kstrndup(buf, count, GFP_KERNEL);
+ char *options __free(kfree) = kstrndup(buf, count, GFP_KERNEL);
if (!options)
return -ENOMEM;
@@ -1457,20 +1458,21 @@ static ssize_t reset_method_store(struct device *dev,
name = strim(name);
+ /* Leave previous methods unchanged if input is invalid */
m = reset_method_lookup(name);
if (!m) {
pci_err(pdev, "Invalid reset method '%s'", name);
- goto error;
+ return -EINVAL;
}
if (pci_reset_fn_methods[m].reset_fn(pdev, PCI_RESET_PROBE)) {
pci_err(pdev, "Unsupported reset method '%s'", name);
- goto error;
+ return -EINVAL;
}
if (n == PCI_NUM_RESET_METHODS - 1) {
pci_err(pdev, "Too many reset methods\n");
- goto error;
+ return -EINVAL;
}
reset_methods[n++] = m;
@@ -1483,13 +1485,7 @@ static ssize_t reset_method_store(struct device *dev,
reset_methods[0] != 1)
pci_warn(pdev, "Device-specific reset disabled/de-prioritized by user");
memcpy(pdev->reset_methods, reset_methods, sizeof(pdev->reset_methods));
- kfree(options);
return count;
-
-error:
- /* Leave previous methods unchanged */
- kfree(options);
- return -EINVAL;
}
static DEVICE_ATTR_RW(reset_method);
--
2.39.5
next prev parent reply other threads:[~2024-10-28 17:41 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-28 17:40 [PATCH 0/3] PCI/sysfs: move to sysfs funcs to pci-sysfs.c & do small tweaks Ilpo Järvinen
2024-10-28 17:40 ` [PATCH 1/3] PCI/sysfs: Move reset related sysfs code to correct file Ilpo Järvinen
2024-10-28 17:40 ` Ilpo Järvinen [this message]
2024-10-28 17:53 ` [PATCH 2/3] PCI/sysfs: Use __free() in reset_method_store() Keith Busch
2024-10-28 17:59 ` Ilpo Järvinen
2024-10-28 18:18 ` Ilpo Järvinen
2024-10-30 23:18 ` Keith Busch
2024-10-28 17:40 ` [PATCH 3/3] PCI/sysfs: Remove unnecessary zero in initializer Ilpo Järvinen
2025-01-15 11:56 ` [PATCH 0/3] PCI/sysfs: move to sysfs funcs to pci-sysfs.c & do small tweaks Krzysztof Wilczyński
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241028174046.1736-3-ilpo.jarvinen@linux.intel.com \
--to=ilpo.jarvinen@linux.intel.com \
--cc=bhelgaas@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®