[PATCH] tpm: Replace scnprintf() with sysfs_emit() and sysfs_emit_at() in sysfs show functions

Chelsy Ratnawat posted 1 patch 3 months, 3 weeks ago
There is a newer version of this series
drivers/char/tpm/tpm_ppi.c | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
[PATCH] tpm: Replace scnprintf() with sysfs_emit() and sysfs_emit_at() in sysfs show functions
Posted by Chelsy Ratnawat 3 months, 3 weeks ago
Replace calls to scnprintf() with sysfs_emit() and sysfs_emit_at() in
sysfs show functions. These helpers are preferred in sysfs callbacks
because they automatically handle buffer sizing (PAGE_SIZE) and
improve safety and readability.

Signed-off-by: Chelsy Ratnawat <chelsyratnawat2001@gmail.com>
---
 drivers/char/tpm/tpm_ppi.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/drivers/char/tpm/tpm_ppi.c b/drivers/char/tpm/tpm_ppi.c
index bc7b1b4501b3..421c6264bac9 100644
--- a/drivers/char/tpm/tpm_ppi.c
+++ b/drivers/char/tpm/tpm_ppi.c
@@ -52,7 +52,7 @@ static ssize_t tpm_show_ppi_version(struct device *dev,
 {
 	struct tpm_chip *chip = to_tpm_chip(dev);
 
-	return scnprintf(buf, PAGE_SIZE, "%s\n", chip->ppi_version);
+	return sysfs_emit(buf, "%s\n", chip->ppi_version);
 }
 
 static ssize_t tpm_show_ppi_request(struct device *dev,
@@ -87,11 +87,11 @@ static ssize_t tpm_show_ppi_request(struct device *dev,
 		else {
 			req = obj->package.elements[1].integer.value;
 			if (tpm_ppi_req_has_parameter(req))
-				size = scnprintf(buf, PAGE_SIZE,
+				size = sysfs_emit(buf,
 				    "%llu %llu\n", req,
 				    obj->package.elements[2].integer.value);
 			else
-				size = scnprintf(buf, PAGE_SIZE,
+				size = sysfs_emit(buf,
 						"%llu\n", req);
 		}
 	} else if (obj->package.count == 2 &&
@@ -100,7 +100,7 @@ static ssize_t tpm_show_ppi_request(struct device *dev,
 		if (obj->package.elements[0].integer.value)
 			size = -EFAULT;
 		else
-			size = scnprintf(buf, PAGE_SIZE, "%llu\n",
+			size = sysfs_emit(buf, "%llu\n",
 				 obj->package.elements[1].integer.value);
 	}
 
@@ -211,9 +211,9 @@ static ssize_t tpm_show_ppi_transition_action(struct device *dev,
 	}
 
 	if (ret < ARRAY_SIZE(info) - 1)
-		status = scnprintf(buf, PAGE_SIZE, "%d: %s\n", ret, info[ret]);
+		status = sysfs_emit(buf, "%d: %s\n", ret, info[ret]);
 	else
-		status = scnprintf(buf, PAGE_SIZE, "%d: %s\n", ret,
+		status = sysfs_emit(buf, "%d: %s\n", ret,
 				   info[ARRAY_SIZE(info)-1]);
 	return status;
 }
@@ -255,22 +255,22 @@ static ssize_t tpm_show_ppi_response(struct device *dev,
 	res = ret_obj[2].integer.value;
 	if (req) {
 		if (res == 0)
-			status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
+			status = sysfs_emit(buf, "%llu %s\n", req,
 					   "0: Success");
 		else if (res == 0xFFFFFFF0)
-			status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
+			status = sysfs_emit(buf, "%llu %s\n", req,
 					   "0xFFFFFFF0: User Abort");
 		else if (res == 0xFFFFFFF1)
-			status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
+			status = sysfs_emit(buf, "%llu %s\n", req,
 					   "0xFFFFFFF1: BIOS Failure");
 		else if (res >= 1 && res <= 0x00000FFF)
-			status = scnprintf(buf, PAGE_SIZE, "%llu %llu: %s\n",
+			status = sysfs_emit(buf, "%llu %llu: %s\n",
 					   req, res, "Corresponding TPM error");
 		else
-			status = scnprintf(buf, PAGE_SIZE, "%llu %llu: %s\n",
+			status = sysfs_emit(buf, "%llu %llu: %s\n",
 					   req, res, "Error");
 	} else {
-		status = scnprintf(buf, PAGE_SIZE, "%llu: %s\n",
+		status = sysfs_emit(buf, "%llu: %s\n",
 				   req, "No Recent Request");
 	}
 
@@ -314,7 +314,7 @@ static ssize_t show_ppi_operations(acpi_handle dev_handle, char *buf, u32 start,
 		}
 
 		if (ret > 0 && ret < ARRAY_SIZE(info))
-			str += scnprintf(str, PAGE_SIZE, "%d %d: %s\n",
+			str += sysfs_emit_at(str, "%d %d: %s\n",
 					 i, ret, info[ret]);
 	}
 
-- 
2.47.1
Re: [PATCH] tpm: Replace scnprintf() with sysfs_emit() and sysfs_emit_at() in sysfs show functions
Posted by Jarkko Sakkinen 3 months, 2 weeks ago
On Thu, Jun 19, 2025 at 05:37:07AM -0700, Chelsy Ratnawat wrote:
> Replace calls to scnprintf() with sysfs_emit() and sysfs_emit_at() in
> sysfs show functions. These helpers are preferred in sysfs callbacks
> because they automatically handle buffer sizing (PAGE_SIZE) and
> improve safety and readability.
> 
> Signed-off-by: Chelsy Ratnawat <chelsyratnawat2001@gmail.com>

Some comments about the commit message.

Don't use "readability" as an argument as it is something that we cannot
measure, and thus useless documentation.

I neither don't understand "PAGE_SIZE" in the parentheses, or how it
"connects" to the sentence. And what do you mean exactly by safety?

Off-topic'ish:

In the Jargon world, safety is actually different than security :-)
Commercial airline planes have great safety (through redundancy)
and not as great (information) security. Just to reflect why
"safety" is neither an argument ...


> ---
>  drivers/char/tpm/tpm_ppi.c | 26 +++++++++++++-------------
>  1 file changed, 13 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/char/tpm/tpm_ppi.c b/drivers/char/tpm/tpm_ppi.c
> index bc7b1b4501b3..421c6264bac9 100644
> --- a/drivers/char/tpm/tpm_ppi.c
> +++ b/drivers/char/tpm/tpm_ppi.c
> @@ -52,7 +52,7 @@ static ssize_t tpm_show_ppi_version(struct device *dev,
>  {
>  	struct tpm_chip *chip = to_tpm_chip(dev);
>  
> -	return scnprintf(buf, PAGE_SIZE, "%s\n", chip->ppi_version);
> +	return sysfs_emit(buf, "%s\n", chip->ppi_version);
>  }
>  
>  static ssize_t tpm_show_ppi_request(struct device *dev,
> @@ -87,11 +87,11 @@ static ssize_t tpm_show_ppi_request(struct device *dev,
>  		else {
>  			req = obj->package.elements[1].integer.value;
>  			if (tpm_ppi_req_has_parameter(req))
> -				size = scnprintf(buf, PAGE_SIZE,
> +				size = sysfs_emit(buf,
>  				    "%llu %llu\n", req,
>  				    obj->package.elements[2].integer.value);
>  			else
> -				size = scnprintf(buf, PAGE_SIZE,
> +				size = sysfs_emit(buf,
>  						"%llu\n", req);
>  		}
>  	} else if (obj->package.count == 2 &&
> @@ -100,7 +100,7 @@ static ssize_t tpm_show_ppi_request(struct device *dev,
>  		if (obj->package.elements[0].integer.value)
>  			size = -EFAULT;
>  		else
> -			size = scnprintf(buf, PAGE_SIZE, "%llu\n",
> +			size = sysfs_emit(buf, "%llu\n",
>  				 obj->package.elements[1].integer.value);
>  	}
>  
> @@ -211,9 +211,9 @@ static ssize_t tpm_show_ppi_transition_action(struct device *dev,
>  	}
>  
>  	if (ret < ARRAY_SIZE(info) - 1)
> -		status = scnprintf(buf, PAGE_SIZE, "%d: %s\n", ret, info[ret]);
> +		status = sysfs_emit(buf, "%d: %s\n", ret, info[ret]);
>  	else
> -		status = scnprintf(buf, PAGE_SIZE, "%d: %s\n", ret,
> +		status = sysfs_emit(buf, "%d: %s\n", ret,
>  				   info[ARRAY_SIZE(info)-1]);
>  	return status;
>  }
> @@ -255,22 +255,22 @@ static ssize_t tpm_show_ppi_response(struct device *dev,
>  	res = ret_obj[2].integer.value;
>  	if (req) {
>  		if (res == 0)
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
> +			status = sysfs_emit(buf, "%llu %s\n", req,
>  					   "0: Success");
>  		else if (res == 0xFFFFFFF0)
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
> +			status = sysfs_emit(buf, "%llu %s\n", req,
>  					   "0xFFFFFFF0: User Abort");
>  		else if (res == 0xFFFFFFF1)
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
> +			status = sysfs_emit(buf, "%llu %s\n", req,
>  					   "0xFFFFFFF1: BIOS Failure");
>  		else if (res >= 1 && res <= 0x00000FFF)
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %llu: %s\n",
> +			status = sysfs_emit(buf, "%llu %llu: %s\n",
>  					   req, res, "Corresponding TPM error");
>  		else
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %llu: %s\n",
> +			status = sysfs_emit(buf, "%llu %llu: %s\n",
>  					   req, res, "Error");
>  	} else {
> -		status = scnprintf(buf, PAGE_SIZE, "%llu: %s\n",
> +		status = sysfs_emit(buf, "%llu: %s\n",
>  				   req, "No Recent Request");
>  	}
>  
> @@ -314,7 +314,7 @@ static ssize_t show_ppi_operations(acpi_handle dev_handle, char *buf, u32 start,
>  		}
>  
>  		if (ret > 0 && ret < ARRAY_SIZE(info))
> -			str += scnprintf(str, PAGE_SIZE, "%d %d: %s\n",
> +			str += sysfs_emit_at(str, "%d %d: %s\n",
>  					 i, ret, info[ret]);
>  	}
>  
> -- 
> 2.47.1
> 

BR, Jarkko
Re: [PATCH] tpm: Replace scnprintf() with sysfs_emit() and sysfs_emit_at() in sysfs show functions
Posted by Christophe JAILLET 3 months, 2 weeks ago
Le 19/06/2025 à 14:37, Chelsy Ratnawat a écrit :
> Replace calls to scnprintf() with sysfs_emit() and sysfs_emit_at() in
> sysfs show functions. These helpers are preferred in sysfs callbacks
> because they automatically handle buffer sizing (PAGE_SIZE) and
> improve safety and readability.
> 
> Signed-off-by: Chelsy Ratnawat <chelsyratnawat2001@gmail.com>
> ---
>   drivers/char/tpm/tpm_ppi.c | 26 +++++++++++++-------------
>   1 file changed, 13 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/char/tpm/tpm_ppi.c b/drivers/char/tpm/tpm_ppi.c
> index bc7b1b4501b3..421c6264bac9 100644
> --- a/drivers/char/tpm/tpm_ppi.c
> +++ b/drivers/char/tpm/tpm_ppi.c
> @@ -52,7 +52,7 @@ static ssize_t tpm_show_ppi_version(struct device *dev,
>   {
>   	struct tpm_chip *chip = to_tpm_chip(dev);
>   
> -	return scnprintf(buf, PAGE_SIZE, "%s\n", chip->ppi_version);
> +	return sysfs_emit(buf, "%s\n", chip->ppi_version);
>   }
>   
>   static ssize_t tpm_show_ppi_request(struct device *dev,
> @@ -87,11 +87,11 @@ static ssize_t tpm_show_ppi_request(struct device *dev,
>   		else {
>   			req = obj->package.elements[1].integer.value;
>   			if (tpm_ppi_req_has_parameter(req))
> -				size = scnprintf(buf, PAGE_SIZE,
> +				size = sysfs_emit(buf,
>   				    "%llu %llu\n", req,
>   				    obj->package.elements[2].integer.value);
>   			else
> -				size = scnprintf(buf, PAGE_SIZE,
> +				size = sysfs_emit(buf,
>   						"%llu\n", req);
>   		}
>   	} else if (obj->package.count == 2 &&
> @@ -100,7 +100,7 @@ static ssize_t tpm_show_ppi_request(struct device *dev,
>   		if (obj->package.elements[0].integer.value)
>   			size = -EFAULT;
>   		else
> -			size = scnprintf(buf, PAGE_SIZE, "%llu\n",
> +			size = sysfs_emit(buf, "%llu\n",
>   				 obj->package.elements[1].integer.value);
>   	}
>   
> @@ -211,9 +211,9 @@ static ssize_t tpm_show_ppi_transition_action(struct device *dev,
>   	}
>   
>   	if (ret < ARRAY_SIZE(info) - 1)
> -		status = scnprintf(buf, PAGE_SIZE, "%d: %s\n", ret, info[ret]);
> +		status = sysfs_emit(buf, "%d: %s\n", ret, info[ret]);
>   	else
> -		status = scnprintf(buf, PAGE_SIZE, "%d: %s\n", ret,
> +		status = sysfs_emit(buf, "%d: %s\n", ret,
>   				   info[ARRAY_SIZE(info)-1]);
>   	return status;
>   }
> @@ -255,22 +255,22 @@ static ssize_t tpm_show_ppi_response(struct device *dev,
>   	res = ret_obj[2].integer.value;
>   	if (req) {
>   		if (res == 0)
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
> +			status = sysfs_emit(buf, "%llu %s\n", req,
>   					   "0: Success");
>   		else if (res == 0xFFFFFFF0)
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
> +			status = sysfs_emit(buf, "%llu %s\n", req,
>   					   "0xFFFFFFF0: User Abort");
>   		else if (res == 0xFFFFFFF1)
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
> +			status = sysfs_emit(buf, "%llu %s\n", req,
>   					   "0xFFFFFFF1: BIOS Failure");
>   		else if (res >= 1 && res <= 0x00000FFF)
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %llu: %s\n",
> +			status = sysfs_emit(buf, "%llu %llu: %s\n",
>   					   req, res, "Corresponding TPM error");
>   		else
> -			status = scnprintf(buf, PAGE_SIZE, "%llu %llu: %s\n",
> +			status = sysfs_emit(buf, "%llu %llu: %s\n",
>   					   req, res, "Error");
>   	} else {
> -		status = scnprintf(buf, PAGE_SIZE, "%llu: %s\n",
> +		status = sysfs_emit(buf, "%llu: %s\n",
>   				   req, "No Recent Request");
>   	}
>   
> @@ -314,7 +314,7 @@ static ssize_t show_ppi_operations(acpi_handle dev_handle, char *buf, u32 start,
>   		}
>   
>   		if (ret > 0 && ret < ARRAY_SIZE(info))
> -			str += scnprintf(str, PAGE_SIZE, "%d %d: %s\n",
> +			str += sysfs_emit_at(str, "%d %d: %s\n",

sysfs_emit_at() does not work like that.
But does this code even compile?

CJ

>   					 i, ret, info[ret]);
>   	}
>