Commit f859ab61 authored by David Howells's avatar David Howells

rxrpc: Fix service endpoint expiry

RxRPC service endpoints expire like they're supposed to by the following
means:

 (1) Mark dead rxrpc_net structs (with ->live) rather than twiddling the
     global service conn timeout, otherwise the first rxrpc_net struct to
     die will cause connections on all others to expire immediately from
     then on.

 (2) Mark local service endpoints for which the socket has been closed
     (->service_closed) so that the expiration timeout can be much
     shortened for service and client connections going through that
     endpoint.

 (3) rxrpc_put_service_conn() needs to schedule the reaper when the usage
     count reaches 1, not 0, as idle conns have a 1 count.

 (4) The accumulator for the earliest time we might want to schedule for
     should be initialised to jiffies + MAX_JIFFY_OFFSET, not ULONG_MAX as
     the comparison functions use signed arithmetic.

 (5) Simplify the expiration handling, adding the expiration value to the
     idle timestamp each time rather than keeping track of the time in the
     past before which the idle timestamp must go to be expired.  This is
     much easier to read.

 (6) Ignore the timeouts if the net namespace is dead.

 (7) Restart the service reaper work item rather the client reaper.
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
parent 415f44e4
...@@ -49,6 +49,7 @@ enum rxrpc_conn_trace { ...@@ -49,6 +49,7 @@ enum rxrpc_conn_trace {
rxrpc_conn_put_client, rxrpc_conn_put_client,
rxrpc_conn_put_service, rxrpc_conn_put_service,
rxrpc_conn_queued, rxrpc_conn_queued,
rxrpc_conn_reap_service,
rxrpc_conn_seen, rxrpc_conn_seen,
}; };
...@@ -221,6 +222,7 @@ enum rxrpc_congest_change { ...@@ -221,6 +222,7 @@ enum rxrpc_congest_change {
EM(rxrpc_conn_put_client, "PTc") \ EM(rxrpc_conn_put_client, "PTc") \
EM(rxrpc_conn_put_service, "PTs") \ EM(rxrpc_conn_put_service, "PTs") \
EM(rxrpc_conn_queued, "QUE") \ EM(rxrpc_conn_queued, "QUE") \
EM(rxrpc_conn_reap_service, "RPs") \
E_(rxrpc_conn_seen, "SEE") E_(rxrpc_conn_seen, "SEE")
#define rxrpc_client_traces \ #define rxrpc_client_traces \
......
...@@ -867,6 +867,19 @@ static int rxrpc_release_sock(struct sock *sk) ...@@ -867,6 +867,19 @@ static int rxrpc_release_sock(struct sock *sk)
sock_orphan(sk); sock_orphan(sk);
sk->sk_shutdown = SHUTDOWN_MASK; sk->sk_shutdown = SHUTDOWN_MASK;
/* We want to kill off all connections from a service socket
* as fast as possible because we can't share these; client
* sockets, on the other hand, can share an endpoint.
*/
switch (sk->sk_state) {
case RXRPC_SERVER_BOUND:
case RXRPC_SERVER_BOUND2:
case RXRPC_SERVER_LISTENING:
case RXRPC_SERVER_LISTEN_DISABLED:
rx->local->service_closed = true;
break;
}
spin_lock_bh(&sk->sk_receive_queue.lock); spin_lock_bh(&sk->sk_receive_queue.lock);
sk->sk_state = RXRPC_CLOSE; sk->sk_state = RXRPC_CLOSE;
spin_unlock_bh(&sk->sk_receive_queue.lock); spin_unlock_bh(&sk->sk_receive_queue.lock);
......
...@@ -84,6 +84,7 @@ struct rxrpc_net { ...@@ -84,6 +84,7 @@ struct rxrpc_net {
unsigned int nr_client_conns; unsigned int nr_client_conns;
unsigned int nr_active_client_conns; unsigned int nr_active_client_conns;
bool kill_all_client_conns; bool kill_all_client_conns;
bool live;
spinlock_t client_conn_cache_lock; /* Lock for ->*_client_conns */ spinlock_t client_conn_cache_lock; /* Lock for ->*_client_conns */
spinlock_t client_conn_discard_lock; /* Prevent multiple discarders */ spinlock_t client_conn_discard_lock; /* Prevent multiple discarders */
struct list_head waiting_client_conns; struct list_head waiting_client_conns;
...@@ -265,6 +266,7 @@ struct rxrpc_local { ...@@ -265,6 +266,7 @@ struct rxrpc_local {
rwlock_t services_lock; /* lock for services list */ rwlock_t services_lock; /* lock for services list */
int debug_id; /* debug ID for printks */ int debug_id; /* debug ID for printks */
bool dead; bool dead;
bool service_closed; /* Service socket closed */
struct sockaddr_rxrpc srx; /* local address */ struct sockaddr_rxrpc srx; /* local address */
}; };
...@@ -881,6 +883,7 @@ void rxrpc_process_connection(struct work_struct *); ...@@ -881,6 +883,7 @@ void rxrpc_process_connection(struct work_struct *);
* conn_object.c * conn_object.c
*/ */
extern unsigned int rxrpc_connection_expiry; extern unsigned int rxrpc_connection_expiry;
extern unsigned int rxrpc_closed_conn_expiry;
struct rxrpc_connection *rxrpc_alloc_connection(gfp_t); struct rxrpc_connection *rxrpc_alloc_connection(gfp_t);
struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *, struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *,
......
...@@ -1079,6 +1079,8 @@ void rxrpc_discard_expired_client_conns(struct work_struct *work) ...@@ -1079,6 +1079,8 @@ void rxrpc_discard_expired_client_conns(struct work_struct *work)
expiry = rxrpc_conn_idle_client_expiry; expiry = rxrpc_conn_idle_client_expiry;
if (nr_conns > rxrpc_reap_client_connections) if (nr_conns > rxrpc_reap_client_connections)
expiry = rxrpc_conn_idle_client_fast_expiry; expiry = rxrpc_conn_idle_client_fast_expiry;
if (conn->params.local->service_closed)
expiry = rxrpc_closed_conn_expiry * HZ;
conn_expires_at = conn->idle_timestamp + expiry; conn_expires_at = conn->idle_timestamp + expiry;
......
...@@ -20,7 +20,8 @@ ...@@ -20,7 +20,8 @@
/* /*
* Time till a connection expires after last use (in seconds). * Time till a connection expires after last use (in seconds).
*/ */
unsigned int rxrpc_connection_expiry = 10 * 60; unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60;
unsigned int __read_mostly rxrpc_closed_conn_expiry = 10;
static void rxrpc_destroy_connection(struct rcu_head *); static void rxrpc_destroy_connection(struct rcu_head *);
...@@ -321,7 +322,7 @@ void rxrpc_put_service_conn(struct rxrpc_connection *conn) ...@@ -321,7 +322,7 @@ void rxrpc_put_service_conn(struct rxrpc_connection *conn)
n = atomic_dec_return(&conn->usage); n = atomic_dec_return(&conn->usage);
trace_rxrpc_conn(conn, rxrpc_conn_put_service, n, here); trace_rxrpc_conn(conn, rxrpc_conn_put_service, n, here);
ASSERTCMP(n, >=, 0); ASSERTCMP(n, >=, 0);
if (n == 0) { if (n == 1) {
rxnet = conn->params.local->rxnet; rxnet = conn->params.local->rxnet;
rxrpc_queue_delayed_work(&rxnet->service_conn_reaper, 0); rxrpc_queue_delayed_work(&rxnet->service_conn_reaper, 0);
} }
...@@ -363,15 +364,14 @@ void rxrpc_service_connection_reaper(struct work_struct *work) ...@@ -363,15 +364,14 @@ void rxrpc_service_connection_reaper(struct work_struct *work)
struct rxrpc_net *rxnet = struct rxrpc_net *rxnet =
container_of(to_delayed_work(work), container_of(to_delayed_work(work),
struct rxrpc_net, service_conn_reaper); struct rxrpc_net, service_conn_reaper);
unsigned long reap_older_than, earliest, idle_timestamp, now; unsigned long expire_at, earliest, idle_timestamp, now;
LIST_HEAD(graveyard); LIST_HEAD(graveyard);
_enter(""); _enter("");
now = jiffies; now = jiffies;
reap_older_than = now - rxrpc_connection_expiry * HZ; earliest = now + MAX_JIFFY_OFFSET;
earliest = ULONG_MAX;
write_lock(&rxnet->conn_lock); write_lock(&rxnet->conn_lock);
list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) { list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
...@@ -381,15 +381,21 @@ void rxrpc_service_connection_reaper(struct work_struct *work) ...@@ -381,15 +381,21 @@ void rxrpc_service_connection_reaper(struct work_struct *work)
if (conn->state == RXRPC_CONN_SERVICE_PREALLOC) if (conn->state == RXRPC_CONN_SERVICE_PREALLOC)
continue; continue;
idle_timestamp = READ_ONCE(conn->idle_timestamp); if (rxnet->live) {
_debug("reap CONN %d { u=%d,t=%ld }", idle_timestamp = READ_ONCE(conn->idle_timestamp);
conn->debug_id, atomic_read(&conn->usage), expire_at = idle_timestamp + rxrpc_connection_expiry * HZ;
(long)reap_older_than - (long)idle_timestamp); if (conn->params.local->service_closed)
expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ;
if (time_after(idle_timestamp, reap_older_than)) {
if (time_before(idle_timestamp, earliest)) _debug("reap CONN %d { u=%d,t=%ld }",
earliest = idle_timestamp; conn->debug_id, atomic_read(&conn->usage),
continue; (long)expire_at - (long)now);
if (time_before(now, expire_at)) {
if (time_before(expire_at, earliest))
earliest = expire_at;
continue;
}
} }
/* The usage count sits at 1 whilst the object is unused on the /* The usage count sits at 1 whilst the object is unused on the
...@@ -397,6 +403,7 @@ void rxrpc_service_connection_reaper(struct work_struct *work) ...@@ -397,6 +403,7 @@ void rxrpc_service_connection_reaper(struct work_struct *work)
*/ */
if (atomic_cmpxchg(&conn->usage, 1, 0) != 1) if (atomic_cmpxchg(&conn->usage, 1, 0) != 1)
continue; continue;
trace_rxrpc_conn(conn, rxrpc_conn_reap_service, 0, 0);
if (rxrpc_conn_is_client(conn)) if (rxrpc_conn_is_client(conn))
BUG(); BUG();
...@@ -407,10 +414,10 @@ void rxrpc_service_connection_reaper(struct work_struct *work) ...@@ -407,10 +414,10 @@ void rxrpc_service_connection_reaper(struct work_struct *work)
} }
write_unlock(&rxnet->conn_lock); write_unlock(&rxnet->conn_lock);
if (earliest != ULONG_MAX) { if (earliest != now + MAX_JIFFY_OFFSET) {
_debug("reschedule reaper %ld", (long) earliest - now); _debug("reschedule reaper %ld", (long)earliest - (long)now);
ASSERT(time_after(earliest, now)); ASSERT(time_after(earliest, now));
rxrpc_queue_delayed_work(&rxnet->client_conn_reaper, rxrpc_queue_delayed_work(&rxnet->service_conn_reaper,
earliest - now); earliest - now);
} }
...@@ -439,7 +446,6 @@ void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet) ...@@ -439,7 +446,6 @@ void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet)
rxrpc_destroy_all_client_connections(rxnet); rxrpc_destroy_all_client_connections(rxnet);
rxrpc_connection_expiry = 0;
cancel_delayed_work(&rxnet->client_conn_reaper); cancel_delayed_work(&rxnet->client_conn_reaper);
rxrpc_queue_delayed_work(&rxnet->client_conn_reaper, 0); rxrpc_queue_delayed_work(&rxnet->client_conn_reaper, 0);
flush_workqueue(rxrpc_workqueue); flush_workqueue(rxrpc_workqueue);
......
...@@ -22,6 +22,7 @@ static __net_init int rxrpc_init_net(struct net *net) ...@@ -22,6 +22,7 @@ static __net_init int rxrpc_init_net(struct net *net)
struct rxrpc_net *rxnet = rxrpc_net(net); struct rxrpc_net *rxnet = rxrpc_net(net);
int ret; int ret;
rxnet->live = true;
get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch)); get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch));
rxnet->epoch |= RXRPC_RANDOM_EPOCH; rxnet->epoch |= RXRPC_RANDOM_EPOCH;
...@@ -60,6 +61,7 @@ static __net_init int rxrpc_init_net(struct net *net) ...@@ -60,6 +61,7 @@ static __net_init int rxrpc_init_net(struct net *net)
return 0; return 0;
err_proc: err_proc:
rxnet->live = false;
return ret; return ret;
} }
...@@ -70,6 +72,7 @@ static __net_exit void rxrpc_exit_net(struct net *net) ...@@ -70,6 +72,7 @@ static __net_exit void rxrpc_exit_net(struct net *net)
{ {
struct rxrpc_net *rxnet = rxrpc_net(net); struct rxrpc_net *rxnet = rxrpc_net(net);
rxnet->live = false;
rxrpc_destroy_all_calls(rxnet); rxrpc_destroy_all_calls(rxnet);
rxrpc_destroy_all_connections(rxnet); rxrpc_destroy_all_connections(rxnet);
rxrpc_destroy_all_locals(rxnet); rxrpc_destroy_all_locals(rxnet);
......
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