Commit | Line | Data |
---|---|---|
17926a79 DH |
1 | /* RxRPC individual remote procedure call handling |
2 | * | |
3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. | |
4 | * Written by David Howells (dhowells@redhat.com) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
9b6d5398 JP |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
5a0e3ad6 | 14 | #include <linux/slab.h> |
17926a79 DH |
15 | #include <linux/module.h> |
16 | #include <linux/circ_buf.h> | |
7727640c | 17 | #include <linux/spinlock_types.h> |
17926a79 DH |
18 | #include <net/sock.h> |
19 | #include <net/af_rxrpc.h> | |
20 | #include "ar-internal.h" | |
21 | ||
5b8848d1 | 22 | const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = { |
f5c17aae | 23 | [RXRPC_CALL_UNINITIALISED] = "Uninit ", |
999b69f8 | 24 | [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn", |
1f8481d1 DH |
25 | [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq", |
26 | [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl", | |
27 | [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl", | |
00e90712 | 28 | [RXRPC_CALL_SERVER_PREALLOC] = "SvPrealc", |
1f8481d1 DH |
29 | [RXRPC_CALL_SERVER_SECURING] = "SvSecure", |
30 | [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept", | |
31 | [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq", | |
32 | [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq", | |
33 | [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl", | |
34 | [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK", | |
35 | [RXRPC_CALL_COMPLETE] = "Complete", | |
f5c17aae DH |
36 | }; |
37 | ||
38 | const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = { | |
39 | [RXRPC_CALL_SUCCEEDED] = "Complete", | |
1f8481d1 DH |
40 | [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort", |
41 | [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort", | |
f5c17aae | 42 | [RXRPC_CALL_LOCAL_ERROR] = "LocError", |
1f8481d1 | 43 | [RXRPC_CALL_NETWORK_ERROR] = "NetError", |
1f8481d1 DH |
44 | }; |
45 | ||
17926a79 | 46 | struct kmem_cache *rxrpc_call_jar; |
17926a79 | 47 | |
248f219c DH |
48 | static void rxrpc_call_timer_expired(unsigned long _call) |
49 | { | |
50 | struct rxrpc_call *call = (struct rxrpc_call *)_call; | |
51 | ||
52 | _enter("%d", call->debug_id); | |
53 | ||
405dea1d DH |
54 | if (call->state < RXRPC_CALL_COMPLETE) |
55 | rxrpc_set_timer(call, rxrpc_timer_expired, ktime_get_real()); | |
248f219c | 56 | } |
17926a79 | 57 | |
b89372f2 DH |
58 | static struct lock_class_key rxrpc_call_user_mutex_lock_class_key; |
59 | ||
2341e077 DH |
60 | /* |
61 | * find an extant server call | |
62 | * - called in process context with IRQs enabled | |
63 | */ | |
64 | struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx, | |
65 | unsigned long user_call_ID) | |
66 | { | |
67 | struct rxrpc_call *call; | |
68 | struct rb_node *p; | |
69 | ||
70 | _enter("%p,%lx", rx, user_call_ID); | |
71 | ||
72 | read_lock(&rx->call_lock); | |
73 | ||
74 | p = rx->calls.rb_node; | |
75 | while (p) { | |
76 | call = rb_entry(p, struct rxrpc_call, sock_node); | |
77 | ||
78 | if (user_call_ID < call->user_call_ID) | |
79 | p = p->rb_left; | |
80 | else if (user_call_ID > call->user_call_ID) | |
81 | p = p->rb_right; | |
82 | else | |
83 | goto found_extant_call; | |
84 | } | |
85 | ||
86 | read_unlock(&rx->call_lock); | |
87 | _leave(" = NULL"); | |
88 | return NULL; | |
89 | ||
90 | found_extant_call: | |
fff72429 | 91 | rxrpc_get_call(call, rxrpc_call_got); |
2341e077 DH |
92 | read_unlock(&rx->call_lock); |
93 | _leave(" = %p [%d]", call, atomic_read(&call->usage)); | |
94 | return call; | |
95 | } | |
96 | ||
17926a79 DH |
97 | /* |
98 | * allocate a new call | |
99 | */ | |
b89372f2 | 100 | struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp) |
17926a79 DH |
101 | { |
102 | struct rxrpc_call *call; | |
103 | ||
104 | call = kmem_cache_zalloc(rxrpc_call_jar, gfp); | |
105 | if (!call) | |
106 | return NULL; | |
107 | ||
248f219c DH |
108 | call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE, |
109 | sizeof(struct sk_buff *), | |
17926a79 | 110 | gfp); |
248f219c DH |
111 | if (!call->rxtx_buffer) |
112 | goto nomem; | |
17926a79 | 113 | |
248f219c DH |
114 | call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp); |
115 | if (!call->rxtx_annotations) | |
116 | goto nomem_2; | |
117 | ||
540b1c48 | 118 | mutex_init(&call->user_mutex); |
b89372f2 DH |
119 | |
120 | /* Prevent lockdep reporting a deadlock false positive between the afs | |
121 | * filesystem and sys_sendmsg() via the mmap sem. | |
122 | */ | |
123 | if (rx->sk.sk_kern_sock) | |
124 | lockdep_set_class(&call->user_mutex, | |
125 | &rxrpc_call_user_mutex_lock_class_key); | |
126 | ||
248f219c DH |
127 | setup_timer(&call->timer, rxrpc_call_timer_expired, |
128 | (unsigned long)call); | |
17926a79 | 129 | INIT_WORK(&call->processor, &rxrpc_process_call); |
999b69f8 | 130 | INIT_LIST_HEAD(&call->link); |
45025bce | 131 | INIT_LIST_HEAD(&call->chan_wait_link); |
17926a79 | 132 | INIT_LIST_HEAD(&call->accept_link); |
248f219c DH |
133 | INIT_LIST_HEAD(&call->recvmsg_link); |
134 | INIT_LIST_HEAD(&call->sock_link); | |
45025bce | 135 | init_waitqueue_head(&call->waitq); |
17926a79 DH |
136 | spin_lock_init(&call->lock); |
137 | rwlock_init(&call->state_lock); | |
138 | atomic_set(&call->usage, 1); | |
139 | call->debug_id = atomic_inc_return(&rxrpc_debug_id); | |
e754eba6 | 140 | call->tx_total_len = -1; |
17926a79 DH |
141 | |
142 | memset(&call->sock_node, 0xed, sizeof(call->sock_node)); | |
143 | ||
248f219c | 144 | /* Leave space in the ring to handle a maxed-out jumbo packet */ |
75e42126 | 145 | call->rx_winsize = rxrpc_rx_window_size; |
248f219c DH |
146 | call->tx_winsize = 16; |
147 | call->rx_expect_next = 1; | |
57494343 | 148 | |
f7aec129 | 149 | call->cong_cwnd = 2; |
57494343 | 150 | call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1; |
17926a79 | 151 | return call; |
248f219c DH |
152 | |
153 | nomem_2: | |
154 | kfree(call->rxtx_buffer); | |
155 | nomem: | |
156 | kmem_cache_free(rxrpc_call_jar, call); | |
157 | return NULL; | |
17926a79 DH |
158 | } |
159 | ||
160 | /* | |
999b69f8 | 161 | * Allocate a new client call. |
17926a79 | 162 | */ |
b89372f2 DH |
163 | static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx, |
164 | struct sockaddr_rxrpc *srx, | |
aa390bbe | 165 | gfp_t gfp) |
17926a79 DH |
166 | { |
167 | struct rxrpc_call *call; | |
57494343 | 168 | ktime_t now; |
17926a79 DH |
169 | |
170 | _enter(""); | |
171 | ||
b89372f2 | 172 | call = rxrpc_alloc_call(rx, gfp); |
17926a79 DH |
173 | if (!call) |
174 | return ERR_PTR(-ENOMEM); | |
999b69f8 | 175 | call->state = RXRPC_CALL_CLIENT_AWAIT_CONN; |
999b69f8 | 176 | call->service_id = srx->srx_service; |
71f3ca40 | 177 | call->tx_phase = true; |
57494343 DH |
178 | now = ktime_get_real(); |
179 | call->acks_latest_ts = now; | |
180 | call->cong_tstamp = now; | |
999b69f8 DH |
181 | |
182 | _leave(" = %p", call); | |
183 | return call; | |
184 | } | |
185 | ||
186 | /* | |
248f219c | 187 | * Initiate the call ack/resend/expiry timer. |
999b69f8 | 188 | */ |
248f219c | 189 | static void rxrpc_start_call_timer(struct rxrpc_call *call) |
999b69f8 | 190 | { |
df0adc78 | 191 | ktime_t now = ktime_get_real(), expire_at; |
248f219c | 192 | |
df0adc78 | 193 | expire_at = ktime_add_ms(now, rxrpc_max_call_lifetime); |
248f219c DH |
194 | call->expire_at = expire_at; |
195 | call->ack_at = expire_at; | |
a5af7e1f | 196 | call->ping_at = expire_at; |
248f219c | 197 | call->resend_at = expire_at; |
df0adc78 DH |
198 | call->timer.expires = jiffies + LONG_MAX / 2; |
199 | rxrpc_set_timer(call, rxrpc_timer_begin, now); | |
17926a79 DH |
200 | } |
201 | ||
202 | /* | |
540b1c48 DH |
203 | * Set up a call for the given parameters. |
204 | * - Called with the socket lock held, which it must release. | |
205 | * - If it returns a call, the call's lock will need releasing by the caller. | |
17926a79 | 206 | */ |
2341e077 | 207 | struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx, |
19ffa01c | 208 | struct rxrpc_conn_parameters *cp, |
999b69f8 | 209 | struct sockaddr_rxrpc *srx, |
17926a79 | 210 | unsigned long user_call_ID, |
e754eba6 | 211 | s64 tx_total_len, |
17926a79 | 212 | gfp_t gfp) |
540b1c48 | 213 | __releases(&rx->sk.sk_lock.slock) |
17926a79 | 214 | { |
2341e077 | 215 | struct rxrpc_call *call, *xcall; |
2baec2c3 | 216 | struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk)); |
2341e077 | 217 | struct rb_node *parent, **pp; |
e34d4234 | 218 | const void *here = __builtin_return_address(0); |
999b69f8 | 219 | int ret; |
17926a79 | 220 | |
999b69f8 | 221 | _enter("%p,%lx", rx, user_call_ID); |
17926a79 | 222 | |
b89372f2 | 223 | call = rxrpc_alloc_client_call(rx, srx, gfp); |
2341e077 | 224 | if (IS_ERR(call)) { |
540b1c48 | 225 | release_sock(&rx->sk); |
2341e077 DH |
226 | _leave(" = %ld", PTR_ERR(call)); |
227 | return call; | |
17926a79 DH |
228 | } |
229 | ||
e754eba6 | 230 | call->tx_total_len = tx_total_len; |
a84a46d7 DH |
231 | trace_rxrpc_call(call, rxrpc_call_new_client, atomic_read(&call->usage), |
232 | here, (const void *)user_call_ID); | |
e34d4234 | 233 | |
540b1c48 DH |
234 | /* We need to protect a partially set up call against the user as we |
235 | * will be acting outside the socket lock. | |
236 | */ | |
237 | mutex_lock(&call->user_mutex); | |
238 | ||
999b69f8 | 239 | /* Publish the call, even though it is incompletely set up as yet */ |
17926a79 DH |
240 | write_lock(&rx->call_lock); |
241 | ||
242 | pp = &rx->calls.rb_node; | |
243 | parent = NULL; | |
244 | while (*pp) { | |
245 | parent = *pp; | |
2341e077 | 246 | xcall = rb_entry(parent, struct rxrpc_call, sock_node); |
17926a79 | 247 | |
2341e077 | 248 | if (user_call_ID < xcall->user_call_ID) |
17926a79 | 249 | pp = &(*pp)->rb_left; |
2341e077 | 250 | else if (user_call_ID > xcall->user_call_ID) |
17926a79 DH |
251 | pp = &(*pp)->rb_right; |
252 | else | |
357f5ef6 | 253 | goto error_dup_user_ID; |
17926a79 DH |
254 | } |
255 | ||
248f219c | 256 | rcu_assign_pointer(call->socket, rx); |
357f5ef6 DH |
257 | call->user_call_ID = user_call_ID; |
258 | __set_bit(RXRPC_CALL_HAS_USERID, &call->flags); | |
fff72429 | 259 | rxrpc_get_call(call, rxrpc_call_got_userid); |
17926a79 DH |
260 | rb_link_node(&call->sock_node, parent, pp); |
261 | rb_insert_color(&call->sock_node, &rx->calls); | |
248f219c DH |
262 | list_add(&call->sock_link, &rx->sock_calls); |
263 | ||
17926a79 DH |
264 | write_unlock(&rx->call_lock); |
265 | ||
2baec2c3 DH |
266 | write_lock(&rxnet->call_lock); |
267 | list_add_tail(&call->link, &rxnet->calls); | |
268 | write_unlock(&rxnet->call_lock); | |
17926a79 | 269 | |
540b1c48 DH |
270 | /* From this point on, the call is protected by its own lock. */ |
271 | release_sock(&rx->sk); | |
272 | ||
248f219c DH |
273 | /* Set up or get a connection record and set the protocol parameters, |
274 | * including channel number and call ID. | |
275 | */ | |
276 | ret = rxrpc_connect_call(call, cp, srx, gfp); | |
999b69f8 DH |
277 | if (ret < 0) |
278 | goto error; | |
279 | ||
a84a46d7 | 280 | trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage), |
54fde423 | 281 | here, NULL); |
a84a46d7 | 282 | |
248f219c DH |
283 | rxrpc_start_call_timer(call); |
284 | ||
17926a79 DH |
285 | _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id); |
286 | ||
287 | _leave(" = %p [new]", call); | |
288 | return call; | |
289 | ||
2341e077 DH |
290 | /* We unexpectedly found the user ID in the list after taking |
291 | * the call_lock. This shouldn't happen unless the user races | |
292 | * with itself and tries to add the same user ID twice at the | |
293 | * same time in different threads. | |
294 | */ | |
357f5ef6 | 295 | error_dup_user_ID: |
17926a79 | 296 | write_unlock(&rx->call_lock); |
540b1c48 | 297 | release_sock(&rx->sk); |
8d94aa38 | 298 | ret = -EEXIST; |
357f5ef6 DH |
299 | |
300 | error: | |
301 | __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, | |
302 | RX_CALL_DEAD, ret); | |
a84a46d7 DH |
303 | trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage), |
304 | here, ERR_PTR(ret)); | |
357f5ef6 | 305 | rxrpc_release_call(rx, call); |
540b1c48 | 306 | mutex_unlock(&call->user_mutex); |
357f5ef6 DH |
307 | rxrpc_put_call(call, rxrpc_call_put); |
308 | _leave(" = %d", ret); | |
309 | return ERR_PTR(ret); | |
17926a79 DH |
310 | } |
311 | ||
c038a58c DH |
312 | /* |
313 | * Retry a call to a new address. It is expected that the Tx queue of the call | |
314 | * will contain data previously packaged for an old call. | |
315 | */ | |
316 | int rxrpc_retry_client_call(struct rxrpc_sock *rx, | |
317 | struct rxrpc_call *call, | |
318 | struct rxrpc_conn_parameters *cp, | |
319 | struct sockaddr_rxrpc *srx, | |
320 | gfp_t gfp) | |
321 | { | |
322 | const void *here = __builtin_return_address(0); | |
323 | int ret; | |
324 | ||
325 | /* Set up or get a connection record and set the protocol parameters, | |
326 | * including channel number and call ID. | |
327 | */ | |
328 | ret = rxrpc_connect_call(call, cp, srx, gfp); | |
329 | if (ret < 0) | |
330 | goto error; | |
331 | ||
332 | trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage), | |
333 | here, NULL); | |
334 | ||
335 | rxrpc_start_call_timer(call); | |
336 | ||
337 | _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id); | |
338 | ||
339 | if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events)) | |
340 | rxrpc_queue_call(call); | |
341 | ||
342 | _leave(" = 0"); | |
343 | return 0; | |
344 | ||
345 | error: | |
346 | rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, | |
347 | RX_CALL_DEAD, ret); | |
348 | trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage), | |
349 | here, ERR_PTR(ret)); | |
350 | _leave(" = %d", ret); | |
351 | return ret; | |
352 | } | |
353 | ||
17926a79 | 354 | /* |
248f219c DH |
355 | * Set up an incoming call. call->conn points to the connection. |
356 | * This is called in BH context and isn't allowed to fail. | |
17926a79 | 357 | */ |
248f219c DH |
358 | void rxrpc_incoming_call(struct rxrpc_sock *rx, |
359 | struct rxrpc_call *call, | |
360 | struct sk_buff *skb) | |
17926a79 | 361 | { |
248f219c | 362 | struct rxrpc_connection *conn = call->conn; |
42886ffe | 363 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
248f219c | 364 | u32 chan; |
17926a79 | 365 | |
248f219c | 366 | _enter(",%d", call->conn->debug_id); |
e34d4234 | 367 | |
248f219c DH |
368 | rcu_assign_pointer(call->socket, rx); |
369 | call->call_id = sp->hdr.callNumber; | |
370 | call->service_id = sp->hdr.serviceId; | |
371 | call->cid = sp->hdr.cid; | |
372 | call->state = RXRPC_CALL_SERVER_ACCEPTING; | |
373 | if (sp->hdr.securityIndex > 0) | |
374 | call->state = RXRPC_CALL_SERVER_SECURING; | |
57494343 | 375 | call->cong_tstamp = skb->tstamp; |
248f219c DH |
376 | |
377 | /* Set the channel for this call. We don't get channel_lock as we're | |
378 | * only defending against the data_ready handler (which we're called | |
379 | * from) and the RESPONSE packet parser (which is only really | |
380 | * interested in call_counter and can cope with a disagreement with the | |
381 | * call pointer). | |
a1399f8b | 382 | */ |
248f219c DH |
383 | chan = sp->hdr.cid & RXRPC_CHANNELMASK; |
384 | conn->channels[chan].call_counter = call->call_id; | |
385 | conn->channels[chan].call_id = call->call_id; | |
a1399f8b | 386 | rcu_assign_pointer(conn->channels[chan].call, call); |
17926a79 | 387 | |
85f32278 DH |
388 | spin_lock(&conn->params.peer->lock); |
389 | hlist_add_head(&call->error_link, &conn->params.peer->error_targets); | |
390 | spin_unlock(&conn->params.peer->lock); | |
17926a79 | 391 | |
17926a79 DH |
392 | _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id); |
393 | ||
248f219c DH |
394 | rxrpc_start_call_timer(call); |
395 | _leave(""); | |
17926a79 DH |
396 | } |
397 | ||
8d94aa38 DH |
398 | /* |
399 | * Queue a call's work processor, getting a ref to pass to the work queue. | |
400 | */ | |
401 | bool rxrpc_queue_call(struct rxrpc_call *call) | |
402 | { | |
403 | const void *here = __builtin_return_address(0); | |
404 | int n = __atomic_add_unless(&call->usage, 1, 0); | |
8d94aa38 DH |
405 | if (n == 0) |
406 | return false; | |
407 | if (rxrpc_queue_work(&call->processor)) | |
2ab27215 | 408 | trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL); |
8d94aa38 DH |
409 | else |
410 | rxrpc_put_call(call, rxrpc_call_put_noqueue); | |
411 | return true; | |
412 | } | |
413 | ||
414 | /* | |
415 | * Queue a call's work processor, passing the callers ref to the work queue. | |
416 | */ | |
417 | bool __rxrpc_queue_call(struct rxrpc_call *call) | |
418 | { | |
419 | const void *here = __builtin_return_address(0); | |
420 | int n = atomic_read(&call->usage); | |
8d94aa38 DH |
421 | ASSERTCMP(n, >=, 1); |
422 | if (rxrpc_queue_work(&call->processor)) | |
2ab27215 | 423 | trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL); |
8d94aa38 DH |
424 | else |
425 | rxrpc_put_call(call, rxrpc_call_put_noqueue); | |
426 | return true; | |
427 | } | |
428 | ||
e34d4234 DH |
429 | /* |
430 | * Note the re-emergence of a call. | |
431 | */ | |
432 | void rxrpc_see_call(struct rxrpc_call *call) | |
433 | { | |
434 | const void *here = __builtin_return_address(0); | |
435 | if (call) { | |
436 | int n = atomic_read(&call->usage); | |
e34d4234 | 437 | |
2ab27215 | 438 | trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL); |
e34d4234 DH |
439 | } |
440 | } | |
441 | ||
442 | /* | |
443 | * Note the addition of a ref on a call. | |
444 | */ | |
fff72429 | 445 | void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op) |
e34d4234 DH |
446 | { |
447 | const void *here = __builtin_return_address(0); | |
448 | int n = atomic_inc_return(&call->usage); | |
e34d4234 | 449 | |
2ab27215 | 450 | trace_rxrpc_call(call, op, n, here, NULL); |
e34d4234 DH |
451 | } |
452 | ||
453 | /* | |
248f219c | 454 | * Detach a call from its owning socket. |
e34d4234 | 455 | */ |
248f219c | 456 | void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call) |
e34d4234 | 457 | { |
a84a46d7 | 458 | const void *here = __builtin_return_address(0); |
248f219c DH |
459 | struct rxrpc_connection *conn = call->conn; |
460 | bool put = false; | |
461 | int i; | |
e34d4234 | 462 | |
248f219c | 463 | _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage)); |
e34d4234 | 464 | |
a84a46d7 DH |
465 | trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage), |
466 | here, (const void *)call->flags); | |
17926a79 | 467 | |
a84a46d7 | 468 | ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); |
e34d4234 | 469 | |
17926a79 DH |
470 | spin_lock_bh(&call->lock); |
471 | if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) | |
472 | BUG(); | |
473 | spin_unlock_bh(&call->lock); | |
474 | ||
248f219c | 475 | del_timer_sync(&call->timer); |
17926a79 | 476 | |
248f219c DH |
477 | /* Make sure we don't get any more notifications */ |
478 | write_lock_bh(&rx->recvmsg_lock); | |
e653cfe4 | 479 | |
248f219c | 480 | if (!list_empty(&call->recvmsg_link)) { |
17926a79 DH |
481 | _debug("unlinking once-pending call %p { e=%lx f=%lx }", |
482 | call, call->events, call->flags); | |
248f219c DH |
483 | list_del(&call->recvmsg_link); |
484 | put = true; | |
485 | } | |
486 | ||
487 | /* list_empty() must return false in rxrpc_notify_socket() */ | |
488 | call->recvmsg_link.next = NULL; | |
489 | call->recvmsg_link.prev = NULL; | |
490 | ||
491 | write_unlock_bh(&rx->recvmsg_lock); | |
492 | if (put) | |
493 | rxrpc_put_call(call, rxrpc_call_put); | |
494 | ||
495 | write_lock(&rx->call_lock); | |
496 | ||
497 | if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { | |
17926a79 DH |
498 | rb_erase(&call->sock_node, &rx->calls); |
499 | memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); | |
8d94aa38 | 500 | rxrpc_put_call(call, rxrpc_call_put_userid); |
17926a79 | 501 | } |
17926a79 | 502 | |
248f219c DH |
503 | list_del(&call->sock_link); |
504 | write_unlock(&rx->call_lock); | |
505 | ||
506 | _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); | |
507 | ||
508 | if (conn) | |
8d94aa38 | 509 | rxrpc_disconnect_call(call); |
e653cfe4 | 510 | |
248f219c | 511 | for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) { |
71f3ca40 DH |
512 | rxrpc_free_skb(call->rxtx_buffer[i], |
513 | (call->tx_phase ? rxrpc_skb_tx_cleaned : | |
514 | rxrpc_skb_rx_cleaned)); | |
248f219c | 515 | call->rxtx_buffer[i] = NULL; |
17926a79 | 516 | } |
17926a79 DH |
517 | |
518 | _leave(""); | |
519 | } | |
520 | ||
c038a58c DH |
521 | /* |
522 | * Prepare a kernel service call for retry. | |
523 | */ | |
524 | int rxrpc_prepare_call_for_retry(struct rxrpc_sock *rx, struct rxrpc_call *call) | |
525 | { | |
526 | const void *here = __builtin_return_address(0); | |
527 | int i; | |
528 | u8 last = 0; | |
529 | ||
530 | _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage)); | |
531 | ||
532 | trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage), | |
533 | here, (const void *)call->flags); | |
534 | ||
535 | ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); | |
536 | ASSERTCMP(call->completion, !=, RXRPC_CALL_REMOTELY_ABORTED); | |
537 | ASSERTCMP(call->completion, !=, RXRPC_CALL_LOCALLY_ABORTED); | |
538 | ASSERT(list_empty(&call->recvmsg_link)); | |
539 | ||
540 | del_timer_sync(&call->timer); | |
541 | ||
542 | _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, call->conn); | |
543 | ||
544 | if (call->conn) | |
545 | rxrpc_disconnect_call(call); | |
546 | ||
547 | if (rxrpc_is_service_call(call) || | |
548 | !call->tx_phase || | |
549 | call->tx_hard_ack != 0 || | |
550 | call->rx_hard_ack != 0 || | |
551 | call->rx_top != 0) | |
552 | return -EINVAL; | |
553 | ||
554 | call->state = RXRPC_CALL_UNINITIALISED; | |
555 | call->completion = RXRPC_CALL_SUCCEEDED; | |
556 | call->call_id = 0; | |
557 | call->cid = 0; | |
558 | call->cong_cwnd = 0; | |
559 | call->cong_extra = 0; | |
560 | call->cong_ssthresh = 0; | |
561 | call->cong_mode = 0; | |
562 | call->cong_dup_acks = 0; | |
563 | call->cong_cumul_acks = 0; | |
564 | call->acks_lowest_nak = 0; | |
565 | ||
566 | for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) { | |
567 | last |= call->rxtx_annotations[i]; | |
568 | call->rxtx_annotations[i] &= RXRPC_TX_ANNO_LAST; | |
569 | call->rxtx_annotations[i] |= RXRPC_TX_ANNO_RETRANS; | |
570 | } | |
571 | ||
572 | _leave(" = 0"); | |
573 | return 0; | |
574 | } | |
575 | ||
17926a79 DH |
576 | /* |
577 | * release all the calls associated with a socket | |
578 | */ | |
579 | void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) | |
580 | { | |
581 | struct rxrpc_call *call; | |
17926a79 DH |
582 | |
583 | _enter("%p", rx); | |
584 | ||
0360da6d DH |
585 | while (!list_empty(&rx->to_be_accepted)) { |
586 | call = list_entry(rx->to_be_accepted.next, | |
587 | struct rxrpc_call, accept_link); | |
588 | list_del(&call->accept_link); | |
3a92789a | 589 | rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET); |
0360da6d DH |
590 | rxrpc_put_call(call, rxrpc_call_put); |
591 | } | |
592 | ||
248f219c DH |
593 | while (!list_empty(&rx->sock_calls)) { |
594 | call = list_entry(rx->sock_calls.next, | |
595 | struct rxrpc_call, sock_link); | |
596 | rxrpc_get_call(call, rxrpc_call_got); | |
3a92789a | 597 | rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET); |
26cb02aa | 598 | rxrpc_send_abort_packet(call); |
8d94aa38 | 599 | rxrpc_release_call(rx, call); |
248f219c | 600 | rxrpc_put_call(call, rxrpc_call_put); |
f36b5e44 DH |
601 | } |
602 | ||
17926a79 DH |
603 | _leave(""); |
604 | } | |
605 | ||
606 | /* | |
607 | * release a call | |
608 | */ | |
fff72429 | 609 | void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op) |
17926a79 | 610 | { |
2baec2c3 | 611 | struct rxrpc_net *rxnet; |
e34d4234 | 612 | const void *here = __builtin_return_address(0); |
2ab27215 | 613 | int n; |
17926a79 | 614 | |
e34d4234 | 615 | ASSERT(call != NULL); |
17926a79 | 616 | |
e34d4234 | 617 | n = atomic_dec_return(&call->usage); |
2ab27215 | 618 | trace_rxrpc_call(call, op, n, here, NULL); |
e34d4234 DH |
619 | ASSERTCMP(n, >=, 0); |
620 | if (n == 0) { | |
621 | _debug("call %d dead", call->debug_id); | |
248f219c | 622 | ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); |
17926a79 | 623 | |
2baec2c3 DH |
624 | if (!list_empty(&call->link)) { |
625 | rxnet = rxrpc_net(sock_net(&call->socket->sk)); | |
626 | write_lock(&rxnet->call_lock); | |
627 | list_del_init(&call->link); | |
628 | write_unlock(&rxnet->call_lock); | |
629 | } | |
e34d4234 | 630 | |
8d94aa38 | 631 | rxrpc_cleanup_call(call); |
17926a79 | 632 | } |
17926a79 DH |
633 | } |
634 | ||
dee46364 DH |
635 | /* |
636 | * Final call destruction under RCU. | |
637 | */ | |
638 | static void rxrpc_rcu_destroy_call(struct rcu_head *rcu) | |
639 | { | |
640 | struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu); | |
641 | ||
df5d8bf7 | 642 | rxrpc_put_peer(call->peer); |
248f219c DH |
643 | kfree(call->rxtx_buffer); |
644 | kfree(call->rxtx_annotations); | |
dee46364 DH |
645 | kmem_cache_free(rxrpc_call_jar, call); |
646 | } | |
647 | ||
17926a79 DH |
648 | /* |
649 | * clean up a call | |
650 | */ | |
00e90712 | 651 | void rxrpc_cleanup_call(struct rxrpc_call *call) |
17926a79 | 652 | { |
248f219c | 653 | int i; |
17926a79 | 654 | |
248f219c | 655 | _net("DESTROY CALL %d", call->debug_id); |
17926a79 DH |
656 | |
657 | memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); | |
658 | ||
248f219c | 659 | del_timer_sync(&call->timer); |
17926a79 | 660 | |
8d94aa38 | 661 | ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE); |
17926a79 | 662 | ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); |
e653cfe4 | 663 | ASSERTCMP(call->conn, ==, NULL); |
17926a79 | 664 | |
248f219c DH |
665 | /* Clean up the Rx/Tx buffer */ |
666 | for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) | |
71f3ca40 DH |
667 | rxrpc_free_skb(call->rxtx_buffer[i], |
668 | (call->tx_phase ? rxrpc_skb_tx_cleaned : | |
669 | rxrpc_skb_rx_cleaned)); | |
17926a79 | 670 | |
71f3ca40 | 671 | rxrpc_free_skb(call->tx_pending, rxrpc_skb_tx_cleaned); |
17926a79 | 672 | |
dee46364 | 673 | call_rcu(&call->rcu, rxrpc_rcu_destroy_call); |
17926a79 DH |
674 | } |
675 | ||
676 | /* | |
2baec2c3 DH |
677 | * Make sure that all calls are gone from a network namespace. To reach this |
678 | * point, any open UDP sockets in that namespace must have been closed, so any | |
679 | * outstanding calls cannot be doing I/O. | |
17926a79 | 680 | */ |
2baec2c3 | 681 | void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet) |
17926a79 DH |
682 | { |
683 | struct rxrpc_call *call; | |
684 | ||
685 | _enter(""); | |
8d94aa38 | 686 | |
2baec2c3 | 687 | if (list_empty(&rxnet->calls)) |
8d94aa38 | 688 | return; |
248f219c | 689 | |
2baec2c3 | 690 | write_lock(&rxnet->call_lock); |
17926a79 | 691 | |
2baec2c3 DH |
692 | while (!list_empty(&rxnet->calls)) { |
693 | call = list_entry(rxnet->calls.next, struct rxrpc_call, link); | |
17926a79 DH |
694 | _debug("Zapping call %p", call); |
695 | ||
e34d4234 | 696 | rxrpc_see_call(call); |
17926a79 DH |
697 | list_del_init(&call->link); |
698 | ||
248f219c | 699 | pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n", |
8d94aa38 | 700 | call, atomic_read(&call->usage), |
8d94aa38 DH |
701 | rxrpc_call_states[call->state], |
702 | call->flags, call->events); | |
17926a79 | 703 | |
2baec2c3 | 704 | write_unlock(&rxnet->call_lock); |
17926a79 | 705 | cond_resched(); |
2baec2c3 | 706 | write_lock(&rxnet->call_lock); |
17926a79 DH |
707 | } |
708 | ||
2baec2c3 | 709 | write_unlock(&rxnet->call_lock); |
17926a79 | 710 | } |