diff mbox series

[net-next,2/8] devlink: use min_t to calculate data_size

Message ID 20221117220803.2773887-3-jacob.e.keller@intel.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series devlink: support direct read from region | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2 this patch: 2
netdev/cc_maintainers warning 3 maintainers not CCed: davem@davemloft.net pabeni@redhat.com edumazet@google.com
netdev/build_clang success Errors and warnings before: 5 this patch: 5
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 2 this patch: 2
netdev/checkpatch warning WARNING: Non-standard signature: Noticed-by:
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jacob Keller Nov. 17, 2022, 10:07 p.m. UTC
The calculation for the data_size in the devlink_nl_read_snapshot_fill
function uses an if statement that is better expressed using the min_t
macro.

Noticed-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
---
 net/core/devlink.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

Comments

Jakub Kicinski Nov. 19, 2022, 1:36 a.m. UTC | #1
On Thu, 17 Nov 2022 14:07:57 -0800 Jacob Keller wrote:
> The calculation for the data_size in the devlink_nl_read_snapshot_fill
> function uses an if statement that is better expressed using the min_t
> macro.
> 
> Noticed-by: Jakub Kicinski <kuba@kernel.org>

I'm afraid that's not a real tag. You can just drop it, 
I get sufficient credits.

> diff --git a/net/core/devlink.c b/net/core/devlink.c
> index 96afc7013959..932476956d7e 100644
> --- a/net/core/devlink.c
> +++ b/net/core/devlink.c
> @@ -6410,14 +6410,10 @@ static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
>  	*new_offset = start_offset;
>  
>  	while (curr_offset < end_offset) {
> -		u32 data_size;
> +		u32 data_size = min_t(u32, end_offset - curr_offset,
> +				      DEVLINK_REGION_READ_CHUNK_SIZE);

nit: don't put multi-line statements on the declaration line if it's 
not the only variable.

>  		u8 *data;
>  
> -		if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
> -			data_size = end_offset - curr_offset;
> -		else
> -			data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
Jacob Keller Nov. 21, 2022, 5:51 p.m. UTC | #2
On 11/18/2022 5:36 PM, Jakub Kicinski wrote:
> On Thu, 17 Nov 2022 14:07:57 -0800 Jacob Keller wrote:
>> The calculation for the data_size in the devlink_nl_read_snapshot_fill
>> function uses an if statement that is better expressed using the min_t
>> macro.
>>
>> Noticed-by: Jakub Kicinski <kuba@kernel.org>
> 
> I'm afraid that's not a real tag. You can just drop it,
> I get sufficient credits.
> 

Sure. I pulled this forward from some time ago, not sure why I had added 
it back then. A grep through the log shows its been used a handful of 
times, but it likely just slipped in without review in the past. Will 
remove.

>> diff --git a/net/core/devlink.c b/net/core/devlink.c
>> index 96afc7013959..932476956d7e 100644
>> --- a/net/core/devlink.c
>> +++ b/net/core/devlink.c
>> @@ -6410,14 +6410,10 @@ static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
>>   	*new_offset = start_offset;
>>   
>>   	while (curr_offset < end_offset) {
>> -		u32 data_size;
>> +		u32 data_size = min_t(u32, end_offset - curr_offset,
>> +				      DEVLINK_REGION_READ_CHUNK_SIZE);
> 
> nit: don't put multi-line statements on the declaration line if it's
> not the only variable.
> 

Sure, that makes sense.

>>   		u8 *data;
>>   
>> -		if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
>> -			data_size = end_offset - curr_offset;
>> -		else
>> -			data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
Jacob Keller Nov. 21, 2022, 6:35 p.m. UTC | #3
On 11/21/2022 9:51 AM, Jacob Keller wrote:
> On 11/18/2022 5:36 PM, Jakub Kicinski wrote:
>>> diff --git a/net/core/devlink.c b/net/core/devlink.c
>>> index 96afc7013959..932476956d7e 100644
>>> --- a/net/core/devlink.c
>>> +++ b/net/core/devlink.c
>>> @@ -6410,14 +6410,10 @@ static int 
>>> devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
>>>       *new_offset = start_offset;
>>>       while (curr_offset < end_offset) {
>>> -        u32 data_size;
>>> +        u32 data_size = min_t(u32, end_offset - curr_offset,
>>> +                      DEVLINK_REGION_READ_CHUNK_SIZE);
>>
>> nit: don't put multi-line statements on the declaration line if it's
>> not the only variable.
>>
> 
> Sure, that makes sense.
> 

This becomes the only variable in patch 5 of 8. It ends up making the 
diff look more complicated if I change it back to a combined 
declare+assign in that patch.
Jakub Kicinski Nov. 21, 2022, 7:06 p.m. UTC | #4
On Mon, 21 Nov 2022 10:35:34 -0800 Jacob Keller wrote:
> > Sure, that makes sense.
> 
> This becomes the only variable in patch 5 of 8. It ends up making the 
> diff look more complicated if I change it back to a combined 
> declare+assign in that patch.

Don't change it back to declare+assign, then? :)
In general declare+assign should be used sparingly IMO.
My eyes are trained to skip right past the variable declarations,
the goal is to make the code clear, not short :S

BTW you can probably make DEVLINK_REGION_READ_CHUNK_SIZE a ULL to switch
from min_t() to min() ?
Jacob Keller Nov. 21, 2022, 9:16 p.m. UTC | #5
On 11/21/2022 11:06 AM, Jakub Kicinski wrote:
> On Mon, 21 Nov 2022 10:35:34 -0800 Jacob Keller wrote:
>>> Sure, that makes sense.
>>
>> This becomes the only variable in patch 5 of 8. It ends up making the
>> diff look more complicated if I change it back to a combined
>> declare+assign in that patch.
> 
> Don't change it back to declare+assign, then? :)
> In general declare+assign should be used sparingly IMO.
> My eyes are trained to skip right past the variable declarations,
> the goal is to make the code clear, not short :S
> 
> BTW you can probably make DEVLINK_REGION_READ_CHUNK_SIZE a ULL to switch
> from min_t() to min() ?

Fair enough. It looked a bit weird when it was:

u32 data_size;

data_size = ...

But I think thats ok.

Thanks,
Jake
diff mbox series

Patch

diff --git a/net/core/devlink.c b/net/core/devlink.c
index 96afc7013959..932476956d7e 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -6410,14 +6410,10 @@  static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
 	*new_offset = start_offset;
 
 	while (curr_offset < end_offset) {
-		u32 data_size;
+		u32 data_size = min_t(u32, end_offset - curr_offset,
+				      DEVLINK_REGION_READ_CHUNK_SIZE);
 		u8 *data;
 
-		if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
-			data_size = end_offset - curr_offset;
-		else
-			data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
-
 		data = &snapshot->data[curr_offset];
 		err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
 							    data, data_size,