diff mbox series

[net-next] devlink: append split port number to the port name

Message ID 20210526103508.760849-1-jiri@resnulli.us (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next] devlink: append split port number to the port name | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for net-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 1 maintainers not CCed: jiri@nvidia.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 1 this patch: 1
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 16 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 1 this patch: 1
netdev/header_inline success Link

Commit Message

Jiri Pirko May 26, 2021, 10:35 a.m. UTC
From: Jiri Pirko <jiri@nvidia.com>

Instead of doing sprintf twice in case the port is split or not, append
the split port suffix in case the port is split.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
---
 net/core/devlink.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

Comments

Jakub Kicinski May 27, 2021, 12:05 a.m. UTC | #1
On Wed, 26 May 2021 12:35:08 +0200 Jiri Pirko wrote:
> From: Jiri Pirko <jiri@nvidia.com>
> 
> Instead of doing sprintf twice in case the port is split or not, append
> the split port suffix in case the port is split.
> 
> Signed-off-by: Jiri Pirko <jiri@nvidia.com>

> diff --git a/net/core/devlink.c b/net/core/devlink.c
> index 06b2b1941dce..c7754c293010 100644
> --- a/net/core/devlink.c
> +++ b/net/core/devlink.c
> @@ -8632,12 +8632,10 @@ static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
>  	switch (attrs->flavour) {
>  	case DEVLINK_PORT_FLAVOUR_PHYSICAL:
>  	case DEVLINK_PORT_FLAVOUR_VIRTUAL:
> -		if (!attrs->split)
> -			n = snprintf(name, len, "p%u", attrs->phys.port_number);
> -		else
> -			n = snprintf(name, len, "p%us%u",
> -				     attrs->phys.port_number,
> -				     attrs->phys.split_subport_number);
> +		n = snprintf(name, len, "p%u", attrs->phys.port_number);

snprintf() can return n > len, you need to check for this before
passing len - n as an unsigned argument below.

> +		if (attrs->split)
> +			n += snprintf(name + n, len - n, "s%u",
> +				      attrs->phys.split_subport_number);
>  		break;
>  	case DEVLINK_PORT_FLAVOUR_CPU:
>  	case DEVLINK_PORT_FLAVOUR_DSA:
Jiri Pirko May 27, 2021, 10:12 a.m. UTC | #2
Thu, May 27, 2021 at 02:05:56AM CEST, kuba@kernel.org wrote:
>On Wed, 26 May 2021 12:35:08 +0200 Jiri Pirko wrote:
>> From: Jiri Pirko <jiri@nvidia.com>
>> 
>> Instead of doing sprintf twice in case the port is split or not, append
>> the split port suffix in case the port is split.
>> 
>> Signed-off-by: Jiri Pirko <jiri@nvidia.com>
>
>> diff --git a/net/core/devlink.c b/net/core/devlink.c
>> index 06b2b1941dce..c7754c293010 100644
>> --- a/net/core/devlink.c
>> +++ b/net/core/devlink.c
>> @@ -8632,12 +8632,10 @@ static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
>>  	switch (attrs->flavour) {
>>  	case DEVLINK_PORT_FLAVOUR_PHYSICAL:
>>  	case DEVLINK_PORT_FLAVOUR_VIRTUAL:
>> -		if (!attrs->split)
>> -			n = snprintf(name, len, "p%u", attrs->phys.port_number);
>> -		else
>> -			n = snprintf(name, len, "p%us%u",
>> -				     attrs->phys.port_number,
>> -				     attrs->phys.split_subport_number);
>> +		n = snprintf(name, len, "p%u", attrs->phys.port_number);
>
>snprintf() can return n > len, you need to check for this before
>passing len - n as an unsigned argument below.

Correct, will send v2. Thanks!

>
>> +		if (attrs->split)
>> +			n += snprintf(name + n, len - n, "s%u",
>> +				      attrs->phys.split_subport_number);
>>  		break;
>>  	case DEVLINK_PORT_FLAVOUR_CPU:
>>  	case DEVLINK_PORT_FLAVOUR_DSA:
>
diff mbox series

Patch

diff --git a/net/core/devlink.c b/net/core/devlink.c
index 06b2b1941dce..c7754c293010 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -8632,12 +8632,10 @@  static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
 	switch (attrs->flavour) {
 	case DEVLINK_PORT_FLAVOUR_PHYSICAL:
 	case DEVLINK_PORT_FLAVOUR_VIRTUAL:
-		if (!attrs->split)
-			n = snprintf(name, len, "p%u", attrs->phys.port_number);
-		else
-			n = snprintf(name, len, "p%us%u",
-				     attrs->phys.port_number,
-				     attrs->phys.split_subport_number);
+		n = snprintf(name, len, "p%u", attrs->phys.port_number);
+		if (attrs->split)
+			n += snprintf(name + n, len - n, "s%u",
+				      attrs->phys.split_subport_number);
 		break;
 	case DEVLINK_PORT_FLAVOUR_CPU:
 	case DEVLINK_PORT_FLAVOUR_DSA: