diff mbox series

[net-next,v1] net: Use csum_replace_... and csum_sub() helpers instead of opencoding

Message ID fe60030b6f674d9bf41f56426a4b0a8a9db0d20f.1645112415.git.christophe.leroy@csgroup.eu (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [net-next,v1] net: Use csum_replace_... and csum_sub() helpers instead of opencoding | 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 Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 3 this patch: 7
netdev/cc_maintainers success CCed 9 of 9 maintainers
netdev/build_clang success Errors and warnings before: 18 this patch: 18
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 8 this patch: 12
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 19 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Christophe Leroy Feb. 17, 2022, 3:43 p.m. UTC
In a couple places, open coded calculation can be avoided
and replaced by the equivalent csum_replace4() and
csum_replace_by_diff().

There's also one place where csum_sub() should be used instead of
csum_add().

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
 net/core/utils.c            | 4 ++--
 net/netfilter/nft_payload.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

Comments

Jakub Kicinski Feb. 17, 2022, 5:24 p.m. UTC | #1
On Thu, 17 Feb 2022 16:43:55 +0100 Christophe Leroy wrote:
>  static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum)
>  {
> -	*sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), fsum), tsum));
> +	csum_replace4(sum, fsum, tsum);

Sparse says:

net/netfilter/nft_payload.c:560:28: warning: incorrect type in argument 2 (different base types)
net/netfilter/nft_payload.c:560:28:    expected restricted __be32 [usertype] from
net/netfilter/nft_payload.c:560:28:    got restricted __wsum [usertype] fsum
Christophe Leroy Feb. 17, 2022, 6:11 p.m. UTC | #2
Le 17/02/2022 à 18:24, Jakub Kicinski a écrit :
> On Thu, 17 Feb 2022 16:43:55 +0100 Christophe Leroy wrote:
>>   static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum)
>>   {
>> -	*sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), fsum), tsum));
>> +	csum_replace4(sum, fsum, tsum);
> 
> Sparse says:
> 
> net/netfilter/nft_payload.c:560:28: warning: incorrect type in argument 2 (different base types)
> net/netfilter/nft_payload.c:560:28:    expected restricted __be32 [usertype] from
> net/netfilter/nft_payload.c:560:28:    got restricted __wsum [usertype] fsum

Yes I saw it in patchwork, thanks.

Looks like csum_replace4() expects __be32 inputs, I'll look at it but 
I'm not inclined at adding force cast, so will probably leave 
nft_csum_replace() as is.

Thanks
Christophe
Jakub Kicinski Feb. 17, 2022, 6:14 p.m. UTC | #3
On Thu, 17 Feb 2022 18:11:58 +0000 Christophe Leroy wrote:
> Looks like csum_replace4() expects __be32 inputs, I'll look at it but 
> I'm not inclined at adding force cast, so will probably leave 
> nft_csum_replace() as is.

That may imply also leaving it in your tree..
Christophe Leroy Feb. 17, 2022, 6:39 p.m. UTC | #4
Le 17/02/2022 à 19:14, Jakub Kicinski a écrit :
> On Thu, 17 Feb 2022 18:11:58 +0000 Christophe Leroy wrote:
>> Looks like csum_replace4() expects __be32 inputs, I'll look at it but
>> I'm not inclined at adding force cast, so will probably leave
>> nft_csum_replace() as is.
> 
> That may imply also leaving it in your tree..

By "leave as is" I meant I'll drop the change.

I'll send v2 without this hunk.

Christophe
Jakub Kicinski Feb. 17, 2022, 6:54 p.m. UTC | #5
On Thu, 17 Feb 2022 18:39:07 +0000 Christophe Leroy wrote:
> Le 17/02/2022 à 19:14, Jakub Kicinski a écrit :
> > On Thu, 17 Feb 2022 18:11:58 +0000 Christophe Leroy wrote:  
> >> Looks like csum_replace4() expects __be32 inputs, I'll look at it but
> >> I'm not inclined at adding force cast, so will probably leave
> >> nft_csum_replace() as is.  
> > 
> > That may imply also leaving it in your tree..  
> 
> By "leave as is" I meant I'll drop the change.
> 
> I'll send v2 without this hunk.

