[PATCH v2 i-g-t 2/7] lib/xe/xe_sriov_debugfs: Add validation for provisioned ranges
Laguna, Lukasz
lukasz.laguna at intel.com
Wed Jan 15 15:55:55 UTC 2025
On 1/14/2025 16:08, Marcin Bernatowicz wrote:
> Introduce `xe_sriov_pf_debugfs_read_check_ranges`, adding a validation
> step on top of `xe_sriov_pf_debugfs_read_provisioned_ranges`.
>
> Enforce checks for:
> - Ranges when no VFs are expected.
> - Duplicate, missing, or out-of-range VF IDs.
>
> The function ensures the returned ranges are sorted by VF ID.
>
> Signed-off-by: Marcin Bernatowicz<marcin.bernatowicz at linux.intel.com>
> Cc: Adam Miszczak<adam.miszczak at linux.intel.com>
> Cc: Jakub Kolakowski<jakub1.kolakowski at intel.com>
> Cc: Lukasz Laguna<lukasz.laguna at intel.com>
> Cc: Michał Wajdeczko<michal.wajdeczko at intel.com>
> Cc: Michał Winiarski<michal.winiarski at intel.com>
> Cc: Narasimha C V<narasimha.c.v at intel.com>
> Cc: Piotr Piórkowski<piotr.piorkowski at intel.com>
> Cc: Satyanarayana K V P<satyanarayana.k.v.p at intel.com>
> Cc: Tomasz Lis<tomasz.lis at intel.com>
> ---
> lib/xe/xe_sriov_debugfs.c | 116 ++++++++++++++++++++++++++++++++++++++
> lib/xe/xe_sriov_debugfs.h | 4 ++
> 2 files changed, 120 insertions(+)
>
> diff --git a/lib/xe/xe_sriov_debugfs.c b/lib/xe/xe_sriov_debugfs.c
> index 92a477764..abb1bf7d5 100644
> --- a/lib/xe/xe_sriov_debugfs.c
> +++ b/lib/xe/xe_sriov_debugfs.c
> @@ -206,6 +206,122 @@ cleanup:
> return ret;
> }
>
> +static int compare_ranges_by_vf_id(const void *a, const void *b)
> +{
> + const struct xe_sriov_provisioned_range *range_a = a;
> + const struct xe_sriov_provisioned_range *range_b = b;
> +
> + return (range_a->vf_id - range_b->vf_id);
> +}
> +
> +#define MAX_DEBUG_ENTRIES 70U
> +
> +static int validate_vf_ids(enum xe_sriov_shared_res res,
> + struct xe_sriov_provisioned_range *ranges,
> + unsigned int nr_ranges, unsigned int expected_num_vfs)
> +{
> + unsigned int current_vf_id = 0;
> +
> + /* If no VFs are expected, ensure no ranges are provided */
> + if (expected_num_vfs == 0) {
> + if (nr_ranges > 0) {
> + unsigned int limit = min(nr_ranges, MAX_DEBUG_ENTRIES);
> +
> + igt_debug("%s: Unexpected %u ranges when expected num_vfs == 0\n",
> + xe_sriov_debugfs_provisioned_attr_name(res),
> + nr_ranges);
> + for (unsigned int i = 0; i < limit; i++) {
> + igt_debug((res == XE_SRIOV_SHARED_RES_GGTT) ?
> + "%s:VF%u: %lx-%lx\n" :
> + "%s:VF%u: %lu-%lu\n",
> + xe_sriov_shared_res_to_string(res),
> + ranges[i].vf_id, ranges[i].start, ranges[i].end);
> + }
> + igt_debug_on_f(nr_ranges > MAX_DEBUG_ENTRIES,
> + "%s: Output truncated to first %u ranges out of %u\n",
> + xe_sriov_debugfs_provisioned_attr_name(res),
> + MAX_DEBUG_ENTRIES, nr_ranges);
> +
> + return -ERANGE;
> + }
> + return 0; /* Valid case: no VFs, no ranges */
> + }
> +
> + if (igt_debug_on_f(nr_ranges == 0,
> + "%s: No VF ranges\n",
> + xe_sriov_debugfs_provisioned_attr_name(res)))
> + return -ENOENT;
> +
> + igt_assert(ranges);
> + qsort(ranges, nr_ranges, sizeof(ranges[0]), compare_ranges_by_vf_id);
> +
> + for (unsigned int i = 0; i < nr_ranges; i++) {
> + unsigned int vf_id = ranges[i].vf_id;
> +
> + if (igt_debug_on_f(vf_id == current_vf_id,
> + "%s: Duplicate VF%u entry found\n",
> + xe_sriov_debugfs_provisioned_attr_name(res), vf_id))
> + return -EEXIST;
> +
> + if (igt_debug_on_f(vf_id < 1 || vf_id > expected_num_vfs,
> + "%s: Out of range VF%u\n",
> + xe_sriov_debugfs_provisioned_attr_name(res), vf_id))
> + return -ERANGE;
> +
> + if (igt_debug_on_f(vf_id > current_vf_id + 1,
> + "%s: Missing VF%u\n",
> + xe_sriov_debugfs_provisioned_attr_name(res),
> + current_vf_id + 1))
> + return -ESRCH;
> +
> + current_vf_id = vf_id;
> + }
> +
> + if (igt_debug_on_f(current_vf_id != expected_num_vfs,
> + "%s: Missing VF%u\n",
> + xe_sriov_debugfs_provisioned_attr_name(res), expected_num_vfs))
> + return -ESRCH;
> +
> + return 0;
> +}
> +
> +/**
> + * xe_sriov_pf_debugfs_read_check_ranges:
> + * @pf_fd: PF device file descriptor
> + * @res: resource
> + * @gt_id: GT number
> + * @ranges: pointer to array of provisioned ranges
> + * @expected_num_vfs: expected number of provisioned VFs
> + *
> + * Reads and validates provisioned ranges of shared resources.
> + * If successfully validated, returns num_vfs allocated ranges
> + * sorted by VF id.
> + * The caller should free the allocated space.
> + *
> + * Return: 0 if successful in reading valid ranges, otherwise negative error code.
> + */
> +int xe_sriov_pf_debugfs_read_check_ranges(int pf_fd, enum xe_sriov_shared_res res,
> + unsigned int gt_id,
> + struct xe_sriov_provisioned_range **ranges,
> + unsigned int expected_num_vfs)
> +{
> + unsigned int nr_ranges;
> + int ret;
> +
> + ret = xe_sriov_pf_debugfs_read_provisioned_ranges(pf_fd, res, gt_id,
> + ranges, &nr_ranges);
> + if (ret)
> + return ret;
> +
> + ret = validate_vf_ids(res, *ranges, nr_ranges, expected_num_vfs);
> + if (ret) {
> + free(*ranges);
> + *ranges = NULL;
> + }
> +
> + return ret;
> +}
> +
> static int xe_sriov_pf_debugfs_path_open(int pf, unsigned int vf_num,
> unsigned int gt_num)
> {
> diff --git a/lib/xe/xe_sriov_debugfs.h b/lib/xe/xe_sriov_debugfs.h
> index 2db965f9b..cd5f932be 100644
> --- a/lib/xe/xe_sriov_debugfs.h
> +++ b/lib/xe/xe_sriov_debugfs.h
> @@ -16,6 +16,10 @@ int xe_sriov_pf_debugfs_read_provisioned_ranges(int pf_fd, enum xe_sriov_shared_
> unsigned int gt_id,
> struct xe_sriov_provisioned_range **ranges,
> unsigned int *nr_ranges);
> +int xe_sriov_pf_debugfs_read_check_ranges(int pf_fd, enum xe_sriov_shared_res res,
> + unsigned int gt_id,
> + struct xe_sriov_provisioned_range **ranges,
> + unsigned int expected_num_vfs);
> int __xe_sriov_pf_debugfs_get_u32(int pf, unsigned int vf_num,
> unsigned int gt_num, const char *attr,
> uint32_t *value);
LGTM,
Reviewed-by: Lukasz Laguna <lukasz.laguna at intel.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/igt-dev/attachments/20250115/ce9074ae/attachment.htm>
More information about the igt-dev
mailing list