linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Krzysztof Wilczyński" <kw@linux.com>
To: Amey Narkhede <ameynarkhede03@gmail.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>,
	alex.williamson@redhat.com,
	Raphael Norwitz <raphael.norwitz@nutanix.com>,
	linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
	Shanker Donthineni <sdonthineni@nvidia.com>,
	Sinan Kaya <okaya@kernel.org>
Subject: Re: [PATCH v5 4/7] PCI/sysfs: Allow userspace to query and set device reset mechanism
Date: Sun, 6 Jun 2021 14:58:00 +0200	[thread overview]
Message-ID: <20210606125800.GA76573@rocinante.localdomain> (raw)
In-Reply-To: <20210529192527.2708-5-ameynarkhede03@gmail.com>

Hi Amey and Shanker,

[...]
> +static ssize_t reset_method_show(struct device *dev,
> +				 struct device_attribute *attr,
> +				 char *buf)
> +{
> +	struct pci_dev *pdev = to_pci_dev(dev);
> +	ssize_t len = 0;
> +	int i, prio;
> +
> +	for (prio = PCI_RESET_METHODS_NUM; prio; prio--) {
> +		for (i = 0; i < PCI_RESET_METHODS_NUM; i++) {
> +			if (prio == pdev->reset_methods[i]) {
> +				len += sysfs_emit_at(buf, len, "%s%s",
> +						     len ? "," : "",
> +						     pci_reset_fn_methods[i].name);
> +				break;
> +			}
> +		}
> +
> +		if (i == PCI_RESET_METHODS_NUM)
> +			break;
> +	}
> +
> +	return len;
> +}

Make sure to include trailing newline when exposing values through the
sysfs object to the userspace in the above show() function.

[...]
> +static ssize_t reset_method_store(struct device *dev,
> +				  struct device_attribute *attr,
> +				  const char *buf, size_t count)
[...]
> +
> +	while ((name = strsep((char **)&buf, ",")) != NULL) {
[...]

This is something that I wonder could benefit from the following:

  char *options, *end;

  if (count >= (PAGE_SIZE - 1))
	return -EINVAL;
  
  options = kstrndup(buf, count, GFP_KERNEL);
  if (!options)
  	return -ENOMEM;
  
  while ((name = strsep(&options, ",")) != NULL) {
  	...
  }
  
  ...
  
  kfree(options);

Why?  To avoid changing the string buffer that has been passed to
reset_method_store() as strsep() while parsing will update the content
of the buffer.  The cast to (char **), aside of most definitely allowing
to suppress the probable compiler warning, will also allow for what
should be a technically a constant string (to which we got a pointer to)
to be modified.  I am not sure how much could this be of a problem, but
I would try not to do it, if possible.

[...]
> +set_reset_methods:
> +	memcpy(pdev->reset_methods, reset_methods, sizeof(reset_methods));
> +	return count;
> +}
> +
> +static DEVICE_ATTR_RW(reset_method);

A small nitpikc: customary there is no space (a newline) between the
function and the macro, the macro follows immediately after.

	Krzysztof

  parent reply	other threads:[~2021-06-06 12:58 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-29 19:25 [PATCH v5 0/7] Expose and manage PCI device reset Amey Narkhede
2021-05-29 19:25 ` [PATCH v5 1/7] PCI: Add pcie_reset_flr to follow calling convention of other reset methods Amey Narkhede
2021-05-29 19:25 ` [PATCH v5 2/7] PCI: Add new array for keeping track of ordering of " Amey Narkhede
2021-06-02 21:37   ` Shanker R Donthineni
2021-06-05 20:55   ` Bjorn Helgaas
2021-05-29 19:25 ` [PATCH v5 3/7] PCI: Remove reset_fn field from pci_dev Amey Narkhede
2021-06-02 22:16   ` Shanker R Donthineni
2021-06-05 20:57   ` Bjorn Helgaas
2021-05-29 19:25 ` [PATCH v5 4/7] PCI/sysfs: Allow userspace to query and set device reset mechanism Amey Narkhede
2021-06-02 22:17   ` Shanker R Donthineni
2021-06-06 12:58   ` Krzysztof Wilczyński [this message]
2021-06-06 14:27     ` Shanker R Donthineni
2021-05-29 19:25 ` [PATCH v5 5/7] PCI: Add support for a function level reset based on _RST method Amey Narkhede
2021-06-05 20:53   ` Bjorn Helgaas
2021-06-06  0:04     ` Shanker R Donthineni
2021-06-06  5:00     ` Shanker R Donthineni
2021-06-09 12:51     ` Shanker R Donthineni
2021-05-29 19:25 ` [PATCH v5 6/7] PCI: Enable NO_BUS_RESET quirk for Nvidia GPUs Amey Narkhede
2021-05-29 19:25 ` [PATCH v5 7/7] PCI: Change the type of probe argument in reset functions Amey Narkhede
2021-06-02 22:15   ` Shanker R Donthineni

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=20210606125800.GA76573@rocinante.localdomain \
    --to=kw@linux.com \
    --cc=alex.williamson@redhat.com \
    --cc=ameynarkhede03@gmail.com \
    --cc=bhelgaas@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=okaya@kernel.org \
    --cc=raphael.norwitz@nutanix.com \
    --cc=sdonthineni@nvidia.com \
    /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;
as well as URLs for NNTP newsgroup(s).