public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Alex Williamson <alex@shazbot.org>
To: Raghavendra Rao Ananta <rananta@google.com>
Cc: David Matlack <dmatlack@google.com>,
	Vipin Sharma <vipinsh@google.com>,
	Josh Hilke <jrhilke@google.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	alex@shazbot.org
Subject: Re: [PATCH v7 3/8] vfio: selftests: Introduce a sysfs lib
Date: Mon, 6 Apr 2026 15:12:34 -0600	[thread overview]
Message-ID: <20260406151234.7bbca348@shazbot.org> (raw)
In-Reply-To: <20260402173059.1018805-4-rananta@google.com>

On Thu,  2 Apr 2026 17:30:54 +0000
Raghavendra Rao Ananta <rananta@google.com> wrote:
> diff --git a/tools/testing/selftests/vfio/lib/sysfs.c b/tools/testing/selftests/vfio/lib/sysfs.c
> new file mode 100644
> index 0000000000000..f1e6889556527
> --- /dev/null
> +++ b/tools/testing/selftests/vfio/lib/sysfs.c
> @@ -0,0 +1,150 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include <fcntl.h>
> +#include <unistd.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <linux/limits.h>
> +
> +#include <libvfio.h>
> +
> +/* Enough to hold MODULE_NAME_LEN */
> +#define DRIVER_NAME_SZ 64
> +
> +#define readlink_safe(_path, _buf) ({				\
> +	int __ret = readlink(_path, _buf, sizeof(_buf) - 1);	\
> +	if (__ret != -1)					\
> +		_buf[__ret] = 0;				\
> +	__ret;							\
> +})

Not a current issue, but the use of sizeof() here is a danger for
future users that might pass something other than a stack array.  We
could avoid this by adding a build time test (untested):

    _Static_assert(!__builtin_types_compatible_p(                       \
                       __typeof__(_buf), char *),                       \
                   "readlink_safe: _buf must be an array, not a pointer"); \

Maybe overkill for an internal helper, but we could avoid the hazard.

...
> +char *sysfs_driver_get(const char *bdf)
> +{
> +	char driver_path[PATH_MAX];
> +	char path[PATH_MAX];
> +	char *out_driver;
> +	int ret;
> +
> +	out_driver = calloc(DRIVER_NAME_SZ, sizeof(char));
> +	VFIO_ASSERT_NOT_NULL(out_driver);
> +
> +	snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/driver", bdf);
> +	ret = readlink_safe(path, driver_path);
> +	if (ret == -1) {
> +		free(out_driver);
> +
> +		if (errno == ENOENT)
> +			return NULL;
> +
> +		VFIO_FAIL("Failed to read %s\n", path);
> +	}
> +
> +	strncpy(out_driver, basename(driver_path), DRIVER_NAME_SZ - 1);

Nit, I'm not really sure what DRIVER_NAME_SZ buys us other than an
artificial limit.  We're already bound by PATH_MAX and could just do a
strdup() for the return.  Neither are blockers.  Thanks,

Alex

> +	return out_driver;
> +}

  reply	other threads:[~2026-04-06 21:12 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-02 17:30 [PATCH v7 0/8] vfio: selftest: Add SR-IOV UAPI test Raghavendra Rao Ananta
2026-04-02 17:30 ` [PATCH v7 1/8] vfio: selftests: Add -Wall and -Werror to the Makefile Raghavendra Rao Ananta
2026-04-02 17:30 ` [PATCH v7 2/8] vfio: selftests: Introduce snprintf_assert() Raghavendra Rao Ananta
2026-04-02 17:30 ` [PATCH v7 3/8] vfio: selftests: Introduce a sysfs lib Raghavendra Rao Ananta
2026-04-06 21:12   ` Alex Williamson [this message]
2026-04-02 17:30 ` [PATCH v7 4/8] vfio: selftests: Extend container/iommufd setup for passing vf_token Raghavendra Rao Ananta
2026-04-02 17:30 ` [PATCH v7 5/8] vfio: selftests: Expose more vfio_pci_device functions Raghavendra Rao Ananta
2026-04-02 17:30 ` [PATCH v7 6/8] vfio: selftests: Add helper to set/override a vf_token Raghavendra Rao Ananta
2026-04-02 17:30 ` [PATCH v7 7/8] vfio: selftests: Add helpers to alloc/free vfio_pci_device Raghavendra Rao Ananta
2026-04-02 17:30 ` [PATCH v7 8/8] vfio: selftests: Add tests to validate SR-IOV UAPI Raghavendra Rao Ananta
2026-04-06 22:24   ` David Matlack

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=20260406151234.7bbca348@shazbot.org \
    --to=alex@shazbot.org \
    --cc=dmatlack@google.com \
    --cc=jrhilke@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rananta@google.com \
    --cc=vipinsh@google.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