Skip to contentSkip to conversation

port network layer onto glib

PATCH Superseded Liu Ping Fan 4 participants 25 messages Jun 13, 2013 – Jul 3, 2013 Networking

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover port network layer onto glib

Currently, I drop slirp and frontend related code (I guess I can reuse some code by mdroth's QContext later). And this series only resolve the net core re-entrant problem.

v1->v2:
introduce netqueue re-entrant detection and defer it to BH

Liu Ping Fan (6):
net: introduce lock to protect NetQueue
net: introduce lock to protect NetClientState's peer's access
net: make netclient re-entrant with refcnt
net: force NetQue opaque to be NetClientState
net: defer nested call to BH
net: hub use lock to protect ports list

 hw/core/qdev-properties-system.c |  14 +++++
 include/net/net.h                |  10 ++++
 include/net/queue.h              |   2 +-
 net/hub.c                        |  28 ++++++++-
 net/net.c                        | 124 +++++++++++++++++++++++++++++++++++++--
 net/queue.c                      |  57 ++++++++++++++++--
 net/slirp.c                      |   3 +-
 7 files changed, 225 insertions(+), 13 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v2 1/6 net: introduce lock to protect NetQueue

From: Liu Ping Fan <[email protected]>

NetQueue will be accessed by nc and its peers at the same time, need lock to protect it.

---
 include/net/net.h |  1 +
 net/queue.c       | 13 +++++++++++++
 2 files changed, 14 insertions(+)
include/net/net.h +1 -0
index 43d85a1..2f72b26 100644
@@ -5,6 +5,7 @@
 #include "qemu-common.h"
 #include "qapi/qmp/qdict.h"
 #include "qemu/option.h"
+#include "qemu/thread.h"
 #include "net/queue.h"
 #include "migration/vmstate.h"
 #include "qapi-types.h"
net/queue.c +13 -0
index 859d02a..c6d4241 100644
@@ -53,6 +53,7 @@ struct NetQueue {
     uint32_t nq_maxlen;
     uint32_t nq_count;
 
+    QemuMutex lock;
     QTAILQ_HEAD(packets, NetPacket) packets;
 
     unsigned delivering : 1;
@@ -68,6 +69,7 @@ NetQueue *qemu_new_net_queue(void *opaque)
     queue->nq_maxlen = 10000;
     queue->nq_count = 0;
 
+    qemu_mutex_init(&queue->lock);
     QTAILQ_INIT(&queue->packets);
 
     queue->delivering = 0;
@@ -96,7 +98,9 @@ static void qemu_net_queue_append(NetQueue *queue,
 {
     NetPacket *packet;
 
+    qemu_mutex_lock(&queue->lock);
     if (queue->nq_count >= queue->nq_maxlen && !sent_cb) {
+        qemu_mutex_unlock(&queue->lock);
         return; /* drop if queue full and no callback */
     }
     packet = g_malloc(sizeof(NetPacket) + size);
@@ -108,6 +112,7 @@ static void qemu_net_queue_append(NetQueue *queue,
 
     queue->nq_count++;
     QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
+    qemu_mutex_unlock(&queue->lock);
 }
 
 static void qemu_net_queue_append_iov(NetQueue *queue,
@@ -121,7 +126,9 @@ static void qemu_net_queue_append_iov(NetQueue *queue,
     size_t max_len = 0;
     int i;
 
+    qemu_mutex_lock(&queue->lock);
     if (queue->nq_count >= queue->nq_maxlen && !sent_cb) {
+        qemu_mutex_unlock(&queue->lock);
         return; /* drop if queue full and no callback */
     }
     for (i = 0; i < iovcnt; i++) {
@@ -143,6 +150,7 @@ static void qemu_net_queue_append_iov(NetQueue *queue,
 
     queue->nq_count++;
     QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
+    qemu_mutex_unlock(&queue->lock);
 }
 
 static ssize_t qemu_net_queue_deliver(NetQueue *queue,
@@ -229,6 +237,7 @@ void qemu_net_queue_purge(NetQueue *queue, NetClientState *from)
 {
     NetPacket *packet, *next;
 
+    qemu_mutex_lock(&queue->lock);
     QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) {
         if (packet->sender == from) {
             QTAILQ_REMOVE(&queue->packets, packet, entry);
@@ -236,10 +245,12 @@ void qemu_net_queue_purge(NetQueue *queue, NetClientState *from)
             g_free(packet);
         }
     }
+    qemu_mutex_unlock(&queue->lock);
 }
 
 bool qemu_net_queue_flush(NetQueue *queue)
 {
+    qemu_mutex_lock(&queue->lock);
     while (!QTAILQ_EMPTY(&queue->packets)) {
         NetPacket *packet;
         int ret;
@@ -256,6 +267,7 @@ bool qemu_net_queue_flush(NetQueue *queue)
         if (ret == 0) {
             queue->nq_count++;
             QTAILQ_INSERT_HEAD(&queue->packets, packet, entry);
+            qemu_mutex_unlock(&queue->lock);
             return false;
         }
 
@@ -265,5 +277,6 @@ bool qemu_net_queue_flush(NetQueue *queue)
 
         g_free(packet);
     }
+    qemu_mutex_unlock(&queue->lock);
     return true;
 }
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v2 2/6 net: introduce lock to protect NetClientState's peer's access

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Introduce nc->peer_lock to shield off the race of nc->peer's reader and deleter. With it, after deleter finish, no new qemu_send_packet_xx() will append packet to peer->send_queue, therefore no new reference from packet->sender to nc will exist in nc->peer->send_queue.

---
 include/net/net.h |  6 +++++
 net/net.c         | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 net/queue.c       |  4 +--
 3 files changed, 83 insertions(+), 6 deletions(-)
include/net/net.h +6 -0
index 2f72b26..ea46f13 100644
@@ -67,6 +67,10 @@ struct NetClientState {
     NetClientInfo *info;
     int link_down;
     QTAILQ_ENTRY(NetClientState) next;
+    /* protect the race access of peer only between reader and writer.
+         * to resolve the writer's race condition, resort on biglock.
+         */
+    QemuMutex peer_lock;
     NetClientState *peer;
     NetQueue *send_queue;
     char *model;
@@ -79,6 +83,7 @@ struct NetClientState {
 
 typedef struct NICState {
     NetClientState *ncs;
+    NetClientState **pending_peer;
     NICConf *conf;
     void *opaque;
     bool peer_deleted;
@@ -106,6 +111,7 @@ NetClientState *qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
                                               const char *client_str);
 typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
+int qemu_can_send_packet_nolock(NetClientState *sender);
 int qemu_can_send_packet(NetClientState *nc);
 ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
                           int iovcnt);
net/net.c +75 -4

171 lines not on this page · raw message

net/queue.c +2 -2
index c6d4241..7d6c52e 100644
@@ -192,7 +192,7 @@ ssize_t qemu_net_queue_send(NetQueue *queue,
 {
     ssize_t ret;
 
-    if (queue->delivering || !qemu_can_send_packet(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
         qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
         return 0;
     }
@@ -217,7 +217,7 @@ ssize_t qemu_net_queue_send_iov(NetQueue *queue,
 {
     ssize_t ret;
 
-    if (queue->delivering || !qemu_can_send_packet(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
         qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
         return 0;
     }
Signature · 1 line
1.8.1.4
#3 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] net: introduce lock to protect NetClientState's peer's access

include/net/net.h struct NetClientState { · line 72

Quoted 7 lines · Liu Ping Fan
include/net/net.h +3 -0
@@ -67,6 +67,10 @@ struct NetClientState {
     NetClientInfo *info;
     int link_down;
     QTAILQ_ENTRY(NetClientState) next;
+    /* protect the race access of peer only between reader and writer.
+         * to resolve the writer's race condition, resort on biglock.
+         */

Indentation

net/net.c static void qemu_free_net_client(NetClientState *nc) · line 306

Quoted 5 lines
net/net.c +1 -0
@@ -301,6 +303,38 @@ static void qemu_free_net_client(NetClientState *nc)
     }
 }
 
+/* elimate the reference and sync with exit of rx/tx action.

s/elimate/Eliminate/

net/net.c static void qemu_free_net_client(NetClientState *nc) · line 315

Quoted 9 lines
net/net.c +9 -0
+ * And flush out peer's queue.
+ */
+static void qemu_net_client_detach_flush(NetClientState *nc)
+{
+    NetClientState *peer;
+
+    /* reader of self's peer field , fixme? the deleters are not concurrent,
+         * so this pair lock can save.
+         */

Indentation, also please resolve the fixme.

Quoted 11 lines
net/net.c +7 -0
@@ -394,6 +433,28 @@ int qemu_can_send_packet(NetClientState *sender)
     return 1;
 }
 
+int qemu_can_send_packet(NetClientState *sender)
+{
+    int ret = 1;
+
+    qemu_mutex_lock(&sender->peer_lock);
+    if (!sender->peer) {
+        goto unlock;

net/net.c int qemu_can_send_packet(NetClientState *sender) · line 452

Quoted 10 lines
net/net.c +10 -0
+    }
+
+    if (sender->peer->receive_disabled) {
+        ret = 0;
+        goto unlock;
+    } else if (sender->peer->info->can_receive &&
+               !sender->peer->info->can_receive(sender->peer)) {
+        ret = 0;
+        goto unlock;
+    }

Just call qemu_can_send_packet_nolock() instead of duplicating code?

#4 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] net: introduce lock to protect NetClientState's peer's access

Quoted 11 lines · Stefan Hajnoczi

include/net/net.h struct NetClientState { · line 72

Quoted 7 lines · Liu Ping Fan
include/net/net.h +3 -0
@@ -67,6 +67,10 @@ struct NetClientState {
     NetClientInfo *info;
     int link_down;
     QTAILQ_ENTRY(NetClientState) next;
+    /* protect the race access of peer only between reader and writer.
+         * to resolve the writer's race condition, resort on biglock.
+         */

Indentation

Will fix.

Quoted 8 lines

net/net.c static void qemu_free_net_client(NetClientState *nc) · line 306

Quoted 5 lines
net/net.c +1 -0
@@ -301,6 +303,38 @@ static void qemu_free_net_client(NetClientState *nc)
     }
 }
 
+/* elimate the reference and sync with exit of rx/tx action.

s/elimate/Eliminate/

Will fix

Quoted 12 lines

net/net.c static void qemu_free_net_client(NetClientState *nc) · line 315

Quoted 9 lines
net/net.c +9 -0
+ * And flush out peer's queue.
+ */
+static void qemu_net_client_detach_flush(NetClientState *nc)
+{
+    NetClientState *peer;
+
+    /* reader of self's peer field , fixme? the deleters are not concurrent,
+         * so this pair lock can save.
+         */

Indentation, also please resolve the fixme.

So, here can I take the assumption that the deleters are serialized by biglock, and remove the lock following this comment?

Quoted 15 lines
Quoted 15 lines
net/net.c +11 -0
@@ -394,6 +433,28 @@ int qemu_can_send_packet(NetClientState *sender)
     return 1;
 }
 
+int qemu_can_send_packet(NetClientState *sender)
+{
+    int ret = 1;
+
+    qemu_mutex_lock(&sender->peer_lock);
+    if (!sender->peer) {
+        goto unlock;
+    }
+
+    if (sender->peer->receive_disabled) {
+        ret = 0;
Quoted 8 lines

net/net.c int qemu_can_send_packet(NetClientState *sender) · line 452

Quoted 6 lines
net/net.c +6 -0
+        goto unlock;
+    } else if (sender->peer->info->can_receive &&
+               !sender->peer->info->can_receive(sender->peer)) {
+        ret = 0;
+        goto unlock;
+    }

Just call qemu_can_send_packet_nolock() instead of duplicating code?

Yes.

Thx & regards,
pingfan

#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] net: introduce lock to protect NetClientState's peer's access

Quoted 14 lines · liu ping fan
Quoted 13 lines · Stefan Hajnoczi

net/net.c static void qemu_free_net_client(NetClientState *nc) · line 315

Quoted 9 lines · Liu Ping Fan

+ * And flush out peer's queue.
+ */

net/net.c +7 -0
+static void qemu_net_client_detach_flush(NetClientState *nc)
+{
+    NetClientState *peer;
+
+    /* reader of self's peer field , fixme? the deleters are not concurrent,
+         * so this pair lock can save.
+         */

Indentation, also please resolve the fixme.

Quoted 2 lines

So, here can I take the assumption that the deleters are serialized by biglock, and remove the lock following this comment?

Ah, I understand the comment now. Is there any advantage to dropping the lock? IMO it's clearer to take the lock consistently instead of "optimizing" cases we think only get called from the main loop.

#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] net: introduce lock to protect NetClientState's peer's access

Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines · liu ping fan
Quoted 13 lines · Stefan Hajnoczi

net/net.c static void qemu_free_net_client(NetClientState *nc) · line 315

Quoted 9 lines · Liu Ping Fan

+ * And flush out peer's queue.
+ */

net/net.c +7 -0
+static void qemu_net_client_detach_flush(NetClientState *nc)
+{
+    NetClientState *peer;
+
+    /* reader of self's peer field , fixme? the deleters are not concurrent,
+         * so this pair lock can save.
+         */

Indentation, also please resolve the fixme.

Quoted 4 lines
Quoted 2 lines

So, here can I take the assumption that the deleters are serialized by biglock, and remove the lock following this comment?

Ah, I understand the comment now. Is there any advantage to dropping

:), only two atomic instruction in rare path.

Quoted 2 lines

the lock? IMO it's clearer to take the lock consistently instead of "optimizing" cases we think only get called from the main loop.

Reasonable, will keep them.

#7 lore raw

PATCH v2 3/6 net: make netclient re-entrant with refcnt

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

With refcnt, NetClientState's user can run agaist deleter.

---
 hw/core/qdev-properties-system.c | 14 ++++++++++++
 include/net/net.h                |  3 +++
 net/hub.c                        |  3 +++
 net/net.c                        | 47 +++++++++++++++++++++++++++++++++++++---
 net/slirp.c                      |  3 ++-
 5 files changed, 66 insertions(+), 4 deletions(-)
hw/core/qdev-properties-system.c +14 -0
index 0eada32..41cc7e6 100644
@@ -302,6 +302,7 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque,
         return;
     }
 
+    /* inc ref, released when unset property */
     hubport = net_hub_port_find(id);
     if (!hubport) {
         error_set(errp, QERR_INVALID_PARAMETER_VALUE,
@@ -311,11 +312,24 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque,
     *ptr = hubport;
 }
 
+static void release_vlan(Object *obj, const char *name, void *opaque)
+{
+    DeviceState *dev = DEVICE(obj);
+    Property *prop = opaque;
+    NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
+    NetClientState **ptr = &peers_ptr->ncs[0];
+
+    if (*ptr) {
+        netclient_unref(*ptr);
+    }
+}
+
 PropertyInfo qdev_prop_vlan = {
     .name  = "vlan",
     .print = print_vlan,
     .get   = get_vlan,
     .set   = set_vlan,
+    .release = release_vlan,
 };
 
 int qdev_prop_set_drive(DeviceState *dev, const char *name,
include/net/net.h +3 -0
index ea46f13..1a31d1b 100644
@@ -64,6 +64,7 @@ typedef struct NetClientInfo {
 } NetClientInfo;
 
 struct NetClientState {
+    int ref;
     NetClientInfo *info;
     int link_down;
     QTAILQ_ENTRY(NetClientState) next;
@@ -92,6 +93,8 @@ typedef struct NICState {
 NetClientState *qemu_find_netdev(const char *id);
 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
                                  NetClientOptionsKind type, int max);
+void netclient_ref(NetClientState *nc);
+void netclient_unref(NetClientState *nc);
 NetClientState *qemu_new_net_client(NetClientInfo *info,
                                     NetClientState *peer,
                                     const char *model,
net/hub.c +3 -0
index df32074..9c6c559 100644
@@ -201,6 +201,7 @@ NetClientState *net_hub_find_client_by_name(int hub_id, const char *name)
                 peer = port->nc.peer;
 
                 if (peer && strcmp(peer->name, name) == 0) {
+                    netclient_ref(peer);
                     return peer;
                 }
             }
@@ -223,6 +224,7 @@ NetClientState *net_hub_port_find(int hub_id)
             QLIST_FOREACH(port, &hub->ports, next) {
                 nc = port->nc.peer;
                 if (!nc) {
+                    netclient_ref(&port->nc);
                     return &(port->nc);
                 }
             }
@@ -231,6 +233,7 @@ NetClientState *net_hub_port_find(int hub_id)
     }
 
     nc = net_hub_add_port(hub_id, NULL);
+    netclient_ref(nc);
     return nc;
 }
 
net/net.c +44 -3

207 lines not on this page · raw message

net/slirp.c +2 -1
index b3f35d5..e541548 100644
@@ -346,7 +346,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict)
 
     err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp,
                                host_addr, host_port);
-
+    netclient_unref(&s->nc);
     monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
                    err ? "not found" : "removed");
     return;
@@ -437,6 +437,7 @@ void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict)
     }
     if (s) {
         slirp_hostfwd(s, redir_str, 0);
+        netclient_unref(&s->nc);
     }
 
 }
Signature · 1 line
1.8.1.4
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] net: make netclient re-entrant with refcnt

Quoted 3 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

With refcnt, NetClientState's user can run agaist deleter.

Please split this into two patches:

1. net_clients lock
2. NetClientState refcount

Quoted 37 lines
---
 hw/core/qdev-properties-system.c | 14 ++++++++++++
 include/net/net.h                |  3 +++
 net/hub.c                        |  3 +++
 net/net.c                        | 47 +++++++++++++++++++++++++++++++++++++---
 net/slirp.c                      |  3 ++-
 5 files changed, 66 insertions(+), 4 deletions(-)
hw/core/qdev-properties-system.c +12 -0
index 0eada32..41cc7e6 100644
@@ -302,6 +302,7 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque,
         return;
     }
 
+    /* inc ref, released when unset property */
     hubport = net_hub_port_find(id);
     if (!hubport) {
         error_set(errp, QERR_INVALID_PARAMETER_VALUE,
@@ -311,11 +312,24 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque,
     *ptr = hubport;
 }
 
+static void release_vlan(Object *obj, const char *name, void *opaque)
+{
+    DeviceState *dev = DEVICE(obj);
+    Property *prop = opaque;
+    NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
+    NetClientState **ptr = &peers_ptr->ncs[0];
+
+    if (*ptr) {
+        netclient_unref(*ptr);
+    }
+}

hw/core/qdev-properties-system.c static void set_vlan(Object *obj, Visitor *v, void *opaque, · line 335

Quoted 10 lines
hw/core/qdev-properties-system.c +2 -0
+
 PropertyInfo qdev_prop_vlan = {
     .name  = "vlan",
     .print = print_vlan,
     .get   = get_vlan,
     .set   = set_vlan,
+    .release = release_vlan,
 };
 
 int qdev_prop_set_drive(DeviceState *dev, const char *name,

What about the netdev property? I don't see any refcount code there.

hw/core/qdev-properties-system.c void net_cleanup(void) · line 1149

Quoted 5 lines
hw/core/qdev-properties-system.c +1 -0
@@ -1109,6 +1146,7 @@ void net_cleanup(void)
             qemu_del_net_client(nc);
         }
     }
+    qemu_mutex_destroy(&net_clients_lock);

Why is it okay to iterate over net_clients here without the lock?

#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] net: make netclient re-entrant with refcnt

Quoted 10 lines · Stefan Hajnoczi
Quoted 3 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

With refcnt, NetClientState's user can run agaist deleter.

Please split this into two patches:

1. net_clients lock
2. NetClientState refcount

Ok.

Quoted 46 lines
Quoted 46 lines
---
 hw/core/qdev-properties-system.c | 14 ++++++++++++
 include/net/net.h                |  3 +++
 net/hub.c                        |  3 +++
 net/net.c                        | 47 +++++++++++++++++++++++++++++++++++++---
 net/slirp.c                      |  3 ++-
 5 files changed, 66 insertions(+), 4 deletions(-)
hw/core/qdev-properties-system.c +14 -0
index 0eada32..41cc7e6 100644
@@ -302,6 +302,7 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque,
         return;
     }
 
+    /* inc ref, released when unset property */
     hubport = net_hub_port_find(id);
     if (!hubport) {
         error_set(errp, QERR_INVALID_PARAMETER_VALUE,
@@ -311,11 +312,24 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque,
     *ptr = hubport;
 }
 
+static void release_vlan(Object *obj, const char *name, void *opaque)
+{
+    DeviceState *dev = DEVICE(obj);
+    Property *prop = opaque;
+    NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
+    NetClientState **ptr = &peers_ptr->ncs[0];
+
+    if (*ptr) {
+        netclient_unref(*ptr);
+    }
+}
+
 PropertyInfo qdev_prop_vlan = {
     .name  = "vlan",
     .print = print_vlan,
     .get   = get_vlan,
     .set   = set_vlan,
+    .release = release_vlan,
 };
 
Quoted 4 lines

hw/core/qdev-properties-system.c static void set_vlan(Object *obj, Visitor *v, void *opaque, · line 335

Quoted 1 line
hw/core/qdev-properties-system.c +0 -0
 int qdev_prop_set_drive(DeviceState *dev, const char *name,

What about the netdev property? I don't see any refcount code there.

Yes, the release of netdev and vlan property should all free its backend. Will add the code.

Quoted 7 lines

hw/core/qdev-properties-system.c void net_cleanup(void) · line 1149

Quoted 5 lines
hw/core/qdev-properties-system.c +1 -0
@@ -1109,6 +1146,7 @@ void net_cleanup(void)
             qemu_del_net_client(nc);
         }
     }
+    qemu_mutex_destroy(&net_clients_lock);

Why is it okay to iterate over net_clients here without the lock?

atexit(&net_cleanup); So no other racers exist.

Thx & Regards,
Pingfan

#10 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] net: make netclient re-entrant with refcnt

Quoted 11 lines · liu ping fan
Quoted 8 lines · Stefan Hajnoczi

net/net.c void net_cleanup(void) · line 1149

Quoted 5 lines · Liu Ping Fan
net/net.c +1 -0
@@ -1109,6 +1146,7 @@ void net_cleanup(void)
             qemu_del_net_client(nc);
         }
     }
+    qemu_mutex_destroy(&net_clients_lock);

Why is it okay to iterate over net_clients here without the lock?

atexit(&net_cleanup); So no other racers exist.

What about dataplane? The device may not be reset when net_cleanup runs.

It's best not to make assumptions, taking the lock is easy.

Stefan

#11 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] net: make netclient re-entrant with refcnt

Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · liu ping fan
Quoted 8 lines · Stefan Hajnoczi

net/net.c void net_cleanup(void) · line 1149

Quoted 5 lines · Liu Ping Fan
net/net.c +1 -0
@@ -1109,6 +1146,7 @@ void net_cleanup(void)
             qemu_del_net_client(nc);
         }
     }
+    qemu_mutex_destroy(&net_clients_lock);

Why is it okay to iterate over net_clients here without the lock?

Quoted 4 lines
Quoted 1 line

atexit(&net_cleanup); So no other racers exist.

What about dataplane? The device may not be reset when net_cleanup runs.

Does the func registered by atexit run after all of the other threads terminate?

Quoted 2 lines

It's best not to make assumptions, taking the lock is easy.

Yes, assumptions are not reliable. I will take the lock for the next version.

Thx & regards,
Pingfan

Quoted 1 line

Stefan

#12 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] net: make netclient re-entrant with refcnt

Quoted 12 lines · liu ping fan
Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · liu ping fan
Quoted 8 lines · Stefan Hajnoczi

net/net.c void net_cleanup(void) · line 1149

Quoted 5 lines · Liu Ping Fan
net/net.c +1 -0
@@ -1109,6 +1146,7 @@ void net_cleanup(void)
             qemu_del_net_client(nc);
         }
     }
+    qemu_mutex_destroy(&net_clients_lock);

Why is it okay to iterate over net_clients here without the lock?

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

atexit(&net_cleanup); So no other racers exist.

What about dataplane? The device may not be reset when net_cleanup runs.

Does the func registered by atexit run after all of the other threads terminate?

I imagine that atexit(3) runs while detached threads are still alive, but I'm not sure about the exact rules. The pthread specification links I found online didn't state the rules.

Stefan

#13 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] net: make netclient re-entrant with refcnt

Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines · liu ping fan
Quoted 13 lines · Stefan Hajnoczi
Quoted 11 lines · liu ping fan
Quoted 8 lines · Stefan Hajnoczi

net/net.c void net_cleanup(void) · line 1149

Quoted 5 lines · Liu Ping Fan
net/net.c +1 -0
@@ -1109,6 +1146,7 @@ void net_cleanup(void)
             qemu_del_net_client(nc);
         }
     }
+    qemu_mutex_destroy(&net_clients_lock);

Why is it okay to iterate over net_clients here without the lock?

atexit(&net_cleanup); So no other racers exist.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

What about dataplane? The device may not be reset when net_cleanup runs.

Does the func registered by atexit run after all of the other threads terminate?

I imagine that atexit(3) runs while detached threads are still alive, but I'm not sure about the exact rules. The pthread specification links I found online didn't state the rules.

Haha, finally, got some hint for this. pthread_exit(3) says:
After the last thread in a process terminates, the
process terminates as by calling exit(3) with an exit status of zero; thus, process-shared
resources are released and functions registered using atexit(3)
are called.

Regards,
Pingfan

Quoted 1 line

Stefan

#14 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] net: make netclient re-entrant with refcnt

Quoted 24 lines · liu ping fan
Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines · liu ping fan
Quoted 13 lines · Stefan Hajnoczi
Quoted 11 lines · liu ping fan
Quoted 8 lines · Stefan Hajnoczi

Review void net_cleanup(void) · line 1149

Quoted 5 lines · Liu Ping Fan
(quoted diff) +1 -0
@@ -1109,6 +1146,7 @@ void net_cleanup(void)
             qemu_del_net_client(nc);
         }
     }
+    qemu_mutex_destroy(&net_clients_lock);

Why is it okay to iterate over net_clients here without the lock?

atexit(&net_cleanup); So no other racers exist.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

What about dataplane? The device may not be reset when net_cleanup runs.

Does the func registered by atexit run after all of the other threads terminate?

I imagine that atexit(3) runs while detached threads are still alive, but I'm not sure about the exact rules. The pthread specification links I found online didn't state the rules.

Quoted 6 lines

Haha, finally, got some hint for this. pthread_exit(3) says:
After the last thread in a process terminates, the
process terminates as by calling exit(3) with an exit status of zero; thus, process-shared
resources are released and functions registered using atexit(3)
are called.

That's only true for non-detached threads. A program can exit while detached threads are running.

Stefan

#15 lore raw

PATCH v2 4/6 net: force NetQue opaque to be NetClientState

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

qemu_net_client_setup() is the only user of qemu_new_net_queue(), which will pass in NetClientState. By forcing it be a NetClientState, we can ref/unref NetQueue's owner

---
 include/net/queue.h | 2 +-
 net/queue.c         | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/net/queue.h +1 -1
index fc02b33..ddb6d98 100644
@@ -34,7 +34,7 @@ typedef void (NetPacketSent) (NetClientState *sender, ssize_t ret);
 #define QEMU_NET_PACKET_FLAG_NONE  0
 #define QEMU_NET_PACKET_FLAG_RAW  (1<<0)
 
