Commit 413c2d04 authored by Julian Anastasov's avatar Julian Anastasov Committed by Pablo Neira Ayuso

ipvs: convert dests to rcu

In previous commits the schedulers started to access
svc->destinations with _rcu list traversal primitives
because the IP_VS_WAIT_WHILE macro still plays the role of
grace period. Now it is time to finish the updating part,
i.e. adding and deleting of dests with _rcu suffix before
removing the IP_VS_WAIT_WHILE in next commit.

We use the same rule for conns as for the
schedulers: dests can be searched in RCU read-side critical
section where ip_vs_dest_hold can be called by ip_vs_bind_dest.

Some things are not perfect, for example, calling
functions like ip_vs_lookup_dest from updating code under
RCU, just because we use some function both from reader
and from updater.
Signed-off-by: default avatarJulian Anastasov <ja@ssi.bg>
Signed-off-by: default avatarSimon Horman <horms@verge.net.au>
parent ba3a3ce1
...@@ -1434,7 +1434,7 @@ extern struct ip_vs_dest * ...@@ -1434,7 +1434,7 @@ extern struct ip_vs_dest *
ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr, ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr,
__be16 dport, const union nf_inet_addr *vaddr, __be16 vport, __be16 dport, const union nf_inet_addr *vaddr, __be16 vport,
__u16 protocol, __u32 fwmark, __u32 flags); __u16 protocol, __u32 fwmark, __u32 flags);
extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp); extern void ip_vs_try_bind_dest(struct ip_vs_conn *cp);
static inline void ip_vs_dest_hold(struct ip_vs_dest *dest) static inline void ip_vs_dest_hold(struct ip_vs_dest *dest)
{ {
......
...@@ -611,10 +611,11 @@ ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest) ...@@ -611,10 +611,11 @@ ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
* Check if there is a destination for the connection, if so * Check if there is a destination for the connection, if so
* bind the connection to the destination. * bind the connection to the destination.
*/ */
struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp) void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
{ {
struct ip_vs_dest *dest; struct ip_vs_dest *dest;
rcu_read_lock();
dest = ip_vs_find_dest(ip_vs_conn_net(cp), cp->af, &cp->daddr, dest = ip_vs_find_dest(ip_vs_conn_net(cp), cp->af, &cp->daddr,
cp->dport, &cp->vaddr, cp->vport, cp->dport, &cp->vaddr, cp->vport,
cp->protocol, cp->fwmark, cp->flags); cp->protocol, cp->fwmark, cp->flags);
...@@ -624,7 +625,8 @@ struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp) ...@@ -624,7 +625,8 @@ struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
spin_lock(&cp->lock); spin_lock(&cp->lock);
if (cp->dest) { if (cp->dest) {
spin_unlock(&cp->lock); spin_unlock(&cp->lock);
return dest; rcu_read_unlock();
return;
} }
/* Applications work depending on the forwarding method /* Applications work depending on the forwarding method
...@@ -648,7 +650,7 @@ struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp) ...@@ -648,7 +650,7 @@ struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
if (pd && atomic_read(&pd->appcnt)) if (pd && atomic_read(&pd->appcnt))
ip_vs_bind_app(cp, pd->pp); ip_vs_bind_app(cp, pd->pp);
} }
return dest; rcu_read_unlock();
} }
......
...@@ -565,8 +565,8 @@ bool ip_vs_has_real_service(struct net *net, int af, __u16 protocol, ...@@ -565,8 +565,8 @@ bool ip_vs_has_real_service(struct net *net, int af, __u16 protocol,
return false; return false;
} }
/* /* Lookup destination by {addr,port} in the given service
* Lookup destination by {addr,port} in the given service * Called under RCU lock.
*/ */
static struct ip_vs_dest * static struct ip_vs_dest *
ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr, ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
...@@ -577,7 +577,7 @@ ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr, ...@@ -577,7 +577,7 @@ ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
/* /*
* Find the destination for the given service * Find the destination for the given service
*/ */
list_for_each_entry(dest, &svc->destinations, n_list) { list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
if ((dest->af == svc->af) if ((dest->af == svc->af)
&& ip_vs_addr_equal(svc->af, &dest->addr, daddr) && ip_vs_addr_equal(svc->af, &dest->addr, daddr)
&& (dest->port == dport)) { && (dest->port == dport)) {
...@@ -591,10 +591,11 @@ ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr, ...@@ -591,10 +591,11 @@ ip_vs_lookup_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
/* /*
* Find destination by {daddr,dport,vaddr,protocol} * Find destination by {daddr,dport,vaddr,protocol}
* Cretaed to be used in ip_vs_process_message() in * Created to be used in ip_vs_process_message() in
* the backup synchronization daemon. It finds the * the backup synchronization daemon. It finds the
* destination to be bound to the received connection * destination to be bound to the received connection
* on the backup. * on the backup.
* Called under RCU lock, no refcnt is returned.
*/ */
struct ip_vs_dest *ip_vs_find_dest(struct net *net, int af, struct ip_vs_dest *ip_vs_find_dest(struct net *net, int af,
const union nf_inet_addr *daddr, const union nf_inet_addr *daddr,
...@@ -615,8 +616,6 @@ struct ip_vs_dest *ip_vs_find_dest(struct net *net, int af, ...@@ -615,8 +616,6 @@ struct ip_vs_dest *ip_vs_find_dest(struct net *net, int af,
dest = ip_vs_lookup_dest(svc, daddr, port); dest = ip_vs_lookup_dest(svc, daddr, port);
if (!dest) if (!dest)
dest = ip_vs_lookup_dest(svc, daddr, port ^ dport); dest = ip_vs_lookup_dest(svc, daddr, port ^ dport);
if (dest)
ip_vs_dest_hold(dest);
ip_vs_service_put(svc); ip_vs_service_put(svc);
return dest; return dest;
} }
...@@ -826,7 +825,7 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest, ...@@ -826,7 +825,7 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0); IP_VS_WAIT_WHILE(atomic_read(&svc->usecnt) > 0);
if (add) { if (add) {
list_add(&dest->n_list, &svc->destinations); list_add_rcu(&dest->n_list, &svc->destinations);
svc->num_dests++; svc->num_dests++;
if (svc->scheduler->add_dest) if (svc->scheduler->add_dest)
svc->scheduler->add_dest(svc, dest); svc->scheduler->add_dest(svc, dest);
...@@ -933,10 +932,10 @@ ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) ...@@ -933,10 +932,10 @@ ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
ip_vs_addr_copy(svc->af, &daddr, &udest->addr); ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
/* /* We use function that requires RCU lock */
* Check if the dest already exists in the list rcu_read_lock();
*/
dest = ip_vs_lookup_dest(svc, &daddr, dport); dest = ip_vs_lookup_dest(svc, &daddr, dport);
rcu_read_unlock();
if (dest != NULL) { if (dest != NULL) {
IP_VS_DBG(1, "%s(): dest already exists\n", __func__); IP_VS_DBG(1, "%s(): dest already exists\n", __func__);
...@@ -997,10 +996,10 @@ ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) ...@@ -997,10 +996,10 @@ ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
ip_vs_addr_copy(svc->af, &daddr, &udest->addr); ip_vs_addr_copy(svc->af, &daddr, &udest->addr);
/* /* We use function that requires RCU lock */
* Lookup the destination list rcu_read_lock();
*/
dest = ip_vs_lookup_dest(svc, &daddr, dport); dest = ip_vs_lookup_dest(svc, &daddr, dport);
rcu_read_unlock();
if (dest == NULL) { if (dest == NULL) {
IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__); IP_VS_DBG(1, "%s(): dest doesn't exist\n", __func__);
...@@ -1069,7 +1068,7 @@ static void __ip_vs_unlink_dest(struct ip_vs_service *svc, ...@@ -1069,7 +1068,7 @@ static void __ip_vs_unlink_dest(struct ip_vs_service *svc,
/* /*
* Remove it from the d-linked destination list. * Remove it from the d-linked destination list.
*/ */
list_del(&dest->n_list); list_del_rcu(&dest->n_list);
svc->num_dests--; svc->num_dests--;
if (svcupd && svc->scheduler->del_dest) if (svcupd && svc->scheduler->del_dest)
...@@ -1094,7 +1093,10 @@ ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) ...@@ -1094,7 +1093,10 @@ ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
EnterFunction(2); EnterFunction(2);
/* We use function that requires RCU lock */
rcu_read_lock();
dest = ip_vs_lookup_dest(svc, &udest->addr, dport); dest = ip_vs_lookup_dest(svc, &udest->addr, dport);
rcu_read_unlock();
if (dest == NULL) { if (dest == NULL) {
IP_VS_DBG(1, "%s(): destination not found!\n", __func__); IP_VS_DBG(1, "%s(): destination not found!\n", __func__);
...@@ -2104,7 +2106,7 @@ static int ip_vs_info_seq_show(struct seq_file *seq, void *v) ...@@ -2104,7 +2106,7 @@ static int ip_vs_info_seq_show(struct seq_file *seq, void *v)
else else
seq_putc(seq, '\n'); seq_putc(seq, '\n');
list_for_each_entry(dest, &svc->destinations, n_list) { list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
#ifdef CONFIG_IP_VS_IPV6 #ifdef CONFIG_IP_VS_IPV6
if (dest->af == AF_INET6) if (dest->af == AF_INET6)
seq_printf(seq, seq_printf(seq,
......
...@@ -858,23 +858,20 @@ static void ip_vs_proc_conn(struct net *net, struct ip_vs_conn_param *param, ...@@ -858,23 +858,20 @@ static void ip_vs_proc_conn(struct net *net, struct ip_vs_conn_param *param,
flags |= cp->flags & ~IP_VS_CONN_F_BACKUP_UPD_MASK; flags |= cp->flags & ~IP_VS_CONN_F_BACKUP_UPD_MASK;
cp->flags = flags; cp->flags = flags;
spin_unlock(&cp->lock); spin_unlock(&cp->lock);
if (!dest) { if (!dest)
dest = ip_vs_try_bind_dest(cp); ip_vs_try_bind_dest(cp);
if (dest)
ip_vs_dest_put(dest);
}
} else { } else {
/* /*
* Find the appropriate destination for the connection. * Find the appropriate destination for the connection.
* If it is not found the connection will remain unbound * If it is not found the connection will remain unbound
* but still handled. * but still handled.
*/ */
rcu_read_lock();
dest = ip_vs_find_dest(net, type, daddr, dport, param->vaddr, dest = ip_vs_find_dest(net, type, daddr, dport, param->vaddr,
param->vport, protocol, fwmark, flags); param->vport, protocol, fwmark, flags);
cp = ip_vs_conn_new(param, daddr, dport, flags, dest, fwmark); cp = ip_vs_conn_new(param, daddr, dport, flags, dest, fwmark);
if (dest) rcu_read_unlock();
ip_vs_dest_put(dest);
if (!cp) { if (!cp) {
if (param->pe_data) if (param->pe_data)
kfree(param->pe_data); kfree(param->pe_data);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment