2024-03-29 17:00:54

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH net-next 0/2] net: fix variable shadowing spam from headers

W=12 and/or C=1 are useful to test new code, but warnings coming from
the generic headers are noisy and distract a lot.
Fix the two most noisy networking header files by using __UNIQUE_ID()
for the variables declared inside macros. The rest seems to be clean,
except for the recent Clang's enum-conversion (which's sanity is still
under discussion).

Alexander Lobakin (2):
net/tcp: fix -Wshadow / Sparse shadow warnings in tcp_hash_fail()
netdev_queues: fix -Wshadow / Sparse shadow warnings throughout the
file

include/net/netdev_queues.h | 54 +++++++++++++++++++++++++++----------
include/net/tcp_ao.h | 6 ++++-
2 files changed, 45 insertions(+), 15 deletions(-)

--
2.44.0



2024-03-29 17:01:58

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH net-next 2/2] netdev_queues: fix -Wshadow / Sparse shadow warnings throughout the file

Fix the following spam coming from <net/netdev_queues.h> when building
with W=12 and/or C=1:

Clang:

drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:9: warning: declaration shadows a local variable [-Wshadow]
1992 | return netif_txq_maybe_stop(nq, IDPF_DESC_UNUSED(tx_q), size, size);
| ^
/include/net/netdev_queues.h:137:11: note: expanded from macro 'netif_txq_maybe_stop'
137 | _res = netif_txq_try_stop(txq, get_desc, start_thrs); \
| ^
/include/net/netdev_queues.h:92:7: note: expanded from macro 'netif_txq_try_stop'
92 | int _res; \
| ^
drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:9: note: previous declaration is here
/include/net/netdev_queues.h:133:7: note: expanded from macro 'netif_txq_maybe_stop'
133 | int _res; \
| ^

Sparse:

drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: warning: symbol '_res' shadows an earlier one
drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: originally declared here

Use __UNIQUE_ID() in all of the macros which declare local variables.

Signed-off-by: Alexander Lobakin <[email protected]>
---
include/net/netdev_queues.h | 54 +++++++++++++++++++++++++++----------
1 file changed, 40 insertions(+), 14 deletions(-)

diff --git a/include/net/netdev_queues.h b/include/net/netdev_queues.h
index 1ec408585373..317d6bfe32c7 100644
--- a/include/net/netdev_queues.h
+++ b/include/net/netdev_queues.h
@@ -87,14 +87,14 @@ struct netdev_stat_ops {
* be updated before invoking the macros.
*/

-#define netif_txq_try_stop(txq, get_desc, start_thrs) \
+#define _netif_txq_try_stop(txq, get_desc, start_thrs, _res) \
({ \
int _res; \
\
netif_tx_stop_queue(txq); \
/* Producer index and stop bit must be visible \
* to consumer before we recheck. \
- * Pairs with a barrier in __netif_txq_completed_wake(). \
+ * Pairs with a barrier in ___netif_txq_completed_wake(). \
*/ \
smp_mb__after_atomic(); \
\
@@ -107,16 +107,20 @@ struct netdev_stat_ops {
_res = -1; \
} \
_res; \
- }) \
+ })
+#define netif_txq_try_stop(txq, get_desc, start_thrs) \
+ _netif_txq_try_stop(txq, get_desc, start_thrs, \
+ __UNIQUE_ID(res_))