Thanks, sorry for the snarky comment, I read it as "take it or leave
it". I've heard too many of those lately.
kernel test robot Feb. 18, 2022, 7:43 a.m. UTC | #6
Hi Christophe,

I love your patch! Perhaps something to improve:

[auto build test WARNING on net-next/master]

url:    https://github.com/0day-ci/linux/commits/Christophe-Leroy/net-Use-csum_replace_-and-csum_sub-helpers-instead-of-opencoding/20220217-234555
base:   https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git c8b441d2fbd0e005541c7363fd5346971b6febcb
config: x86_64-rhel-8.3-kselftests (https://download.01.org/0day-ci/archive/20220218/202202181535.l3nA3dto-lkp@intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-dirty
        # https://github.com/0day-ci/linux/commit/cec9ed7cf59fe6dafcec0a30811024d22fad8cbd
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Christophe-Leroy/net-Use-csum_replace_-and-csum_sub-helpers-instead-of-opencoding/20220217-234555
        git checkout cec9ed7cf59fe6dafcec0a30811024d22fad8cbd
        # save the config file to linux build tree
        mkdir build_dir
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>


sparse warnings: (new ones prefixed by >>)
   net/netfilter/nft_payload.c: note: in included file (through include/net/sctp/sctp.h, include/net/sctp/checksum.h):
   include/net/sctp/structs.h:335:41: sparse: sparse: array of flexible structures
>> net/netfilter/nft_payload.c:560:28: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected restricted __be32 [usertype] from @@     got restricted __wsum [usertype] fsum @@
   net/netfilter/nft_payload.c:560:28: sparse:     expected restricted __be32 [usertype] from
   net/netfilter/nft_payload.c:560:28: sparse:     got restricted __wsum [usertype] fsum
>> net/netfilter/nft_payload.c:560:34: sparse: sparse: incorrect type in argument 3 (different base types) @@     expected restricted __be32 [usertype] to @@     got restricted __wsum [usertype] tsum @@
   net/netfilter/nft_payload.c:560:34: sparse:     expected restricted __be32 [usertype] to
   net/netfilter/nft_payload.c:560:34: sparse:     got restricted __wsum [usertype] tsum
>> net/netfilter/nft_payload.c:560:28: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected restricted __be32 [usertype] from @@     got restricted __wsum [usertype] fsum @@
   net/netfilter/nft_payload.c:560:28: sparse:     expected restricted __be32 [usertype] from
   net/netfilter/nft_payload.c:560:28: sparse:     got restricted __wsum [usertype] fsum
>> net/netfilter/nft_payload.c:560:34: sparse: sparse: incorrect type in argument 3 (different base types) @@     expected restricted __be32 [usertype] to @@     got restricted __wsum [usertype] tsum @@
   net/netfilter/nft_payload.c:560:34: sparse:     expected restricted __be32 [usertype] to
   net/netfilter/nft_payload.c:560:34: sparse:     got restricted __wsum [usertype] tsum

vim +560 net/netfilter/nft_payload.c

   557	
   558	static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum)
   559	{
 > 560		csum_replace4(sum, fsum, tsum);
   561		if (*sum == 0)
   562			*sum = CSUM_MANGLED_0;
   563	}
   564	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot April 22, 2022, 11:48 a.m. UTC | #7
Hi Christophe,

I love your patch! Perhaps something to improve:

