<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <div class="moz-cite-prefix">On 11/19/2024 16:55, Marcin Bernatowicz
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:20241119155538.605000-3-marcin.bernatowicz@linux.intel.com">
      <pre wrap="" class="moz-quote-pre">Introduce an append_range helper to reduce code duplication in handling
provisioned PTE ranges. Limit debug logs to the first 70 entries for
clarity and improved reasoning. Enhance error handling by propagating
realloc failures.

Signed-off-by: Marcin Bernatowicz <a class="moz-txt-link-rfc2396E" href="mailto:marcin.bernatowicz@linux.intel.com"><marcin.bernatowicz@linux.intel.com></a>
Cc: Adam Miszczak <a class="moz-txt-link-rfc2396E" href="mailto:adam.miszczak@linux.intel.com"><adam.miszczak@linux.intel.com></a>
Cc: C V Narasimha <a class="moz-txt-link-rfc2396E" href="mailto:narasimha.c.v@intel.com"><narasimha.c.v@intel.com></a>
Cc: Jakub Kolakowski <a class="moz-txt-link-rfc2396E" href="mailto:jakub1.kolakowski@intel.com"><jakub1.kolakowski@intel.com></a>
Cc: K V P Satyanarayana <a class="moz-txt-link-rfc2396E" href="mailto:satyanarayana.k.v.p@intel.com"><satyanarayana.k.v.p@intel.com></a>
Cc: Lukasz Laguna <a class="moz-txt-link-rfc2396E" href="mailto:lukasz.laguna@intel.com"><lukasz.laguna@intel.com></a>
Cc: Michał Wajdeczko <a class="moz-txt-link-rfc2396E" href="mailto:michal.wajdeczko@intel.com"><michal.wajdeczko@intel.com></a>
Cc: Michał Winiarski <a class="moz-txt-link-rfc2396E" href="mailto:michal.winiarski@intel.com"><michal.winiarski@intel.com></a>
Cc: Piotr Piórkowski <a class="moz-txt-link-rfc2396E" href="mailto:piotr.piorkowski@intel.com"><piotr.piorkowski@intel.com></a>
Cc: Tomasz Lis <a class="moz-txt-link-rfc2396E" href="mailto:tomasz.lis@intel.com"><tomasz.lis@intel.com></a>
---
 lib/xe/xe_sriov_provisioning.c | 71 ++++++++++++++++++++++------------
 1 file changed, 47 insertions(+), 24 deletions(-)

diff --git a/lib/xe/xe_sriov_provisioning.c b/lib/xe/xe_sriov_provisioning.c
index 7cde2c240..66c0576aa 100644
--- a/lib/xe/xe_sriov_provisioning.c
+++ b/lib/xe/xe_sriov_provisioning.c
@@ -3,7 +3,7 @@
  * Copyright(c) 2024 Intel Corporation. All rights reserved.
  */
 
-#include <stdlib.h>
+#include <errno.h>
 
 #include "igt_core.h"
 #include "intel_chipset.h"
@@ -48,6 +48,37 @@ static uint64_t get_vfid_mask(int fd)
                GGTT_PTE_VFID_MASK : PRE_1250_IP_VER_GGTT_PTE_VFID_MASK;
 }
 