-NetQueue *qemu_new_net_queue(void *opaque);
+NetQueue *qemu_new_net_queue(NetClientState *opaque);
 
 void qemu_del_net_queue(NetQueue *queue);
 
net/queue.c +2 -2
index 7d6c52e..58222b0 100644
@@ -49,7 +49,7 @@ struct NetPacket {
 };
 
 struct NetQueue {
-    void *opaque;
+    NetClientState *opaque;
     uint32_t nq_maxlen;
     uint32_t nq_count;
 
@@ -59,7 +59,7 @@ struct NetQueue {
     unsigned delivering : 1;
 };
 
-NetQueue *qemu_new_net_queue(void *opaque)
+NetQueue *qemu_new_net_queue(NetClientState *opaque)
 {
     NetQueue *queue;
 
Signature · 1 line
1.8.1.4
#16 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] net: force NetQue opaque to be NetClientState

Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

qemu_net_client_setup() is the only user of qemu_new_net_queue(), which will pass in NetClientState. By forcing it be a NetClientState, we can ref/unref NetQueue's owner

Please s/opaque/nc/ in net/queue.[hc] since it's no longer "opaque" :).

Also, qemu_deliver_packet()/qemu_deliver_packet_iov() can take an NetClientState *nc instead of void *opaque.

Quoted 1 line

pingfank here and pingfanl in the From: header. Are both okay and which do you prefer to use?

#17 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] net: force NetQue opaque to be NetClientState

Quoted 9 lines · Stefan Hajnoczi
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

qemu_net_client_setup() is the only user of qemu_new_net_queue(), which will pass in NetClientState. By forcing it be a NetClientState, we can ref/unref NetQueue's owner

Please s/opaque/nc/ in net/queue.[hc] since it's no longer "opaque" :).

Ok, I will.

Quoted 7 lines

Also, qemu_deliver_packet()/qemu_deliver_packet_iov() can take an NetClientState *nc instead of void *opaque.

Quoted 1 line

pingfank here and pingfanl in the From: header. Are both okay and which do you prefer to use?

Change my disk, totally re-install my system, and mix up with internal mail address. Will fix it.

Thx & regards,
Pingfan

#18 lore raw

PATCH v2 5/6 net: defer nested call to BH

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Nested call caused by ->receive() will raise issue like deadlock, so postphone it to BH.

---
 net/queue.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)
net/queue.c +38 -2
index 58222b0..9c343ab 100644
@@ -24,6 +24,8 @@
 #include "net/queue.h"
 #include "qemu/queue.h"
 #include "net/net.h"
+#include "block/aio.h"
+#include "qemu/main-loop.h"
 
 /* The delivery handler may only return zero if it will call
  * qemu_net_queue_flush() when it determines that it is once again able
@@ -183,6 +185,22 @@ static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
     return ret;
 }
 
+typedef struct NetQueBH {
+    QEMUBH *bh;
+    NetClientState *nc;
+} NetQueBH;
+
+static void qemu_net_queue_send_bh(void *opaque)
+{
+    NetQueBH *q_bh = opaque;
+    NetQueue *queue = q_bh->nc->send_queue;
+
+    qemu_net_queue_flush(queue);
+    netclient_unref(q_bh->nc);
+    qemu_bh_delete(q_bh->bh);
+    g_slice_free(NetQueBH, q_bh);
+}
+
 ssize_t qemu_net_queue_send(NetQueue *queue,
                             NetClientState *sender,
                             unsigned flags,
@@ -192,8 +210,17 @@ ssize_t qemu_net_queue_send(NetQueue *queue,
 {
     ssize_t ret;
 
-    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)
+        || sender->send_queue->delivering) {
         qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
+        /* Nested call will be deferred to BH */
+        if (sender->send_queue->delivering) {
+            NetQueBH *que_bh = g_slice_new(NetQueBH);
+            que_bh->bh = qemu_bh_new(qemu_net_queue_send_bh, que_bh);
+            que_bh->nc = queue->opaque;
+            netclient_ref(queue->opaque);
+            qemu_bh_schedule(que_bh->bh);
+        }
         return 0;
     }
 
