cc940e8f69
Issue Description: deadlock happens when runnging iperf TCP tx on usrsock with the following commands: device command: iperf2 -c <PC IP address> -u -i 1 -p 1122 -t 10 PC command: iperf -s -u -i 1 -p 1122 iperf's thread holds net_lock before call usrsock_setup_request_callback and waits for conn->resp.sem by calling nxsem_wait_uninterruptible(&conn->resp.sem), while conn->resp.sem can only be signaled in usrsock_response, who is waiting for net_lock in tptun's thread. Solution: call net_lockedwait_uninterruptible to make sure net_lock can be released when conn->resp.sem is not available. Here's the backtrace: up_block_task nuttx/arch/arm/src/common/arm_blocktask.c:151 nxsem_wait nuttx/sched/semaphore/sem_wait.c:185 (discriminator 2) nxsem_wait_uninterruptible nuttx/sched/semaphore/sem_wait.c:223 (discriminator 1) usrsock_setup_request_callback nuttx/net/usrsock/usrsock_conn.c:228 usrsock_setup_data_request_callback nuttx/net/usrsock/usrsock_conn.c:257 usrsock_getsockopt nuttx/net/usrsock/usrsock_getsockopt.c:202 psock_getsockopt nuttx/net/socket/getsockopt.c:448 getsockopt nuttx/net/socket/getsockopt.c:515 getsock_tcp_windowsize external/iperf2/iperf2/src/tcp_window_size.c:165 reporter_reportsettings external/iperf2/iperf2/src/ReportDefault.c:384 reporter_print external/iperf2/iperf2/src/Reporter.c:1402 reporter_process_report external/iperf2/iperf2/src/Reporter.c:860 reporter_process_report external/iperf2/iperf2/src/Reporter.c:828 reporter_spawn external/iperf2/iperf2/src/Reporter.c:720 thread_run_wrapper external/iperf2/iperf2/compat/Thread.c:270 pthread_startup nuttx/libs/libc/pthread/pthread_create.c:59 (discriminator 2) pthread_start nuttx/sched/pthread/pthread_create.c:182 (discriminator 4) up_block_task nuttx/arch/arm/src/common/arm_blocktask.c:151 nxsem_wait nuttx/sched/semaphore/sem_wait.c:185 (discriminator 2) sem_wait nuttx/sched/semaphore/sem_wait.c:271 nxmutex_lock nuttx/include/nuttx/mutex.h:165 nxrmutex_lock nuttx/include/nuttx/mutex.h:369 usrsock_handle_req_response nuttx/net/usrsock/usrsock_devif.c:466 usrsockdev_write nuttx/drivers/usrsock/usrsock_dev.c:304 file_write nuttx/fs/vfs/fs_write.c:90 usrsock_rpmsg_default_handler apps/netutils/usrsock_rpmsg/usrsock_rpmsg_client.c:137 rpmsg_virtio_rx_callback nuttx/openamp/open-amp/lib/rpmsg/rpmsg_virtio.c:371 virtqueue_notification nuttx/openamp/open-amp/lib/virtio/virtqueue.c:706 rproc_virtio_notified nuttx/openamp/open-amp/lib/remoteproc/remoteproc_virtio.c:337 (discriminator 2) remoteproc_get_notification nuttx/openamp/open-amp/lib/remoteproc/remoteproc.c:999 rptun_worker nuttx/drivers/rptun/rptun.c:333 rptun_thread nuttx/drivers/rptun/rptun.c:364 (discriminator 1) nxtask_start nuttx/sched/task/task_start.c:144 Signed-off-by: liangchaozhong <liangchaozhong@xiaomi.com>
334 lines
9.4 KiB
C
334 lines
9.4 KiB
C
/****************************************************************************
|
|
* net/usrsock/usrsock_conn.c
|
|
*
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
* this work for additional information regarding copyright ownership. The
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
* "License"); you may not use this file except in compliance with the
|
|
* License. You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
* License for the specific language governing permissions and limitations
|
|
* under the License.
|
|
*
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <nuttx/config.h>
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_USRSOCK)
|
|
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <debug.h>
|
|
|
|
#include <arch/irq.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
#include <nuttx/mutex.h>
|
|
#include <nuttx/semaphore.h>
|
|
#include <nuttx/net/netconfig.h>
|
|
#include <nuttx/net/net.h>
|
|
#include <nuttx/net/usrsock.h>
|
|
|
|
#include "usrsock/usrsock.h"
|
|
|
|
/****************************************************************************
|
|
* Private Data
|
|
****************************************************************************/
|
|
|
|
/* The array containing all usrsock connections. */
|
|
|
|
#ifndef CONFIG_NET_ALLOC_CONNS
|
|
static struct usrsock_conn_s g_usrsock_connections[CONFIG_NET_USRSOCK_CONNS];
|
|
#endif
|
|
|
|
/* A list of all free usrsock connections */
|
|
|
|
static dq_queue_t g_free_usrsock_connections;
|
|
static mutex_t g_free_lock = NXMUTEX_INITIALIZER;
|
|
|
|
/* A list of all allocated usrsock connections */
|
|
|
|
static dq_queue_t g_active_usrsock_connections;
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: usrsock_alloc()
|
|
*
|
|
* Description:
|
|
* Allocate a new, uninitialized usrsock connection structure. This is
|
|
* normally something done by the implementation of the socket() API
|
|
*
|
|
****************************************************************************/
|
|
|
|
FAR struct usrsock_conn_s *usrsock_alloc(void)
|
|
{
|
|
FAR struct usrsock_conn_s *conn;
|
|
#ifdef CONFIG_NET_ALLOC_CONNS
|
|
int i;
|
|
#endif
|
|
|
|
/* The free list is protected by a a mutex. */
|
|
|
|
nxmutex_lock(&g_free_lock);
|
|
#ifdef CONFIG_NET_ALLOC_CONNS
|
|
if (dq_peek(&g_free_usrsock_connections) == NULL)
|
|
{
|
|
conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_USRSOCK_CONNS);
|
|
if (conn != NULL)
|
|
{
|
|
for (i = 0; i < CONFIG_NET_USRSOCK_CONNS; i++)
|
|
{
|
|
dq_addlast(&conn[i].sconn.node, &g_free_usrsock_connections);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
conn = (FAR struct usrsock_conn_s *)
|
|
dq_remfirst(&g_free_usrsock_connections);
|
|
if (conn)
|
|
{
|
|
/* Make sure that the connection is marked as uninitialized */
|
|
|
|
nxsem_init(&conn->resp.sem, 0, 1);
|
|
conn->usockid = -1;
|
|
conn->state = USRSOCK_CONN_STATE_UNINITIALIZED;
|
|
|
|
/* Enqueue the connection into the active list */
|
|
|
|
dq_addlast(&conn->sconn.node, &g_active_usrsock_connections);
|
|
}
|
|
|
|
nxmutex_unlock(&g_free_lock);
|
|
return conn;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: usrsock_free()
|
|
*
|
|
* Description:
|
|
* Free a usrsock connection structure that is no longer in use. This
|
|
* should be done by the implementation of close().
|
|
*
|
|
****************************************************************************/
|
|
|
|
void usrsock_free(FAR struct usrsock_conn_s *conn)
|
|
{
|
|
/* The free list is protected by a mutex. */
|
|
|
|
DEBUGASSERT(conn->crefs == 0);
|
|
|
|
nxmutex_lock(&g_free_lock);
|
|
|
|
/* Remove the connection from the active list */
|
|
|
|
dq_rem(&conn->sconn.node, &g_active_usrsock_connections);
|
|
|
|
/* Reset structure */
|
|
|
|
nxsem_destroy(&conn->resp.sem);
|
|
memset(conn, 0, sizeof(*conn));
|
|
|
|
/* Free the connection */
|
|
|
|
dq_addlast(&conn->sconn.node, &g_free_usrsock_connections);
|
|
nxmutex_unlock(&g_free_lock);
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: usrsock_nextconn()
|
|
*
|
|
* Description:
|
|
* Traverse the list of allocated usrsock connections
|
|
*
|
|
* Assumptions:
|
|
* This function is called from usrsock device logic.
|
|
*
|
|
****************************************************************************/
|
|
|
|
FAR struct usrsock_conn_s *usrsock_nextconn(FAR struct usrsock_conn_s *conn)
|
|
{
|
|
if (!conn)
|
|
{
|
|
return (FAR struct usrsock_conn_s *)g_active_usrsock_connections.head;
|
|
}
|
|
else
|
|
{
|
|
return (FAR struct usrsock_conn_s *)conn->sconn.node.flink;
|
|
}
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: usrsock_active()
|
|
*
|
|
* Description:
|
|
* Find a connection structure that is the appropriate
|
|
* connection for usrsock
|
|
*
|
|
****************************************************************************/
|
|
|
|
FAR struct usrsock_conn_s *usrsock_active(int16_t usockid)
|
|
{
|
|
FAR struct usrsock_conn_s *conn = NULL;
|
|
|
|
while ((conn = usrsock_nextconn(conn)) != NULL)
|
|
{
|
|
if (conn->usockid == usockid)
|
|
{
|
|
return conn;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: usrsock_setup_request_callback()
|
|
****************************************************************************/
|
|
|
|
int usrsock_setup_request_callback(FAR struct usrsock_conn_s *conn,
|
|
FAR struct usrsock_reqstate_s *pstate,
|
|
FAR devif_callback_event_t event,
|
|
uint16_t flags)
|
|
{
|
|
int ret = -EBUSY;
|
|
|
|
nxsem_init(&pstate->recvsem, 0, 0);
|
|
|
|
pstate->conn = conn;
|
|
pstate->result = -EAGAIN;
|
|
pstate->completed = false;
|
|
pstate->unlock = false;
|
|
|
|
/* Set up the callback in the connection */
|
|
|
|
pstate->cb = devif_callback_alloc(NULL, &conn->sconn.list,
|
|
&conn->sconn.list_tail);
|
|
if (pstate->cb)
|
|
{
|
|
/* Take a lock since only one outstanding request is allowed */
|
|
|
|
if ((flags & USRSOCK_EVENT_REQ_COMPLETE) != 0)
|
|
{
|
|
net_sem_wait_uninterruptible(&conn->resp.sem);
|
|
pstate->unlock = true;
|
|
}
|
|
|
|
/* Set up the connection event handler */
|
|
|
|
pstate->cb->flags = flags;
|
|
pstate->cb->priv = (FAR void *)pstate;
|
|
pstate->cb->event = event;
|
|
|
|
ret = OK;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: usrsock_setup_data_request_callback()
|
|
****************************************************************************/
|
|
|
|
int usrsock_setup_data_request_callback(
|
|
FAR struct usrsock_conn_s *conn,
|
|
FAR struct usrsock_data_reqstate_s *pstate,
|
|
FAR devif_callback_event_t event,
|
|
uint16_t flags)
|
|
{
|
|
pstate->valuelen = 0;
|
|
pstate->valuelen_nontrunc = 0;
|
|
return usrsock_setup_request_callback(conn, &pstate->reqstate, event,
|
|
flags);
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: usrsock_teardown_request_callback()
|
|
****************************************************************************/
|
|
|
|
void usrsock_teardown_request_callback(FAR struct usrsock_reqstate_s *pstate)
|
|
{
|
|
FAR struct usrsock_conn_s *conn = pstate->conn;
|
|
|
|
if (pstate->unlock)
|
|
{
|
|
nxsem_post(&conn->resp.sem);
|
|
}
|
|
|
|
/* Make sure that no further events are processed */
|
|
|
|
devif_conn_callback_free(NULL, pstate->cb, &conn->sconn.list,
|
|
&conn->sconn.list_tail);
|
|
nxsem_destroy(&pstate->recvsem);
|
|
|
|
pstate->cb = NULL;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: usrsock_setup_datain
|
|
****************************************************************************/
|
|
|
|
void usrsock_setup_datain(FAR struct usrsock_conn_s *conn,
|
|
FAR struct iovec *iov, unsigned int iovcnt)
|
|
{
|
|
unsigned int i;
|
|
|
|
conn->resp.datain.iov = iov;
|
|
conn->resp.datain.pos = 0;
|
|
conn->resp.datain.total = 0;
|
|
conn->resp.datain.iovcnt = iovcnt;
|
|
|
|
for (i = 0; i < iovcnt; i++)
|
|
{
|
|
conn->resp.datain.total += iov[i].iov_len;
|
|
}
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: usrsock_initialize()
|
|
*
|
|
* Description:
|
|
* Initialize the User Socket connection structures. Called once and only
|
|
* from the networking layer.
|
|
*
|
|
****************************************************************************/
|
|
|
|
void usrsock_initialize(void)
|
|
{
|
|
#ifndef CONFIG_NET_ALLOC_CONNS
|
|
FAR struct usrsock_conn_s *conn;
|
|
int i;
|
|
|
|
for (i = 0; i < CONFIG_NET_USRSOCK_CONNS; i++)
|
|
{
|
|
conn = &g_usrsock_connections[i];
|
|
|
|
/* Mark the connection closed and move it to the free list */
|
|
|
|
conn->usockid = -1;
|
|
conn->state = USRSOCK_CONN_STATE_UNINITIALIZED;
|
|
dq_addlast(&conn->sconn.node, &g_free_usrsock_connections);
|
|
}
|
|
#endif
|
|
|
|
/* Register /dev/usrsock character device. */
|
|
|
|
usrsock_register();
|
|
}
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_USRSOCK */
|