Skip to content

Commit 75c02a0

Browse files
ImanSeyedjoergroedel
authored andcommitted
iommu/vt-d: Replace snprintf with scnprintf in dmar_latency_snapshot()
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>
1 parent 1b237f1 commit 75c02a0

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

drivers/iommu/intel/debugfs.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,17 +648,11 @@ DEFINE_SHOW_ATTRIBUTE(ir_translation_struct);
648648
static void latency_show_one(struct seq_file *m, struct intel_iommu *iommu,
649649
struct dmar_drhd_unit *drhd)
650650
{
651-
int ret;
652-
653651
seq_printf(m, "IOMMU: %s Register Base Address: %llx\n",
654652
iommu->name, drhd->reg_base_addr);
655653

656-
ret = dmar_latency_snapshot(iommu, debug_buf, DEBUG_BUFFER_SIZE);
657-
if (ret < 0)
658-
seq_puts(m, "Failed to get latency snapshot");
659-
else
660-
seq_puts(m, debug_buf);
661-
seq_puts(m, "\n");
654+
dmar_latency_snapshot(iommu, debug_buf, DEBUG_BUFFER_SIZE);
655+
seq_printf(m, "%s\n", debug_buf);
662656
}
663657

664658
static int latency_show(struct seq_file *m, void *v)

drivers/iommu/intel/perf.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static char *latency_type_names[] = {
113113
" svm_prq"
114114
};
115115

116-
int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
116+
void dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
117117
{
118118
struct latency_statistic *lstat = iommu->perf_statistic;
119119
unsigned long flags;
@@ -122,15 +122,15 @@ int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
122122
memset(str, 0, size);
123123

124124
for (i = 0; i < COUNTS_NUM; i++)
125-
bytes += snprintf(str + bytes, size - bytes,
125+
bytes += scnprintf(str + bytes, size - bytes,
126126
"%s", latency_counter_names[i]);
127127

128128
spin_lock_irqsave(&latency_lock, flags);
129129
for (i = 0; i < DMAR_LATENCY_NUM; i++) {
130130
if (!dmar_latency_enabled(iommu, i))
131131
continue;
132132

133-
bytes += snprintf(str + bytes, size - bytes,
133+
bytes += scnprintf(str + bytes, size - bytes,
134134
"\n%s", latency_type_names[i]);
135135

136136
for (j = 0; j < COUNTS_NUM; j++) {
@@ -156,11 +156,9 @@ int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
156156
break;
157157
}
158158

159-
bytes += snprintf(str + bytes, size - bytes,
159+
bytes += scnprintf(str + bytes, size - bytes,
160160
"%12lld", val);
161161
}
162162
}
163163
spin_unlock_irqrestore(&latency_lock, flags);
164-
165-
return bytes;
166164
}

drivers/iommu/intel/perf.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void dmar_latency_disable(struct intel_iommu *iommu, enum latency_type type);
4040
bool dmar_latency_enabled(struct intel_iommu *iommu, enum latency_type type);
4141
void dmar_latency_update(struct intel_iommu *iommu, enum latency_type type,
4242
u64 latency);
43-
int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size);
43+
void dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size);
4444
#else
4545
static inline int
4646
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
6464
{
6565
}
6666

67-
static inline int
67+
static inline void
6868
dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)
6969
{
70-
return 0;
7170
}
7271
#endif /* CONFIG_DMAR_PERF */

0 commit comments

Comments
 (0)