Linux-Media Archive on lore.kernel.org
 help / color / Atom feed
* [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit
@ 2020-10-22 13:37 Sumera Priyadarsini
  2020-10-22 13:40 ` [PATCH 1/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit() Sumera Priyadarsini
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Sumera Priyadarsini @ 2020-10-22 13:37 UTC (permalink / raw)
  To: dri-devel
  Cc: outreachy-kernel, alexander.deucher, christian.koenig, airlied,
	daniel, melissa.srw, linux-media, amd-gfx, linux-kernel

Using snprintf() for show() methods holds the risk of buffer overrun
as snprintf() does not know the PAGE_SIZE maximum of the temporary
buffer used to output sysfs content.

This patchset is a series of Coccinelle cleanups across the staging
directory to convert snprintf with scnprintf in the relevant files.

Sumera Priyadarsini (5):
  gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()

 drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c   | 8 ++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c  | 4 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c      | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c      | 4 ++--
 5 files changed, 10 insertions(+), 10 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  2020-10-22 13:37 [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Sumera Priyadarsini
@ 2020-10-22 13:40 ` Sumera Priyadarsini
  2020-10-22 13:43 ` [PATCH 2/5] " Sumera Priyadarsini
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Sumera Priyadarsini @ 2020-10-22 13:40 UTC (permalink / raw)
  To: dri-devel
  Cc: outreachy-kernel, alexander.deucher, christian.koenig, airlied,
	daniel, melissa.srw, linux-media, amd-gfx, linux-kernel

Using snprintf() for show() methods holds the risk of buffer overrun
as snprintf() does not know the PAGE_SIZE maximum of the temporary
buffer used to output sysfs content.

Modify amdgpu_atombios.c to use sysfs_emit() instead which knows the
size of the temporary buffer.

Issue found with Coccinelle.

Signed-off-by: Sumera Priyadarsini <sylphrenadin@gmail.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c
index 469352e2d6ec..3c19862c94c7 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c
@@ -1947,7 +1947,7 @@ static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 	struct atom_context *ctx = adev->mode_info.atom_context;
 
-	return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
+	return sysfs_emit(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
 }
 
 static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
-- 
2.25.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  2020-10-22 13:37 [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Sumera Priyadarsini
  2020-10-22 13:40 ` [PATCH 1/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit() Sumera Priyadarsini
@ 2020-10-22 13:43 ` Sumera Priyadarsini
  2020-10-22 13:47 ` [PATCH 3/5] " Sumera Priyadarsini
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Sumera Priyadarsini @ 2020-10-22 13:43 UTC (permalink / raw)
  To: dri-devel
  Cc: outreachy-kernel, alexander.deucher, christian.koenig, airlied,
	daniel, melissa.srw, linux-media, amd-gfx, linux-kernel

Using snprintf() for show() methods holds the risk of buffer overrun
as snprintf() does not know the PAGE_SIZE maximum of the temporary
buffer used to output sysfs content.

Modify amdgpu_device.c to use sysfs_emit() instead which knows the
size of the temporary buffer.

Issue found with Coccinelle.

Signed-off-by: Sumera Priyadarsini <sylphrenadin@gmail.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
index f7307af76452..7eef6b20578f 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
@@ -135,7 +135,7 @@ static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 	uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
 
-	return snprintf(buf, PAGE_SIZE, "%llu\n", cnt);
+	return sysfs_emit(buf, PAGE_SIZE, "%llu\n", cnt);
 }
 
 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
@@ -159,7 +159,7 @@ static ssize_t amdgpu_device_get_product_name(struct device *dev,
 	struct drm_device *ddev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 
-	return snprintf(buf, PAGE_SIZE, "%s\n", adev->product_name);
+	return sysfs_emit(buf, PAGE_SIZE, "%s\n", adev->product_name);
 }
 
 static DEVICE_ATTR(product_name, S_IRUGO,
@@ -181,7 +181,7 @@ static ssize_t amdgpu_device_get_product_number(struct device *dev,
 	struct drm_device *ddev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 
-	return snprintf(buf, PAGE_SIZE, "%s\n", adev->product_number);
+	return sysfs_emit(buf, PAGE_SIZE, "%s\n", adev->product_number);
 }
 
 static DEVICE_ATTR(product_number, S_IRUGO,
@@ -203,7 +203,7 @@ static ssize_t amdgpu_device_get_serial_number(struct device *dev,
 	struct drm_device *ddev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 
-	return snprintf(buf, PAGE_SIZE, "%s\n", adev->serial);
+	return sysfs_emit(buf, PAGE_SIZE, "%s\n", adev->serial);
 }
 
 static DEVICE_ATTR(serial_number, S_IRUGO,
-- 
2.25.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 3/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  2020-10-22 13:37 [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Sumera Priyadarsini
  2020-10-22 13:40 ` [PATCH 1/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit() Sumera Priyadarsini
  2020-10-22 13:43 ` [PATCH 2/5] " Sumera Priyadarsini
@ 2020-10-22 13:47 ` Sumera Priyadarsini
  2020-10-22 13:47 ` [PATCH 4/5] " Sumera Priyadarsini
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Sumera Priyadarsini @ 2020-10-22 13:47 UTC (permalink / raw)
  To: dri-devel
  Cc: outreachy-kernel, alexander.deucher, christian.koenig, airlied,
	daniel, melissa.srw, linux-media, amd-gfx, linux-kernel

Using snprintf() for show() methods holds the risk of buffer overrun
as snprintf() does not know the PAGE_SIZE maximum of the temporary
buffer used to output sysfs content.

Modify amdgpu_gtt_mgr.c to use sysfs_emit() instead which knows the
size of the temporary buffer.

Issue found with Coccinelle.

Signed-off-by: Sumera Priyadarsini <sylphrenadin@gmail.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c
index 1721739def84..441e07ee1967 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c
@@ -49,7 +49,7 @@ static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev,
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 	struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
 
-	return snprintf(buf, PAGE_SIZE, "%llu\n",
+	return sysfs_emit(buf, PAGE_SIZE, "%llu\n",
 			man->size * PAGE_SIZE);
 }
 
@@ -68,7 +68,7 @@ static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev,
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 	struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
 
-	return snprintf(buf, PAGE_SIZE, "%llu\n",
+	return sysfs_emit(buf, PAGE_SIZE, "%llu\n",
 			amdgpu_gtt_mgr_usage(man));
 }
 
-- 
2.25.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 4/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  2020-10-22 13:37 [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Sumera Priyadarsini
                   ` (2 preceding siblings ...)
  2020-10-22 13:47 ` [PATCH 3/5] " Sumera Priyadarsini
@ 2020-10-22 13:47 ` Sumera Priyadarsini
  2020-10-22 13:54   ` [Outreachy kernel] " Greg KH
  2020-10-22 13:49 ` [PATCH 5/5] " Sumera Priyadarsini
  2020-10-22 14:13 ` [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Daniel Vetter
  5 siblings, 1 reply; 8+ messages in thread
From: Sumera Priyadarsini @ 2020-10-22 13:47 UTC (permalink / raw)
  To: dri-devel
  Cc: outreachy-kernel, alexander.deucher, christian.koenig, airlied,
	daniel, melissa.srw, linux-media, amd-gfx, linux-kernel

Using snprintf() for show() methods holds the risk of buffer overrun
as snprintf() does not know the PAGE_SIZE maximum of the temporary
buffer used to output sysfs content.

Modify amdgpu_psp.c to use sysfs_emit() instead which knows the
size of the temporary buffer.

Issue found with Coccinelle.

Signed-off-by: Sumera Priyadarsini <sylphrenadin@gmail.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
index d6c38e24f130..4d1d1e1b005d 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
@@ -2621,7 +2621,7 @@ static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
 		return ret;
 	}
 
-	return snprintf(buf, PAGE_SIZE, "%x\n", fw_ver);
+	return sysfs_emit(buf, PAGE_SIZE, "%x\n", fw_ver);
 }
 
 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
-- 
2.25.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 5/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  2020-10-22 13:37 [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Sumera Priyadarsini
                   ` (3 preceding siblings ...)
  2020-10-22 13:47 ` [PATCH 4/5] " Sumera Priyadarsini
@ 2020-10-22 13:49 ` Sumera Priyadarsini
  2020-10-22 14:13 ` [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Daniel Vetter
  5 siblings, 0 replies; 8+ messages in thread
From: Sumera Priyadarsini @ 2020-10-22 13:49 UTC (permalink / raw)
  To: dri-devel
  Cc: outreachy-kernel, alexander.deucher, christian.koenig, airlied,
	daniel, melissa.srw, linux-media, amd-gfx, linux-kernel

Using snprintf() for show() methods holds the risk of buffer overrun
as snprintf() does not know the PAGE_SIZE maximum of the temporary
buffer used to output sysfs content.

Modify amdgpu_ras.c to use sysfs_emit() instead which knows the
size of the temporary buffer.

Issue found with Coccinelle.

Signed-off-by: Sumera Priyadarsini <sylphrenadin@gmail.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
index e5ea14774c0c..6d9901e1b4b0 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
@@ -429,13 +429,13 @@ static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
 	};
 
 	if (!amdgpu_ras_get_error_query_ready(obj->adev))
-		return snprintf(buf, PAGE_SIZE,
+		return sysfs_emit(buf, PAGE_SIZE,
 				"Query currently inaccessible\n");
 
 	if (amdgpu_ras_error_query(obj->adev, &info))
 		return -EINVAL;
 
-	return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
+	return sysfs_emit(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
 			"ue", info.ue_count,
 			"ce", info.ce_count);
 }
-- 
2.25.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Outreachy kernel] [PATCH 4/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
  2020-10-22 13:47 ` [PATCH 4/5] " Sumera Priyadarsini
@ 2020-10-22 13:54   ` Greg KH
  0 siblings, 0 replies; 8+ messages in thread
From: Greg KH @ 2020-10-22 13:54 UTC (permalink / raw)
  To: Sumera Priyadarsini
  Cc: dri-devel, outreachy-kernel, alexander.deucher, christian.koenig,
	airlied, daniel, melissa.srw, linux-media, amd-gfx, linux-kernel

On Thu, Oct 22, 2020 at 07:17:56PM +0530, Sumera Priyadarsini wrote:
> Using snprintf() for show() methods holds the risk of buffer overrun
> as snprintf() does not know the PAGE_SIZE maximum of the temporary
> buffer used to output sysfs content.
> 
> Modify amdgpu_psp.c to use sysfs_emit() instead which knows the
> size of the temporary buffer.
> 
> Issue found with Coccinelle.
> 
> Signed-off-by: Sumera Priyadarsini <sylphrenadin@gmail.com>
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
> index d6c38e24f130..4d1d1e1b005d 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
> @@ -2621,7 +2621,7 @@ static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
>  		return ret;
>  	}
>  
> -	return snprintf(buf, PAGE_SIZE, "%x\n", fw_ver);
> +	return sysfs_emit(buf, PAGE_SIZE, "%x\n", fw_ver);

Did you build this code?  I don't think it is correct...

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit
  2020-10-22 13:37 [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Sumera Priyadarsini
                   ` (4 preceding siblings ...)
  2020-10-22 13:49 ` [PATCH 5/5] " Sumera Priyadarsini
@ 2020-10-22 14:13 ` Daniel Vetter
  5 siblings, 0 replies; 8+ messages in thread
From: Daniel Vetter @ 2020-10-22 14:13 UTC (permalink / raw)
  To: Sumera Priyadarsini
  Cc: dri-devel, outreachy-kernel, alexander.deucher, christian.koenig,
	airlied, daniel, melissa.srw, linux-media, amd-gfx, linux-kernel

On Thu, Oct 22, 2020 at 07:07:50PM +0530, Sumera Priyadarsini wrote:
> Using snprintf() for show() methods holds the risk of buffer overrun
> as snprintf() does not know the PAGE_SIZE maximum of the temporary
> buffer used to output sysfs content.
> 
> This patchset is a series of Coccinelle cleanups across the staging
> directory to convert snprintf with scnprintf in the relevant files.

I think you need to edit your template here since this is now drivers/gpu,
not staging :-)
-Daniel

> 
> Sumera Priyadarsini (5):
>   gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
>   gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
>   gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
>   gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
>   gpu: drm: amdgpu: Replace snprintf() with sysfs_emit()
> 
>  drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c | 2 +-
>  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c   | 8 ++++----
>  drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c  | 4 ++--
>  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c      | 2 +-
>  drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c      | 4 ++--
>  5 files changed, 10 insertions(+), 10 deletions(-)
> 
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, back to index

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-22 13:37 [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Sumera Priyadarsini
2020-10-22 13:40 ` [PATCH 1/5] gpu: drm: amdgpu: Replace snprintf() with sysfs_emit() Sumera Priyadarsini
2020-10-22 13:43 ` [PATCH 2/5] " Sumera Priyadarsini
2020-10-22 13:47 ` [PATCH 3/5] " Sumera Priyadarsini
2020-10-22 13:47 ` [PATCH 4/5] " Sumera Priyadarsini
2020-10-22 13:54   ` [Outreachy kernel] " Greg KH
2020-10-22 13:49 ` [PATCH 5/5] " Sumera Priyadarsini
2020-10-22 14:13 ` [Outreachy kernel][PATCH 0/5] drm/amdgpu: Replace snprintf() with sysfs_emit Daniel Vetter

Linux-Media Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-media/0 linux-media/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-media linux-media/ https://lore.kernel.org/linux-media \
		linux-media@vger.kernel.org
	public-inbox-index linux-media

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-media


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git