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