Skip to content

Commit 4f95dd7

Browse files
committed
rxrpc: Rework local endpoint management
Rework the local RxRPC endpoint management. Local endpoint objects are maintained in a flat list as before. This should be okay as there shouldn't be more than one per open AF_RXRPC socket (there can be fewer as local endpoints can be shared if their local service ID is 0 and they share the same local transport parameters). Changes: (1) Local endpoints may now only be shared if they have local service ID 0 (ie. they're not being used for listening). This prevents a scenario where process A is listening of the Cache Manager port and process B contacts a fileserver - which may then attempt to send CM requests back to B. But if A and B are sharing a local endpoint, A will get the CM requests meant for B. (2) We use a mutex to handle lookups and don't provide RCU-only lookups since we only expect to access the list when opening a socket or destroying an endpoint. The local endpoint object is pointed to by the transport socket's sk_user_data for the life of the transport socket - allowing us to refer to it directly from the sk_data_ready and sk_error_report callbacks. (3) atomic_inc_not_zero() now exists and can be used to only share a local endpoint if the last reference hasn't yet gone. (4) We can remove rxrpc_local_lock - a spinlock that had to be taken with BH processing disabled given that we assume sk_user_data won't change under us. (5) The transport socket is shut down before we clear the sk_user_data pointer so that we can be sure that the transport socket's callbacks won't be invoked once the RCU destruction is scheduled. (6) Local endpoints have a work item that handles both destruction and event processing. The means that destruction doesn't then need to wait for event processing. The event queues can then be cleared after the transport socket is shut down. (7) Local endpoints are no longer available for resurrection beyond the life of the sockets that had them open. As soon as their last ref goes, they are scheduled for destruction and may not have their usage count moved from 0. Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 8756361 commit 4f95dd7

7 files changed

Lines changed: 276 additions & 230 deletions

File tree

