From: Sabrina Dubroca <sd@queasysnail.net>
To: Antonio Quartulli <antonio@openvpn.net>
Cc: netdev@vger.kernel.org, kuba@kernel.org, pabeni@redhat.com,
ryazanov.s.a@gmail.com, edumazet@google.com, andrew@lunn.ch
Subject: Re: [PATCH net-next v6 15/25] ovpn: implement multi-peer support
Date: Thu, 5 Sep 2024 12:47:04 +0200 [thread overview]
Message-ID: <ZtmMKDPZzsFdbTpq@hog> (raw)
In-Reply-To: <79b087fc-1e73-4ce5-82cb-b309326ae78e@openvpn.net>
2024-09-05, 10:02:58 +0200, Antonio Quartulli wrote:
> On 03/09/2024 16:40, Sabrina Dubroca wrote:
> > 2024-08-27, 14:07:55 +0200, Antonio Quartulli wrote:
> > > static int ovpn_net_init(struct net_device *dev)
> > > {
> > > struct ovpn_struct *ovpn = netdev_priv(dev);
> > > + int i, err = gro_cells_init(&ovpn->gro_cells, dev);
> >
> > I'm not a fan of "hiding" the gro_cells_init call up here. I'd prefer
> > if this was done just before the corresponding "if (err)".
>
> I am all with you, but I remember in the past something complaining about
> "variable declared and then re-assigned right after".
>
> But maybe this is not the case anymore.
If you had something like:
int err;
err = -EINVAL;
sure, it would make sense to combine them.
>
> Will move the initialization down.
Thanks.
> > > +
> > > + spin_lock_init(&ovpn->peers->lock_by_id);
> > > + spin_lock_init(&ovpn->peers->lock_by_vpn_addr);
> > > + spin_lock_init(&ovpn->peers->lock_by_transp_addr);
> >
> > What's the benefit of having 3 separate locks instead of a single lock
> > protecting all the hashtables?
>
> The main reason was to avoid a deadlock - I thought I had added a comment
> about it...
Ok.
I could have missed it, I'm not looking at the comments much now that
I'm familiar with the code.
> The problem was a deadlock between acquiring peer->lock and
> ovpn->peers->lock in float() and in then opposite sequence in peers_free().
> (IIRC this happens due to ovpn_peer_reset_sockaddr() acquiring peer->lock)
I don't see a problem with ovpn_peer_reset_sockaddr, but ovpn_peer_put
can be called with lock_by_id held and then take peer->lock (in
ovpn_peer_release), which would be the opposite order to
ovpn_peer_float if the locks were merged (peer->lock then
lock_by_transp_addr).
This should be solvable with a single lock by delaying the bind
cleanup via call_rcu instead of doing it immediately with
ovpn_peer_release (after that delay, nothing should be using
peer->bind anymore, since we have no reference and no more
rcu_read_lock sections that could have found peer, so we can free
immediately and no need to take peer->lock). And it's I think a bit
more "correct" wrt RCU rules, since at ovpn_peer_put time, even with
refcount=0, we could have a reader still using the peer and deciding
to update its bind (not the case with how ovpn_peer_float is called,
since we have a reference on the peer).
(This could be completely wrong and/or make no sense at all :))
But I'm not going to insist on this, you can keep the separate locks.
> Splitting the larger peers->lock allowed me to avoid this scenario, because
> I don't need to jump through any hoop to coordinate access to different
> hashtables.
>
> >
> > > +
> > > + for (i = 0; i < ARRAY_SIZE(ovpn->peers->by_id); i++) {
> > > + INIT_HLIST_HEAD(&ovpn->peers->by_id[i]);
> > > + INIT_HLIST_HEAD(&ovpn->peers->by_vpn_addr[i]);
> > > + INIT_HLIST_NULLS_HEAD(&ovpn->peers->by_transp_addr[i],
> > > + i);
> > > + }
> > > + }
> > > +
> > > + return 0;
> > > }
> >
> > > +static int ovpn_peer_add_mp(struct ovpn_struct *ovpn, struct ovpn_peer *peer)
> > > +{
> > > + struct sockaddr_storage sa = { 0 };
> > > + struct hlist_nulls_head *nhead;
> > > + struct sockaddr_in6 *sa6;
> > > + struct sockaddr_in *sa4;
> > > + struct hlist_head *head;
> > > + struct ovpn_bind *bind;
> > > + struct ovpn_peer *tmp;
> > > + size_t salen;
> > > +
> > > + spin_lock_bh(&ovpn->peers->lock_by_id);
> > > + /* do not add duplicates */
> > > + tmp = ovpn_peer_get_by_id(ovpn, peer->id);
> > > + if (tmp) {
> > > + ovpn_peer_put(tmp);
> > > + spin_unlock_bh(&ovpn->peers->lock_by_id);
> > > + return -EEXIST;
> > > + }
> > > +
> > > + hlist_add_head_rcu(&peer->hash_entry_id,
> > > + ovpn_get_hash_head(ovpn->peers->by_id, &peer->id,
> > > + sizeof(peer->id)));
> > > + spin_unlock_bh(&ovpn->peers->lock_by_id);
> > > +
> > > + bind = rcu_dereference_protected(peer->bind, true);
> > > + /* peers connected via TCP have bind == NULL */
> > > + if (bind) {
> > > + switch (bind->remote.in4.sin_family) {
> > > + case AF_INET:
> > > + sa4 = (struct sockaddr_in *)&sa;
> > > +
> > > + sa4->sin_family = AF_INET;
> > > + sa4->sin_addr.s_addr = bind->remote.in4.sin_addr.s_addr;
> > > + sa4->sin_port = bind->remote.in4.sin_port;
> > > + salen = sizeof(*sa4);
> > > + break;
> > > + case AF_INET6:
> > > + sa6 = (struct sockaddr_in6 *)&sa;
> > > +
> > > + sa6->sin6_family = AF_INET6;
> > > + sa6->sin6_addr = bind->remote.in6.sin6_addr;
> > > + sa6->sin6_port = bind->remote.in6.sin6_port;
> > > + salen = sizeof(*sa6);
> > > + break;
> > > + default:
> >
> > And remove from the by_id hashtable? Or is that handled somewhere that
> > I missed (I don't think ovpn_peer_unhash gets called in that case)?
>
> No we don't call unhash in this case as we assume the adding just failed
> entirely.
>
> I will add the removal before returning the error (moving the add below the
> switch would extend the locked area too much.)
I don't think setting a few variables would be too much to do under
the lock (and it would address the issues in my 2nd reply to this
patch).
--
Sabrina
next prev parent reply other threads:[~2024-09-05 10:47 UTC|newest]
Thread overview: 58+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-27 12:07 [PATCH net-next v6 00/25] Introducing OpenVPN Data Channel Offload Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 01/25] netlink: add NLA_POLICY_MAX_LEN macro Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 02/25] rtnetlink: don't crash on unregister if no dellink exists Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 03/25] net: introduce OpenVPN Data Channel Offload (ovpn) Antonio Quartulli
2024-09-05 14:38 ` Sabrina Dubroca
2024-09-06 12:26 ` Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 04/25] ovpn: add basic netlink support Antonio Quartulli
2024-09-06 19:26 ` Simon Horman
2024-09-09 8:35 ` Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 05/25] ovpn: add basic interface creation/destruction/management routines Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 06/25] ovpn: implement interface creation/destruction via netlink Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 07/25] ovpn: keep carrier always on Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 08/25] ovpn: introduce the ovpn_peer object Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 09/25] ovpn: introduce the ovpn_socket object Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 10/25] ovpn: implement basic TX path (UDP) Antonio Quartulli
2024-08-30 17:02 ` Sabrina Dubroca
2024-09-02 12:03 ` Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 11/25] ovpn: implement basic RX " Antonio Quartulli
2024-09-02 11:22 ` Sabrina Dubroca
2024-09-02 12:24 ` Antonio Quartulli
2024-09-06 19:18 ` Simon Horman
2024-09-09 8:37 ` Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 12/25] ovpn: implement packet processing Antonio Quartulli
2024-09-02 14:42 ` Sabrina Dubroca
2024-09-04 12:07 ` Antonio Quartulli
2024-09-04 15:01 ` Sabrina Dubroca
2024-09-06 13:19 ` Antonio Quartulli
2024-09-10 13:04 ` Sabrina Dubroca
2024-09-11 12:52 ` Antonio Quartulli
2024-09-11 13:30 ` Sabrina Dubroca
2024-09-12 8:33 ` Antonio Quartulli
2024-09-22 19:51 ` Sergey Ryazanov
2024-09-23 12:48 ` Antonio Quartulli
2024-09-06 19:29 ` Simon Horman
2024-09-09 8:38 ` Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 13/25] ovpn: store tunnel and transport statistics Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 14/25] ovpn: implement TCP transport Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 15/25] ovpn: implement multi-peer support Antonio Quartulli
2024-09-03 14:40 ` Sabrina Dubroca
2024-09-04 10:10 ` Sabrina Dubroca
2024-09-06 13:26 ` Antonio Quartulli
2024-09-05 8:02 ` Antonio Quartulli
2024-09-05 10:47 ` Sabrina Dubroca [this message]
2024-09-09 9:12 ` Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 16/25] ovpn: implement peer lookup logic Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 17/25] ovpn: implement keepalive mechanism Antonio Quartulli
2024-09-03 15:17 ` Sabrina Dubroca
2024-09-09 9:17 ` Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 18/25] ovpn: add support for updating local UDP endpoint Antonio Quartulli
2024-08-27 12:07 ` [PATCH net-next v6 19/25] ovpn: add support for peer floating Antonio Quartulli
2024-09-05 9:55 ` Sabrina Dubroca
2024-09-09 8:52 ` Antonio Quartulli
2024-08-27 12:08 ` [PATCH net-next v6 20/25] ovpn: implement peer add/dump/delete via netlink Antonio Quartulli
2024-08-27 12:08 ` [PATCH net-next v6 21/25] ovpn: implement key add/del/swap " Antonio Quartulli
2024-08-27 12:08 ` [PATCH net-next v6 22/25] ovpn: kill key and notify userspace in case of IV exhaustion Antonio Quartulli
2024-08-27 12:08 ` [PATCH net-next v6 23/25] ovpn: notify userspace when a peer is deleted Antonio Quartulli
2024-08-27 12:08 ` [PATCH net-next v6 24/25] ovpn: add basic ethtool support Antonio Quartulli
2024-08-27 12:08 ` [PATCH net-next v6 25/25] testing/selftest: add test tool and scripts for ovpn module Antonio Quartulli
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZtmMKDPZzsFdbTpq@hog \
--to=sd@queasysnail.net \
--cc=andrew@lunn.ch \
--cc=antonio@openvpn.net \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=ryazanov.s.a@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).