/**
- * netif_txq_maybe_stop() - locklessly stop a Tx queue, if needed
+ * _netif_txq_maybe_stop() - locklessly stop a Tx queue, if needed
* @txq: struct netdev_queue to stop/start
* @get_desc: get current number of free descriptors (see requirements below!)
* @stop_thrs: minimal number of available descriptors for queue to be left
* enabled
* @start_thrs: minimal number of descriptors to re-enable the queue, can be
* equal to @stop_thrs or higher to avoid frequent waking
+ * @_res: __UNIQUE_ID() to avoid variable name clash
*
* All arguments may be evaluated multiple times, beware of side effects.
* @get_desc must be a formula or a function call, it must always
@@ -128,7 +132,8 @@ struct netdev_stat_ops {
* 1 if the queue was left enabled
* -1 if the queue was re-enabled (raced with waking)
*/
-#define netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs) \
+#define _netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs, \
+ _res) \
({ \
int _res; \
\
@@ -136,7 +141,10 @@ struct netdev_stat_ops {
if (unlikely(get_desc < stop_thrs)) \
_res = netif_txq_try_stop(txq, get_desc, start_thrs); \
_res; \
- }) \
+ })
+#define netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs) \
+ _netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs, \
+ __UNIQUE_ID(res_))

/* Variant of netdev_tx_completed_queue() which guarantees smp_mb() if
* @bytes != 0, regardless of kernel config.
@@ -152,7 +160,7 @@ netdev_txq_completed_mb(struct netdev_queue *dev_queue,
}

/**
- * __netif_txq_completed_wake() - locklessly wake a Tx queue, if needed
+ * ___netif_txq_completed_wake() - locklessly wake a Tx queue, if needed
* @txq: struct netdev_queue to stop/start
* @pkts: number of packets completed
* @bytes: number of bytes completed
@@ -160,6 +168,7 @@ netdev_txq_completed_mb(struct netdev_queue *dev_queue,
* @start_thrs: minimal number of descriptors to re-enable the queue
* @down_cond: down condition, predicate indicating that the queue should
* not be woken up even if descriptors are available
+ * @_res: __UNIQUE_ID() to avoid variable name clash
*
* All arguments may be evaluated multiple times.
* @get_desc must be a formula or a function call, it must always
@@ -171,15 +180,15 @@ netdev_txq_completed_mb(struct netdev_queue *dev_queue,
* 1 if the queue was already enabled (or disabled but @down_cond is true)
* -1 if the queue was left unchanged (@start_thrs not reached)
*/
-#define __netif_txq_completed_wake(txq, pkts, bytes, \
- get_desc, start_thrs, down_cond) \
+#define ___netif_txq_completed_wake(txq, pkts, bytes, get_desc, \
+ start_thrs, down_cond, _res) \
({ \
int _res; \
\
/* Report to BQL and piggy back on its barrier. \
* Barrier makes sure that anybody stopping the queue \
* after this point sees the new consumer index. \
- * Pairs with barrier in netif_txq_try_stop(). \
+ * Pairs with barrier in _netif_txq_try_stop(). \
*/ \
netdev_txq_completed_mb(txq, pkts, bytes); \
\
@@ -194,30 +203,43 @@ netdev_txq_completed_mb(struct netdev_queue *dev_queue,
} \
_res; \
})
+#define __netif_txq_completed_wake(txq, pkts, bytes, get_desc, \
+ start_thrs, down_cond) \
+ ___netif_txq_completed_wake(txq, pkts, bytes, get_desc, \
+ start_thrs, down_cond, \
+ __UNIQUE_ID(res_))

#define netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs) \
__netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs, false)

/* subqueue variants follow */

-#define netif_subqueue_try_stop(dev, idx, get_desc, start_thrs) \
+#define _netif_subqueue_try_stop(dev, idx, get_desc, start_thrs, txq) \
({ \
struct netdev_queue *txq; \
\
txq = netdev_get_tx_queue(dev, idx); \
netif_txq_try_stop(txq, get_desc, start_thrs); \
})
+#define netif_subqueue_try_stop(dev, idx, get_desc, start_thrs) \
+ _netif_subqueue_try_stop(dev, idx, get_desc, start_thrs, \
+ __UNIQUE_ID(txq_))