@@ -217,8 +244,17 @@ ssize_t qemu_net_queue_send_iov(NetQueue *queue,
 {
     ssize_t ret;
 
-    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)
+        || sender->send_queue->delivering) {
         qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
+        /* Nested call will be deferred to BH */
+        if (sender->send_queue->delivering) {
+            NetQueBH *que_bh = g_slice_new(NetQueBH);
+            que_bh->bh = qemu_bh_new(qemu_net_queue_send_bh, que_bh);
+            que_bh->nc = queue->opaque;
+            netclient_ref(queue->opaque);
+            qemu_bh_schedule(que_bh->bh);
+        }
         return 0;
     }
 
Signature · 1 line
1.8.1.4
#19 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] net: defer nested call to BH

Quoted 9 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Nested call caused by ->receive() will raise issue like deadlock, so postphone it to BH.

---
 net/queue.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

Does this patch belong before the netqueue lock patch? The commit history should be bisectable without temporary failures/deadlocks.

Quoted 14 lines
net/queue.c +2 -0
index 58222b0..9c343ab 100644
@@ -24,6 +24,8 @@
 #include "net/queue.h"
 #include "qemu/queue.h"
 #include "net/net.h"
+#include "block/aio.h"
+#include "qemu/main-loop.h"
 
 /* The delivery handler may only return zero if it will call
  * qemu_net_queue_flush() when it determines that it is once again able

net/queue.c static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue, · line 188

Quoted 4 lines
net/queue.c +1 -0
@@ -183,6 +185,22 @@ static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
     return ret;
 }
 
+typedef struct NetQueBH {

This file uses "Queue" consistently, please don't add "Que" here.

net/queue.c ssize_t qemu_net_queue_send(NetQueue *queue, · line 214

Quoted 7 lines
net/queue.c +2 -1
@@ -192,8 +210,17 @@ ssize_t qemu_net_queue_send(NetQueue *queue,
 {
     ssize_t ret;
 
-    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)
+        || sender->send_queue->delivering) {

Not sure this is safe, we're only holding one NetClientState->peer_lock and one NetQueue->lock. How can we access both queue->delivering and sender->send_queue->delivering safely?

#20 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] net: defer nested call to BH

Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Nested call caused by ->receive() will raise issue like deadlock, so postphone it to BH.

Quoted 8 lines
Quoted 4 lines
---
 net/queue.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

Does this patch belong before the netqueue lock patch? The commit history should be bisectable without temporary failures/deadlocks.

Ok.

Quoted 25 lines
Quoted 14 lines
net/queue.c +2 -0
index 58222b0..9c343ab 100644
@@ -24,6 +24,8 @@
 #include "net/queue.h"
 #include "qemu/queue.h"
 #include "net/net.h"
+#include "block/aio.h"
+#include "qemu/main-loop.h"
 
 /* The delivery handler may only return zero if it will call
  * qemu_net_queue_flush() when it determines that it is once again able

net/queue.c static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue, · line 188

Quoted 4 lines
net/queue.c +1 -0
@@ -183,6 +185,22 @@ static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
     return ret;
 }
 
+typedef struct NetQueBH {

This file uses "Queue" consistently, please don't add "Que" here.

Quoted 4 lines
net/queue.c +0 -0
@@ -192,8 +210,17 @@ ssize_t qemu_net_queue_send(NetQueue *queue,
 {
     ssize_t ret;
 
Quoted 7 lines

net/queue.c ssize_t qemu_net_queue_send(NetQueue *queue, · line 214

Quoted 3 lines
net/queue.c +2 -1
-    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)
+        || sender->send_queue->delivering) {

Not sure this is safe, we're only holding one NetClientState->peer_lock and one NetQueue->lock. How can we access both queue->delivering and sender->send_queue->delivering safely?

Yes, you are right, it is not safely. The queue->delivering is protected by peer_lock and we do not take the verse direction lock . So finally the above code can not tell out the nested calling "A-->B-->A" from "A-->B, B-->A" (where A, B stands for a NetClientState).
What about using TLS to trace the nested calling? With it, we can avoid AB-BA lock problem.

Thx & regards,
Pingfan

#21 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] net: defer nested call to BH

Quoted 49 lines · liu ping fan
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Nested call caused by ->receive() will raise issue like deadlock, so postphone it to BH.

Quoted 8 lines
Quoted 4 lines
---
 net/queue.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

Does this patch belong before the netqueue lock patch? The commit history should be bisectable without temporary failures/deadlocks.

Ok.

Quoted 25 lines
Quoted 14 lines
net/queue.c +2 -0
index 58222b0..9c343ab 100644
@@ -24,6 +24,8 @@
 #include "net/queue.h"
 #include "qemu/queue.h"
 #include "net/net.h"
+#include "block/aio.h"
+#include "qemu/main-loop.h"
 
 /* The delivery handler may only return zero if it will call
  * qemu_net_queue_flush() when it determines that it is once again able

net/queue.c static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue, · line 188

Quoted 4 lines
net/queue.c +1 -0
@@ -183,6 +185,22 @@ static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
     return ret;
 }
 
+typedef struct NetQueBH {

This file uses "Queue" consistently, please don't add "Que" here.

Quoted 4 lines
net/queue.c +0 -0
@@ -192,8 +210,17 @@ ssize_t qemu_net_queue_send(NetQueue *queue,
 {
     ssize_t ret;
 
Quoted 7 lines

net/queue.c ssize_t qemu_net_queue_send(NetQueue *queue, · line 214

Quoted 3 lines
net/queue.c +2 -1
-    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)
+        || sender->send_queue->delivering) {

Not sure this is safe, we're only holding one NetClientState->peer_lock and one NetQueue->lock. How can we access both queue->delivering and sender->send_queue->delivering safely?

Quoted 7 lines

Yes, you are right, it is not safely. The queue->delivering is protected by peer_lock and we do not take the verse direction lock . So finally the above code can not tell out the nested calling "A-->B-->A" from "A-->B, B-->A" (where A, B stands for a NetClientState).
What about using TLS to trace the nested calling? With it, we can avoid AB-BA lock problem.

I would take a step back and see if there's a way to avoid reaching into inspect sender->send_queue->delivering here.

Stefan

#22 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] net: defer nested call to BH

In reply to Liu Ping Fan

Il 13/06/2013 11:03, Liu Ping Fan ha scritto:

Quoted 4 lines

From: Liu Ping Fan <[email protected]>

Nested call caused by ->receive() will raise issue like deadlock, so postphone it to BH.

When I read this patch, I am completely puzzled.

All I see is that a qemu_net_queue_flush is done in a bottom half. I have no clue of how you get a nested call of ->receive, and I have no clue of what this patch does to prevent it (since the bottom half is asynchronous).

A bottom half is not a synchronization primitive. If you use a lock, or a condition variable, or RCU, you can usually assume that the reader understands how you're using it (though not if you're doing fancy tricks). If you are introducing infrastructure, you can use long comments or docs/ and expect the reviewer to read those.

But if you're doing tricky stuff (such as if you have global/local locks, or you have parts of the code that run lockless, or if you are modifying the behavior of an existing subsystem to prevent deadlocks), you need to document _every single step that you do_.

For example, let's take the patches you had for hostmem.c. You had a single patch doing all the work:

hostmem: make hostmem global and RAM hotunplg safe

The hostmem listener will translate gpa to hva quickly. Make it global, so other component can use it.
Also this patch adopt MemoryRegionOps's ref/unref interface to make it survive the RAM hotunplug.

No reference whatsoever to the reference counting of the hostmem itself, and how you are using the locks. The "also" in the commit message is a big warning sign, too (though I'm guilty of adding many "also"s in the commit messages).

When I took your ideas and applied them to memory.c, here is how I explained it:

memory: use a new FlatView pointer on every topology update

This is the first step towards converting as->current_map to RCU-style updates, where the FlatView updates run concurrently with uses of an old FlatView.
---
memory: add reference counting to FlatView

With this change, a FlatView can be used even after a concurrent update has replaced it. Because we do not have RCU, we use a mutex to protect the small critical sections that read/write the as->current_map pointer. Accesses to the FlatView can be done outside the mutex.

And in the code:

/* flat_view_mutex is taken around reading as->current_map; the
* critical section is extremely short, so I'm using a single mutex
* for every AS. We could also RCU for the read-side.
*
* The BQL is taken around transaction commits, hence both locks are
* taken while writing to as->current_map (with the BQL taken
* outside).
*/