+#define MAX_DEBUG_ENTRIES 70
+
+static int append_range(struct xe_sriov_provisioned_range **ranges,
+                       unsigned int *nr_ranges, unsigned int vf_id,
+                       uint32_t start, uint32_t end)
+{
+       struct xe_sriov_provisioned_range *new_ranges;
+
+       new_ranges = realloc(*ranges,
+                            (*nr_ranges + 1) * sizeof(struct xe_sriov_provisioned_range));
+       if (!new_ranges) {
+               free(*ranges);
+               *ranges = NULL;
+               *nr_ranges = 0;
+               return -ENOMEM;
+       }
+
+       *ranges = new_ranges;
+       if (*nr_ranges < MAX_DEBUG_ENTRIES)
+               igt_debug("Found VF%u GGTT range [%#x-%#x] num_ptes=%ld\n",
+                         vf_id, start, end,
+                         (end - start + sizeof(xe_ggtt_pte_t)) /
+                         sizeof(xe_ggtt_pte_t));
+       (*ranges)[*nr_ranges].vf_id = vf_id;
+       (*ranges)[*nr_ranges].start = start;
+       (*ranges)[*nr_ranges].end = end;
+       (*nr_ranges)++;
+
+       return 0;
+}
+
 /**
  * xe_sriov_find_ggtt_provisioned_pte_offsets - Find GGTT provisioned PTE offsets
  * @pf_fd: File descriptor for the Physical Function
@@ -76,6 +107,7 @@ int xe_sriov_find_ggtt_provisioned_pte_offsets(int pf_fd, int gt, struct xe_mmio
        uint32_t current_start = 0;
        uint32_t current_end = 0;
        xe_ggtt_pte_t pte;
+       int ret;
 
        *ranges = NULL;
        *nr_ranges = 0;
@@ -86,18 +118,11 @@ int xe_sriov_find_ggtt_provisioned_pte_offsets(int pf_fd, int gt, struct xe_mmio
 
                if (vf_id != current_vf_id) {
                        if (current_vf_id != -1) {
-                               /* End the current range */
-                               *ranges = realloc(*ranges, (*nr_ranges + 1) *
-                                                 sizeof(struct xe_sriov_provisioned_range));
-                               igt_assert(*ranges);
-                               igt_debug("Found VF%u ggtt range [%#x-%#x] num_ptes=%ld\n",
-                                         current_vf_id, current_start, current_end,
-                                         (current_end - current_start + sizeof(xe_ggtt_pte_t)) /
-                                         sizeof(xe_ggtt_pte_t));
-                               (*ranges)[*nr_ranges].vf_id = current_vf_id;
-                               (*ranges)[*nr_ranges].start = current_start;
-                               (*ranges)[*nr_ranges].end = current_end;
-                               (*nr_ranges)++;
+                               /* End the current range and append it */
+                               ret = append_range(ranges, nr_ranges, current_vf_id,
+                                                  current_start, current_end);
+                               if (ret < 0)
+                                       return ret;
                        }
                        /* Start a new range */
                        current_vf_id = vf_id;
@@ -107,18 +132,16 @@ int xe_sriov_find_ggtt_provisioned_pte_offsets(int pf_fd, int gt, struct xe_mmio
        }
 
        if (current_vf_id != -1) {
-               *ranges = realloc(*ranges, (*nr_ranges + 1) *
-                                 sizeof(struct xe_sriov_provisioned_range));
-               igt_assert(*ranges);
-               igt_debug("Found VF%u ggtt range [%#x-%#x] num_ptes=%ld\n",
-                         current_vf_id, current_start, current_end,
-                         (current_end - current_start + sizeof(xe_ggtt_pte_t)) /
-                         sizeof(xe_ggtt_pte_t));
-               (*ranges)[*nr_ranges].vf_id = current_vf_id;
-               (*ranges)[*nr_ranges].start = current_start;
-               (*ranges)[*nr_ranges].end = current_end;
-               (*nr_ranges)++;
+               /* Append the last range */
+               ret = append_range(ranges, nr_ranges, current_vf_id,
+                                  current_start, current_end);
+               if (ret < 0)
+                       return ret;
        }
 
+       if (*nr_ranges > MAX_DEBUG_ENTRIES)
+               igt_debug("Ranges output trimmed to first %u entries out of %u",
+                         MAX_DEBUG_ENTRIES, *nr_ranges);
+
        return 0;
 }</pre>
    </blockquote>
    <br>
    LGTM,<br>
    <span lang="en-US">Reviewed-by: </span><span lang="pl">L</span><span lang="en-US">ukasz </span><span lang="pl">L</span><span lang="en-US">aguna <</span><a href="mailto:lukasz.laguna@intel.com"><span lang="en-US">lukasz.laguna@intel.com</span></a><span lang="en-US">><br>
      <br>
      <br>
    </span><span style="white-space: pre-wrap">
</span>
  </body>
</html>