iommu: Use sysfs_emit() for sysfs show
Subsystems (3)
Lu Baolu
maintainerPATCH 1/1 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.
--- drivers/iommu/amd/init.c | 4 ++-- drivers/iommu/intel/iommu.c | 17 +++++++++-------- drivers/iommu/iommu.c | 31 +++++++++++++++---------------- 3 files changed, 26 insertions(+), 26 deletions(-)
drivers/iommu/amd/init.c +2 -2
@@ -1941,7 +1941,7 @@ static ssize_t amd_iommu_show_cap(struct device *dev, char *buf) {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);
@@ -1950,7 +1950,7 @@ static ssize_t amd_iommu_show_features(struct device *dev, char *buf) {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);
drivers/iommu/intel/iommu.c +9 -8
@@ -3760,8 +3760,8 @@ static ssize_t version_show(struct device *dev, {struct intel_iommu *iommu =dev_to_intel_iommu (dev);u32 ver =readl (iommu->reg +DMAR_VER_REG ); -return sprintf (buf,"%d:%d\n" , -DMAR_VER_MAJOR (ver),DMAR_VER_MINOR (ver)); +return sysfs_emit (buf,"%d:%d\n" , +DMAR_VER_MAJOR (ver),DMAR_VER_MINOR (ver)); }static DEVICE_ATTR_RO (version);
@@ -3769,7 +3769,7 @@ static ssize_t address_show(struct device *dev, struct device_attribute *attr, char *buf) { 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);
@@ -3777,7 +3777,7 @@ static ssize_t cap_show(struct device *dev, struct device_attribute *attr, char *buf) { 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);
@@ -3785,7 +3785,7 @@ static ssize_t ecap_show(struct device *dev, struct device_attribute *attr, char *buf) { 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);
@@ -3793,7 +3793,7 @@ static ssize_t domains_supported_show(struct device *dev, struct device_attribute *attr, char *buf) { 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);
@@ -3801,8 +3801,9 @@ static ssize_t domains_used_show(struct device *dev, struct device_attribute *attr, char *buf) { struct intel_iommu *iommu = dev_to_intel_iommu(dev); -return sprintf (buf,"%d\n" ,bitmap_weight (iommu->domain_ids, -cap_ndoms (iommu->cap))); +return sysfs_emit (buf,"%d\n" , +bitmap_weight (iommu->domain_ids, +cap_ndoms (iommu->cap))); }static DEVICE_ATTR_RO (domains_used);
drivers/iommu/iommu.c +15 -16
@@ -550,7 +550,7 @@ static void iommu_group_remove_file(struct iommu_group *group,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); }/**
@@ -663,52 +663,51 @@ static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, {struct iommu_resv_region *region, *next;struct list_head group_resv_regions; -char *str = buf; +int offset =0 ;INIT_LIST_HEAD (&group_resv_regions);iommu_get_group_resv_regions (group, &group_resv_regions);list_for_each_entry_safe (region, next, &group_resv_regions, list) { - str +=sprintf (str,"0x%016llx 0x%016llx %s\n" , - (long long int )region->start, - (long long int )(region->start + - region->length -1 ), - iommu_group_resv_type_string[region->type]); + offset +=sysfs_emit_at (buf, offset,"0x%016llx 0x%016llx %s\n" , + (long long )region->start, + (long long )(region->start + + region->length -1 ), + iommu_group_resv_type_string[region->type]);kfree (region); } -return (str - buf); +return offset; }static ssize_t iommu_group_show_type (struct iommu_group *group,char *buf) { -char *type ="unknown\n" ; +char *type ="unknown" ;mutex_lock (&group->mutex);if (group->default_domain) {switch (group->default_domain->type) {case IOMMU_DOMAIN_BLOCKED : - type ="blocked\n" ; + type ="blocked" ;break ;case IOMMU_DOMAIN_IDENTITY : - type ="identity\n" ; + type ="identity" ;break ;case IOMMU_DOMAIN_UNMANAGED : - type ="unmanaged\n" ; + type ="unmanaged" ;break ;case IOMMU_DOMAIN_DMA : - type ="DMA\n" ; + type ="DMA" ;break ;case IOMMU_DOMAIN_DMA_FQ : - type ="DMA-FQ\n" ; + type ="DMA-FQ" ;break ; } }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);
Signature · 1 line
2.34.1
Quoted 18 lines · Lu Baolu
Use
sysfs_emit()instead of thesprintf()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 Lu Baolu <[email protected]>--- drivers/iommu/amd/init.c | 4 ++-- drivers/iommu/intel/iommu.c | 17 +++++++++-------- drivers/iommu/iommu.c | 31 +++++++++++++++---------------- 3 files changed, 26 insertions(+), 26 deletions(-)drivers/iommu/amd/init.c +0
-0
drivers/iommu/amd/init.c static ssize_t amd_iommu_show_cap(struct device *dev, · line 1945
Quoted 6 lines
drivers/iommu/amd/init.c +1
-1@@ -1941,7 +1941,7 @@ static ssize_t amd_iommu_show_cap(struct device *dev, char *buf) {struct amd_iommu *iommu =dev_to_amd_iommu (dev); -return sprintf (buf,"%x\n" , iommu->cap); +return sysfs_emit (buf,"%x\n" , iommu->cap); }
It would be nice to add the missing whitespace after declarations here and other places to follow the coding convention
The conversions look OK though
Jason
Quoted 4 lines · Lu Baolu
drivers/iommu/amd/init.c | 4 ++-- drivers/iommu/intel/iommu.c | 17 +++++++++-------- drivers/iommu/iommu.c | 31 +++++++++++++++---------------- 3 files changed, 26 insertions(+), 26 deletions(-)
Applied, thanks.