It is still quite concise, but it explains both the concurrency to expect, and the locking policies.

This patch is changing the behavior of existing code in a complex scenario and in a tricky way. If you want your patches accepted (or even reviewed), you _must_ learn how to explain the scenarios and your fixes.

And believe me, it is not a language problem; people with much worse English than yours manage this all the time. It is more of an attitude problem, and I've explained it many times.

Paolo

Quoted 80 lines
---
 net/queue.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)
net/queue.c +38 -2
index 58222b0..9c343ab 100644
@@ -24,6 +24,8 @@
 #include "net/queue.h"
 #include "qemu/queue.h"
 #include "net/net.h"
+#include "block/aio.h"
+#include "qemu/main-loop.h"
 
 /* The delivery handler may only return zero if it will call
  * qemu_net_queue_flush() when it determines that it is once again able
@@ -183,6 +185,22 @@ static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
     return ret;
 }
 
+typedef struct NetQueBH {
+    QEMUBH *bh;
+    NetClientState *nc;
+} NetQueBH;
+
+static void qemu_net_queue_send_bh(void *opaque)
+{
+    NetQueBH *q_bh = opaque;
+    NetQueue *queue = q_bh->nc->send_queue;
+
+    qemu_net_queue_flush(queue);
+    netclient_unref(q_bh->nc);
+    qemu_bh_delete(q_bh->bh);
+    g_slice_free(NetQueBH, q_bh);
+}
+
 ssize_t qemu_net_queue_send(NetQueue *queue,
                             NetClientState *sender,
                             unsigned flags,
@@ -192,8 +210,17 @@ ssize_t qemu_net_queue_send(NetQueue *queue,
 {
     ssize_t ret;
 
-    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)
+        || sender->send_queue->delivering) {
         qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
+        /* Nested call will be deferred to BH */
+        if (sender->send_queue->delivering) {
+            NetQueBH *que_bh = g_slice_new(NetQueBH);
+            que_bh->bh = qemu_bh_new(qemu_net_queue_send_bh, que_bh);
+            que_bh->nc = queue->opaque;
+            netclient_ref(queue->opaque);
+            qemu_bh_schedule(que_bh->bh);
+        }
         return 0;
     }
 
