2019-05-20 20:08:01 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2016-04-04 16:00:34 +03:00
/* Local endpoint object management
2007-04-27 02:48:28 +04:00
*
2016-04-04 16:00:35 +03:00
* Copyright ( C ) 2016 Red Hat , Inc . All Rights Reserved .
2007-04-27 02:48:28 +04:00
* Written by David Howells ( dhowells @ redhat . com )
*/
2016-06-02 22:08:52 +03:00
# define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2007-04-27 02:48:28 +04:00
# include <linux/module.h>
# include <linux/net.h>
# include <linux/skbuff.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2015-04-01 18:31:26 +03:00
# include <linux/udp.h>
# include <linux/ip.h>
2016-04-04 16:00:35 +03:00
# include <linux/hashtable.h>
2007-04-27 02:48:28 +04:00
# include <net/sock.h>
2018-10-04 13:10:51 +03:00
# include <net/udp.h>
2021-02-03 11:54:23 +03:00
# include <net/udp_tunnel.h>
2007-04-27 02:48:28 +04:00
# include <net/af_rxrpc.h>
# include "ar-internal.h"
2016-04-04 16:00:35 +03:00
static void rxrpc_local_rcu ( struct rcu_head * ) ;
2007-04-27 02:48:28 +04:00
2022-10-12 11:51:12 +03:00
/*
* Handle an ICMP / ICMP6 error turning up at the tunnel . Push it through the
* usual mechanism so that it gets parsed and presented through the UDP
* socket ' s error_report ( ) .
*/
static void rxrpc_encap_err_rcv ( struct sock * sk , struct sk_buff * skb , int err ,
__be16 port , u32 info , u8 * payload )
{
if ( ip_hdr ( skb ) - > version = = IPVERSION )
return ip_icmp_error ( sk , skb , err , port , info , payload ) ;
2022-11-10 12:39:39 +03:00
if ( IS_ENABLED ( CONFIG_AF_RXRPC_IPV6 ) )
return ipv6_icmp_error ( sk , skb , err , port , info , payload ) ;
2022-10-12 11:51:12 +03:00
}
2007-04-27 02:48:28 +04:00
/*
2016-04-04 16:00:35 +03:00
* Compare a local to an address . Return - ve , 0 or + ve to indicate less than ,
* same or greater than .
*
* We explicitly don ' t compare the RxRPC service ID as we want to reject
* conflicting uses by differing services . Further , we don ' t want to share
* addresses with different options ( IPv6 ) , so we don ' t compare those bits
* either .
2007-04-27 02:48:28 +04:00
*/
2016-04-04 16:00:35 +03:00
static long rxrpc_local_cmp_key ( const struct rxrpc_local * local ,
const struct sockaddr_rxrpc * srx )
{
long diff ;
diff = ( ( local - > srx . transport_type - srx - > transport_type ) ? :
( local - > srx . transport_len - srx - > transport_len ) ? :
( local - > srx . transport . family - srx - > transport . family ) ) ;
if ( diff ! = 0 )
return diff ;
switch ( srx - > transport . family ) {
case AF_INET :
/* If the choice of UDP port is left up to the transport, then
* the endpoint record doesn ' t match .
*/
return ( ( u16 __force ) local - > srx . transport . sin . sin_port -
( u16 __force ) srx - > transport . sin . sin_port ) ? :
memcmp ( & local - > srx . transport . sin . sin_addr ,
& srx - > transport . sin . sin_addr ,
sizeof ( struct in_addr ) ) ;
2016-09-17 09:26:01 +03:00
# ifdef CONFIG_AF_RXRPC_IPV6
2016-09-13 10:49:05 +03:00
case AF_INET6 :
/* If the choice of UDP6 port is left up to the transport, then
* the endpoint record doesn ' t match .
*/
return ( ( u16 __force ) local - > srx . transport . sin6 . sin6_port -
( u16 __force ) srx - > transport . sin6 . sin6_port ) ? :
memcmp ( & local - > srx . transport . sin6 . sin6_addr ,
& srx - > transport . sin6 . sin6_addr ,
sizeof ( struct in6_addr ) ) ;
2016-09-17 09:26:01 +03:00
# endif
2016-04-04 16:00:35 +03:00
default :
BUG ( ) ;
}
}
2022-11-02 19:46:13 +03:00
static void rxrpc_client_conn_reap_timeout ( struct timer_list * timer )
{
struct rxrpc_local * local =
container_of ( timer , struct rxrpc_local , client_conn_reap_timer ) ;
if ( local - > kill_all_client_conns & &
test_and_set_bit ( RXRPC_CLIENT_CONN_REAP_TIMER , & local - > client_conn_flags ) )
rxrpc_wake_up_io_thread ( local ) ;
}
2016-04-04 16:00:35 +03:00
/*
* Allocate a new local endpoint .
*/
2022-10-21 01:17:06 +03:00
static struct rxrpc_local * rxrpc_alloc_local ( struct net * net ,
2017-05-24 19:02:32 +03:00
const struct sockaddr_rxrpc * srx )
2007-04-27 02:48:28 +04:00
{
struct rxrpc_local * local ;
2022-10-21 00:58:56 +03:00
u32 tmp ;
2007-04-27 02:48:28 +04:00
local = kzalloc ( sizeof ( struct rxrpc_local ) , GFP_KERNEL ) ;
if ( local ) {
2022-05-21 10:45:22 +03:00
refcount_set ( & local - > ref , 1 ) ;
2019-08-09 17:20:41 +03:00
atomic_set ( & local - > active_users , 1 ) ;
2022-10-21 01:17:06 +03:00
local - > net = net ;
local - > rxnet = rxrpc_net ( net ) ;
2022-05-21 10:45:15 +03:00
INIT_HLIST_NODE ( & local - > link ) ;
2007-04-27 02:48:28 +04:00
init_rwsem ( & local - > defrag_sem ) ;
rxrpc: Fix I/O thread startup getting skipped
When starting a kthread, the __kthread_create_on_node() function, as called
from kthread_run(), waits for a completion to indicate that the task_struct
(or failure state) of the new kernel thread is available before continuing.
This does not wait, however, for the thread function to be invoked and,
indeed, will skip it if kthread_stop() gets called before it gets there.
If this happens, though, kthread_run() will have returned successfully,
indicating that the thread was started and returning the task_struct
pointer. The actual error indication is returned by kthread_stop().
Note that this is ambiguous, as the caller cannot tell whether the -EINTR
error code came from kthread() or from the thread function.
This was encountered in the new rxrpc I/O thread, where if the system is
being pounded hard by, say, syzbot, the check of KTHREAD_SHOULD_STOP can be
delayed long enough for kthread_stop() to get called when rxrpc releases a
socket - and this causes an oops because the I/O thread function doesn't
get started and thus doesn't remove the rxrpc_local struct from the
local_endpoints list.
Fix this by using a completion to wait for the thread to actually enter
rxrpc_io_thread(). This makes sure the thread can't be prematurely
stopped and makes sure the relied-upon cleanup is done.
Fixes: a275da62e8c1 ("rxrpc: Create a per-local endpoint receive queue and I/O thread")
Reported-by: syzbot+3538a6a72efa8b059c38@syzkaller.appspotmail.com
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: Hillf Danton <hdanton@sina.com>
Link: https://lore.kernel.org/r/000000000000229f1505ef2b6159@google.com/
Signed-off-by: David S. Miller <davem@davemloft.net>
2022-12-15 19:20:13 +03:00
init_completion ( & local - > io_thread_ready ) ;
2022-10-10 10:45:20 +03:00
skb_queue_head_init ( & local - > rx_queue ) ;
2022-10-20 11:08:34 +03:00
INIT_LIST_HEAD ( & local - > conn_attend_q ) ;
2022-10-10 17:51:39 +03:00
INIT_LIST_HEAD ( & local - > call_attend_q ) ;
2022-11-02 19:46:13 +03:00
rxrpc: Rewrite the client connection manager
Rewrite the rxrpc client connection manager so that it can support multiple
connections for a given security key to a peer. The following changes are
made:
(1) For each open socket, the code currently maintains an rbtree with the
connections placed into it, keyed by communications parameters. This
is tricky to maintain as connections can be culled from the tree or
replaced within it. Connections can require replacement for a number
of reasons, e.g. their IDs span too great a range for the IDR data
type to represent efficiently, the call ID numbers on that conn would
overflow or the conn got aborted.
This is changed so that there's now a connection bundle object placed
in the tree, keyed on the same parameters. The bundle, however, does
not need to be replaced.
(2) An rxrpc_bundle object can now manage the available channels for a set
of parallel connections. The lock that manages this is moved there
from the rxrpc_connection struct (channel_lock).
(3) There'a a dummy bundle for all incoming connections to share so that
they have a channel_lock too. It might be better to give each
incoming connection its own bundle. This bundle is not needed to
manage which channels incoming calls are made on because that's the
solely at whim of the client.
(4) The restrictions on how many client connections are around are
removed. Instead, a previous patch limits the number of client calls
that can be allocated. Ordinarily, client connections are reaped
after 2 minutes on the idle queue, but when more than a certain number
of connections are in existence, the reaper starts reaping them after
2s of idleness instead to get the numbers back down.
It could also be made such that new call allocations are forced to
wait until the number of outstanding connections subsides.
Signed-off-by: David Howells <dhowells@redhat.com>
2020-07-01 13:15:32 +03:00
local - > client_bundles = RB_ROOT ;
spin_lock_init ( & local - > client_bundles_lock ) ;
2022-11-02 19:46:13 +03:00
local - > kill_all_client_conns = false ;
INIT_LIST_HEAD ( & local - > idle_client_conns ) ;
timer_setup ( & local - > client_conn_reap_timer ,
rxrpc_client_conn_reap_timeout , 0 ) ;
2007-04-27 02:48:28 +04:00
spin_lock_init ( & local - > lock ) ;
rwlock_init ( & local - > services_lock ) ;
local - > debug_id = atomic_inc_return ( & rxrpc_debug_id ) ;
memcpy ( & local - > srx , srx , sizeof ( * srx ) ) ;
2017-06-05 16:30:49 +03:00
local - > srx . srx_service = 0 ;
2022-10-21 00:58:56 +03:00
idr_init ( & local - > conn_ids ) ;
get_random_bytes ( & tmp , sizeof ( tmp ) ) ;
tmp & = 0x3fffffff ;
if ( tmp = = 0 )
tmp = 1 ;
idr_set_cursor ( & local - > conn_ids , tmp ) ;
2022-10-19 11:45:43 +03:00
INIT_LIST_HEAD ( & local - > new_client_calls ) ;
spin_lock_init ( & local - > client_call_lock ) ;
2022-10-21 00:58:56 +03:00
2022-10-21 15:00:34 +03:00
trace_rxrpc_local ( local - > debug_id , rxrpc_local_new , 1 , 1 ) ;
2007-04-27 02:48:28 +04:00
}
_leave ( " = %p " , local ) ;
return local ;
}
/*
* create the local socket
2016-04-04 16:00:35 +03:00
* - must be called with rxrpc_local_mutex locked
2007-04-27 02:48:28 +04:00
*/
2017-05-24 19:02:32 +03:00
static int rxrpc_open_socket ( struct rxrpc_local * local , struct net * net )
2007-04-27 02:48:28 +04:00
{
2021-02-07 11:23:14 +03:00
struct udp_tunnel_sock_cfg tuncfg = { NULL } ;
struct sockaddr_rxrpc * srx = & local - > srx ;
struct udp_port_cfg udp_conf = { 0 } ;
2022-10-10 10:45:20 +03:00
struct task_struct * io_thread ;
2018-10-04 13:10:51 +03:00
struct sock * usk ;
2020-05-28 08:12:32 +03:00
int ret ;
2007-04-27 02:48:28 +04:00
2016-09-13 10:49:05 +03:00
_enter ( " %p{%d,%d} " ,
2021-02-07 11:23:14 +03:00
local , srx - > transport_type , srx - > transport . family ) ;
udp_conf . family = srx - > transport . family ;
2022-04-29 23:05:16 +03:00
udp_conf . use_udp_checksums = true ;
2021-02-07 11:23:14 +03:00
if ( udp_conf . family = = AF_INET ) {
udp_conf . local_ip = srx - > transport . sin . sin_addr ;
udp_conf . local_udp_port = srx - > transport . sin . sin_port ;
2021-02-12 13:48:14 +03:00
# if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
2021-02-07 11:23:14 +03:00
} else {
udp_conf . local_ip6 = srx - > transport . sin6 . sin6_addr ;
udp_conf . local_udp_port = srx - > transport . sin6 . sin6_port ;
2022-04-29 23:05:16 +03:00
udp_conf . use_udp6_tx_checksums = true ;
udp_conf . use_udp6_rx_checksums = true ;
2021-02-12 13:48:14 +03:00
# endif
2021-02-07 11:23:14 +03:00
}
ret = udp_sock_create ( net , & udp_conf , & local - > socket ) ;
2007-04-27 02:48:28 +04:00
if ( ret < 0 ) {
_leave ( " = %d [socket] " , ret ) ;
return ret ;
}
2021-02-07 11:23:14 +03:00
tuncfg . encap_type = UDP_ENCAP_RXRPC ;
2022-10-10 12:55:24 +03:00
tuncfg . encap_rcv = rxrpc_encap_rcv ;
2022-08-26 17:39:28 +03:00
tuncfg . encap_err_rcv = rxrpc_encap_err_rcv ;
2021-02-07 11:23:14 +03:00
tuncfg . sk_user_data = local ;
setup_udp_tunnel_sock ( net , local - > socket , & tuncfg ) ;
2018-10-05 16:05:35 +03:00
/* set the socket up */
2018-10-04 13:10:51 +03:00
usk = local - > socket - > sk ;
usk - > sk_error_report = rxrpc_error_report ;
2018-10-05 16:05:35 +03:00
2021-02-07 11:23:14 +03:00
switch ( srx - > transport . family ) {
2018-09-27 17:13:09 +03:00
case AF_INET6 :
/* we want to receive ICMPv6 errors */
2021-02-07 11:23:14 +03:00
ip6_sock_set_recverr ( usk ) ;
2007-04-27 02:48:28 +04:00
2018-09-27 17:13:09 +03:00
/* Fall through and set IPv4 options too otherwise we don't get
* errors from IPv4 packets sent through the IPv6 socket .
*/
2020-08-24 01:36:59 +03:00
fallthrough ;
2018-09-27 17:13:09 +03:00
case AF_INET :
2018-05-11 01:26:00 +03:00
/* we want to receive ICMP errors */
2021-02-07 11:23:14 +03:00
ip_sock_set_recverr ( usk ) ;
2018-05-11 01:26:00 +03:00
/* we want to set the don't fragment bit */
2021-02-07 11:23:14 +03:00
ip_sock_set_mtu_discover ( usk , IP_PMTUDISC_DO ) ;
2018-09-27 17:13:08 +03:00
/* We want receive timestamps. */
2021-02-07 11:23:14 +03:00
sock_enable_timestamps ( usk ) ;
2018-05-11 01:26:00 +03:00
break ;
default :
BUG ( ) ;
2007-04-27 02:48:28 +04:00
}
2022-10-10 10:45:20 +03:00
io_thread = kthread_run ( rxrpc_io_thread , local ,
" krxrpcio/%u " , ntohs ( udp_conf . local_udp_port ) ) ;
if ( IS_ERR ( io_thread ) ) {
ret = PTR_ERR ( io_thread ) ;
goto error_sock ;
}
rxrpc: Fix I/O thread startup getting skipped
When starting a kthread, the __kthread_create_on_node() function, as called
from kthread_run(), waits for a completion to indicate that the task_struct
(or failure state) of the new kernel thread is available before continuing.
This does not wait, however, for the thread function to be invoked and,
indeed, will skip it if kthread_stop() gets called before it gets there.
If this happens, though, kthread_run() will have returned successfully,
indicating that the thread was started and returning the task_struct
pointer. The actual error indication is returned by kthread_stop().
Note that this is ambiguous, as the caller cannot tell whether the -EINTR
error code came from kthread() or from the thread function.
This was encountered in the new rxrpc I/O thread, where if the system is
being pounded hard by, say, syzbot, the check of KTHREAD_SHOULD_STOP can be
delayed long enough for kthread_stop() to get called when rxrpc releases a
socket - and this causes an oops because the I/O thread function doesn't
get started and thus doesn't remove the rxrpc_local struct from the
local_endpoints list.
Fix this by using a completion to wait for the thread to actually enter
rxrpc_io_thread(). This makes sure the thread can't be prematurely
stopped and makes sure the relied-upon cleanup is done.
Fixes: a275da62e8c1 ("rxrpc: Create a per-local endpoint receive queue and I/O thread")
Reported-by: syzbot+3538a6a72efa8b059c38@syzkaller.appspotmail.com
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: Hillf Danton <hdanton@sina.com>
Link: https://lore.kernel.org/r/000000000000229f1505ef2b6159@google.com/
Signed-off-by: David S. Miller <davem@davemloft.net>
2022-12-15 19:20:13 +03:00
wait_for_completion ( & local - > io_thread_ready ) ;
2022-10-10 10:45:20 +03:00
local - > io_thread = io_thread ;
2007-04-27 02:48:28 +04:00
_leave ( " = 0 " ) ;
return 0 ;
2022-10-10 10:45:20 +03:00
error_sock :
kernel_sock_shutdown ( local - > socket , SHUT_RDWR ) ;
local - > socket - > sk - > sk_user_data = NULL ;
sock_release ( local - > socket ) ;
local - > socket = NULL ;
return ret ;
2007-04-27 02:48:28 +04:00
}
/*
2016-04-04 16:00:35 +03:00
* Look up or create a new local endpoint using the specified local address .
2007-04-27 02:48:28 +04:00
*/
2017-05-24 19:02:32 +03:00
struct rxrpc_local * rxrpc_lookup_local ( struct net * net ,
const struct sockaddr_rxrpc * srx )
2007-04-27 02:48:28 +04:00
{
struct rxrpc_local * local ;
2017-05-24 19:02:32 +03:00
struct rxrpc_net * rxnet = rxrpc_net ( net ) ;
2022-05-21 10:45:15 +03:00
struct hlist_node * cursor ;
2016-04-04 16:00:35 +03:00
long diff ;
2007-04-27 02:48:28 +04:00
int ret ;
2016-09-13 10:49:05 +03:00
_enter ( " {%d,%d,%pISp} " ,
srx - > transport_type , srx - > transport . family , & srx - > transport ) ;
2007-04-27 02:48:28 +04:00
2017-05-24 19:02:32 +03:00
mutex_lock ( & rxnet - > local_mutex ) ;
2007-04-27 02:48:28 +04:00
2022-05-21 10:45:15 +03:00
hlist_for_each ( cursor , & rxnet - > local_endpoints ) {
local = hlist_entry ( cursor , struct rxrpc_local , link ) ;
2007-04-27 02:48:28 +04:00
2016-04-04 16:00:35 +03:00
diff = rxrpc_local_cmp_key ( local , srx ) ;
2022-05-21 10:45:15 +03:00
if ( diff ! = 0 )
2007-04-27 02:48:28 +04:00
continue ;
2016-04-04 16:00:35 +03:00
/* Services aren't allowed to share transport sockets, so
* reject that here . It is possible that the object is dying -
* but it may also still have the local transport address that
* we want bound .
*/
if ( srx - > srx_service ) {
local = NULL ;
goto addr_in_use ;
}
2007-04-27 02:48:28 +04:00
2022-05-21 10:45:15 +03:00
/* Found a match. We want to replace a dying object.
* Attempting to bind the transport socket may still fail if
* we ' re attempting to use a local address that the dying
* object is still using .
2016-04-04 16:00:35 +03:00
*/
2022-10-21 15:00:34 +03:00
if ( ! rxrpc_use_local ( local , rxrpc_local_use_lookup ) )
2016-04-04 16:00:35 +03:00
break ;
2007-04-27 02:48:28 +04:00
2016-04-04 16:00:35 +03:00
goto found ;
}
2007-04-27 02:48:28 +04:00
2022-10-21 01:17:06 +03:00
local = rxrpc_alloc_local ( net , srx ) ;
2016-04-04 16:00:35 +03:00
if ( ! local )
goto nomem ;
2007-04-27 02:48:28 +04:00
2017-05-24 19:02:32 +03:00
ret = rxrpc_open_socket ( local , net ) ;
2016-04-04 16:00:35 +03:00
if ( ret < 0 )
goto sock_error ;
2022-05-21 10:45:15 +03:00
if ( cursor ) {
hlist_replace_rcu ( cursor , & local - > link ) ;
cursor - > pprev = NULL ;
} else {
hlist_add_head_rcu ( & local - > link , & rxnet - > local_endpoints ) ;
}
2007-04-27 02:48:28 +04:00
2016-04-04 16:00:35 +03:00
found :
2017-05-24 19:02:32 +03:00
mutex_unlock ( & rxnet - > local_mutex ) ;
2016-04-04 16:00:35 +03:00
_leave ( " = %p " , local ) ;
2007-04-27 02:48:28 +04:00
return local ;
2016-04-04 16:00:35 +03:00
nomem :
ret = - ENOMEM ;
sock_error :
2017-05-24 19:02:32 +03:00
mutex_unlock ( & rxnet - > local_mutex ) ;
2019-04-24 19:44:11 +03:00
if ( local )
call_rcu ( & local - > rcu , rxrpc_local_rcu ) ;
2016-04-04 16:00:35 +03:00
_leave ( " = %d " , ret ) ;
return ERR_PTR ( ret ) ;
2007-04-27 02:48:28 +04:00
2016-04-04 16:00:35 +03:00
addr_in_use :
2017-05-24 19:02:32 +03:00
mutex_unlock ( & rxnet - > local_mutex ) ;
2016-04-04 16:00:35 +03:00
_leave ( " = -EADDRINUSE " ) ;
return ERR_PTR ( - EADDRINUSE ) ;
}
2007-04-27 02:48:28 +04:00
2018-03-30 23:05:28 +03:00
/*
* Get a ref on a local endpoint .
*/
2022-10-21 15:00:34 +03:00
struct rxrpc_local * rxrpc_get_local ( struct rxrpc_local * local ,
enum rxrpc_local_trace why )
2018-03-30 23:05:28 +03:00
{
2022-10-21 15:00:34 +03:00
int r , u ;
2018-03-30 23:05:28 +03:00
2022-10-21 15:00:34 +03:00
u = atomic_read ( & local - > active_users ) ;
2022-05-21 10:45:22 +03:00
__refcount_inc ( & local - > ref , & r ) ;
2022-10-21 15:00:34 +03:00
trace_rxrpc_local ( local - > debug_id , why , r + 1 , u ) ;
2018-03-30 23:05:28 +03:00
return local ;
}
/*
* Get a ref on a local endpoint unless its usage has already reached 0.
*/
2022-10-21 15:00:34 +03:00
struct rxrpc_local * rxrpc_get_local_maybe ( struct rxrpc_local * local ,
enum rxrpc_local_trace why )
2018-03-30 23:05:28 +03:00
{
2022-10-21 15:00:34 +03:00
int r , u ;
2018-03-30 23:05:28 +03:00
2022-10-21 15:00:34 +03:00
if ( local & & __refcount_inc_not_zero ( & local - > ref , & r ) ) {
u = atomic_read ( & local - > active_users ) ;
trace_rxrpc_local ( local - > debug_id , why , r + 1 , u ) ;
return local ;
2018-03-30 23:05:28 +03:00
}
2022-10-21 15:00:34 +03:00
return NULL ;
2018-03-30 23:05:28 +03:00
}
/*
* Drop a ref on a local endpoint .
*/
2022-10-21 15:00:34 +03:00
void rxrpc_put_local ( struct rxrpc_local * local , enum rxrpc_local_trace why )
2018-03-30 23:05:28 +03:00
{
2020-01-31 00:50:35 +03:00
unsigned int debug_id ;
2022-05-21 10:45:22 +03:00
bool dead ;
2022-10-21 15:00:34 +03:00
int r , u ;
2018-03-30 23:05:28 +03:00
if ( local ) {
2020-01-31 00:50:35 +03:00
debug_id = local - > debug_id ;
2022-10-21 15:00:34 +03:00
u = atomic_read ( & local - > active_users ) ;
2022-05-21 10:45:22 +03:00
dead = __refcount_dec_and_test ( & local - > ref , & r ) ;
2022-10-21 15:00:34 +03:00
trace_rxrpc_local ( debug_id , why , r , u ) ;
2018-03-30 23:05:28 +03:00
2022-05-21 10:45:22 +03:00
if ( dead )
2019-08-09 17:20:41 +03:00
call_rcu ( & local - > rcu , rxrpc_local_rcu ) ;
2018-03-30 23:05:28 +03:00
}
}
2019-08-09 17:20:41 +03:00
/*
* Start using a local endpoint .
*/
2022-10-21 15:00:34 +03:00
struct rxrpc_local * rxrpc_use_local ( struct rxrpc_local * local ,
enum rxrpc_local_trace why )
2019-08-09 17:20:41 +03:00
{
2022-10-21 15:00:34 +03:00
local = rxrpc_get_local_maybe ( local , rxrpc_local_get_for_use ) ;
2019-08-09 17:20:41 +03:00
if ( ! local )
return NULL ;
2022-10-21 15:00:34 +03:00
if ( ! __rxrpc_use_local ( local , why ) ) {
rxrpc_put_local ( local , rxrpc_local_put_for_use ) ;
2019-08-09 17:20:41 +03:00
return NULL ;
}
return local ;
}
/*
* Cease using a local endpoint . Once the number of active users reaches 0 , we
2020-01-23 16:13:41 +03:00
* start the closure of the transport in the I / O thread . .
2019-08-09 17:20:41 +03:00
*/
2022-10-21 15:00:34 +03:00
void rxrpc_unuse_local ( struct rxrpc_local * local , enum rxrpc_local_trace why )
2019-08-09 17:20:41 +03:00
{
2022-12-15 19:20:04 +03:00
unsigned int debug_id ;
2022-11-16 15:02:22 +03:00
int r , u ;
if ( local ) {
2022-12-15 19:20:04 +03:00
debug_id = local - > debug_id ;
2022-11-16 15:02:22 +03:00
r = refcount_read ( & local - > ref ) ;
u = atomic_dec_return ( & local - > active_users ) ;
trace_rxrpc_local ( debug_id , why , r , u ) ;
if ( u = = 0 )
kthread_stop ( local - > io_thread ) ;
}
2019-08-09 17:20:41 +03:00
}
2007-04-27 02:48:28 +04:00
/*
2016-04-04 16:00:35 +03:00
* Destroy a local endpoint ' s socket and then hand the record to RCU to dispose
* of .
*
* Closing the socket cannot be done from bottom half context or RCU callback
* context because it might sleep .
2007-04-27 02:48:28 +04:00
*/
2022-10-10 10:45:20 +03:00
void rxrpc_destroy_local ( struct rxrpc_local * local )
2007-04-27 02:48:28 +04:00
{
2016-04-04 16:00:35 +03:00
struct socket * socket = local - > socket ;
2017-05-24 19:02:32 +03:00
struct rxrpc_net * rxnet = local - > rxnet ;
2007-04-27 02:48:28 +04:00
2016-04-04 16:00:35 +03:00
_enter ( " %d " , local - > debug_id ) ;
2007-04-27 02:48:28 +04:00
2019-08-29 16:12:11 +03:00
local - > dead = true ;
2017-05-24 19:02:32 +03:00
mutex_lock ( & rxnet - > local_mutex ) ;
2022-05-21 10:45:15 +03:00
hlist_del_init_rcu ( & local - > link ) ;
2017-05-24 19:02:32 +03:00
mutex_unlock ( & rxnet - > local_mutex ) ;
2016-04-04 16:00:35 +03:00
2019-08-29 16:12:11 +03:00
rxrpc_clean_up_local_conns ( local ) ;
rxrpc_service_connection_reaper ( & rxnet - > service_conn_reaper ) ;
2016-09-30 00:37:15 +03:00
ASSERT ( ! local - > service ) ;
2016-04-04 16:00:35 +03:00
if ( socket ) {
local - > socket = NULL ;
kernel_sock_shutdown ( socket , SHUT_RDWR ) ;
socket - > sk - > sk_user_data = NULL ;
sock_release ( socket ) ;
}
/* At this point, there should be no more packets coming in to the
* local endpoint .
*/
2022-10-10 10:45:20 +03:00
rxrpc_purge_queue ( & local - > rx_queue ) ;
2022-10-19 11:45:43 +03:00
rxrpc_purge_client_connections ( local ) ;
2007-04-27 02:48:28 +04:00
}
2016-04-04 16:00:35 +03:00
/*
* Destroy a local endpoint after the RCU grace period expires .
*/
static void rxrpc_local_rcu ( struct rcu_head * rcu )
{
struct rxrpc_local * local = container_of ( rcu , struct rxrpc_local , rcu ) ;
2007-04-27 02:48:28 +04:00
2022-10-21 15:00:34 +03:00
rxrpc_see_local ( local , rxrpc_local_free ) ;
2007-04-27 02:48:28 +04:00
kfree ( local ) ;
}
/*
2016-04-04 16:00:35 +03:00
* Verify the local endpoint list is empty by this point .
2007-04-27 02:48:28 +04:00
*/
2017-05-24 19:02:32 +03:00
void rxrpc_destroy_all_locals ( struct rxrpc_net * rxnet )
2007-04-27 02:48:28 +04:00
{
2016-04-04 16:00:35 +03:00
struct rxrpc_local * local ;
2007-04-27 02:48:28 +04:00
_enter ( " " ) ;
2016-06-27 19:11:19 +03:00
flush_workqueue ( rxrpc_workqueue ) ;
2007-04-27 02:48:28 +04:00
2022-05-21 10:45:15 +03:00
if ( ! hlist_empty ( & rxnet - > local_endpoints ) ) {
2017-05-24 19:02:32 +03:00
mutex_lock ( & rxnet - > local_mutex ) ;
2022-05-21 10:45:15 +03:00
hlist_for_each_entry ( local , & rxnet - > local_endpoints , link ) {
2016-06-27 19:11:19 +03:00
pr_err ( " AF_RXRPC: Leaked local %p {%d} \n " ,
2022-05-21 10:45:22 +03:00
local , refcount_read ( & local - > ref ) ) ;
2016-06-27 19:11:19 +03:00
}
2017-05-24 19:02:32 +03:00
mutex_unlock ( & rxnet - > local_mutex ) ;
2016-06-27 19:11:19 +03:00
BUG ( ) ;
2007-04-27 02:48:28 +04:00
}
}