‎net/rxrpc/af_rxrpc.c‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ static int rxrpc_validate_address(struct rxrpc_sock *rx,
102102

103103
switch (srx->transport.family) {
104104
case AF_INET:
105+
if (srx->transport_len < sizeof(struct sockaddr_in))
106+
return -EINVAL;
105107
_debug("INET: %x @ %pI4",
106108
ntohs(srx->transport.sin.sin_port),
107109
&srx->transport.sin.sin_addr);
@@ -835,12 +837,27 @@ static void __exit af_rxrpc_exit(void)
835837
rxrpc_destroy_all_calls();
836838
rxrpc_destroy_all_connections();
837839
rxrpc_destroy_all_transports();
838-
rxrpc_destroy_all_locals();
839840

840841
ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
841842

843+
/* We need to flush the scheduled work twice because the local endpoint
844+
* records involve a work item in their destruction as they can only be
845+
* destroyed from process context. However, a connection may have a
846+
* work item outstanding - and this will pin the local endpoint record
847+
* until the connection goes away.
848+
*
849+
* Peers don't pin locals and calls pin sockets - which prevents the
850+
* module from being unloaded - so we should only need two flushes.
851+
*/
842852
_debug("flush scheduled work");
843853
flush_workqueue(rxrpc_workqueue);
854+
_debug("flush scheduled work 2");
855+
flush_workqueue(rxrpc_workqueue);
856+
_debug("synchronise RCU");
857+
rcu_barrier();
858+
_debug("destroy locals");
859+
rxrpc_destroy_all_locals();
860+
844861
remove_proc_entry("rxrpc_conns", init_net.proc_net);
845862
remove_proc_entry("rxrpc_calls", init_net.proc_net);
846863
destroy_workqueue(rxrpc_workqueue);

‎net/rxrpc/ar-internal.h‎

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,26 @@ struct rxrpc_security {
170170
};
171171

172172
/*
173-
* RxRPC local transport endpoint definition
174-
* - matched by local port, address and protocol type
173+
* RxRPC local transport endpoint description
174+
* - owned by a single AF_RXRPC socket
175+
* - pointed to by transport socket struct sk_user_data
175176
*/
176177
struct rxrpc_local {
178+
struct rcu_head rcu;
179+
atomic_t usage;
180+
struct list_head link;
177181
struct socket *socket; /* my UDP socket */
178-
struct work_struct destroyer; /* endpoint destroyer */
179-
struct work_struct acceptor; /* incoming call processor */
180-
struct work_struct rejecter; /* packet reject writer */
181-
struct work_struct event_processor; /* endpoint event processor */
182+
struct work_struct processor;
182183
struct list_head services; /* services listening on this endpoint */
183-
struct list_head link; /* link in endpoint list */
184184
struct rw_semaphore defrag_sem; /* control re-enablement of IP DF bit */
185185
struct sk_buff_head accept_queue; /* incoming calls awaiting acceptance */
186186
struct sk_buff_head reject_queue; /* packets awaiting rejection */
187187
struct sk_buff_head event_queue; /* endpoint event packets awaiting processing */
188+
struct mutex conn_lock; /* Client connection creation lock */
188189
spinlock_t lock; /* access lock */
189190
rwlock_t services_lock; /* lock for services list */
190-
atomic_t usage;
191191
int debug_id; /* debug ID for printks */
192+
bool dead;
192193
struct sockaddr_rxrpc srx; /* local address */
193194
};
194195

@@ -487,7 +488,7 @@ extern struct rxrpc_transport *rxrpc_name_to_transport(struct rxrpc_sock *,
487488
/*
488489
* call_accept.c
489490
*/
490-
void rxrpc_accept_incoming_calls(struct work_struct *);
491+
void rxrpc_accept_incoming_calls(struct rxrpc_local *);
491492
struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *, unsigned long);
492493
int rxrpc_reject_call(struct rxrpc_sock *);
493494

@@ -527,7 +528,7 @@ void __exit rxrpc_destroy_all_calls(void);
527528
*/
528529
void rxrpc_process_connection(struct work_struct *);
529530
void rxrpc_reject_packet(struct rxrpc_local *, struct sk_buff *);
530-
void rxrpc_reject_packets(struct work_struct *);
531+
void rxrpc_reject_packets(struct rxrpc_local *);
531532

532533
/*
533534
* conn_object.c
@@ -575,17 +576,32 @@ int rxrpc_get_server_data_key(struct rxrpc_connection *, const void *, time_t,
575576
/*
576577
* local_event.c
577578
*/
578-
extern void rxrpc_process_local_events(struct work_struct *);
579+
extern void rxrpc_process_local_events(struct rxrpc_local *);
579580

580581
/*
581582
* local_object.c
582583
*/
583-
extern rwlock_t rxrpc_local_lock;
584-
585-
struct rxrpc_local *rxrpc_lookup_local(struct sockaddr_rxrpc *);
586-
void rxrpc_put_local(struct rxrpc_local *);
584+
struct rxrpc_local *rxrpc_lookup_local(const struct sockaddr_rxrpc *);
585+
void __rxrpc_put_local(struct rxrpc_local *);
587586
void __exit rxrpc_destroy_all_locals(void);
588587

588+
static inline void rxrpc_get_local(struct rxrpc_local *local)
589+
{
590+
atomic_inc(&local->usage);
591+
}
592+
593+
static inline
594+
struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
595+
{
596+
return atomic_inc_not_zero(&local->usage) ? local : NULL;
597+
}
598+
599+
static inline void rxrpc_put_local(struct rxrpc_local *local)
600+
{
601+
if (atomic_dec_and_test(&local->usage))
602+
__rxrpc_put_local(local);
603+
}
604+
589605
/*
590606
* misc.c
591607
*/
@@ -874,15 +890,6 @@ static inline void rxrpc_purge_queue(struct sk_buff_head *list)
874890
rxrpc_free_skb(skb);
875891
}
876892

877-
static inline void __rxrpc_get_local(struct rxrpc_local *local, const char *f)
878-
{
879-
CHECK_SLAB_OKAY(&local->usage);
880-
if (atomic_inc_return(&local->usage) == 1)
881-
printk("resurrected (%s)\n", f);
882-
}
883-
884-
#define rxrpc_get_local(LOCAL) __rxrpc_get_local((LOCAL), __func__)
885-
886893
#define rxrpc_get_call(CALL) \
887894
do { \
888895
CHECK_SLAB_OKAY(&(CALL)->usage); \

‎net/rxrpc/call_accept.c‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,8 @@ static int rxrpc_accept_incoming_call(struct rxrpc_local *local,
202202
* accept incoming calls that need peer, transport and/or connection setting up
203203
* - the packets we get are all incoming client DATA packets that have seq == 1
204204
*/
205-
void rxrpc_accept_incoming_calls(struct work_struct *work)
205+
void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
206206
{
207-
struct rxrpc_local *local =
208-
container_of(work, struct rxrpc_local, acceptor);
209207
struct rxrpc_skb_priv *sp;
210208
struct sockaddr_rxrpc srx;
211209
struct rxrpc_sock *rx;
@@ -215,21 +213,8 @@ void rxrpc_accept_incoming_calls(struct work_struct *work)
215213

216214
_enter("%d", local->debug_id);
217215

218-
read_lock_bh(&rxrpc_local_lock);
219-
if (atomic_read(&local->usage) > 0)
220-
rxrpc_get_local(local);
221-
else
222-
local = NULL;
223-
read_unlock_bh(&rxrpc_local_lock);
224-
if (!local) {
225-
_leave(" [local dead]");
226-
return;
227-
}
228-
229-
process_next_packet:
230216
skb = skb_dequeue(&local->accept_queue);
231217
if (!skb) {
232-
rxrpc_put_local(local);
233218
_leave("\n");
234219
return;
235220
}
@@ -292,7 +277,7 @@ void rxrpc_accept_incoming_calls(struct work_struct *work)
292277
case -ECONNRESET: /* old calls are ignored */
293278
case -ECONNABORTED: /* aborted calls are reaborted or ignored */
294279
case 0:
295-
goto process_next_packet;
280+
return;
296281
case -ECONNREFUSED:
297282
goto invalid_service;
298283
case -EBUSY:
@@ -308,18 +293,18 @@ void rxrpc_accept_incoming_calls(struct work_struct *work)
308293
busy:
309294
rxrpc_busy(local, &srx, &whdr);
310295
rxrpc_free_skb(skb);
311-
goto process_next_packet;
296+
return;
312297

313298
invalid_service:
314299
skb->priority = RX_INVALID_OPERATION;
315300
rxrpc_reject_packet(local, skb);
316-
goto process_next_packet;
301+
return;
317302

318303
/* can't change connection security type mid-flow */
319304
security_mismatch:
320305
skb->priority = RX_PROTOCOL_ERROR;
321306
rxrpc_reject_packet(local, skb);
322-
goto process_next_packet;
307+
return;
323308
}
324309

325310
/*

‎net/rxrpc/conn_event.c‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,36 +314,27 @@ void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
314314
{
315315
CHECK_SLAB_OKAY(&local->usage);
316316

317-
if (!atomic_inc_not_zero(&local->usage)) {
318-
printk("resurrected on reject\n");
319-
BUG();
320-
}
321-
322317
skb_queue_tail(&local->reject_queue, skb);
323-
rxrpc_queue_work(&local->rejecter);
318+
rxrpc_queue_work(&local->processor);
324319
}
325320

326321
/*
327322
* reject packets through the local endpoint
328323
*/
329-
void rxrpc_reject_packets(struct work_struct *work)
324+
void rxrpc_reject_packets(struct rxrpc_local *local)
330325
{
331326
union {
332327
struct sockaddr sa;
333328
struct sockaddr_in sin;
334329
} sa;
335330
struct rxrpc_skb_priv *sp;
336331
struct rxrpc_wire_header whdr;
337-
struct rxrpc_local *local;
338332
struct sk_buff *skb;
339333
struct msghdr msg;
340334
struct kvec iov[2];
341335
size_t size;
342336
__be32 code;
343337

344-
local = container_of(work, struct rxrpc_local, rejecter);
345-
rxrpc_get_local(local);
346-
347338
_enter("%d", local->debug_id);
348339

349340
iov[0].iov_base = &whdr;
@@ -395,9 +386,7 @@ void rxrpc_reject_packets(struct work_struct *work)
395386
}
396387

397388
rxrpc_free_skb(skb);
398-
rxrpc_put_local(local);
399389
}
400390

401-
rxrpc_put_local(local);
402391
_leave("");
403392
}

‎net/rxrpc/input.c‎

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,8 @@ static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
594594
{
595595
_enter("%p,%p", local, skb);
596596

597-
atomic_inc(&local->usage);
598597
skb_queue_tail(&local->event_queue, skb);
599-
rxrpc_queue_work(&local->event_processor);
598+
rxrpc_queue_work(&local->processor);
600599
}
601600

602601
/*
@@ -664,33 +663,24 @@ static struct rxrpc_connection *rxrpc_conn_from_local(struct rxrpc_local *local,
664663
/*
665664
* handle data received on the local endpoint
666665
* - may be called in interrupt context
666+
*
667+
* The socket is locked by the caller and this prevents the socket from being
668+
* shut down and the local endpoint from going away, thus sk_user_data will not
669+
* be cleared until this function returns.
667670
*/
668671
void rxrpc_data_ready(struct sock *sk)
669672
{
670673
struct rxrpc_skb_priv *sp;
671-
struct rxrpc_local *local;
674+
struct rxrpc_local *local = sk->sk_user_data;
672675
struct sk_buff *skb;
673676
int ret;
674677

675678
_enter("%p", sk);
676679

677680
ASSERT(!irqs_disabled());
678681

679-
read_lock_bh(&rxrpc_local_lock);
680-
local = sk->sk_user_data;
681-
if (local && atomic_read(&local->usage) > 0)
682-
rxrpc_get_local(local);
683-
else
684-
local = NULL;
685-
read_unlock_bh(&rxrpc_local_lock);
686-
if (!local) {
687-
_leave(" [local dead]");
688-
return;
689-
}
690-
691682
skb = skb_recv_datagram(sk, 0, 1, &ret);
692683
if (!skb) {
693-
rxrpc_put_local(local);
694684
if (ret == -EAGAIN)
695685
return;
696686
_debug("UDP socket error %d", ret);
@@ -704,7 +694,6 @@ void rxrpc_data_ready(struct sock *sk)
704694
/* we'll probably need to checksum it (didn't call sock_recvmsg) */
705695
if (skb_checksum_complete(skb)) {
706696
rxrpc_free_skb(skb);
707-
rxrpc_put_local(local);
708697
__UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
709698
_leave(" [CSUM failed]");
710699
return;
@@ -769,7 +758,6 @@ void rxrpc_data_ready(struct sock *sk)
769758
}
770759

771760
out:
772-
rxrpc_put_local(local);
773761
return;
774762

775763
cant_route_call:
@@ -779,8 +767,7 @@ void rxrpc_data_ready(struct sock *sk)
779767
if (sp->hdr.seq == 1) {
780768
_debug("first packet");
781769
skb_queue_tail(&local->accept_queue, skb);
782-
rxrpc_queue_work(&local->acceptor);
783-
rxrpc_put_local(local);
770+
rxrpc_queue_work(&local->processor);
784771
_leave(" [incoming]");
785772
return;
786773
}
@@ -793,13 +780,11 @@ void rxrpc_data_ready(struct sock *sk)
793780
_debug("reject type %d",sp->hdr.type);
794781
rxrpc_reject_packet(local, skb);
795782
}
796-
rxrpc_put_local(local);
797783
_leave(" [no call]");
798784
return;
799785

800786
bad_message:
801787
skb->priority = RX_PROTOCOL_ERROR;
802788
rxrpc_reject_packet(local, skb);
803-
rxrpc_put_local(local);
804789
_leave(" [badmsg]");
805790
}

‎net/rxrpc/local_event.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,15 @@ static void rxrpc_send_version_request(struct rxrpc_local *local,
8282
/*
8383
* Process event packets targetted at a local endpoint.
8484
*/
85-
void rxrpc_process_local_events(struct work_struct *work)
85+
void rxrpc_process_local_events(struct rxrpc_local *local)
8686
{
87-
struct rxrpc_local *local = container_of(work, struct rxrpc_local, event_processor);
8887
struct sk_buff *skb;
8988
char v;
9089

9190
_enter("");
9291

93-
atomic_inc(&local->usage);
94-
95-
while ((skb = skb_dequeue(&local->event_queue))) {
92+
skb = skb_dequeue(&local->event_queue);
93+
if (skb) {
9694
struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
9795

9896
_debug("{%d},{%u}", local->debug_id, sp->hdr.type);
@@ -111,10 +109,8 @@ void rxrpc_process_local_events(struct work_struct *work)
111109
break;
112110
}
113111

114-
rxrpc_put_local(local);
115112
rxrpc_free_skb(skb);
116113
}
117114

118-
rxrpc_put_local(local);
119115
_leave("");
120116
}

0 commit comments

Comments
 (0)