Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 2 | /* RxRPC virtual connection handler, common bits. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 3 | * |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 4 | * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 12 | #include <linux/net.h> |
| 13 | #include <linux/skbuff.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include "ar-internal.h" |
| 15 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 16 | /* |
| 17 | * Time till a connection expires after last use (in seconds). |
| 18 | */ |
David Howells | f859ab6 | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 19 | unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60; |
| 20 | unsigned int __read_mostly rxrpc_closed_conn_expiry = 10; |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 21 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 22 | static void rxrpc_destroy_connection(struct rcu_head *); |
| 23 | |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 24 | static void rxrpc_connection_timer(struct timer_list *timer) |
| 25 | { |
| 26 | struct rxrpc_connection *conn = |
| 27 | container_of(timer, struct rxrpc_connection, timer); |
| 28 | |
| 29 | rxrpc_queue_conn(conn); |
| 30 | } |
| 31 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 32 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 33 | * allocate a new connection |
| 34 | */ |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 35 | struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 36 | { |
| 37 | struct rxrpc_connection *conn; |
| 38 | |
| 39 | _enter(""); |
| 40 | |
| 41 | conn = kzalloc(sizeof(struct rxrpc_connection), gfp); |
| 42 | if (conn) { |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 43 | INIT_LIST_HEAD(&conn->cache_link); |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 44 | timer_setup(&conn->timer, &rxrpc_connection_timer, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 45 | INIT_WORK(&conn->processor, &rxrpc_process_connection); |
David Howells | 4d028b2 | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 46 | INIT_LIST_HEAD(&conn->proc_link); |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 47 | INIT_LIST_HEAD(&conn->link); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 48 | skb_queue_head_init(&conn->rx_queue); |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 49 | conn->security = &rxrpc_no_security; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 50 | spin_lock_init(&conn->state_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 51 | conn->debug_id = atomic_inc_return(&rxrpc_debug_id); |
David Howells | f51b448 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 52 | conn->idle_timestamp = jiffies; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Adrian Bunk | 16c61ad | 2007-06-15 15:15:43 -0700 | [diff] [blame] | 55 | _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 56 | return conn; |
| 57 | } |
| 58 | |
| 59 | /* |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 60 | * Look up a connection in the cache by protocol parameters. |
| 61 | * |
| 62 | * If successful, a pointer to the connection is returned, but no ref is taken. |
| 63 | * NULL is returned if there is no match. |
| 64 | * |
David Howells | 0099dc5 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 65 | * When searching for a service call, if we find a peer but no connection, we |
| 66 | * return that through *_peer in case we need to create a new service call. |
| 67 | * |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 68 | * The caller must be holding the RCU read lock. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 69 | */ |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 70 | struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *local, |
David Howells | 0099dc5 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 71 | struct sk_buff *skb, |
| 72 | struct rxrpc_peer **_peer) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 73 | { |
| 74 | struct rxrpc_connection *conn; |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 75 | struct rxrpc_conn_proto k; |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 76 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 77 | struct sockaddr_rxrpc srx; |
| 78 | struct rxrpc_peer *peer; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 79 | |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 80 | _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 81 | |
David Howells | 5a790b7 | 2018-10-04 09:32:28 +0100 | [diff] [blame] | 82 | if (rxrpc_extract_addr_from_skb(&srx, skb) < 0) |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 83 | goto not_found; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 84 | |
David Howells | 46894a1 | 2018-10-04 09:32:28 +0100 | [diff] [blame] | 85 | if (srx.transport.family != local->srx.transport.family && |
| 86 | (srx.transport.family == AF_INET && |
| 87 | local->srx.transport.family != AF_INET6)) { |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 88 | pr_warn_ratelimited("AF_RXRPC: Protocol mismatch %u not %u\n", |
| 89 | srx.transport.family, |
| 90 | local->srx.transport.family); |
| 91 | goto not_found; |
| 92 | } |
| 93 | |
| 94 | k.epoch = sp->hdr.epoch; |
| 95 | k.cid = sp->hdr.cid & RXRPC_CIDMASK; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 96 | |
David Howells | dc71db3 | 2018-09-27 15:13:08 +0100 | [diff] [blame] | 97 | if (rxrpc_to_server(sp)) { |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 98 | /* We need to look up service connections by the full protocol |
| 99 | * parameter set. We look up the peer first as an intermediate |
| 100 | * step and then the connection from the peer's tree. |
| 101 | */ |
| 102 | peer = rxrpc_lookup_peer_rcu(local, &srx); |
| 103 | if (!peer) |
| 104 | goto not_found; |
David Howells | 0099dc5 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 105 | *_peer = peer; |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 106 | conn = rxrpc_find_service_conn_rcu(peer, skb); |
| 107 | if (!conn || atomic_read(&conn->usage) == 0) |
| 108 | goto not_found; |
| 109 | _leave(" = %p", conn); |
| 110 | return conn; |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 111 | } else { |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 112 | /* Look up client connections by connection ID alone as their |
| 113 | * IDs are unique for this machine. |
| 114 | */ |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 115 | conn = idr_find(&rxrpc_client_conn_ids, |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 116 | sp->hdr.cid >> RXRPC_CIDSHIFT); |
| 117 | if (!conn || atomic_read(&conn->usage) == 0) { |
| 118 | _debug("no conn"); |
| 119 | goto not_found; |
| 120 | } |
| 121 | |
| 122 | if (conn->proto.epoch != k.epoch || |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 123 | conn->params.local != local) |
| 124 | goto not_found; |
| 125 | |
| 126 | peer = conn->params.peer; |
| 127 | switch (srx.transport.family) { |
| 128 | case AF_INET: |
| 129 | if (peer->srx.transport.sin.sin_port != |
| 130 | srx.transport.sin.sin_port || |
| 131 | peer->srx.transport.sin.sin_addr.s_addr != |
| 132 | srx.transport.sin.sin_addr.s_addr) |
| 133 | goto not_found; |
| 134 | break; |
David Howells | d191274 | 2016-09-17 07:26:01 +0100 | [diff] [blame] | 135 | #ifdef CONFIG_AF_RXRPC_IPV6 |
David Howells | 75b54cb | 2016-09-13 08:49:05 +0100 | [diff] [blame] | 136 | case AF_INET6: |
| 137 | if (peer->srx.transport.sin6.sin6_port != |
| 138 | srx.transport.sin6.sin6_port || |
| 139 | memcmp(&peer->srx.transport.sin6.sin6_addr, |
| 140 | &srx.transport.sin6.sin6_addr, |
| 141 | sizeof(struct in6_addr)) != 0) |
| 142 | goto not_found; |
| 143 | break; |
David Howells | d191274 | 2016-09-17 07:26:01 +0100 | [diff] [blame] | 144 | #endif |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 145 | default: |
| 146 | BUG(); |
| 147 | } |
| 148 | |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 149 | _leave(" = %p", conn); |
| 150 | return conn; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 151 | } |
| 152 | |
David Howells | 1291e9d1 | 2016-06-30 12:02:53 +0100 | [diff] [blame] | 153 | not_found: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 154 | _leave(" = NULL"); |
| 155 | return NULL; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 159 | * Disconnect a call and clear any channel it occupies when that call |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 160 | * terminates. The caller must hold the channel_lock and must release the |
| 161 | * call's ref on the connection. |
| 162 | */ |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 163 | void __rxrpc_disconnect_call(struct rxrpc_connection *conn, |
| 164 | struct rxrpc_call *call) |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 165 | { |
David Howells | 01a90a4 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 166 | struct rxrpc_channel *chan = |
| 167 | &conn->channels[call->cid & RXRPC_CHANNELMASK]; |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 168 | |
David Howells | 01a90a4 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 169 | _enter("%d,%x", conn->debug_id, call->cid); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 170 | |
| 171 | if (rcu_access_pointer(chan->call) == call) { |
| 172 | /* Save the result of the call so that we can repeat it if necessary |
| 173 | * through the channel, whilst disposing of the actual call record. |
| 174 | */ |
David Howells | b1d9f7fd | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 175 | trace_rxrpc_disconnect_call(call); |
David Howells | 17e9e23 | 2018-02-06 16:44:12 +0000 | [diff] [blame] | 176 | switch (call->completion) { |
| 177 | case RXRPC_CALL_SUCCEEDED: |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 178 | chan->last_seq = call->rx_hard_ack; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 179 | chan->last_type = RXRPC_PACKET_TYPE_ACK; |
David Howells | 17e9e23 | 2018-02-06 16:44:12 +0000 | [diff] [blame] | 180 | break; |
| 181 | case RXRPC_CALL_LOCALLY_ABORTED: |
| 182 | chan->last_abort = call->abort_code; |
| 183 | chan->last_type = RXRPC_PACKET_TYPE_ABORT; |
| 184 | break; |
| 185 | default: |
| 186 | chan->last_abort = RX_USER_ABORT; |
| 187 | chan->last_type = RXRPC_PACKET_TYPE_ABORT; |
| 188 | break; |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 189 | } |
David Howells | 17e9e23 | 2018-02-06 16:44:12 +0000 | [diff] [blame] | 190 | |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 191 | /* Sync with rxrpc_conn_retransmit(). */ |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 192 | smp_wmb(); |
| 193 | chan->last_call = chan->call_id; |
| 194 | chan->call_id = chan->call_counter; |
| 195 | |
| 196 | rcu_assign_pointer(chan->call, NULL); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | _leave(""); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Disconnect a call and clear any channel it occupies when that call |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 204 | * terminates. |
| 205 | */ |
| 206 | void rxrpc_disconnect_call(struct rxrpc_call *call) |
| 207 | { |
| 208 | struct rxrpc_connection *conn = call->conn; |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 209 | |
David Howells | f7aec12 | 2017-06-14 17:56:50 +0100 | [diff] [blame] | 210 | call->peer->cong_cwnd = call->cong_cwnd; |
| 211 | |
David Howells | 65550098 | 2020-07-29 00:03:56 +0100 | [diff] [blame] | 212 | if (!hlist_unhashed(&call->error_link)) { |
| 213 | spin_lock_bh(&call->peer->lock); |
| 214 | hlist_del_rcu(&call->error_link); |
| 215 | spin_unlock_bh(&call->peer->lock); |
| 216 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 217 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 218 | if (rxrpc_is_client_call(call)) |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 219 | return rxrpc_disconnect_client_call(conn->bundle, call); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 220 | |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 221 | spin_lock(&conn->bundle->channel_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 222 | __rxrpc_disconnect_call(conn, call); |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 223 | spin_unlock(&conn->bundle->channel_lock); |
David Howells | e653cfe | 2016-04-04 14:00:38 +0100 | [diff] [blame] | 224 | |
David Howells | b39a934 | 2020-02-06 13:55:01 +0000 | [diff] [blame] | 225 | set_bit(RXRPC_CALL_DISCONNECTED, &call->flags); |
David Howells | f51b448 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 226 | conn->idle_timestamp = jiffies; |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 230 | * Kill off a connection. |
| 231 | */ |
| 232 | void rxrpc_kill_connection(struct rxrpc_connection *conn) |
| 233 | { |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 234 | struct rxrpc_net *rxnet = conn->params.local->rxnet; |
| 235 | |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 236 | ASSERT(!rcu_access_pointer(conn->channels[0].call) && |
| 237 | !rcu_access_pointer(conn->channels[1].call) && |
| 238 | !rcu_access_pointer(conn->channels[2].call) && |
| 239 | !rcu_access_pointer(conn->channels[3].call)); |
| 240 | ASSERT(list_empty(&conn->cache_link)); |
| 241 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 242 | write_lock(&rxnet->conn_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 243 | list_del_init(&conn->proc_link); |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 244 | write_unlock(&rxnet->conn_lock); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 245 | |
| 246 | /* Drain the Rx queue. Note that even though we've unpublished, an |
| 247 | * incoming packet could still be being added to our Rx queue, so we |
| 248 | * will need to drain it again in the RCU cleanup handler. |
| 249 | */ |
| 250 | rxrpc_purge_queue(&conn->rx_queue); |
| 251 | |
| 252 | /* Leave final destruction to RCU. The connection processor work item |
| 253 | * must carry a ref on the connection to prevent us getting here whilst |
| 254 | * it is queued or running. |
| 255 | */ |
| 256 | call_rcu(&conn->rcu, rxrpc_destroy_connection); |
| 257 | } |
| 258 | |
| 259 | /* |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 260 | * Queue a connection's work processor, getting a ref to pass to the work |
| 261 | * queue. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 262 | */ |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 263 | bool rxrpc_queue_conn(struct rxrpc_connection *conn) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 264 | { |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 265 | const void *here = __builtin_return_address(0); |
Mark Rutland | bfc18e3 | 2018-06-21 13:13:04 +0100 | [diff] [blame] | 266 | int n = atomic_fetch_add_unless(&conn->usage, 1, 0); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 267 | if (n == 0) |
| 268 | return false; |
| 269 | if (rxrpc_queue_work(&conn->processor)) |
David Howells | 4c1295d | 2019-10-07 10:58:29 +0100 | [diff] [blame] | 270 | trace_rxrpc_conn(conn->debug_id, rxrpc_conn_queued, n + 1, here); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 271 | else |
| 272 | rxrpc_put_connection(conn); |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Note the re-emergence of a connection. |
| 278 | */ |
| 279 | void rxrpc_see_connection(struct rxrpc_connection *conn) |
| 280 | { |
| 281 | const void *here = __builtin_return_address(0); |
| 282 | if (conn) { |
| 283 | int n = atomic_read(&conn->usage); |
| 284 | |
David Howells | 4c1295d | 2019-10-07 10:58:29 +0100 | [diff] [blame] | 285 | trace_rxrpc_conn(conn->debug_id, rxrpc_conn_seen, n, here); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Get a ref on a connection. |
| 291 | */ |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 292 | struct rxrpc_connection *rxrpc_get_connection(struct rxrpc_connection *conn) |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 293 | { |
| 294 | const void *here = __builtin_return_address(0); |
| 295 | int n = atomic_inc_return(&conn->usage); |
| 296 | |
David Howells | 4c1295d | 2019-10-07 10:58:29 +0100 | [diff] [blame] | 297 | trace_rxrpc_conn(conn->debug_id, rxrpc_conn_got, n, here); |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 298 | return conn; |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Try to get a ref on a connection. |
| 303 | */ |
| 304 | struct rxrpc_connection * |
| 305 | rxrpc_get_connection_maybe(struct rxrpc_connection *conn) |
| 306 | { |
| 307 | const void *here = __builtin_return_address(0); |
| 308 | |
| 309 | if (conn) { |
Mark Rutland | bfc18e3 | 2018-06-21 13:13:04 +0100 | [diff] [blame] | 310 | int n = atomic_fetch_add_unless(&conn->usage, 1, 0); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 311 | if (n > 0) |
David Howells | 4c1295d | 2019-10-07 10:58:29 +0100 | [diff] [blame] | 312 | trace_rxrpc_conn(conn->debug_id, rxrpc_conn_got, n + 1, here); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 313 | else |
| 314 | conn = NULL; |
| 315 | } |
| 316 | return conn; |
| 317 | } |
| 318 | |
| 319 | /* |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 320 | * Set the service connection reap timer. |
| 321 | */ |
| 322 | static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet, |
| 323 | unsigned long reap_at) |
| 324 | { |
| 325 | if (rxnet->live) |
| 326 | timer_reduce(&rxnet->service_conn_reap_timer, reap_at); |
| 327 | } |
| 328 | |
| 329 | /* |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 330 | * Release a service connection |
| 331 | */ |
| 332 | void rxrpc_put_service_conn(struct rxrpc_connection *conn) |
| 333 | { |
| 334 | const void *here = __builtin_return_address(0); |
David Howells | 4c1295d | 2019-10-07 10:58:29 +0100 | [diff] [blame] | 335 | unsigned int debug_id = conn->debug_id; |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 336 | int n; |
| 337 | |
| 338 | n = atomic_dec_return(&conn->usage); |
David Howells | 4c1295d | 2019-10-07 10:58:29 +0100 | [diff] [blame] | 339 | trace_rxrpc_conn(debug_id, rxrpc_conn_put_service, n, here); |
David Howells | 363deea | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 340 | ASSERTCMP(n, >=, 0); |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 341 | if (n == 1) |
| 342 | rxrpc_set_service_reap_timer(conn->params.local->rxnet, |
| 343 | jiffies + rxrpc_connection_expiry); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* |
| 347 | * destroy a virtual connection |
| 348 | */ |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 349 | static void rxrpc_destroy_connection(struct rcu_head *rcu) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 350 | { |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 351 | struct rxrpc_connection *conn = |
| 352 | container_of(rcu, struct rxrpc_connection, rcu); |
| 353 | |
| 354 | _enter("{%d,u=%d}", conn->debug_id, atomic_read(&conn->usage)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 355 | |
| 356 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); |
| 357 | |
| 358 | _net("DESTROY CONN %d", conn->debug_id); |
| 359 | |
David Howells | 3136ef4 | 2017-11-24 10:18:41 +0000 | [diff] [blame] | 360 | del_timer_sync(&conn->timer); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 361 | rxrpc_purge_queue(&conn->rx_queue); |
| 362 | |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 363 | conn->security->clear(conn); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 364 | key_put(conn->params.key); |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 365 | rxrpc_put_bundle(conn->bundle); |
David Howells | aa390bb | 2016-06-17 10:06:56 +0100 | [diff] [blame] | 366 | rxrpc_put_peer(conn->params.peer); |
David Howells | 31f5f9a16 | 2018-03-30 21:05:33 +0100 | [diff] [blame] | 367 | |
| 368 | if (atomic_dec_and_test(&conn->params.local->rxnet->nr_conns)) |
Linus Torvalds | 5bb053b | 2018-04-03 14:04:18 -0700 | [diff] [blame] | 369 | wake_up_var(&conn->params.local->rxnet->nr_conns); |
David Howells | aa390bb | 2016-06-17 10:06:56 +0100 | [diff] [blame] | 370 | rxrpc_put_local(conn->params.local); |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 371 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 372 | kfree(conn); |
| 373 | _leave(""); |
| 374 | } |
| 375 | |
| 376 | /* |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 377 | * reap dead service connections |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 378 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 379 | void rxrpc_service_connection_reaper(struct work_struct *work) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 380 | { |
| 381 | struct rxrpc_connection *conn, *_p; |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 382 | struct rxrpc_net *rxnet = |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 383 | container_of(work, struct rxrpc_net, service_conn_reaper); |
David Howells | f859ab6 | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 384 | unsigned long expire_at, earliest, idle_timestamp, now; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 385 | |
| 386 | LIST_HEAD(graveyard); |
| 387 | |
| 388 | _enter(""); |
| 389 | |
David Howells | f51b448 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 390 | now = jiffies; |
David Howells | f859ab6 | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 391 | earliest = now + MAX_JIFFY_OFFSET; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 392 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 393 | write_lock(&rxnet->conn_lock); |
| 394 | list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) { |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 395 | ASSERTCMP(atomic_read(&conn->usage), >, 0); |
| 396 | if (likely(atomic_read(&conn->usage) > 1)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 397 | continue; |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 398 | if (conn->state == RXRPC_CONN_SERVICE_PREALLOC) |
| 399 | continue; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 400 | |
David Howells | d12040b | 2019-08-29 14:12:11 +0100 | [diff] [blame] | 401 | if (rxnet->live && !conn->params.local->dead) { |
David Howells | f859ab6 | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 402 | idle_timestamp = READ_ONCE(conn->idle_timestamp); |
| 403 | expire_at = idle_timestamp + rxrpc_connection_expiry * HZ; |
| 404 | if (conn->params.local->service_closed) |
| 405 | expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ; |
David Howells | f51b448 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 406 | |
David Howells | f859ab6 | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 407 | _debug("reap CONN %d { u=%d,t=%ld }", |
| 408 | conn->debug_id, atomic_read(&conn->usage), |
| 409 | (long)expire_at - (long)now); |
| 410 | |
| 411 | if (time_before(now, expire_at)) { |
| 412 | if (time_before(expire_at, earliest)) |
| 413 | earliest = expire_at; |
| 414 | continue; |
| 415 | } |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 416 | } |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 417 | |
| 418 | /* The usage count sits at 1 whilst the object is unused on the |
| 419 | * list; we reduce that to 0 to make the object unavailable. |
| 420 | */ |
| 421 | if (atomic_cmpxchg(&conn->usage, 1, 0) != 1) |
| 422 | continue; |
David Howells | 4c1295d | 2019-10-07 10:58:29 +0100 | [diff] [blame] | 423 | trace_rxrpc_conn(conn->debug_id, rxrpc_conn_reap_service, 0, NULL); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 424 | |
| 425 | if (rxrpc_conn_is_client(conn)) |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 426 | BUG(); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame] | 427 | else |
| 428 | rxrpc_unpublish_service_conn(conn); |
| 429 | |
| 430 | list_move_tail(&conn->link, &graveyard); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 431 | } |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 432 | write_unlock(&rxnet->conn_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 433 | |
David Howells | f859ab6 | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 434 | if (earliest != now + MAX_JIFFY_OFFSET) { |
| 435 | _debug("reschedule reaper %ld", (long)earliest - (long)now); |
David Howells | f51b448 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 436 | ASSERT(time_after(earliest, now)); |
David Howells | 3d7682a | 2017-11-29 14:25:50 +0000 | [diff] [blame] | 437 | rxrpc_set_service_reap_timer(rxnet, earliest); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 438 | } |
| 439 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 440 | while (!list_empty(&graveyard)) { |
| 441 | conn = list_entry(graveyard.next, struct rxrpc_connection, |
| 442 | link); |
| 443 | list_del_init(&conn->link); |
| 444 | |
| 445 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 446 | rxrpc_kill_connection(conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | _leave(""); |
| 450 | } |
| 451 | |
| 452 | /* |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 453 | * preemptively destroy all the service connection records rather than |
| 454 | * waiting for them to time out |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 455 | */ |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 456 | void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 457 | { |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 458 | struct rxrpc_connection *conn, *_p; |
| 459 | bool leak = false; |
| 460 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 461 | _enter(""); |
| 462 | |
David Howells | 31f5f9a16 | 2018-03-30 21:05:33 +0100 | [diff] [blame] | 463 | atomic_dec(&rxnet->nr_conns); |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 464 | rxrpc_destroy_all_client_connections(rxnet); |
David Howells | 45025bc | 2016-08-24 07:30:52 +0100 | [diff] [blame] | 465 | |
David Howells | 3d18cbb | 2017-11-24 10:18:42 +0000 | [diff] [blame] | 466 | del_timer_sync(&rxnet->service_conn_reap_timer); |
| 467 | rxrpc_queue_work(&rxnet->service_conn_reaper); |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 468 | flush_workqueue(rxrpc_workqueue); |
| 469 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 470 | write_lock(&rxnet->conn_lock); |
| 471 | list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) { |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 472 | pr_err("AF_RXRPC: Leaked conn %p {%d}\n", |
| 473 | conn, atomic_read(&conn->usage)); |
| 474 | leak = true; |
| 475 | } |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 476 | write_unlock(&rxnet->conn_lock); |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 477 | BUG_ON(leak); |
| 478 | |
David Howells | 2baec2c | 2017-05-24 17:02:32 +0100 | [diff] [blame] | 479 | ASSERT(list_empty(&rxnet->conn_proc_list)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 480 | |
David Howells | 31f5f9a16 | 2018-03-30 21:05:33 +0100 | [diff] [blame] | 481 | /* We need to wait for the connections to be destroyed by RCU as they |
| 482 | * pin things that we still need to get rid of. |
| 483 | */ |
Linus Torvalds | 5bb053b | 2018-04-03 14:04:18 -0700 | [diff] [blame] | 484 | wait_var_event(&rxnet->nr_conns, !atomic_read(&rxnet->nr_conns)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 485 | _leave(""); |
| 486 | } |