]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
iommu/vt-d: Replace snprintf with scnprintf in dmar_latency_snapshot()
authorSeyediman Seyedarab <ImanDevel@gmail.com>
Thu, 18 Sep 2025 05:01:58 +0000 (13:01 +0800)
committerJoerg Roedel <joerg.roedel@amd.com>
Fri, 19 Sep 2025 07:43:19 +0000 (09:43 +0200)
snprintf() returns the number of bytes that would have been written, not
the number actually written. Using this for offset tracking can cause
buffer overruns if truncation occurs.

Replace snprintf() with scnprintf() to ensure the offset stays within
bounds.

Since scnprintf() never returns a negative value, and zero is not possible
in this context because 'bytes' starts at 0 and 'size - bytes' is
DEBUG_BUFFER_SIZE in the first call, which is large enough to hold the
string literals used, the return value is always positive. An integer
overflow is also completely out of reach here due to the small and fixed
buffer size. The error check in latency_show_one() is therefore
unnecessary. Remove it and make dmar_latency_snapshot() return void.

Signed-off-by: Seyediman Seyedarab <ImanDevel@gmail.com>
Link: https://lore.kernel.org/r/20250731225048.131364-1-ImanDevel@gmail.com
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
drivers/iommu/intel/debugfs.c
drivers/iommu/intel/perf.c
drivers/iommu/intel/perf.h

index affbf4a1558dee4404805bb7e7ad2c57ef0bab2b..65d2f792f0f7c5f6d7f22208de1bf52c2caca1f8 100644 (file)
@@ -648,17 +648,11 @@ DEFINE_SHOW_ATTRIBUTE(ir_translation_struct);
 static void latency_show_one(struct seq_file *m, struct intel_iommu *iommu,
                             struct dmar_drhd_unit *drhd)
 {
-       int ret;
-
        seq_printf(m, "IOMMU: %s Register Base Address: %llx\n",
                   iommu->name, drhd->reg_base_addr);
 
-       ret = dmar_latency_snapshot(iommu, debug_buf, DEBUG_BUFFER_SIZE);
-       if (ret < 0)
-               seq_puts(m, "Failed to get latency snapshot");
-       else
-               seq_puts(m, debug_buf);
-       seq_puts(m, "\n");
+       dmar_latency_snapshot(iommu, debug_buf, DEBUG_BUFFER_SIZE);
+       seq_printf(m, "%s\n", debug_buf);
 }
 
 static int latency_show(struct seq_file *m, void *v)
index adc4de6bbd88e1e0a33470b735766e908d8d1343..dceeadc3ee7cdd76202ad1f10ff83b51a9b112e5 100644 (file)
@@ -113,7 +113,7 @@ static char *latency_type_names[] = {
        "     svm_prq"
 };
 
-int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
+void dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
 {
        struct latency_statistic *lstat = iommu->perf_statistic;
        unsigned long flags;
@@ -122,7 +122,7 @@ int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
        memset(str, 0, size);
 
        for (i = 0; i < COUNTS_NUM; i++)
-               bytes += snprintf(str + bytes, size - bytes,
+               bytes += scnprintf(str + bytes, size - bytes,
                                  "%s", latency_counter_names[i]);
 
        spin_lock_irqsave(&latency_lock, flags);
@@ -130,7 +130,7 @@ int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
                if (!dmar_latency_enabled(iommu, i))
                        continue;
 
-               bytes += snprintf(str + bytes, size - bytes,
+               bytes += scnprintf(str + bytes, size - bytes,
                                  "\n%s", latency_type_names[i]);
 
                for (j = 0; j < COUNTS_NUM; j++) {
@@ -156,11 +156,9 @@ int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
                                break;
                        }
 
-                       bytes += snprintf(str + bytes, size - bytes,
+                       bytes += scnprintf(str + bytes, size - bytes,
                                          "%12lld", val);
                }
        }
        spin_unlock_irqrestore(&latency_lock, flags);
-
-       return bytes;
 }
index df9a36942d643562575888285a2ec6f6a9237f94..1d4baad7e852ebccf4e2f6c3d807a52ac67ec754 100644 (file)
@@ -40,7 +40,7 @@ void dmar_latency_disable(struct intel_iommu *iommu, enum latency_type type);
 bool dmar_latency_enabled(struct intel_iommu *iommu, enum latency_type type);
 void dmar_latency_update(struct intel_iommu *iommu, enum latency_type type,
                         u64 latency);
-int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size);
+void dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size);
 #else
 static inline int
 dmar_latency_enable(struct intel_iommu *iommu, enum latency_type type)
@@ -64,9 +64,8 @@ dmar_latency_update(struct intel_iommu *iommu, enum latency_type type, u64 laten
 {
 }
 
-static inline int
+static inline void
 dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
 {
-       return 0;
 }
 #endif /* CONFIG_DMAR_PERF */