@@ -217,8 +244,17 @@ ssize_t qemu_net_queue_send_iov(NetQueue *queue,
 {
     ssize_t ret;
 
-    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)
+        || sender->send_queue->delivering) {
         qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
+        /* Nested call will be deferred to BH */
+        if (sender->send_queue->delivering) {
+            NetQueBH *que_bh = g_slice_new(NetQueBH);
+            que_bh->bh = qemu_bh_new(qemu_net_queue_send_bh, que_bh);
+            que_bh->nc = queue->opaque;
+            netclient_ref(queue->opaque);
+            qemu_bh_schedule(que_bh->bh);
+        }
         return 0;
     }
 
#23 lore raw

PATCH v2 6/6 net: hub use lock to protect ports list

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Hub ports will run on multi-threads, so use lock to protect them.

---
 net/hub.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
net/hub.c +24 -1
index 9c6c559..2970f8e 100644
@@ -37,6 +37,7 @@ struct NetHub {
     int id;
     QLIST_ENTRY(NetHub) next;
     int num_ports;
+    QemuMutex ports_lock;
     QLIST_HEAD(, NetHubPort) ports;
 };
 
@@ -47,6 +48,7 @@ static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
 {
     NetHubPort *port;
 
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_FOREACH(port, &hub->ports, next) {
         if (port == source_port) {
             continue;
@@ -54,6 +56,7 @@ static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
 
         qemu_send_packet(&port->nc, buf, len);
     }
+    qemu_mutex_unlock(&hub->ports_lock);
     return len;
 }
 
@@ -63,6 +66,7 @@ static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
     NetHubPort *port;
     ssize_t len = iov_size(iov, iovcnt);
 
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_FOREACH(port, &hub->ports, next) {
         if (port == source_port) {
             continue;
@@ -70,6 +74,7 @@ static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
 
         qemu_sendv_packet(&port->nc, iov, iovcnt);
     }
+    qemu_mutex_unlock(&hub->ports_lock);
     return len;
 }
 
@@ -80,6 +85,7 @@ static NetHub *net_hub_new(int id)
     hub = g_malloc(sizeof(*hub));
     hub->id = id;
     hub->num_ports = 0;
+    qemu_mutex_init(&hub->ports_lock);
     QLIST_INIT(&hub->ports);
 
     QLIST_INSERT_HEAD(&hubs, hub, next);
@@ -93,16 +99,19 @@ static int net_hub_port_can_receive(NetClientState *nc)
     NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc);
     NetHub *hub = src_port->hub;
 
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_FOREACH(port, &hub->ports, next) {
         if (port == src_port) {
             continue;
         }
 
         if (qemu_can_send_packet(&port->nc)) {
+            qemu_mutex_unlock(&hub->ports_lock);
             return 1;
         }
     }
 
