[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241016205437.12812-1-kuniyu@amazon.com>
Date: Wed, 16 Oct 2024 13:54:37 -0700
From: Kuniyuki Iwashima <kuniyu@...zon.com>
To: <gnaaman@...venets.com>
CC: <davem@...emloft.net>, <edumazet@...gle.com>, <kuba@...nel.org>,
<kuniyu@...zon.com>, <netdev@...r.kernel.org>, <pabeni@...hat.com>
Subject: Re: [PATCH net-next v4 3/6] Convert neigh_* seq_file functions to use hlist
From: Gilad Naaman <gnaaman@...venets.com>
Date: Wed, 16 Oct 2024 09:11:52 +0000
> > > - if (++state->bucket >= (1 << nht->hash_shift))
> > > - break;
> >
> > Let's keep this,
> >
> >
> > > + while (!n && ++state->bucket < (1 << nht->hash_shift))
> > > + n = neigh_first_rcu(&nht->hash_heads[state->bucket]);
> > >
> > > - n = rcu_dereference(nht->hash_buckets[state->bucket]);
> >
> > and simply fetch neigh_first_entry().
> >
> > Then, we can let the next loop handle the termination condition
> > and keep the code simple.
>
> Unfortunately `hlist_for_each_entry_continue_rcu` dereferences `n`
> first thing using `hlist_next_rcu`, before checking for NULL:
Right, and I noticed we even can't use neigh_first_entry() after
checking against NULL because the first entry will be skipped by
hlist_for_each_entry_continue_rcu().
>
> #define hlist_for_each_entry_continue_rcu(pos, member) \
> for (pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
> &(pos)->member)), typeof(*(pos)), member); \
> pos; \
> pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \
> &(pos)->member)), typeof(*(pos)), member))
>
> If I'm using it, I have to add a null-check after calling `neigh_first_entry`.
>
> Another alternative is to use `hlist_for_each_entry_from_rcu`,
> but it'll require calling `next` one time before the loop.
>
> Is this preferable?
How about factorising the operations inside loops and use
hlist_for_each_entry_continue() and call neigh_get_first()
in neigh_get_next() ?
completely not tested:
---8<---
static struct neighbour *neigh_get_valid(struct seq_file *seq,
struct neighbour *n,
loff_t *pos)
{
struct net *net = seq_file_net(seq);
if (!net_eq(dev_net(n->dev), net))
return NULL;
if (state->neigh_sub_iter) {
loff_t fakep = 0;
void *v;
v = state->neigh_sub_iter(state, n, pos ? pos : &fakep);
if (!v)
return NULL;
if (pos)
return v;
}
if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
return n;
if (READ_ONCE(n->nud_state) & ~NUD_NOARP)
return n;
return NULL;
}
static struct neighbour *neigh_get_first(struct seq_file *seq)
{
struct neigh_seq_state *state = seq->private;
struct neigh_hash_table *nht = state->nht;
struct net *net = seq_file_net(seq);
struct neighbour *n, *tmp;
state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
while (++state->bucket < (1 << nht->hash_shift)) {
neigh_for_each(n, &nht->hash_heads[state->bucket]) {
tmp = neigh_get_valid(seq, n, pos);
if (tmp)
return tmp;
}
}
return NULL;
}
static struct neighbour *neigh_get_next(struct seq_file *seq,
struct neighbour *n,
loff_t *pos)
{
struct neigh_seq_state *state = seq->private;
struct neigh_hash_table *nht = state->nht;
struct net *net = seq_file_net(seq);
struct neighbour *tmp;
if (state->neigh_sub_iter) {
void *v = state->neigh_sub_iter(state, n, pos);
if (v)
return n;
}
hlist_for_each_entry_continue(n, hash) {
tmp = neigh_get_valid(seq, n, pos);
if (tmp) {
n = tmp;
goto out;
}
}
n = neigh_get_first(seq);
out:
if (n && pos)
--(*pos);
return n;
}
---8<---
Powered by blists - more mailing lists