Commit c33fcc13 authored by Lu Baolu's avatar Lu Baolu Committed by Joerg Roedel

iommu: Use sysfs_emit() for sysfs show

Use sysfs_emit() instead of the sprintf() for sysfs entries. sysfs_emit()
knows the maximum of the temporary buffer used for outputting sysfs
content and avoids overrunning the buffer length.

Prefer 'long long' over 'long long int' as suggested by checkpatch.pl.
Signed-off-by: default avatarLu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/20230322123421.278852-1-baolu.lu@linux.intel.comSigned-off-by: default avatarJoerg Roedel <jroedel@suse.de>
parent 4c8444f1
...@@ -1941,7 +1941,7 @@ static ssize_t amd_iommu_show_cap(struct device *dev, ...@@ -1941,7 +1941,7 @@ static ssize_t amd_iommu_show_cap(struct device *dev,
char *buf) char *buf)
{ {
struct amd_iommu *iommu = dev_to_amd_iommu(dev); struct amd_iommu *iommu = dev_to_amd_iommu(dev);
return sprintf(buf, "%x\n", iommu->cap); return sysfs_emit(buf, "%x\n", iommu->cap);
} }
static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL); static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
...@@ -1950,7 +1950,7 @@ static ssize_t amd_iommu_show_features(struct device *dev, ...@@ -1950,7 +1950,7 @@ static ssize_t amd_iommu_show_features(struct device *dev,
char *buf) char *buf)
{ {
struct amd_iommu *iommu = dev_to_amd_iommu(dev); struct amd_iommu *iommu = dev_to_amd_iommu(dev);
return sprintf(buf, "%llx:%llx\n", iommu->features2, iommu->features); return sysfs_emit(buf, "%llx:%llx\n", iommu->features2, iommu->features);
} }
static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL); static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
......
...@@ -3760,8 +3760,8 @@ static ssize_t version_show(struct device *dev, ...@@ -3760,8 +3760,8 @@ static ssize_t version_show(struct device *dev,
{ {
struct intel_iommu *iommu = dev_to_intel_iommu(dev); struct intel_iommu *iommu = dev_to_intel_iommu(dev);
u32 ver = readl(iommu->reg + DMAR_VER_REG); u32 ver = readl(iommu->reg + DMAR_VER_REG);
return sprintf(buf, "%d:%d\n", return sysfs_emit(buf, "%d:%d\n",
DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver)); DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
} }
static DEVICE_ATTR_RO(version); static DEVICE_ATTR_RO(version);
...@@ -3769,7 +3769,7 @@ static ssize_t address_show(struct device *dev, ...@@ -3769,7 +3769,7 @@ static ssize_t address_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct intel_iommu *iommu = dev_to_intel_iommu(dev); struct intel_iommu *iommu = dev_to_intel_iommu(dev);
return sprintf(buf, "%llx\n", iommu->reg_phys); return sysfs_emit(buf, "%llx\n", iommu->reg_phys);
} }
static DEVICE_ATTR_RO(address); static DEVICE_ATTR_RO(address);
...@@ -3777,7 +3777,7 @@ static ssize_t cap_show(struct device *dev, ...@@ -3777,7 +3777,7 @@ static ssize_t cap_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct intel_iommu *iommu = dev_to_intel_iommu(dev); struct intel_iommu *iommu = dev_to_intel_iommu(dev);
return sprintf(buf, "%llx\n", iommu->cap); return sysfs_emit(buf, "%llx\n", iommu->cap);
} }
static DEVICE_ATTR_RO(cap); static DEVICE_ATTR_RO(cap);
...@@ -3785,7 +3785,7 @@ static ssize_t ecap_show(struct device *dev, ...@@ -3785,7 +3785,7 @@ static ssize_t ecap_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct intel_iommu *iommu = dev_to_intel_iommu(dev); struct intel_iommu *iommu = dev_to_intel_iommu(dev);
return sprintf(buf, "%llx\n", iommu->ecap); return sysfs_emit(buf, "%llx\n", iommu->ecap);
} }
static DEVICE_ATTR_RO(ecap); static DEVICE_ATTR_RO(ecap);
...@@ -3793,7 +3793,7 @@ static ssize_t domains_supported_show(struct device *dev, ...@@ -3793,7 +3793,7 @@ static ssize_t domains_supported_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct intel_iommu *iommu = dev_to_intel_iommu(dev); struct intel_iommu *iommu = dev_to_intel_iommu(dev);
return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap)); return sysfs_emit(buf, "%ld\n", cap_ndoms(iommu->cap));
} }
static DEVICE_ATTR_RO(domains_supported); static DEVICE_ATTR_RO(domains_supported);
...@@ -3801,8 +3801,9 @@ static ssize_t domains_used_show(struct device *dev, ...@@ -3801,8 +3801,9 @@ static ssize_t domains_used_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct intel_iommu *iommu = dev_to_intel_iommu(dev); struct intel_iommu *iommu = dev_to_intel_iommu(dev);
return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids, return sysfs_emit(buf, "%d\n",
cap_ndoms(iommu->cap))); bitmap_weight(iommu->domain_ids,
cap_ndoms(iommu->cap)));
} }
static DEVICE_ATTR_RO(domains_used); static DEVICE_ATTR_RO(domains_used);
......
...@@ -616,7 +616,7 @@ static void iommu_group_remove_file(struct iommu_group *group, ...@@ -616,7 +616,7 @@ static void iommu_group_remove_file(struct iommu_group *group,
static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
{ {
return sprintf(buf, "%s\n", group->name); return sysfs_emit(buf, "%s\n", group->name);
} }
/** /**
...@@ -729,52 +729,51 @@ static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, ...@@ -729,52 +729,51 @@ static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
{ {
struct iommu_resv_region *region, *next; struct iommu_resv_region *region, *next;
struct list_head group_resv_regions; struct list_head group_resv_regions;
char *str = buf; int offset = 0;
INIT_LIST_HEAD(&group_resv_regions); INIT_LIST_HEAD(&group_resv_regions);
iommu_get_group_resv_regions(group, &group_resv_regions); iommu_get_group_resv_regions(group, &group_resv_regions);
list_for_each_entry_safe(region, next, &group_resv_regions, list) { list_for_each_entry_safe(region, next, &group_resv_regions, list) {
str += sprintf(str, "0x%016llx 0x%016llx %s\n", offset += sysfs_emit_at(buf, offset, "0x%016llx 0x%016llx %s\n",
(long long int)region->start, (long long)region->start,
(long long int)(region->start + (long long)(region->start +
region->length - 1), region->length - 1),
iommu_group_resv_type_string[region->type]); iommu_group_resv_type_string[region->type]);
kfree(region); kfree(region);
} }
return (str - buf); return offset;
} }
static ssize_t iommu_group_show_type(struct iommu_group *group, static ssize_t iommu_group_show_type(struct iommu_group *group,
char *buf) char *buf)
{ {
char *type = "unknown\n"; char *type = "unknown";
mutex_lock(&group->mutex); mutex_lock(&group->mutex);
if (group->default_domain) { if (group->default_domain) {
switch (group->default_domain->type) { switch (group->default_domain->type) {
case IOMMU_DOMAIN_BLOCKED: case IOMMU_DOMAIN_BLOCKED:
type = "blocked\n"; type = "blocked";
break; break;
case IOMMU_DOMAIN_IDENTITY: case IOMMU_DOMAIN_IDENTITY:
type = "identity\n"; type = "identity";
break; break;
case IOMMU_DOMAIN_UNMANAGED: case IOMMU_DOMAIN_UNMANAGED:
type = "unmanaged\n"; type = "unmanaged";
break; break;
case IOMMU_DOMAIN_DMA: case IOMMU_DOMAIN_DMA:
type = "DMA\n"; type = "DMA";
break; break;
case IOMMU_DOMAIN_DMA_FQ: case IOMMU_DOMAIN_DMA_FQ:
type = "DMA-FQ\n"; type = "DMA-FQ";
break; break;
} }
} }
mutex_unlock(&group->mutex); mutex_unlock(&group->mutex);
strcpy(buf, type);
return strlen(type); return sysfs_emit(buf, "%s\n", type);
} }
static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment