Skip to content

Commit c33fcc1

Browse files
LuBaolujoergroedel
authored andcommitted
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: Lu Baolu <baolu.lu@linux.intel.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20230322123421.278852-1-baolu.lu@linux.intel.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 4c8444f commit c33fcc1

3 files changed

Lines changed: 26 additions & 26 deletions

File tree

drivers/iommu/amd/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,7 +1941,7 @@ static ssize_t amd_iommu_show_cap(struct device *dev,
19411941
char *buf)
19421942
{
19431943
struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1944-
return sprintf(buf, "%x\n", iommu->cap);
1944+
return sysfs_emit(buf, "%x\n", iommu->cap);
19451945
}
19461946
static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
19471947

@@ -1950,7 +1950,7 @@ static ssize_t amd_iommu_show_features(struct device *dev,
19501950
char *buf)
19511951
{
19521952
struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1953-
return sprintf(buf, "%llx:%llx\n", iommu->features2, iommu->features);
1953+
return sysfs_emit(buf, "%llx:%llx\n", iommu->features2, iommu->features);
19541954
}
19551955
static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
19561956

drivers/iommu/intel/iommu.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3760,49 +3760,50 @@ static ssize_t version_show(struct device *dev,
37603760
{
37613761
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
37623762
u32 ver = readl(iommu->reg + DMAR_VER_REG);
3763-
return sprintf(buf, "%d:%d\n",
3764-
DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
3763+
return sysfs_emit(buf, "%d:%d\n",
3764+
DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
37653765
}
37663766
static DEVICE_ATTR_RO(version);
37673767

37683768
static ssize_t address_show(struct device *dev,
37693769
struct device_attribute *attr, char *buf)
37703770
{
37713771
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
3772-
return sprintf(buf, "%llx\n", iommu->reg_phys);
3772+
return sysfs_emit(buf, "%llx\n", iommu->reg_phys);
37733773
}
37743774
static DEVICE_ATTR_RO(address);
37753775

37763776
static ssize_t cap_show(struct device *dev,
37773777
struct device_attribute *attr, char *buf)
37783778
{
37793779
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
3780-
return sprintf(buf, "%llx\n", iommu->cap);
3780+
return sysfs_emit(buf, "%llx\n", iommu->cap);
37813781
}
37823782
static DEVICE_ATTR_RO(cap);
37833783

37843784
static ssize_t ecap_show(struct device *dev,
37853785
struct device_attribute *attr, char *buf)
37863786
{
37873787
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
3788-
return sprintf(buf, "%llx\n", iommu->ecap);
3788+
return sysfs_emit(buf, "%llx\n", iommu->ecap);
37893789
}
37903790
static DEVICE_ATTR_RO(ecap);
37913791

37923792
static ssize_t domains_supported_show(struct device *dev,
37933793
struct device_attribute *attr, char *buf)
37943794
{
37953795
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
3796-
return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
3796+
return sysfs_emit(buf, "%ld\n", cap_ndoms(iommu->cap));
37973797
}
37983798
static DEVICE_ATTR_RO(domains_supported);
37993799

38003800
static ssize_t domains_used_show(struct device *dev,
38013801
struct device_attribute *attr, char *buf)
38023802
{
38033803
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
3804-
return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
3805-
cap_ndoms(iommu->cap)));
3804+
return sysfs_emit(buf, "%d\n",
3805+
bitmap_weight(iommu->domain_ids,
3806+
cap_ndoms(iommu->cap)));
38063807
}
38073808
static DEVICE_ATTR_RO(domains_used);
38083809

drivers/iommu/iommu.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ static void iommu_group_remove_file(struct iommu_group *group,
616616

617617
static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
618618
{
619-
return sprintf(buf, "%s\n", group->name);
619+
return sysfs_emit(buf, "%s\n", group->name);
620620
}
621621

622622
/**
@@ -729,52 +729,51 @@ static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
729729
{
730730
struct iommu_resv_region *region, *next;
731731
struct list_head group_resv_regions;
732-
char *str = buf;
732+
int offset = 0;
733733

734734
INIT_LIST_HEAD(&group_resv_regions);
735735
iommu_get_group_resv_regions(group, &group_resv_regions);
736736

737737
list_for_each_entry_safe(region, next, &group_resv_regions, list) {
738-
str += sprintf(str, "0x%016llx 0x%016llx %s\n",
739-
(long long int)region->start,
740-
(long long int)(region->start +
741-
region->length - 1),
742-
iommu_group_resv_type_string[region->type]);
738+
offset += sysfs_emit_at(buf, offset, "0x%016llx 0x%016llx %s\n",
739+
(long long)region->start,
740+
(long long)(region->start +
741+
region->length - 1),
742+
iommu_group_resv_type_string[region->type]);
743743
kfree(region);
744744
}
745745

746-
return (str - buf);
746+
return offset;
747747
}
748748

749749
static ssize_t iommu_group_show_type(struct iommu_group *group,
750750
char *buf)
751751
{
752-
char *type = "unknown\n";
752+
char *type = "unknown";
753753

754754
mutex_lock(&group->mutex);
755755
if (group->default_domain) {
756756
switch (group->default_domain->type) {
757757
case IOMMU_DOMAIN_BLOCKED:
758-
type = "blocked\n";
758+
type = "blocked";
759759
break;
760760
case IOMMU_DOMAIN_IDENTITY:
761-
type = "identity\n";
761+
type = "identity";
762762
break;
763763
case IOMMU_DOMAIN_UNMANAGED:
764-
type = "unmanaged\n";
764+
type = "unmanaged";
765765
break;
766766
case IOMMU_DOMAIN_DMA:
767-
type = "DMA\n";
767+
type = "DMA";
768768
break;
769769
case IOMMU_DOMAIN_DMA_FQ:
770-
type = "DMA-FQ\n";
770+
type = "DMA-FQ";
771771
break;
772772
}
773773
}
774774
mutex_unlock(&group->mutex);
775-
strcpy(buf, type);
776775

777-
return strlen(type);
776+
return sysfs_emit(buf, "%s\n", type);
778777
}
779778

780779
static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);

0 commit comments

Comments
 (0)