-#define netif_subqueue_maybe_stop(dev, idx, get_desc, stop_thrs, start_thrs) \
+#define _netif_subqueue_maybe_stop(dev, idx, get_desc, stop_thrs, \
+ start_thrs, txq) \
({ \
struct netdev_queue *txq; \
\
txq = netdev_get_tx_queue(dev, idx); \
netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs); \
})
+#define netif_subqueue_maybe_stop(dev, idx, get_desc, stop_thrs, \
+ start_thrs) \
+ _netif_subqueue_maybe_stop(dev, idx, get_desc, stop_thrs, \
+ start_thrs, __UNIQUE_ID(txq_))

-#define netif_subqueue_completed_wake(dev, idx, pkts, bytes, \
- get_desc, start_thrs) \
+#define _netif_subqueue_completed_wake(dev, idx, pkts, bytes, get_desc, \
+ start_thrs, txq) \
({ \
struct netdev_queue *txq; \
\
@@ -225,5 +247,9 @@ netdev_txq_completed_mb(struct netdev_queue *dev_queue,
netif_txq_completed_wake(txq, pkts, bytes, \
get_desc, start_thrs); \
})
+#define netif_subqueue_completed_wake(dev, idx, pkts, bytes, get_desc, \
+ start_thrs) \
+ _netif_subqueue_completed_wake(dev, idx, pkts, bytes, get_desc, \
+ start_thrs, __UNIQUE_ID(txq_))

#endif
--
2.44.0


2024-03-29 17:02:13

by Alexander Lobakin

[permalink] [raw]
Subject: [PATCH net-next 1/2] net/tcp: fix -Wshadow / Sparse shadow warnings in tcp_hash_fail()

Fix the following spam coming from <net/tcp_ao.h> when building with
W=12 and/or C=1:

Clang:

