[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <5F571243-683A-42F0-8E9B-4A8C9853A9D6@gmail.com>
Date: Sat, 9 Apr 2022 01:49:10 +0200
From: Jakob Koschel <jakobkoschel@...il.com>
To: Christophe Leroy <christophe.leroy@...roup.eu>
Cc: "David S. Miller" <davem@...emloft.net>,
Andrew Lunn <andrew@...n.ch>,
Cristiano Giuffrida <c.giuffrida@...nl>,
Eric Dumazet <edumazet@...gle.com>,
Paul Mackerras <paulus@...ba.org>,
Ariel Elior <aelior@...vell.com>,
Florian Fainelli <f.fainelli@...il.com>,
Manish Chopra <manishc@...vell.com>,
Steen Hegelund <Steen.Hegelund@...rochip.com>,
"Bos, H.J." <h.j.bos@...nl>,
"linux-arm-kernel@...ts.infradead.org"
<linux-arm-kernel@...ts.infradead.org>,
Martin Habets <habetsm.xilinx@...il.com>,
Paolo Abeni <pabeni@...hat.com>,
Vivien Didelot <vivien.didelot@...il.com>,
Bjarni Jonasson <bjarni.jonasson@...rochip.com>,
Jiri Pirko <jiri@...nulli.us>, Arnd Bergmann <arnd@...db.de>,
Brian Johannesmeyer <bjohannesmeyer@...il.com>,
Christophe JAILLET <christophe.jaillet@...adoo.fr>,
Jakub Kicinski <kuba@...nel.org>, Di Zhu <zhudi21@...wei.com>,
Lars Povlsen <lars.povlsen@...rochip.com>,
Colin Ian King <colin.king@...el.com>,
"netdev@...r.kernel.org" <netdev@...r.kernel.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"UNGLinuxDriver@...rochip.com" <UNGLinuxDriver@...rochip.com>,
Edward Cree <ecree.xilinx@...il.com>,
Michael Walle <michael@...le.cc>, Xu Wang <vulab@...as.ac.cn>,
Vladimir Oltean <olteanv@...il.com>,
"linuxppc-dev@...ts.ozlabs.org" <linuxppc-dev@...ts.ozlabs.org>,
Casper Andersson <casper.casan@...il.com>,
Mike Rapoport <rppt@...nel.org>
Subject: Re: [PATCH net-next 02/15] net: dsa: sja1105: Remove usage of
iterator for list_add() after loop
Hey Christophe,
> On 8. Apr 2022, at 09:47, Christophe Leroy <christophe.leroy@...roup.eu> wrote:
>
>
>
> Le 07/04/2022 à 12:28, Jakob Koschel a écrit :
>> In preparation to limit the scope of a list iterator to the list
>> traversal loop, use a dedicated pointer to point to the found element [1].
>>
>> Before, the code implicitly used the head when no element was found
>> when using &pos->list. Since the new variable is only set if an
>> element was found, the list_add() is performed within the loop
>> and only done after the loop if it is done on the list head directly.
>>
>> Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1]
>> Signed-off-by: Jakob Koschel <jakobkoschel@...il.com>
>> ---
>> drivers/net/dsa/sja1105/sja1105_vl.c | 14 +++++++++-----
>> 1 file changed, 9 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/net/dsa/sja1105/sja1105_vl.c b/drivers/net/dsa/sja1105/sja1105_vl.c
>> index b7e95d60a6e4..cfcae4d19eef 100644
>> --- a/drivers/net/dsa/sja1105/sja1105_vl.c
>> +++ b/drivers/net/dsa/sja1105/sja1105_vl.c
>> @@ -27,20 +27,24 @@ static int sja1105_insert_gate_entry(struct sja1105_gating_config *gating_cfg,
>> if (list_empty(&gating_cfg->entries)) {
>> list_add(&e->list, &gating_cfg->entries);
>> } else {
>> - struct sja1105_gate_entry *p;
>> + struct sja1105_gate_entry *p = NULL, *iter;
>>
>> - list_for_each_entry(p, &gating_cfg->entries, list) {
>> - if (p->interval == e->interval) {
>> + list_for_each_entry(iter, &gating_cfg->entries, list) {
>> + if (iter->interval == e->interval) {
>> NL_SET_ERR_MSG_MOD(extack,
>> "Gate conflict");
>> rc = -EBUSY;
>> goto err;
>> }
>>
>> - if (e->interval < p->interval)
>> + if (e->interval < iter->interval) {
>> + p = iter;
>> + list_add(&e->list, iter->list.prev);
>> break;
>> + }
>> }
>> - list_add(&e->list, p->list.prev);
>> + if (!p)
>> + list_add(&e->list, gating_cfg->entries.prev);
>> }
>>
>> gating_cfg->num_entries++;
>
> This change looks ugly, why duplicating the list_add() to do the same ?
> At the end of the loop the pointer contains gating_cfg->entries, so it
> was cleaner before.
>
> If you don't want to use the loop index outside the loop, fair enough,
> all you have to do is:
>
> struct sja1105_gate_entry *p, *iter;
>
> list_for_each_entry(iter, &gating_cfg->entries, list) {
> if (iter->interval == e->interval) {
> NL_SET_ERR_MSG_MOD(extack,
> "Gate conflict");
> rc = -EBUSY;
> goto err;
> }
> p = iter;
>
> if (e->interval < iter->interval)
> break;
> }
> list_add(&e->list, p->list.prev);
Thanks for the review and input.
The code you are showing here would have an uninitialized access to 'p'
if the list is empty.
Also 'p->list.prev' will be the second last entry if the list iterator
ran through completely, whereas the original code was pointing to the last
entry of the list.
IMO Vladimir Oltean posted a nice alternative way to solve it, see [1].
That way it keeps the semantics of the code the same and doesn't duplicate
the list_add.
>
>
>
> Christophe
[1] https://lore.kernel.org/linux-kernel/20220408114120.tvf2lxvhfqbnrlml@skbuf/
Thanks,
Jakob
Powered by blists - more mailing lists