+    qemu_mutex_unlock(&hub->ports_lock);
     return 0;
 }
 
@@ -155,8 +164,9 @@ static NetHubPort *net_hub_port_new(NetHub *hub, const char *name)
     port = DO_UPCAST(NetHubPort, nc, nc);
     port->id = id;
     port->hub = hub;
-
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_INSERT_HEAD(&hub->ports, port, next);
+    qemu_mutex_unlock(&hub->ports_lock);
 
     return port;
 }
@@ -197,14 +207,17 @@ NetClientState *net_hub_find_client_by_name(int hub_id, const char *name)
 
     QLIST_FOREACH(hub, &hubs, next) {
         if (hub->id == hub_id) {
+            qemu_mutex_lock(&hub->ports_lock);
             QLIST_FOREACH(port, &hub->ports, next) {
                 peer = port->nc.peer;
 
                 if (peer && strcmp(peer->name, name) == 0) {
                     netclient_ref(peer);
+                    qemu_mutex_unlock(&hub->ports_lock);
                     return peer;
                 }
             }
+            qemu_mutex_unlock(&hub->ports_lock);
         }
     }
     return NULL;
@@ -221,13 +234,16 @@ NetClientState *net_hub_port_find(int hub_id)
 
     QLIST_FOREACH(hub, &hubs, next) {
         if (hub->id == hub_id) {
+            qemu_mutex_lock(&hub->ports_lock);
             QLIST_FOREACH(port, &hub->ports, next) {
                 nc = port->nc.peer;
                 if (!nc) {
                     netclient_ref(&port->nc);
+                    qemu_mutex_unlock(&hub->ports_lock);
                     return &(port->nc);
                 }
             }
+            qemu_mutex_unlock(&hub->ports_lock);
             break;
         }
     }
