From patchwork Wed Mar 22 12:34:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 73403 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2320936wrt; Wed, 22 Mar 2023 05:46:24 -0700 (PDT) X-Google-Smtp-Source: AK7set9VqXj/RuLmzSaoZY6SWxGYaeB5NdMlUZaBA7yBa8t9EcfWSU2NjTyPB9EoLOamgokESUUU X-Received: by 2002:a17:90b:3ecd:b0:234:409:9752 with SMTP id rm13-20020a17090b3ecd00b0023404099752mr3222810pjb.25.1679489184694; Wed, 22 Mar 2023 05:46:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679489184; cv=none; d=google.com; s=arc-20160816; b=BceWIvegUWpMpp+j/V7lQC7pIEBmoo7/XBcQt5bLIWtBxPpBCGl/JfO38hvdFPZhFP jwThPHtIyKjxMf5XVcbRnbOixJOkpWcMAnSd8DgyFdqtKUGq3FbNk9PCFl+h0r3jsnRS yIaBnX9R0t62nU3qdiOi9N4J7Tc2umKo7lJly+JQ4t4ZSzR7nXeg35YZ4/O8QwA9S1OI MscqFEHVaT5UsCq3ucYF4YTtpr2ZnKD6tYpb3mLstKIdvMMsUgFmA7cxYhB0dxb7gbIk mPcP5i4Qz2qbvEF+0oQGAdASvDIqP0DT0/5vimnhmI1jR0dxCC5faNk9967LP9NZcDRv XsOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=VPEdQSiaYsBSsHPjAtC2LZRd1symNn86jWBoLd+iUlU=; b=zAXYoZ+okcdOKvXVsY393IEQGQdLwy9YjhRuXv7oOIy75JMrOovxfHvLTH9iVLikDE kCm0Bpt7YEh3/zHfeKmLgpFRAuwmifA7rdjpgHroVBEC6qhpgQ9cxdnCnEJaC2jOQzCN GEn2IwU0O43xJlo0yl3BW3/Ew6oI5Uq7zmIWBkyGNnvY4Wu7AfNC3wUFyAUc8U0ZZBOl wGwsChXpUOdNAnKt/r1nPIzymj9hTWbhbrW+ArtDyNynsuxi7c4SP09LxXAYyLcZYxUn zD8b46ZY2Rx+kwKRVTQkrkKTB6MhvGYc9sC/J6PFR44QT2RbmHIOT1rqeoMwDjJ67nLA G1Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hZKjfg5F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pf5-20020a17090b1d8500b002377ed22060si17242214pjb.178.2023.03.22.05.46.11; Wed, 22 Mar 2023 05:46:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hZKjfg5F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229980AbjCVMeE (ORCPT + 99 others); Wed, 22 Mar 2023 08:34:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229642AbjCVMeC (ORCPT ); Wed, 22 Mar 2023 08:34:02 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FE8252F5D for ; Wed, 22 Mar 2023 05:34:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679488441; x=1711024441; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=RR2lUtE6MIt2kHIsf8+e8beC+uOvUnJFzEHkAFWyuG0=; b=hZKjfg5FMrQunQWxHooFfL0Xmth9W2jm9vzFkAH2U6ggF8TOSvOuLwKy zbL72YkbsMx63e3s4rCa9R1x7WbXwEmQU07kRecIj+3w8W23Q2eyKfCss cPpue4zZ6DbtDjCb5of2X/wZItsF4YZvOoTU1ffBQZsnoHs7MmlWPBBwS yQb2Yzl+/nu3ky6neRbqsTkkgIe0JxHvl1yTkV4bj24YIYqO6ual8lsAO RlFerl7pCyhwdcFQQ0ax3kiwq7fIYfKu04/+c2aYQnGWJ8+3Zc/ErtuRJ 4EOpl1x/DiB71qzKI2sMHH2zNeShme0VTVnuRvbLgQz26WKSx2K9oIM1+ w==; X-IronPort-AV: E=McAfee;i="6600,9927,10656"; a="366935906" X-IronPort-AV: E=Sophos;i="5.98,281,1673942400"; d="scan'208";a="366935906" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Mar 2023 05:34:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10656"; a="771022466" X-IronPort-AV: E=Sophos;i="5.98,281,1673942400"; d="scan'208";a="771022466" Received: from allen-box.sh.intel.com ([10.239.159.48]) by FMSMGA003.fm.intel.com with ESMTP; 22 Mar 2023 05:33:58 -0700 From: Lu Baolu To: Joerg Roedel Cc: Suravee Suthikulpanit , Jason Gunthorpe , Robin Murphy , Christoph Hellwig , Kevin Tian , Will Deacon , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH 1/1] iommu: Use sysfs_emit() for sysfs show Date: Wed, 22 Mar 2023 20:34:21 +0800 Message-Id: <20230322123421.278852-1-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761071707672704776?= X-GMAIL-MSGID: =?utf-8?q?1761072050898922385?= 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 --- drivers/iommu/amd/init.c | 4 ++-- drivers/iommu/intel/iommu.c | 17 +++++++++-------- drivers/iommu/iommu.c | 31 +++++++++++++++---------------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 19a46b9f7357..a98202018140 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -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); diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 7c2f4bd33582..a366790fb781 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -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); diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 10db680acaed..d1ba4397fded 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -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);