In file included from drivers/net/ethernet/intel/idpf/idpf_txrx.c:4:
In file included from drivers/net/ethernet/intel/idpf/idpf.h:24:
In file included from drivers/net/ethernet/intel/idpf/idpf_txrx.h:8:
/include/net/tcp.h:2812:3: warning: declaration shadows a local variable [-Wshadow]
2812 | tcp_hash_fail("TCP segment has incorrect auth options set",
| ^
/include/net/tcp_ao.h:153:23: note: expanded from macro 'tcp_hash_fail'
153 | const struct tcphdr *th = tcp_hdr(skb); \
| ^
/include/net/tcp.h:2805:23: note: previous declaration is here
2805 | const struct tcphdr *th = tcp_hdr(skb);
| ^
/include/net/tcp.h:2820:4: warning: declaration shadows a local variable [-Wshadow]
2820 | tcp_hash_fail("TCP connection can't start/end using TCP-AO",
| ^
/include/net/tcp_ao.h:153:23: note: expanded from macro 'tcp_hash_fail'
153 | const struct tcphdr *th = tcp_hdr(skb); \
| ^
/include/net/tcp.h:2805:23: note: previous declaration is here
2805 | const struct tcphdr *th = tcp_hdr(skb);
| ^
/include/net/tcp.h:2840:4: warning: declaration shadows a local variable [-Wshadow]
2840 | tcp_hash_fail("AO hash is required, but not found",
| ^
/include/net/tcp_ao.h:153:23: note: expanded from macro 'tcp_hash_fail'
153 | const struct tcphdr *th = tcp_hdr(skb); \
| ^
/include/net/tcp.h:2805:23: note: previous declaration is here
2805 | const struct tcphdr *th = tcp_hdr(skb);
| ^
/include/net/tcp.h:2846:4: warning: declaration shadows a local variable [-Wshadow]

Sparse:

drivers/net/ethernet/intel/idpf/idpf_main.c: note: in included file (through drivers/net/ethernet/intel/idpf/idpf_txrx.h, drivers/net/ethernet/intel/idpf/idpf.h):
/include/net/tcp.h:2812:17: warning: symbol 'th' shadows an earlier one
/include/net/tcp.h:2805:29: originally declared here
/include/net/tcp.h:2820:25: warning: symbol 'th' shadows an earlier one
/include/net/tcp.h:2805:29: originally declared here
/include/net/tcp.h:2840:25: warning: symbol 'th' shadows an earlier one
/include/net/tcp.h:2805:29: originally declared here
/include/net/tcp.h:2846:25: warning: symbol 'th' shadows an earlier one
/include/net/tcp.h:2805:29: originally declared here

Just use __UNIQUE_ID() for the variables declared inside
tcp_hash_fail().

Signed-off-by: Alexander Lobakin <[email protected]>
---
include/net/tcp_ao.h | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h
index 471e177362b4..c5303c9c6f80 100644
--- a/include/net/tcp_ao.h
+++ b/include/net/tcp_ao.h
@@ -148,7 +148,7 @@ static inline bool tcp_hash_should_produce_warnings(void)
return static_branch_tcp_md5() || static_branch_tcp_ao();
}

-#define tcp_hash_fail(msg, family, skb, fmt, ...) \
+#define _tcp_hash_fail(msg, family, skb, th, hdr_flags, f, fmt, ...) \
do { \
const struct tcphdr *th = tcp_hdr(skb); \
char hdr_flags[6]; \
@@ -179,6 +179,10 @@ do { \
hdr_flags, ##__VA_ARGS__); \
} \
} while (0)
+#define tcp_hash_fail(msg, family, skb, fmt, ...) \
+ _tcp_hash_fail(msg, family, skb, __UNIQUE_ID(th_), \
+ __UNIQUE_ID(hdr_flags_), __UNIQUE_ID(f_), fmt, \
+ ##__VA_ARGS__)

#ifdef CONFIG_TCP_AO
/* TCP-AO structures and functions */
--
2.44.0


2024-03-29 20:19:14

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH net-next 2/2] netdev_queues: fix -Wshadow / Sparse shadow warnings throughout the file

On Fri, 29 Mar 2024 18:00:00 +0100 Alexander Lobakin wrote:
> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:9: warning: declaration shadows a local variable [-Wshadow]
> 1992 | return netif_txq_maybe_stop(nq, IDPF_DESC_UNUSED(tx_q), size, size);
> | ^
> ./include/net/netdev_queues.h:137:11: note: expanded from macro 'netif_txq_maybe_stop'
> 137 | _res = netif_txq_try_stop(txq, get_desc, start_thrs); \
> | ^
> ./include/net/netdev_queues.h:92:7: note: expanded from macro 'netif_txq_try_stop'
> 92 | int _res; \
> | ^
> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:9: note: previous declaration is here
> ./include/net/netdev_queues.h:133:7: note: expanded from macro 'netif_txq_maybe_stop'
> 133 | int _res; \
> | ^
>
> Sparse:
>
> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: warning: symbol '_res' shadows an earlier one
> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: originally declared here

I don't see these building with LLVM=1 W=12 C=1
and I really don't like complicating the code because the compiler
is stupid. Can't you solve this with some renames? Add another
underscore or something?

2024-03-29 20:53:57

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH net-next 2/2] netdev_queues: fix -Wshadow / Sparse shadow warnings throughout the file

On Fri, 29 Mar 2024 13:18:57 -0700 Jakub Kicinski wrote:
> > Sparse:
> >
> > drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: warning: symbol '_res' shadows an earlier one
> > drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: originally declared here
>
> I don't see these building with LLVM=1 W=12 C=1
> and I really don't like complicating the code because the compiler
> is stupid. Can't you solve this with some renames? Add another
> underscore or something?

I'm stupid I tried on the test branch which already had your fix..

This is enough:

diff --git a/include/net/netdev_queues.h b/include/net/netdev_queues.h
index 1ec408585373..2270fbb99cf7 100644
--- a/include/net/netdev_queues.h
+++ b/include/net/netdev_queues.h
@@ -89,7 +89,7 @@ struct netdev_stat_ops {

#define netif_txq_try_stop(txq, get_desc, start_thrs) \
({ \
- int _res; \
+ int __res; \
\
netif_tx_stop_queue(txq); \
/* Producer index and stop bit must be visible \
@@ -101,12 +101,12 @@ struct netdev_stat_ops {
/* We need to check again in a case another \
* CPU has just made room available. \
*/ \
- _res = 0; \
+ __res = 0; \
if (unlikely(get_desc >= start_thrs)) { \
netif_tx_start_queue(txq); \
- _res = -1; \
+ __res = -1; \
} \
- _res; \
+ __res; \
}) \

/**

2024-04-02 12:07:34

by Alexander Lobakin

[permalink] [raw]
Subject: Re: [PATCH net-next 2/2] netdev_queues: fix -Wshadow / Sparse shadow warnings throughout the file

From: Jakub Kicinski <[email protected]>
Date: Fri, 29 Mar 2024 13:53:44 -0700

> On Fri, 29 Mar 2024 13:18:57 -0700 Jakub Kicinski wrote:
>>> Sparse:
>>>
>>> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: warning: symbol '_res' shadows an earlier one
>>> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: originally declared here
>>
>> I don't see these building with LLVM=1 W=12 C=1
>> and I really don't like complicating the code because the compiler
>> is stupid. Can't you solve this with some renames? Add another

It's not the compiler, its warnings are valid actually. Shadowing makes
it very easy to confuse variables and make bugs...

>> underscore or something?
>
> I'm stupid I tried on the test branch which already had your fix..

:D Sometimes it happens.

>
> This is enough:
>
> diff --git a/include/net/netdev_queues.h b/include/net/netdev_queues.h
> index 1ec408585373..2270fbb99cf7 100644
> --- a/include/net/netdev_queues.h
> +++ b/include/net/netdev_queues.h
> @@ -89,7 +89,7 @@ struct netdev_stat_ops {
>
> #define netif_txq_try_stop(txq, get_desc, start_thrs) \
> ({ \
> - int _res; \
> + int __res; \
> \
> netif_tx_stop_queue(txq); \
> /* Producer index and stop bit must be visible \
> @@ -101,12 +101,12 @@ struct netdev_stat_ops {
> /* We need to check again in a case another \
> * CPU has just made room available. \
> */ \
> - _res = 0; \
> + __res = 0; \
> if (unlikely(get_desc >= start_thrs)) { \
> netif_tx_start_queue(txq); \
> - _res = -1; \
> + __res = -1; \
> } \
> - _res; \
> + __res; \
> }) \
>
> /**

But what if there's a function which calls one of these functions and
already has _res or __res or something? I know renaming is enough for
the warnings I mentioned, but without __UNIQUE_ID() anything can happen
anytime, so I wanted to fix that once and for all :z

I already saw some macros which have a layer of indirection for
__UNIQUE_ID(), but previously they didn't and then there were fixes
which added underscores, renamed variables etc etc...

Thanks,
Olek

2024-04-02 12:45:36

by Eric Dumazet

[permalink] [raw]
Subject: Re: [PATCH net-next 2/2] netdev_queues: fix -Wshadow / Sparse shadow warnings throughout the file

On Tue, Apr 2, 2024 at 1:55 PM Alexander Lobakin
<[email protected]> wrote:
>
> From: Jakub Kicinski <[email protected]>
> Date: Fri, 29 Mar 2024 13:53:44 -0700
>
> > On Fri, 29 Mar 2024 13:18:57 -0700 Jakub Kicinski wrote:
> >>> Sparse:
> >>>
> >>> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: warning: symbol '_res' shadows an earlier one
> >>> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: originally declared here
> >>
> >> I don't see these building with LLVM=1 W=12 C=1
> >> and I really don't like complicating the code because the compiler
> >> is stupid. Can't you solve this with some renames? Add another
>
> It's not the compiler, its warnings are valid actually. Shadowing makes
> it very easy to confuse variables and make bugs...
>
> >> underscore or something?
> >
> > I'm stupid I tried on the test branch which already had your fix..
>
> :D Sometimes it happens.
>
> >
> > This is enough:
> >
> > diff --git a/include/net/netdev_queues.h b/include/net/netdev_queues.h
> > index 1ec408585373..2270fbb99cf7 100644
> > --- a/include/net/netdev_queues.h
> > +++ b/include/net/netdev_queues.h
> > @@ -89,7 +89,7 @@ struct netdev_stat_ops {
> >
> > #define netif_txq_try_stop(txq, get_desc, start_thrs) \
> > ({ \
> > - int _res; \
> > + int __res; \
> > \
> > netif_tx_stop_queue(txq); \
> > /* Producer index and stop bit must be visible \
> > @@ -101,12 +101,12 @@ struct netdev_stat_ops {
> > /* We need to check again in a case another \
> > * CPU has just made room available. \
> > */ \
> > - _res = 0; \
> > + __res = 0; \
> > if (unlikely(get_desc >= start_thrs)) { \
> > netif_tx_start_queue(txq); \
> > - _res = -1; \
> > + __res = -1; \
> > } \
> > - _res; \
> > + __res; \
> > }) \
> >
> > /**
>
> But what if there's a function which calls one of these functions and
> already has _res or __res or something? I know renaming is enough for
> the warnings I mentioned, but without __UNIQUE_ID() anything can happen
> anytime, so I wanted to fix that once and for all :z
>
> I already saw some macros which have a layer of indirection for
> __UNIQUE_ID(), but previously they didn't and then there were fixes
> which added underscores, renamed variables etc etc...
>

We have hundreds of macros in include/ directory which use local names without
__UNIQUE_ID()

What is the plan ? Hundreds of patches obfuscating them more than they are ?

Can you show us how rb_entry_safe() (random choice) would be rewritten ?

2024-04-02 15:54:56

by Alexander Lobakin

[permalink] [raw]
Subject: Re: [PATCH net-next 2/2] netdev_queues: fix -Wshadow / Sparse shadow warnings throughout the file

From: Eric Dumazet <[email protected]>
Date: Tue, 2 Apr 2024 14:45:07 +0200

> On Tue, Apr 2, 2024 at 1:55 PM Alexander Lobakin
> <[email protected]> wrote:
>>
>> From: Jakub Kicinski <[email protected]>
>> Date: Fri, 29 Mar 2024 13:53:44 -0700
>>
>>> On Fri, 29 Mar 2024 13:18:57 -0700 Jakub Kicinski wrote:
>>>>> Sparse:
>>>>>
>>>>> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: warning: symbol '_res' shadows an earlier one
>>>>> drivers/net/ethernet/intel/idpf/idpf_txrx.c:1992:16: originally declared here
>>>>
>>>> I don't see these building with LLVM=1 W=12 C=1
>>>> and I really don't like complicating the code because the compiler
>>>> is stupid. Can't you solve this with some renames? Add another
>>
>> It's not the compiler, its warnings are valid actually. Shadowing makes
>> it very easy to confuse variables and make bugs...
>>
>>>> underscore or something?
>>>
>>> I'm stupid I tried on the test branch which already had your fix..
>>
>> :D Sometimes it happens.
>>
>>>
>>> This is enough:
>>>
>>> diff --git a/include/net/netdev_queues.h b/include/net/netdev_queues.h
>>> index 1ec408585373..2270fbb99cf7 100644
>>> --- a/include/net/netdev_queues.h
>>> +++ b/include/net/netdev_queues.h
>>> @@ -89,7 +89,7 @@ struct netdev_stat_ops {
>>>
>>> #define netif_txq_try_stop(txq, get_desc, start_thrs) \
>>> ({ \
>>> - int _res; \
>>> + int __res; \
>>> \
>>> netif_tx_stop_queue(txq); \
>>> /* Producer index and stop bit must be visible \
>>> @@ -101,12 +101,12 @@ struct netdev_stat_ops {
>>> /* We need to check again in a case another \
>>> * CPU has just made room available. \
>>> */ \
>>> - _res = 0; \
>>> + __res = 0; \
>>> if (unlikely(get_desc >= start_thrs)) { \
>>> netif_tx_start_queue(txq); \
>>> - _res = -1; \
>>> + __res = -1; \
>>> } \
>>> - _res; \
>>> + __res; \
>>> }) \
>>>
>>> /**
>>
>> But what if there's a function which calls one of these functions and
>> already has _res or __res or something? I know renaming is enough for
>> the warnings I mentioned, but without __UNIQUE_ID() anything can happen
>> anytime, so I wanted to fix that once and for all :z
>>
>> I already saw some macros which have a layer of indirection for
>> __UNIQUE_ID(), but previously they didn't and then there were fixes
>> which added underscores, renamed variables etc etc...
>>
>
> We have hundreds of macros in include/ directory which use local names without
> __UNIQUE_ID()

Most of them were added before __UNIQUE_ID() became norm, weren't they?
Lots of them were switched to __UNIQUE_ID() because of issues, weren't they?

>
> What is the plan ? Hundreds of patches obfuscating them more than they are ?

Only those which flood the console when building with W=12 C=1 to
recheck that my new code is fine.

>
> Can you show us how rb_entry_safe() (random choice) would be rewritten ?

Is it unique in some way?

#define _rb_entry_safe(ptr, type, member, ____ptr) \
({ typeof(ptr) ____ptr = (ptr); \
____ptr ? rb_entry(____ptr, type, member) : NULL; \
})
#define rb_entry_safe(ptr, type, member) \
_rb_entry_safe(ptr, type, member, \
__UNIQUE_ID(ptr_)

(@____ptr can be renamed if needed, this one would give the smallest
code diff)

If you think +1 layer is a terrible obfuscating, look at
<linux/fortify-string.h> :D

Thanks,
Olek

2024-04-02 17:26:41

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH net-next 2/2] netdev_queues: fix -Wshadow / Sparse shadow warnings throughout the file

On Tue, 2 Apr 2024 17:53:08 +0200 Alexander Lobakin wrote:
> >> But what if there's a function which calls one of these functions and
> >> already has _res or __res or something? I know renaming is enough for
> >> the warnings I mentioned, but without __UNIQUE_ID() anything can happen
> >> anytime, so I wanted to fix that once and for all :z
> >>
> >> I already saw some macros which have a layer of indirection for
> >> __UNIQUE_ID(), but previously they didn't and then there were fixes
> >> which added underscores, renamed variables etc etc...
> >>
> >
> > We have hundreds of macros in include/ directory which use local names without
> > __UNIQUE_ID()
>
> Most of them were added before __UNIQUE_ID() became norm, weren't they?
> Lots of them were switched to __UNIQUE_ID() because of issues, weren't they?

Lots of ugly code gets into the kernel. Just look at your patch and
then look at mine.

I understand __UNIQUE_ID() may be useful for libraries or global
macros in the kernel, but within a subsystem, for macros which are
rarely used, we can just patch the macro var names. Sprinkling
__UNIQUE_ID() is in bad taste.

> > What is the plan ? Hundreds of patches obfuscating them more than they are ?
>
> Only those which flood the console when building with W=12 C=1 to
> recheck that my new code is fine.

I have never seen this warning be useful in the context of a macro.
Sure if you shadow inside a function that may be pernicious.
But well written macro will not be a problem.
I guess that it may be really hard for the compiler to understand that
something was a macro but perhaps we should either:
- ignore the warning if the shadowing happens inside a compound
statement
- add a declaration attribute to turn the warning off
?