Subject: Re: [PATCH net-next v2 3/4] s390/qeth: Convert sysfs sprintf to sysfs_emit

From: Simon Horman <[email-masked]>
Date: Thu, 9 Feb 2023 12:27:31 +0100

On Thu, Feb 09, 2023 at 12:04:23PM +0100, Alexandra Winter wrote:
> From: Thorsten Winkler <[email-masked]>
> 
> Following the advice of the Documentation/filesystems/sysfs.rst.
> All sysfs related show()-functions should only use sysfs_emit() or
> sysfs_emit_at() when formatting the value to be returned to user space.
> 
> Reported-by: Jules Irenge <[email-masked]>
> Reported-by: Joe Perches <[email-masked]>
> Reviewed-by: Alexandra Winter <[email-masked]>
> Signed-off-by: Thorsten Winkler <[email-masked]>
> Signed-off-by: Alexandra Winter <[email-masked]>

...

> diff --git a/drivers/s390/net/qeth_l3_sys.c b/drivers/s390/net/qeth_l3_sys.c
> index 6143dd485810..f3986c6e21b9 100644
> --- a/drivers/s390/net/qeth_l3_sys.c
> +++ b/drivers/s390/net/qeth_l3_sys.c

...

> @@ -367,35 +367,21 @@ static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
>  			enum qeth_prot_versions proto)
>  {
>  	struct qeth_ipato_entry *ipatoe;
> -	int str_len = 0;
> +	char addr_str[INET6_ADDRSTRLEN];
> +	int offset = 0;
>  
>  	mutex_lock(&card->ip_lock);
>  	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
> -		char addr_str[INET6_ADDRSTRLEN];
> -		int entry_len;
> -
>  		if (ipatoe->proto != proto)
>  			continue;
>  
> -		entry_len = qeth_l3_ipaddr_to_string(proto, ipatoe->addr,
> -						     addr_str);
> -		if (entry_len < 0)
> -			continue;

Here the return code of qeth_l3_ipaddr_to_string() is checked for an error.

> -
> -		/* Append /%mask to the entry: */
> -		entry_len += 1 + ((proto == QETH_PROT_IPV4) ? 2 : 3);
> -		/* Enough room to format %entry\n into null terminated page? */
> -		if (entry_len + 1 > PAGE_SIZE - str_len - 1)
> -			break;
> -
> -		entry_len = scnprintf(buf, PAGE_SIZE - str_len,
> -				      "%s/%i\n", addr_str, ipatoe->mask_bits);
> -		str_len += entry_len;
> -		buf += entry_len;
> +		qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str);

But here it is not. Is that ok?

Likewise in qeth_l3_dev_ip_add_show().

> +		offset += sysfs_emit_at(buf, offset, "%s/%i\n",
> +					addr_str, ipatoe->mask_bits);
>  	}
>  	mutex_unlock(&card->ip_lock);
>  
> -	return str_len ? str_len : scnprintf(buf, PAGE_SIZE, "\n");
> +	return offset ? offset : sysfs_emit(buf, "\n");
>  }
>  
>  static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
> @@ -501,7 +487,7 @@ static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
>  {
>  	struct qeth_card *card = dev_get_drvdata(dev);
>  
> -	return sprintf(buf, "%u\n", card->ipato.invert6 ? 1 : 0);
> +	return sysfs_emit(buf, "%u\n", card->ipato.invert6 ? 1 : 0);
>  }
>  
>  static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
> @@ -586,35 +572,22 @@ static ssize_t qeth_l3_dev_ip_add_show(struct device *dev, char *buf,
>  				       enum qeth_ip_types type)
>  {
>  	struct qeth_card *card = dev_get_drvdata(dev);
> +	char addr_str[INET6_ADDRSTRLEN];
>  	struct qeth_ipaddr *ipaddr;
> -	int str_len = 0;
> +	int offset = 0;
>  	int i;
>  
>  	mutex_lock(&card->ip_lock);
>  	hash_for_each(card->ip_htable, i, ipaddr, hnode) {
> -		char addr_str[INET6_ADDRSTRLEN];
> -		int entry_len;
> -
>  		if (ipaddr->proto != proto || ipaddr->type != type)
>  			continue;
>  
> -		entry_len = qeth_l3_ipaddr_to_string(proto, (u8 *)&ipaddr->u,
> -						     addr_str);
> -		if (entry_len < 0)
> -			continue;
> -
> -		/* Enough room to format %addr\n into null terminated page? */
> -		if (entry_len + 1 > PAGE_SIZE - str_len - 1)
> -			break;
> -
> -		entry_len = scnprintf(buf, PAGE_SIZE - str_len, "%s\n",
> -				      addr_str);
> -		str_len += entry_len;
> -		buf += entry_len;
> +		qeth_l3_ipaddr_to_string(proto, (u8 *)&ipaddr->u, addr_str);
> +		offset += sysfs_emit_at(buf, offset, "%s\n", addr_str);
>  	}
>  	mutex_unlock(&card->ip_lock);
>  
> -	return str_len ? str_len : scnprintf(buf, PAGE_SIZE, "\n");
> +	return offset ? offset : sysfs_emit(buf, "\n");
>  }
>  
>  static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
> -- 
> 2.37.2
>