[auto build test WARNING on net-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Christophe-Leroy/net-Use-csum_replace_-and-csum_sub-helpers-instead-of-opencoding/20220217-234555
base:   https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git c8b441d2fbd0e005541c7363fd5346971b6febcb
config: x86_64-rhel-8.3-kselftests (https://download.01.org/0day-ci/archive/20220422/202204221937.SbSpkzXW-lkp@intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-dirty
        # https://github.com/intel-lab-lkp/linux/commit/cec9ed7cf59fe6dafcec0a30811024d22fad8cbd
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Christophe-Leroy/net-Use-csum_replace_-and-csum_sub-helpers-instead-of-opencoding/20220217-234555
        git checkout cec9ed7cf59fe6dafcec0a30811024d22fad8cbd
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>


sparse warnings: (new ones prefixed by >>)
   net/netfilter/nft_payload.c: note: in included file (through include/net/sctp/sctp.h, include/net/sctp/checksum.h):
   include/net/sctp/structs.h:335:41: sparse: sparse: array of flexible structures
>> net/netfilter/nft_payload.c:560:28: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected restricted __be32 [usertype] from @@     got restricted __wsum [usertype] fsum @@
   net/netfilter/nft_payload.c:560:28: sparse:     expected restricted __be32 [usertype] from
   net/netfilter/nft_payload.c:560:28: sparse:     got restricted __wsum [usertype] fsum
>> net/netfilter/nft_payload.c:560:34: sparse: sparse: incorrect type in argument 3 (different base types) @@     expected restricted __be32 [usertype] to @@     got restricted __wsum [usertype] tsum @@
   net/netfilter/nft_payload.c:560:34: sparse:     expected restricted __be32 [usertype] to
   net/netfilter/nft_payload.c:560:34: sparse:     got restricted __wsum [usertype] tsum
>> net/netfilter/nft_payload.c:560:28: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected restricted __be32 [usertype] from @@     got restricted __wsum [usertype] fsum @@
   net/netfilter/nft_payload.c:560:28: sparse:     expected restricted __be32 [usertype] from
   net/netfilter/nft_payload.c:560:28: sparse:     got restricted __wsum [usertype] fsum
>> net/netfilter/nft_payload.c:560:34: sparse: sparse: incorrect type in argument 3 (different base types) @@     expected restricted __be32 [usertype] to @@     got restricted __wsum [usertype] tsum @@
   net/netfilter/nft_payload.c:560:34: sparse:     expected restricted __be32 [usertype] to
   net/netfilter/nft_payload.c:560:34: sparse:     got restricted __wsum [usertype] tsum

vim +560 net/netfilter/nft_payload.c

   557	
   558	static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum)
   559	{
 > 560		csum_replace4(sum, fsum, tsum);
   561		if (*sum == 0)
   562			*sum = CSUM_MANGLED_0;
   563	}
   564
diff mbox series

Patch

diff --git a/net/core/utils.c b/net/core/utils.c
index 1f31a39236d5..938495bc1d34 100644
--- a/net/core/utils.c
+++ b/net/core/utils.c
@@ -476,9 +476,9 @@  void inet_proto_csum_replace_by_diff(__sum16 *sum, struct sk_buff *skb,
 				     __wsum diff, bool pseudohdr)
 {
 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
-		*sum = csum_fold(csum_add(diff, ~csum_unfold(*sum)));
+		csum_replace_by_diff(sum, diff);
 		if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
-			skb->csum = ~csum_add(diff, ~skb->csum);
+			skb->csum = ~csum_sub(diff, skb->csum);
 	} else if (pseudohdr) {
 		*sum = ~csum_fold(csum_add(diff, csum_unfold(*sum)));
 	}
diff --git a/net/netfilter/nft_payload.c b/net/netfilter/nft_payload.c
index 5cc06aef4345..b6cfd0759bc5 100644
--- a/net/netfilter/nft_payload.c
+++ b/net/netfilter/nft_payload.c
@@ -557,7 +557,7 @@  const struct nft_expr_ops nft_payload_fast_ops = {
 
 static inline void nft_csum_replace(__sum16 *sum, __wsum fsum, __wsum tsum)
 {
-	*sum = csum_fold(csum_add(csum_sub(~csum_unfold(*sum), fsum), tsum));
+	csum_replace4(sum, fsum, tsum);
 	if (*sum == 0)
 		*sum = CSUM_MANGLED_0;
 }