On Tue, Mar 18, 2025 at 12:02:48PM +0100, Hannes Reinecke wrote:Allocate first, and then free it if the entry is already present.
- list_for_each_entry(tmplport, &fcloop_lports, lport_list) {Hmm. I don't really like this pattern; there is a race condition
- if (tmplport->localport->node_name == opts->wwnn &&
- tmplport->localport->port_name == opts->wwpn)
- goto out_invalid_opts;
+ INIT_LIST_HEAD(&nport->nport_list);
+ nport->node_name = opts->wwnn;
+ nport->port_name = opts->wwpn;
+ refcount_set(&nport->ref, 1);
- if (tmplport->localport->node_name == opts->lpwwnn &&
- tmplport->localport->port_name == opts->lpwwpn)
- lport = tmplport;
+ spin_lock_irqsave(&fcloop_lock, flags);
+ list_add_tail(&nport->nport_list, &fcloop_nports);
+ spin_unlock_irqrestore(&fcloop_lock, flags);
}
between lookup and allocation leading to possibly duplicate entries
on the list.
Yes, that's not a good thing.
Lookup and allocation really need to be under the same lock.
This means the new entry has always to be allocated first and then we
either free it again or insert into the list, because it's not possible
to allocate under the spinlock. Not that beautiful but correctness wins.