2019-05-20 17:08:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-04-04 13:00:34 +00:00
|
|
|
/* Local endpoint object management
|
2007-04-26 22:48:28 +00:00
|
|
|
*
|
2016-04-04 13:00:35 +00:00
|
|
|
* Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
|
2007-04-26 22:48:28 +00:00
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*/
|
|
|
|
|
2016-06-02 19:08:52 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2007-04-26 22:48:28 +00: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 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2015-04-01 15:31:26 +00:00
|
|
|
#include <linux/udp.h>
|
|
|
|
#include <linux/ip.h>
|
2016-04-04 13:00:35 +00:00
|
|
|
#include <linux/hashtable.h>
|
2007-04-26 22:48:28 +00:00
|
|
|
#include <net/sock.h>
|
2018-10-04 10:10:51 +00:00
|
|
|
#include <net/udp.h>
|
2007-04-26 22:48:28 +00:00
|
|
|
#include <net/af_rxrpc.h>
|
|
|
|
#include "ar-internal.h"
|
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
static void rxrpc_local_processor(struct work_struct *);
|
|
|
|
static void rxrpc_local_rcu(struct rcu_head *);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
/*
|
2016-04-04 13:00:35 +00: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-26 22:48:28 +00:00
|
|
|
*/
|
2016-04-04 13:00:35 +00: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 06:26:01 +00:00
|
|
|
#ifdef CONFIG_AF_RXRPC_IPV6
|
2016-09-13 07:49:05 +00: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 06:26:01 +00:00
|
|
|
#endif
|
2016-04-04 13:00:35 +00:00
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a new local endpoint.
|
|
|
|
*/
|
2017-05-24 16:02:32 +00:00
|
|
|
static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
|
|
|
|
const struct sockaddr_rxrpc *srx)
|
2007-04-26 22:48:28 +00:00
|
|
|
{
|
|
|
|
struct rxrpc_local *local;
|
|
|
|
|
|
|
|
local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
|
|
|
|
if (local) {
|
2016-04-04 13:00:35 +00:00
|
|
|
atomic_set(&local->usage, 1);
|
2019-08-09 14:20:41 +00:00
|
|
|
atomic_set(&local->active_users, 1);
|
2017-05-24 16:02:32 +00:00
|
|
|
local->rxnet = rxnet;
|
2007-04-26 22:48:28 +00:00
|
|
|
INIT_LIST_HEAD(&local->link);
|
2016-04-04 13:00:35 +00:00
|
|
|
INIT_WORK(&local->processor, rxrpc_local_processor);
|
2007-04-26 22:48:28 +00:00
|
|
|
init_rwsem(&local->defrag_sem);
|
|
|
|
skb_queue_head_init(&local->reject_queue);
|
2015-04-01 15:31:26 +00:00
|
|
|
skb_queue_head_init(&local->event_queue);
|
2016-06-17 14:42:35 +00:00
|
|
|
local->client_conns = RB_ROOT;
|
|
|
|
spin_lock_init(&local->client_conns_lock);
|
2007-04-26 22:48:28 +00: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 13:30:49 +00:00
|
|
|
local->srx.srx_service = 0;
|
2019-08-13 21:26:36 +00:00
|
|
|
trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
|
2007-04-26 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_leave(" = %p", local);
|
|
|
|
return local;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create the local socket
|
2016-04-04 13:00:35 +00:00
|
|
|
* - must be called with rxrpc_local_mutex locked
|
2007-04-26 22:48:28 +00:00
|
|
|
*/
|
2017-05-24 16:02:32 +00:00
|
|
|
static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
|
2007-04-26 22:48:28 +00:00
|
|
|
{
|
2018-10-04 10:10:51 +00:00
|
|
|
struct sock *usk;
|
2020-05-28 05:12:32 +00:00
|
|
|
int ret;
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-09-13 07:49:05 +00:00
|
|
|
_enter("%p{%d,%d}",
|
|
|
|
local, local->srx.transport_type, local->srx.transport.family);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
/* create a socket to represent the local endpoint */
|
2017-05-24 16:02:32 +00:00
|
|
|
ret = sock_create_kern(net, local->srx.transport.family,
|
2016-09-13 07:49:05 +00:00
|
|
|
local->srx.transport_type, 0, &local->socket);
|
2007-04-26 22:48:28 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
_leave(" = %d [socket]", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-05 13:05:35 +00:00
|
|
|
/* set the socket up */
|
2018-10-04 10:10:51 +00:00
|
|
|
usk = local->socket->sk;
|
|
|
|
inet_sk(usk)->mc_loop = 0;
|
|
|
|
|
|
|
|
/* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
|
|
|
|
inet_inc_convert_csum(usk);
|
|
|
|
|
|
|
|
rcu_assign_sk_user_data(usk, local);
|
|
|
|
|
|
|
|
udp_sk(usk)->encap_type = UDP_ENCAP_RXRPC;
|
|
|
|
udp_sk(usk)->encap_rcv = rxrpc_input_packet;
|
|
|
|
udp_sk(usk)->encap_destroy = NULL;
|
|
|
|
udp_sk(usk)->gro_receive = NULL;
|
|
|
|
udp_sk(usk)->gro_complete = NULL;
|
|
|
|
|
|
|
|
udp_encap_enable();
|
2018-10-12 15:38:36 +00:00
|
|
|
#if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
|
2018-10-04 10:10:51 +00:00
|
|
|
if (local->srx.transport.family == AF_INET6)
|
|
|
|
udpv6_encap_enable();
|
|
|
|
#endif
|
|
|
|
usk->sk_error_report = rxrpc_error_report;
|
2018-10-05 13:05:35 +00:00
|
|
|
|
2007-04-26 22:48:28 +00:00
|
|
|
/* if a local address was supplied then bind it */
|
|
|
|
if (local->srx.transport_len > sizeof(sa_family_t)) {
|
|
|
|
_debug("bind");
|
|
|
|
ret = kernel_bind(local->socket,
|
2016-04-04 13:00:35 +00:00
|
|
|
(struct sockaddr *)&local->srx.transport,
|
2007-04-26 22:48:28 +00:00
|
|
|
local->srx.transport_len);
|
|
|
|
if (ret < 0) {
|
2016-04-04 13:00:35 +00:00
|
|
|
_debug("bind failed %d", ret);
|
2007-04-26 22:48:28 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:26:00 +00:00
|
|
|
switch (local->srx.transport.family) {
|
2018-09-27 14:13:09 +00:00
|
|
|
case AF_INET6:
|
|
|
|
/* we want to receive ICMPv6 errors */
|
2020-05-28 05:12:32 +00:00
|
|
|
ip6_sock_set_recverr(local->socket->sk);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2018-09-27 14:13:09 +00:00
|
|
|
/* Fall through and set IPv4 options too otherwise we don't get
|
|
|
|
* errors from IPv4 packets sent through the IPv6 socket.
|
|
|
|
*/
|
2019-04-01 19:39:31 +00:00
|
|
|
/* Fall through */
|
2018-09-27 14:13:09 +00:00
|
|
|
case AF_INET:
|
2018-05-10 22:26:00 +00:00
|
|
|
/* we want to receive ICMP errors */
|
2020-05-28 05:12:28 +00:00
|
|
|
ip_sock_set_recverr(local->socket->sk);
|
2018-05-10 22:26:00 +00:00
|
|
|
|
|
|
|
/* we want to set the don't fragment bit */
|
2020-05-28 05:12:29 +00:00
|
|
|
ip_sock_set_mtu_discover(local->socket->sk, IP_PMTUDISC_DO);
|
2018-09-27 14:13:08 +00:00
|
|
|
|
|
|
|
/* We want receive timestamps. */
|
2020-05-28 05:12:14 +00:00
|
|
|
sock_enable_timestamps(local->socket->sk);
|
2018-05-10 22:26:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BUG();
|
2007-04-26 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_leave(" = 0");
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2007-11-13 02:10:39 +00:00
|
|
|
kernel_sock_shutdown(local->socket, SHUT_RDWR);
|
2007-04-26 22:48:28 +00:00
|
|
|
local->socket->sk->sk_user_data = NULL;
|
|
|
|
sock_release(local->socket);
|
|
|
|
local->socket = NULL;
|
|
|
|
|
|
|
|
_leave(" = %d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-04-04 13:00:35 +00:00
|
|
|
* Look up or create a new local endpoint using the specified local address.
|
2007-04-26 22:48:28 +00:00
|
|
|
*/
|
2017-05-24 16:02:32 +00:00
|
|
|
struct rxrpc_local *rxrpc_lookup_local(struct net *net,
|
|
|
|
const struct sockaddr_rxrpc *srx)
|
2007-04-26 22:48:28 +00:00
|
|
|
{
|
|
|
|
struct rxrpc_local *local;
|
2017-05-24 16:02:32 +00:00
|
|
|
struct rxrpc_net *rxnet = rxrpc_net(net);
|
2016-04-04 13:00:35 +00:00
|
|
|
struct list_head *cursor;
|
|
|
|
const char *age;
|
|
|
|
long diff;
|
2007-04-26 22:48:28 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-09-13 07:49:05 +00:00
|
|
|
_enter("{%d,%d,%pISp}",
|
|
|
|
srx->transport_type, srx->transport.family, &srx->transport);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2017-05-24 16:02:32 +00:00
|
|
|
mutex_lock(&rxnet->local_mutex);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2017-05-24 16:02:32 +00:00
|
|
|
for (cursor = rxnet->local_endpoints.next;
|
|
|
|
cursor != &rxnet->local_endpoints;
|
2016-04-04 13:00:35 +00:00
|
|
|
cursor = cursor->next) {
|
|
|
|
local = list_entry(cursor, struct rxrpc_local, link);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
diff = rxrpc_local_cmp_key(local, srx);
|
|
|
|
if (diff < 0)
|
2007-04-26 22:48:28 +00:00
|
|
|
continue;
|
2016-04-04 13:00:35 +00:00
|
|
|
if (diff > 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* 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-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
/* Found a match. We 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.
|
|
|
|
*/
|
2019-08-09 14:20:41 +00:00
|
|
|
if (!rxrpc_use_local(local))
|
2016-04-04 13:00:35 +00:00
|
|
|
break;
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
age = "old";
|
|
|
|
goto found;
|
|
|
|
}
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2017-05-24 16:02:32 +00:00
|
|
|
local = rxrpc_alloc_local(rxnet, srx);
|
2016-04-04 13:00:35 +00:00
|
|
|
if (!local)
|
|
|
|
goto nomem;
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2017-05-24 16:02:32 +00:00
|
|
|
ret = rxrpc_open_socket(local, net);
|
2016-04-04 13:00:35 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto sock_error;
|
|
|
|
|
2019-08-09 14:20:41 +00:00
|
|
|
if (cursor != &rxnet->local_endpoints)
|
2019-08-12 22:30:06 +00:00
|
|
|
list_replace_init(cursor, &local->link);
|
2019-08-09 14:20:41 +00:00
|
|
|
else
|
|
|
|
list_add_tail(&local->link, cursor);
|
2016-04-04 13:00:35 +00:00
|
|
|
age = "new";
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
found:
|
2017-05-24 16:02:32 +00:00
|
|
|
mutex_unlock(&rxnet->local_mutex);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-09-13 07:49:05 +00:00
|
|
|
_net("LOCAL %s %d {%pISp}",
|
|
|
|
age, local->debug_id, &local->srx.transport);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
_leave(" = %p", local);
|
2007-04-26 22:48:28 +00:00
|
|
|
return local;
|
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
nomem:
|
|
|
|
ret = -ENOMEM;
|
|
|
|
sock_error:
|
2017-05-24 16:02:32 +00:00
|
|
|
mutex_unlock(&rxnet->local_mutex);
|
2019-04-24 16:44:11 +00:00
|
|
|
if (local)
|
|
|
|
call_rcu(&local->rcu, rxrpc_local_rcu);
|
2016-04-04 13:00:35 +00:00
|
|
|
_leave(" = %d", ret);
|
|
|
|
return ERR_PTR(ret);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
addr_in_use:
|
2017-05-24 16:02:32 +00:00
|
|
|
mutex_unlock(&rxnet->local_mutex);
|
2016-04-04 13:00:35 +00:00
|
|
|
_leave(" = -EADDRINUSE");
|
|
|
|
return ERR_PTR(-EADDRINUSE);
|
|
|
|
}
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2018-03-30 20:05:28 +00:00
|
|
|
/*
|
|
|
|
* Get a ref on a local endpoint.
|
|
|
|
*/
|
|
|
|
struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
|
|
|
|
{
|
|
|
|
const void *here = __builtin_return_address(0);
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = atomic_inc_return(&local->usage);
|
2019-08-13 21:26:36 +00:00
|
|
|
trace_rxrpc_local(local->debug_id, rxrpc_local_got, n, here);
|
2018-03-30 20:05:28 +00:00
|
|
|
return local;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a ref on a local endpoint unless its usage has already reached 0.
|
|
|
|
*/
|
|
|
|
struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
|
|
|
|
{
|
|
|
|
const void *here = __builtin_return_address(0);
|
|
|
|
|
|
|
|
if (local) {
|
atomics/treewide: Rename __atomic_add_unless() => atomic_fetch_add_unless()
While __atomic_add_unless() was originally intended as a building-block
for atomic_add_unless(), it's now used in a number of places around the
kernel. It's the only common atomic operation named __atomic*(), rather
than atomic_*(), and for consistency it would be better named
atomic_fetch_add_unless().
This lack of consistency is slightly confusing, and gets in the way of
scripting atomics. Given that, let's clean things up and promote it to
an official part of the atomics API, in the form of
atomic_fetch_add_unless().
This patch converts definitions and invocations over to the new name,
including the instrumented version, using the following script:
----
git grep -w __atomic_add_unless | while read line; do
sed -i '{s/\<__atomic_add_unless\>/atomic_fetch_add_unless/}' "${line%%:*}";
done
git grep -w __arch_atomic_add_unless | while read line; do
sed -i '{s/\<__arch_atomic_add_unless\>/arch_atomic_fetch_add_unless/}' "${line%%:*}";
done
----
Note that we do not have atomic{64,_long}_fetch_add_unless(), which will
be introduced by later patches.
There should be no functional change as a result of this patch.
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Palmer Dabbelt <palmer@sifive.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/lkml/20180621121321.4761-2-mark.rutland@arm.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2018-06-21 12:13:04 +00:00
|
|
|
int n = atomic_fetch_add_unless(&local->usage, 1, 0);
|
2018-03-30 20:05:28 +00:00
|
|
|
if (n > 0)
|
2019-08-13 21:26:36 +00:00
|
|
|
trace_rxrpc_local(local->debug_id, rxrpc_local_got,
|
|
|
|
n + 1, here);
|
2018-03-30 20:05:28 +00:00
|
|
|
else
|
|
|
|
local = NULL;
|
|
|
|
}
|
|
|
|
return local;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-08-13 21:26:36 +00:00
|
|
|
* Queue a local endpoint and pass the caller's reference to the work item.
|
2018-03-30 20:05:28 +00:00
|
|
|
*/
|
|
|
|
void rxrpc_queue_local(struct rxrpc_local *local)
|
|
|
|
{
|
|
|
|
const void *here = __builtin_return_address(0);
|
2019-08-13 21:26:36 +00:00
|
|
|
unsigned int debug_id = local->debug_id;
|
|
|
|
int n = atomic_read(&local->usage);
|
2018-03-30 20:05:28 +00:00
|
|
|
|
|
|
|
if (rxrpc_queue_work(&local->processor))
|
2019-08-13 21:26:36 +00:00
|
|
|
trace_rxrpc_local(debug_id, rxrpc_local_queued, n, here);
|
2019-08-09 14:20:41 +00:00
|
|
|
else
|
|
|
|
rxrpc_put_local(local);
|
2007-04-26 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 20:05:28 +00:00
|
|
|
/*
|
|
|
|
* Drop a ref on a local endpoint.
|
|
|
|
*/
|
|
|
|
void rxrpc_put_local(struct rxrpc_local *local)
|
|
|
|
{
|
|
|
|
const void *here = __builtin_return_address(0);
|
2020-01-30 21:50:35 +00:00
|
|
|
unsigned int debug_id;
|
2018-03-30 20:05:28 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
if (local) {
|
2020-01-30 21:50:35 +00:00
|
|
|
debug_id = local->debug_id;
|
|
|
|
|
2018-03-30 20:05:28 +00:00
|
|
|
n = atomic_dec_return(&local->usage);
|
2020-01-30 21:50:35 +00:00
|
|
|
trace_rxrpc_local(debug_id, rxrpc_local_put, n, here);
|
2018-03-30 20:05:28 +00:00
|
|
|
|
|
|
|
if (n == 0)
|
2019-08-09 14:20:41 +00:00
|
|
|
call_rcu(&local->rcu, rxrpc_local_rcu);
|
2018-03-30 20:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:20:41 +00:00
|
|
|
/*
|
|
|
|
* Start using a local endpoint.
|
|
|
|
*/
|
|
|
|
struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
|
|
|
|
{
|
|
|
|
local = rxrpc_get_local_maybe(local);
|
|
|
|
if (!local)
|
|
|
|
return NULL;
|
|
|
|
|
2020-01-30 21:50:36 +00:00
|
|
|
if (!__rxrpc_use_local(local)) {
|
2019-08-09 14:20:41 +00:00
|
|
|
rxrpc_put_local(local);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return local;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cease using a local endpoint. Once the number of active users reaches 0, we
|
|
|
|
* start the closure of the transport in the work processor.
|
|
|
|
*/
|
|
|
|
void rxrpc_unuse_local(struct rxrpc_local *local)
|
|
|
|
{
|
2019-08-09 21:47:47 +00:00
|
|
|
if (local) {
|
2020-01-30 21:50:36 +00:00
|
|
|
if (__rxrpc_unuse_local(local)) {
|
|
|
|
rxrpc_get_local(local);
|
2019-08-09 21:47:47 +00:00
|
|
|
rxrpc_queue_local(local);
|
2020-01-30 21:50:36 +00:00
|
|
|
}
|
2019-08-09 21:47:47 +00:00
|
|
|
}
|
2019-08-09 14:20:41 +00:00
|
|
|
}
|
|
|
|
|
2007-04-26 22:48:28 +00:00
|
|
|
/*
|
2016-04-04 13:00:35 +00: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-26 22:48:28 +00:00
|
|
|
*/
|
2016-04-04 13:00:35 +00:00
|
|
|
static void rxrpc_local_destroyer(struct rxrpc_local *local)
|
2007-04-26 22:48:28 +00:00
|
|
|
{
|
2016-04-04 13:00:35 +00:00
|
|
|
struct socket *socket = local->socket;
|
2017-05-24 16:02:32 +00:00
|
|
|
struct rxrpc_net *rxnet = local->rxnet;
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
_enter("%d", local->debug_id);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2019-08-29 13:12:11 +00:00
|
|
|
local->dead = true;
|
|
|
|
|
2017-05-24 16:02:32 +00:00
|
|
|
mutex_lock(&rxnet->local_mutex);
|
2016-04-04 13:00:35 +00:00
|
|
|
list_del_init(&local->link);
|
2017-05-24 16:02:32 +00:00
|
|
|
mutex_unlock(&rxnet->local_mutex);
|
2016-04-04 13:00:35 +00:00
|
|
|
|
2019-08-29 13:12:11 +00:00
|
|
|
rxrpc_clean_up_local_conns(local);
|
|
|
|
rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
|
2016-09-29 21:37:15 +00:00
|
|
|
ASSERT(!local->service);
|
2016-04-04 13:00:35 +00: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.
|
|
|
|
*/
|
|
|
|
rxrpc_purge_queue(&local->reject_queue);
|
|
|
|
rxrpc_purge_queue(&local->event_queue);
|
2007-04-26 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-08-09 14:20:41 +00:00
|
|
|
* Process events on an endpoint. The work item carries a ref which
|
|
|
|
* we must release.
|
2007-04-26 22:48:28 +00:00
|
|
|
*/
|
2016-04-04 13:00:35 +00:00
|
|
|
static void rxrpc_local_processor(struct work_struct *work)
|
2007-04-26 22:48:28 +00:00
|
|
|
{
|
|
|
|
struct rxrpc_local *local =
|
2016-04-04 13:00:35 +00:00
|
|
|
container_of(work, struct rxrpc_local, processor);
|
|
|
|
bool again;
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2019-08-13 21:26:36 +00:00
|
|
|
trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
|
2018-03-30 20:05:28 +00:00
|
|
|
atomic_read(&local->usage), NULL);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
do {
|
|
|
|
again = false;
|
2020-01-30 21:50:36 +00:00
|
|
|
if (!__rxrpc_use_local(local)) {
|
2019-08-09 14:20:41 +00:00
|
|
|
rxrpc_local_destroyer(local);
|
|
|
|
break;
|
|
|
|
}
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
if (!skb_queue_empty(&local->reject_queue)) {
|
|
|
|
rxrpc_reject_packets(local);
|
|
|
|
again = true;
|
|
|
|
}
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
if (!skb_queue_empty(&local->event_queue)) {
|
|
|
|
rxrpc_process_local_events(local);
|
|
|
|
again = true;
|
|
|
|
}
|
2020-01-30 21:50:36 +00:00
|
|
|
|
|
|
|
__rxrpc_unuse_local(local);
|
2016-04-04 13:00:35 +00:00
|
|
|
} while (again);
|
2019-08-09 14:20:41 +00:00
|
|
|
|
|
|
|
rxrpc_put_local(local);
|
2016-04-04 13:00:35 +00:00
|
|
|
}
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00: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-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
_enter("%d", local->debug_id);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2016-04-04 13:00:35 +00:00
|
|
|
ASSERT(!work_pending(&local->processor));
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
_net("DESTROY LOCAL %d", local->debug_id);
|
|
|
|
kfree(local);
|
|
|
|
_leave("");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-04-04 13:00:35 +00:00
|
|
|
* Verify the local endpoint list is empty by this point.
|
2007-04-26 22:48:28 +00:00
|
|
|
*/
|
2017-05-24 16:02:32 +00:00
|
|
|
void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
|
2007-04-26 22:48:28 +00:00
|
|
|
{
|
2016-04-04 13:00:35 +00:00
|
|
|
struct rxrpc_local *local;
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
2016-06-27 16:11:19 +00:00
|
|
|
flush_workqueue(rxrpc_workqueue);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2017-05-24 16:02:32 +00:00
|
|
|
if (!list_empty(&rxnet->local_endpoints)) {
|
|
|
|
mutex_lock(&rxnet->local_mutex);
|
|
|
|
list_for_each_entry(local, &rxnet->local_endpoints, link) {
|
2016-06-27 16:11:19 +00:00
|
|
|
pr_err("AF_RXRPC: Leaked local %p {%d}\n",
|
|
|
|
local, atomic_read(&local->usage));
|
|
|
|
}
|
2017-05-24 16:02:32 +00:00
|
|
|
mutex_unlock(&rxnet->local_mutex);
|
2016-06-27 16:11:19 +00:00
|
|
|
BUG();
|
2007-04-26 22:48:28 +00:00
|
|
|
}
|
|
|
|
}
|