From: "Michael S. Tsirkin" <mst@redhat.com>
To: Dawei Li <set_pte_at@outlook.com>
Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org
Subject: Re: [PATCH] virtio: sanity check on callback of virtio drivers
Date: Wed, 23 Nov 2022 10:49:25 -0500 [thread overview]
Message-ID: <20221123104757-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <TYCP286MB23234D3251765359630AD72ECA0C9@TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM>
On Wed, Nov 23, 2022 at 11:32:27PM +0800, Dawei Li wrote:
> This commit includes changes below:
>
> 1 Since register_virtio_driver doesn't force probe & remove
> to be mandatory callback, so it's caller's job to make the
> sanity check before invocation.
What's the point of these checks? I don't see how any driver
won't have these set. Why waste memory checking?
> 2 Replace sprintf with sysfs_emit or its variants for their
> built-in PAGE_SIZE awareness.
this makes sense.
> Signed-off-by: Dawei Li <set_pte_at@outlook.com>
> ---
> drivers/virtio/virtio.c | 23 +++++++++++++----------
> 1 file changed, 13 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index 828ced060742..e391a8dff333 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -15,7 +15,7 @@ static ssize_t device_show(struct device *_d,
> struct device_attribute *attr, char *buf)
> {
> struct virtio_device *dev = dev_to_virtio(_d);
> - return sprintf(buf, "0x%04x\n", dev->id.device);
> + return sysfs_emit(buf, "0x%04x\n", dev->id.device);
> }
> static DEVICE_ATTR_RO(device);
>
> @@ -23,7 +23,7 @@ static ssize_t vendor_show(struct device *_d,
> struct device_attribute *attr, char *buf)
> {
> struct virtio_device *dev = dev_to_virtio(_d);
> - return sprintf(buf, "0x%04x\n", dev->id.vendor);
> + return sysfs_emit(buf, "0x%04x\n", dev->id.vendor);
> }
> static DEVICE_ATTR_RO(vendor);
>
> @@ -31,7 +31,7 @@ static ssize_t status_show(struct device *_d,
> struct device_attribute *attr, char *buf)
> {
> struct virtio_device *dev = dev_to_virtio(_d);
> - return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
> + return sysfs_emit(buf, "0x%08x\n", dev->config->get_status(dev));
> }
> static DEVICE_ATTR_RO(status);
>
> @@ -39,7 +39,7 @@ static ssize_t modalias_show(struct device *_d,
> struct device_attribute *attr, char *buf)
> {
> struct virtio_device *dev = dev_to_virtio(_d);
> - return sprintf(buf, "virtio:d%08Xv%08X\n",
> + return sysfs_emit(buf, "virtio:d%08Xv%08X\n",
> dev->id.device, dev->id.vendor);
> }
> static DEVICE_ATTR_RO(modalias);
> @@ -54,9 +54,9 @@ static ssize_t features_show(struct device *_d,
> /* We actually represent this as a bitstring, as it could be
> * arbitrary length in future. */
> for (i = 0; i < sizeof(dev->features)*8; i++)
> - len += sprintf(buf+len, "%c",
> + len += sysfs_emit_at(buf, len, "%c",
> __virtio_test_bit(dev, i) ? '1' : '0');
> - len += sprintf(buf+len, "\n");
> + len += sysfs_emit_at(buf, len, "\n");
> return len;
> }
> static DEVICE_ATTR_RO(features);
> @@ -302,9 +302,11 @@ static int virtio_dev_probe(struct device *_d)
> if (err)
> goto err;
>
> - err = drv->probe(dev);
> - if (err)
> - goto err;
> + if (drv->probe) {
> + err = drv->probe(dev);
> + if (err)
> + goto err;
> + }
>
> /* If probe didn't do it, mark device DRIVER_OK ourselves. */
> if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
> @@ -329,7 +331,8 @@ static void virtio_dev_remove(struct device *_d)
>
> virtio_config_disable(dev);
>
> - drv->remove(dev);
> + if (drv->remove)
> + drv->remove(dev);
>
> /* Driver should have reset device. */
> WARN_ON_ONCE(dev->config->get_status(dev));
> --
> 2.25.1
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
parent reply other threads:[~2022-11-23 15:49 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <TYCP286MB23234D3251765359630AD72ECA0C9@TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM>]
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=20221123104757-mutt-send-email-mst@kernel.org \
--to=mst@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=set_pte_at@outlook.com \
--cc=virtualization@lists.linux-foundation.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;
as well as URLs for NNTP newsgroup(s).