@@ -247,12 +263,14 @@ void net_hub_info(Monitor *mon)
 
     QLIST_FOREACH(hub, &hubs, next) {
         monitor_printf(mon, "hub %d\n", hub->id);
+        qemu_mutex_lock(&hub->ports_lock);
         QLIST_FOREACH(port, &hub->ports, next) {
             if (port->nc.peer) {
                 monitor_printf(mon, " \\ ");
                 print_net_client(mon, port->nc.peer);
             }
         }
+        qemu_mutex_unlock(&hub->ports_lock);
     }
 }
 
@@ -309,6 +327,7 @@ void net_hub_check_clients(void)
     QLIST_FOREACH(hub, &hubs, next) {
         int has_nic = 0, has_host_dev = 0;
 
+        qemu_mutex_lock(&hub->ports_lock);
         QLIST_FOREACH(port, &hub->ports, next) {
             peer = port->nc.peer;
             if (!peer) {
@@ -331,6 +350,7 @@ void net_hub_check_clients(void)
                 break;
             }
         }
+        qemu_mutex_unlock(&hub->ports_lock);
         if (has_host_dev && !has_nic) {
             fprintf(stderr, "Warning: vlan %d with no nics\n", hub->id);
         }
@@ -346,12 +366,15 @@ bool net_hub_flush(NetClientState *nc)
 {
     NetHubPort *port;
     NetHubPort *source_port = DO_UPCAST(NetHubPort, nc, nc);
+    NetHub *hub = source_port->hub;
     int ret = 0;
 
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_FOREACH(port, &source_port->hub->ports, next) {
         if (port != source_port) {
             ret += qemu_net_queue_flush(port->nc.send_queue);
         }
     }
+    qemu_mutex_unlock(&hub->ports_lock);
     return ret ? true : false;
 }
Signature · 1 line
1.8.1.4
#24 lore raw
In reply to Liu Ping Fan
Quoted 15 lines · Liu Ping Fan

Currently, I drop slirp and frontend related code (I guess I can reuse some code by mdroth's QContext later). And this series only resolve the net core re-entrant problem.

v1->v2:
introduce netqueue re-entrant detection and defer it to BH

Liu Ping Fan (6):
net: introduce lock to protect NetQueue
net: introduce lock to protect NetClientState's peer's access
net: make netclient re-entrant with refcnt
net: force NetQue opaque to be NetClientState
net: defer nested call to BH
net: hub use lock to protect ports list

Quoted 8 lines
 hw/core/qdev-properties-system.c |  14 +++++
 include/net/net.h                |  10 ++++
 include/net/queue.h              |   2 +-
 net/hub.c                        |  28 ++++++++-
 net/net.c                        | 124 +++++++++++++++++++++++++++++++++++++--
 net/queue.c                      |  57 ++++++++++++++++--
 net/slirp.c                      |   3 +-
 7 files changed, 225 insertions(+), 13 deletions(-)

I've replied with comments for the obvious issues I noticed. For the next revision I'll apply the patches and do a global review of the rx/tx/add/del paths because it's hard to really review this patch-by-patch.