[PATCH-v5 1/4] vsock: track pkt owner vsock

2017-03-14 Thread Peng Tao
So that we can cancel a queued pkt later if necessary.

Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 include/linux/virtio_vsock.h| 3 +++
 net/vmw_vsock/virtio_transport_common.c | 7 +++
 2 files changed, 10 insertions(+)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 9638bfe..584f9a6 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -48,6 +48,8 @@ struct virtio_vsock_pkt {
struct virtio_vsock_hdr hdr;
struct work_struct work;
struct list_head list;
+   /* socket refcnt not held, only use for cancellation */
+   struct vsock_sock *vsk;
void *buf;
u32 len;
u32 off;
@@ -56,6 +58,7 @@ struct virtio_vsock_pkt {
 
 struct virtio_vsock_pkt_info {
u32 remote_cid, remote_port;
+   struct vsock_sock *vsk;
struct msghdr *msg;
u32 pkt_len;
u16 type;
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index 8d592a4..af087b4 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -58,6 +58,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
pkt->len= len;
pkt->hdr.len= cpu_to_le32(len);
pkt->reply  = info->reply;
+   pkt->vsk= info->vsk;
 
if (info->msg && len > 0) {
pkt->buf = kmalloc(len, GFP_KERNEL);
@@ -180,6 +181,7 @@ static int virtio_transport_send_credit_update(struct 
vsock_sock *vsk,
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
.type = type,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -519,6 +521,7 @@ int virtio_transport_connect(struct vsock_sock *vsk)
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_REQUEST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -534,6 +537,7 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int 
mode)
  VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
 (mode & SEND_SHUTDOWN ?
  VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -560,6 +564,7 @@ virtio_transport_stream_enqueue(struct vsock_sock *vsk,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.msg = msg,
.pkt_len = len,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -581,6 +586,7 @@ static int virtio_transport_reset(struct vsock_sock *vsk,
.op = VIRTIO_VSOCK_OP_RST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.reply = !!pkt,
+   .vsk = vsk,
};
 
/* Send RST only if the original pkt is not a RST pkt */
@@ -826,6 +832,7 @@ virtio_transport_send_response(struct vsock_sock *vsk,
.remote_cid = le64_to_cpu(pkt->hdr.src_cid),
.remote_port = le32_to_cpu(pkt->hdr.src_port),
.reply = true,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
-- 
2.7.4



[PATCH-v5 2/4] vhost-vsock: add pkt cancel capability

2017-03-14 Thread Peng Tao
To allow canceling all packets of a connection.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Reviewed-by: Jorgen Hansen <jhan...@vmware.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 drivers/vhost/vsock.c  | 41 +
 include/net/af_vsock.h |  3 +++
 2 files changed, 44 insertions(+)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index ce5e63d..57babce 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -223,6 +223,46 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+vhost_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct vhost_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   /* Find the vhost_vsock according to guest context id  */
+   vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
+   if (!vsock)
+   return -ENODEV;
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->vsk != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct vhost_virtqueue *tx_vq = >vqs[VSOCK_VQ_TX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
+   vhost_poll_queue(_vq->poll);
+   }
+
+   return 0;
+}
+
 static struct virtio_vsock_pkt *
 vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
  unsigned int out, unsigned int in)
@@ -675,6 +715,7 @@ static struct virtio_transport vhost_transport = {
.release  = virtio_transport_release,
.connect  = virtio_transport_connect,
.shutdown = virtio_transport_shutdown,
+   .cancel_pkt   = vhost_transport_cancel_pkt,
 
.dgram_enqueue= virtio_transport_dgram_enqueue,
.dgram_dequeue= virtio_transport_dgram_dequeue,
diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index f275896..f32ed9a 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -100,6 +100,9 @@ struct vsock_transport {
void (*destruct)(struct vsock_sock *);
void (*release)(struct vsock_sock *);
 
+   /* Cancel all pending packets sent on vsock. */
+   int (*cancel_pkt)(struct vsock_sock *vsk);
+
/* Connections. */
int (*connect)(struct vsock_sock *);
 
-- 
2.7.4



[PATCH-v5 3/4] vsock: add pkt cancel capability

2017-03-14 Thread Peng Tao
Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/virtio_transport.c | 42 
 1 file changed, 42 insertions(+)

diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 9d24c0e..bcab8f2 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -213,6 +213,47 @@ virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+virtio_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct virtio_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   vsock = virtio_vsock_get();
+   if (!vsock) {
+   return -ENODEV;
+   }
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->vsk != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
+   new_cnt < virtqueue_get_vring_size(rx_vq))
+   queue_work(virtio_vsock_workqueue, >rx_work);
+   }
+
+   return 0;
+}
+
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
@@ -462,6 +503,7 @@ static struct virtio_transport virtio_transport = {
.release  = virtio_transport_release,
.connect  = virtio_transport_connect,
.shutdown = virtio_transport_shutdown,
+   .cancel_pkt   = virtio_transport_cancel_pkt,
 
.dgram_bind   = virtio_transport_dgram_bind,
.dgram_dequeue= virtio_transport_dgram_dequeue,
-- 
2.7.4



[PATCH-v5 4/4] vsock: cancel packets when failing to connect

2017-03-14 Thread Peng Tao
Otherwise we'll leave the packets queued until releasing vsock device.
E.g., if guest is slow to start up, resulting ETIMEDOUT on connect, guest
will get the connect requests from failed host sockets.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Reviewed-by: Jorgen Hansen <jhan...@vmware.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/af_vsock.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 9192ead..756542a 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1102,10 +1102,19 @@ static const struct proto_ops vsock_dgram_ops = {
.sendpage = sock_no_sendpage,
 };
 
+static int vsock_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   if (!transport->cancel_pkt)
+   return -EOPNOTSUPP;
+
+   return transport->cancel_pkt(vsk);
+}
+
 static void vsock_connect_timeout(struct work_struct *work)
 {
struct sock *sk;
struct vsock_sock *vsk;
+   int cancel = 0;
 
vsk = container_of(work, struct vsock_sock, dwork.work);
sk = sk_vsock(vsk);
@@ -1116,8 +1125,11 @@ static void vsock_connect_timeout(struct work_struct 
*work)
sk->sk_state = SS_UNCONNECTED;
sk->sk_err = ETIMEDOUT;
sk->sk_error_report(sk);
+   cancel = 1;
}
release_sock(sk);
+   if (cancel)
+   vsock_transport_cancel_pkt(vsk);
 
sock_put(sk);
 }
@@ -1224,11 +1236,13 @@ static int vsock_stream_connect(struct socket *sock, 
struct sockaddr *addr,
err = sock_intr_errno(timeout);
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   vsock_transport_cancel_pkt(vsk);
goto out_wait;
} else if (timeout == 0) {
err = -ETIMEDOUT;
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   vsock_transport_cancel_pkt(vsk);
goto out_wait;
}
 
-- 
2.7.4



[PATCH-v5 0/4] vsock: cancel connect packets when failing to connect

2017-03-14 Thread Peng Tao
Currently, if a connect call fails on a signal or timeout (e.g., guest is still
in the process of starting up), we'll just return to caller and leave the 
connect
packet queued and they are sent even though the connection is considered a 
failure,
which can confuse applications with unwanted false connect attempt.

The patchset enables vsock (both host and guest) to cancel queued packets when
a connect attempt is considered to fail.

v5 changelog:
  - change virtio_vsock_pkt->cancel_token back to virtio_vsock_pkt->vsk
v4 changelog:
  - drop two unnecessary void * cast
  - update new callback comment
v3 changelog:
  - define cancel_pkt callback in struct vsock_transport rather than struct 
virtio_transport
  - rename virtio_vsock_pkt->vsk to virtio_vsock_pkt->cancel_token
v2 changelog:
  - fix queued_replies counting and resume tx/rx when necessary

Cheers,
Tao

Peng Tao (4):
  vsock: track pkt owner vsock
  vhost-vsock: add pkt cancel capability
  vsock: add pkt cancel capability
  vsock: cancel packets when failing to connect

 drivers/vhost/vsock.c   | 41 
 include/linux/virtio_vsock.h|  3 +++
 include/net/af_vsock.h  |  3 +++
 net/vmw_vsock/af_vsock.c| 14 +++
 net/vmw_vsock/virtio_transport.c| 42 +
 net/vmw_vsock/virtio_transport_common.c |  7 ++
 6 files changed, 110 insertions(+)

-- 
2.7.4



Re: [PATCH-v4-RESEND 1/4] vsock: track pkt owner vsock

2017-03-02 Thread Peng Tao
On Fri, Mar 3, 2017 at 5:13 AM, David Miller <da...@davemloft.net> wrote:
> From: Peng Tao <bergw...@gmail.com>
> Date: Wed,  1 Mar 2017 11:56:24 +0800
>
>> So that we can cancel a queued pkt later if necessary.
>>
>> Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
>> Signed-off-by: Peng Tao <bergw...@gmail.com>
>> ---
>>  include/linux/virtio_vsock.h| 2 ++
>>  net/vmw_vsock/virtio_transport_common.c | 7 +++
>>  2 files changed, 9 insertions(+)
>>
>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>> index 9638bfe..193ad3a 100644
>> --- a/include/linux/virtio_vsock.h
>> +++ b/include/linux/virtio_vsock.h
>> @@ -48,6 +48,7 @@ struct virtio_vsock_pkt {
>>   struct virtio_vsock_hdr hdr;
>>   struct work_struct work;
>>   struct list_head list;
>> + void *cancel_token; /* only used for cancellation */
>
> The type here is fixed, you only store vhost_sock object pointers
> here, so don't use "void *" please.
It used to be "struct vhost_sock *" but no refcount is held. Stefan
suggested to use "void *cancel_token" to make the code harder to
misuse.

Quoting Stefan:
"This field is just an opaque token used for cancellation rather than
a struct vsock_sock pointer that we are allowed to dereference.  You
could change this field to void *cancel_token to make the code harder
to misuse."

Ref:
https://www.mail-archive.com/netdev@vger.kernel.org/msg142550.html

Cheers,
Tao


[PATCH-v4-RESEND 2/4] vhost-vsock: add pkt cancel capability

2017-02-28 Thread Peng Tao
To allow canceling all packets of a connection.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Reviewed-by: Jorgen Hansen <jhan...@vmware.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 drivers/vhost/vsock.c  | 41 +
 include/net/af_vsock.h |  3 +++
 2 files changed, 44 insertions(+)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index ce5e63d..57babce 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -223,6 +223,46 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+vhost_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct vhost_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   /* Find the vhost_vsock according to guest context id  */
+   vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
+   if (!vsock)
+   return -ENODEV;
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->cancel_token != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct vhost_virtqueue *tx_vq = >vqs[VSOCK_VQ_TX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
+   vhost_poll_queue(_vq->poll);
+   }
+
+   return 0;
+}
+
 static struct virtio_vsock_pkt *
 vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
  unsigned int out, unsigned int in)
@@ -675,6 +715,7 @@ static struct virtio_transport vhost_transport = {
.release  = virtio_transport_release,
.connect  = virtio_transport_connect,
.shutdown = virtio_transport_shutdown,
+   .cancel_pkt   = vhost_transport_cancel_pkt,
 
.dgram_enqueue= virtio_transport_dgram_enqueue,
.dgram_dequeue= virtio_transport_dgram_dequeue,
diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index f275896..f32ed9a 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -100,6 +100,9 @@ struct vsock_transport {
void (*destruct)(struct vsock_sock *);
void (*release)(struct vsock_sock *);
 
+   /* Cancel all pending packets sent on vsock. */
+   int (*cancel_pkt)(struct vsock_sock *vsk);
+
/* Connections. */
int (*connect)(struct vsock_sock *);
 
-- 
2.7.4



[PATCH-v4-RESEND 3/4] vsock: add pkt cancel capability

2017-02-28 Thread Peng Tao
Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/virtio_transport.c | 42 
 1 file changed, 42 insertions(+)

diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 6788264..f5c44b5 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -213,6 +213,47 @@ virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+virtio_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct virtio_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   vsock = virtio_vsock_get();
+   if (!vsock) {
+   return -ENODEV;
+   }
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->cancel_token != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
+   new_cnt < virtqueue_get_vring_size(rx_vq))
+   queue_work(virtio_vsock_workqueue, >rx_work);
+   }
+
+   return 0;
+}
+
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
@@ -462,6 +503,7 @@ static struct virtio_transport virtio_transport = {
.release  = virtio_transport_release,
.connect  = virtio_transport_connect,
.shutdown = virtio_transport_shutdown,
+   .cancel_pkt   = virtio_transport_cancel_pkt,
 
.dgram_bind   = virtio_transport_dgram_bind,
.dgram_dequeue= virtio_transport_dgram_dequeue,
-- 
2.7.4



[PATCH-v4-RESEND 4/4] vsock: cancel packets when failing to connect

2017-02-28 Thread Peng Tao
Otherwise we'll leave the packets queued until releasing vsock device.
E.g., if guest is slow to start up, resulting ETIMEDOUT on connect, guest
will get the connect requests from failed host sockets.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Reviewed-by: Jorgen Hansen <jhan...@vmware.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/af_vsock.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 8a398b3..c73b03a 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1101,10 +1101,19 @@ static const struct proto_ops vsock_dgram_ops = {
.sendpage = sock_no_sendpage,
 };
 
+static int vsock_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   if (!transport->cancel_pkt)
+   return -EOPNOTSUPP;
+
+   return transport->cancel_pkt(vsk);
+}
+
 static void vsock_connect_timeout(struct work_struct *work)
 {
struct sock *sk;
struct vsock_sock *vsk;
+   int cancel = 0;
 
vsk = container_of(work, struct vsock_sock, dwork.work);
sk = sk_vsock(vsk);
@@ -1115,8 +1124,11 @@ static void vsock_connect_timeout(struct work_struct 
*work)
sk->sk_state = SS_UNCONNECTED;
sk->sk_err = ETIMEDOUT;
sk->sk_error_report(sk);
+   cancel = 1;
}
release_sock(sk);
+   if (cancel)
+   vsock_transport_cancel_pkt(vsk);
 
sock_put(sk);
 }
@@ -1223,11 +1235,13 @@ static int vsock_stream_connect(struct socket *sock, 
struct sockaddr *addr,
err = sock_intr_errno(timeout);
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   vsock_transport_cancel_pkt(vsk);
goto out_wait;
} else if (timeout == 0) {
err = -ETIMEDOUT;
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   vsock_transport_cancel_pkt(vsk);
goto out_wait;
}
 
-- 
2.7.4



[PATCH-v4-RESEND 0/4] vsock: cancel connect packets when failing to connect

2017-02-28 Thread Peng Tao
Hi David,

These patchsets were sent before and reviewed by Stefan and Jorgen 
[https://www.spinics.net/lists/kvm/msg142367.html].
If there is any blocker, please do tell and I'll see to it. Thanks!

Currently, if a connect call fails on a signal or timeout (e.g., guest is still
in the process of starting up), we'll just return to caller and leave the 
connect
packet queued and they are sent even though the connection is considered a 
failure,
which can confuse applications with unwanted false connect attempt.

The patchset enables vsock (both host and guest) to cancel queued packets when
a connect attempt is considered to fail.

v4 changelog:
  - drop two unnecessary void * cast
  - update new callback comment
v3 changelog:
  - define cancel_pkt callback in struct vsock_transport rather than struct 
virtio_transport
  - rename virtio_vsock_pkt->vsk to virtio_vsock_pkt->cancel_token
v2 changelog:
  - fix queued_replies counting and resume tx/rx when necessary

Cheers,
Tao

Peng Tao (4):
  vsock: track pkt owner vsock
  vhost-vsock: add pkt cancel capability
  vsock: add pkt cancel capability
  vsock: cancel packets when failing to connect

 drivers/vhost/vsock.c   | 41 
 include/linux/virtio_vsock.h|  2 ++
 include/net/af_vsock.h  |  3 +++
 net/vmw_vsock/af_vsock.c| 14 +++
 net/vmw_vsock/virtio_transport.c| 42 +
 net/vmw_vsock/virtio_transport_common.c |  7 ++
 6 files changed, 109 insertions(+)

-- 
2.7.4



[PATCH-v4-RESEND 1/4] vsock: track pkt owner vsock

2017-02-28 Thread Peng Tao
So that we can cancel a queued pkt later if necessary.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 include/linux/virtio_vsock.h| 2 ++
 net/vmw_vsock/virtio_transport_common.c | 7 +++
 2 files changed, 9 insertions(+)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 9638bfe..193ad3a 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -48,6 +48,7 @@ struct virtio_vsock_pkt {
struct virtio_vsock_hdr hdr;
struct work_struct work;
struct list_head list;
+   void *cancel_token; /* only used for cancellation */
void *buf;
u32 len;
u32 off;
@@ -56,6 +57,7 @@ struct virtio_vsock_pkt {
 
 struct virtio_vsock_pkt_info {
u32 remote_cid, remote_port;
+   struct vsock_sock *vsk;
struct msghdr *msg;
u32 pkt_len;
u16 type;
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index 849c4ad..ab505f1 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -57,6 +57,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
pkt->len= len;
pkt->hdr.len= cpu_to_le32(len);
pkt->reply  = info->reply;
+   pkt->cancel_token   = info->vsk;
 
if (info->msg && len > 0) {
pkt->buf = kmalloc(len, GFP_KERNEL);
@@ -179,6 +180,7 @@ static int virtio_transport_send_credit_update(struct 
vsock_sock *vsk,
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
.type = type,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -518,6 +520,7 @@ int virtio_transport_connect(struct vsock_sock *vsk)
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_REQUEST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -533,6 +536,7 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int 
mode)
  VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
 (mode & SEND_SHUTDOWN ?
  VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -559,6 +563,7 @@ virtio_transport_stream_enqueue(struct vsock_sock *vsk,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.msg = msg,
.pkt_len = len,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -580,6 +585,7 @@ static int virtio_transport_reset(struct vsock_sock *vsk,
.op = VIRTIO_VSOCK_OP_RST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.reply = !!pkt,
+   .vsk = vsk,
};
 
/* Send RST only if the original pkt is not a RST pkt */
@@ -825,6 +831,7 @@ virtio_transport_send_response(struct vsock_sock *vsk,
.remote_cid = le64_to_cpu(pkt->hdr.src_cid),
.remote_port = le32_to_cpu(pkt->hdr.src_port),
.reply = true,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
-- 
2.7.4



Re: [PATCH v4 0/4] vsock: cancel connect packets when failing to connect

2017-01-05 Thread Peng Tao
On Tue, Dec 13, 2016 at 5:50 PM, Stefan Hajnoczi <stefa...@gmail.com> wrote:
> On Mon, Dec 12, 2016 at 08:21:05PM +0800, Peng Tao wrote:
>> Currently, if a connect call fails on a signal or timeout (e.g., guest is 
>> still
>> in the process of starting up), we'll just return to caller and leave the 
>> connect
>> packet queued and they are sent even though the connection is considered a 
>> failure,
>> which can confuse applications with unwanted false connect attempt.
>>
>> The patchset enables vsock (both host and guest) to cancel queued packets 
>> when
>> a connect attempt is considered to fail.
>>
>> v4 changelog:
>>   - drop two unnecessary void * cast
>>   - update new callback commnet
>> v3 changelog:
>>   - define cancel_pkt callback in struct vsock_transport rather than struct 
>> virtio_transport
>>   - rename virtio_vsock_pkt->vsk to virtio_vsock_pkt->cancel_token
>> v2 changelog:
>>   - fix queued_replies counting and resume tx/rx when necessary
>>
>> Cheers,
>> Tao
>>
>> Peng Tao (4):
>>   vsock: track pkt owner vsock
>>   vhost-vsock: add pkt cancel capability
>>   vsock: add pkt cancel capability
>>   vsock: cancel packets when failing to connect
>>
>>  drivers/vhost/vsock.c   | 41 
>> 
>>  include/linux/virtio_vsock.h|  2 ++
>>  include/net/af_vsock.h  |  3 +++
>>  net/vmw_vsock/af_vsock.c| 14 +++
>>  net/vmw_vsock/virtio_transport.c| 42 
>> +
>>  net/vmw_vsock/virtio_transport_common.c |  7 ++
>>  6 files changed, 109 insertions(+)
>>
>> --
>> 2.7.4
>>
>> ___
>> Virtualization mailing list
>> virtualizat...@lists.linux-foundation.org
>> https://lists.linuxfoundation.org/mailman/listinfo/virtualization
>
> Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
ping~

It looks like the patchsets are reviewed but not merged. Is there any blocker?

Cheers,
Tao


[PATCH v4 2/4] vhost-vsock: add pkt cancel capability

2016-12-12 Thread Peng Tao
To allow canceling all packets of a connection.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 drivers/vhost/vsock.c  | 41 +
 include/net/af_vsock.h |  3 +++
 2 files changed, 44 insertions(+)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index a504e2e0..fef8808 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -218,6 +218,46 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+vhost_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct vhost_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   /* Find the vhost_vsock according to guest context id  */
+   vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
+   if (!vsock)
+   return -ENODEV;
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->cancel_token != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct vhost_virtqueue *tx_vq = >vqs[VSOCK_VQ_TX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
+   vhost_poll_queue(_vq->poll);
+   }
+
+   return 0;
+}
+
 static struct virtio_vsock_pkt *
 vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
  unsigned int out, unsigned int in)
@@ -664,6 +704,7 @@ static struct virtio_transport vhost_transport = {
.release  = virtio_transport_release,
.connect  = virtio_transport_connect,
.shutdown = virtio_transport_shutdown,
+   .cancel_pkt   = vhost_transport_cancel_pkt,
 
.dgram_enqueue= virtio_transport_dgram_enqueue,
.dgram_dequeue= virtio_transport_dgram_dequeue,
diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index f275896..f32ed9a 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -100,6 +100,9 @@ struct vsock_transport {
void (*destruct)(struct vsock_sock *);
void (*release)(struct vsock_sock *);
 
+   /* Cancel all pending packets sent on vsock. */
+   int (*cancel_pkt)(struct vsock_sock *vsk);
+
/* Connections. */
int (*connect)(struct vsock_sock *);
 
-- 
2.7.4



[PATCH v4 1/4] vsock: track pkt owner vsock

2016-12-12 Thread Peng Tao
So that we can cancel a queued pkt later if necessary.

Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 include/linux/virtio_vsock.h| 2 ++
 net/vmw_vsock/virtio_transport_common.c | 7 +++
 2 files changed, 9 insertions(+)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 9638bfe..193ad3a 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -48,6 +48,7 @@ struct virtio_vsock_pkt {
struct virtio_vsock_hdr hdr;
struct work_struct work;
struct list_head list;
+   void *cancel_token; /* only used for cancellation */
void *buf;
u32 len;
u32 off;
@@ -56,6 +57,7 @@ struct virtio_vsock_pkt {
 
 struct virtio_vsock_pkt_info {
u32 remote_cid, remote_port;
+   struct vsock_sock *vsk;
struct msghdr *msg;
u32 pkt_len;
u16 type;
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index a53b3a1..ef94eb8 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -57,6 +57,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
pkt->len= len;
pkt->hdr.len= cpu_to_le32(len);
pkt->reply  = info->reply;
+   pkt->cancel_token   = info->vsk;
 
if (info->msg && len > 0) {
pkt->buf = kmalloc(len, GFP_KERNEL);
@@ -180,6 +181,7 @@ static int virtio_transport_send_credit_update(struct 
vsock_sock *vsk,
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
.type = type,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -519,6 +521,7 @@ int virtio_transport_connect(struct vsock_sock *vsk)
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_REQUEST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -534,6 +537,7 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int 
mode)
  VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
 (mode & SEND_SHUTDOWN ?
  VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -560,6 +564,7 @@ virtio_transport_stream_enqueue(struct vsock_sock *vsk,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.msg = msg,
.pkt_len = len,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -581,6 +586,7 @@ static int virtio_transport_reset(struct vsock_sock *vsk,
.op = VIRTIO_VSOCK_OP_RST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.reply = !!pkt,
+   .vsk = vsk,
};
 
/* Send RST only if the original pkt is not a RST pkt */
@@ -826,6 +832,7 @@ virtio_transport_send_response(struct vsock_sock *vsk,
.remote_cid = le32_to_cpu(pkt->hdr.src_cid),
.remote_port = le32_to_cpu(pkt->hdr.src_port),
.reply = true,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
-- 
2.7.4



[PATCH v4 4/4] vsock: cancel packets when failing to connect

2016-12-12 Thread Peng Tao
Otherwise we'll leave the packets queued until releasing vsock device.
E.g., if guest is slow to start up, resulting ETIMEDOUT on connect, guest
will get the connect requests from failed host sockets.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Reviewed-by: Jorgen Hansen <jhan...@vmware.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/af_vsock.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 8a398b3..c73b03a 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1101,10 +1101,19 @@ static const struct proto_ops vsock_dgram_ops = {
.sendpage = sock_no_sendpage,
 };
 
+static int vsock_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   if (!transport->cancel_pkt)
+   return -EOPNOTSUPP;
+
+   return transport->cancel_pkt(vsk);
+}
+
 static void vsock_connect_timeout(struct work_struct *work)
 {
struct sock *sk;
struct vsock_sock *vsk;
+   int cancel = 0;
 
vsk = container_of(work, struct vsock_sock, dwork.work);
sk = sk_vsock(vsk);
@@ -1115,8 +1124,11 @@ static void vsock_connect_timeout(struct work_struct 
*work)
sk->sk_state = SS_UNCONNECTED;
sk->sk_err = ETIMEDOUT;
sk->sk_error_report(sk);
+   cancel = 1;
}
release_sock(sk);
+   if (cancel)
+   vsock_transport_cancel_pkt(vsk);
 
sock_put(sk);
 }
@@ -1223,11 +1235,13 @@ static int vsock_stream_connect(struct socket *sock, 
struct sockaddr *addr,
err = sock_intr_errno(timeout);
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   vsock_transport_cancel_pkt(vsk);
goto out_wait;
} else if (timeout == 0) {
err = -ETIMEDOUT;
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   vsock_transport_cancel_pkt(vsk);
goto out_wait;
}
 
-- 
2.7.4



[PATCH v4 3/4] vsock: add pkt cancel capability

2016-12-12 Thread Peng Tao
Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/virtio_transport.c | 42 
 1 file changed, 42 insertions(+)

diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 936d7ee..b7b78ce 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -170,6 +170,47 @@ virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+virtio_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct virtio_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   vsock = virtio_vsock_get();
+   if (!vsock) {
+   return -ENODEV;
+   }
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->cancel_token != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
+   new_cnt < virtqueue_get_vring_size(rx_vq))
+   queue_work(virtio_vsock_workqueue, >rx_work);
+   }
+
+   return 0;
+}
+
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
@@ -419,6 +460,7 @@ static struct virtio_transport virtio_transport = {
.release  = virtio_transport_release,
.connect  = virtio_transport_connect,
.shutdown = virtio_transport_shutdown,
+   .cancel_pkt   = virtio_transport_cancel_pkt,
 
.dgram_bind   = virtio_transport_dgram_bind,
.dgram_dequeue= virtio_transport_dgram_dequeue,
-- 
2.7.4



[PATCH v4 0/4] vsock: cancel connect packets when failing to connect

2016-12-12 Thread Peng Tao
Currently, if a connect call fails on a signal or timeout (e.g., guest is still
in the process of starting up), we'll just return to caller and leave the 
connect
packet queued and they are sent even though the connection is considered a 
failure,
which can confuse applications with unwanted false connect attempt.

The patchset enables vsock (both host and guest) to cancel queued packets when
a connect attempt is considered to fail.

v4 changelog:
  - drop two unnecessary void * cast
  - update new callback commnet
v3 changelog:
  - define cancel_pkt callback in struct vsock_transport rather than struct 
virtio_transport
  - rename virtio_vsock_pkt->vsk to virtio_vsock_pkt->cancel_token
v2 changelog:
  - fix queued_replies counting and resume tx/rx when necessary

Cheers,
Tao

Peng Tao (4):
  vsock: track pkt owner vsock
  vhost-vsock: add pkt cancel capability
  vsock: add pkt cancel capability
  vsock: cancel packets when failing to connect

 drivers/vhost/vsock.c   | 41 
 include/linux/virtio_vsock.h|  2 ++
 include/net/af_vsock.h  |  3 +++
 net/vmw_vsock/af_vsock.c| 14 +++
 net/vmw_vsock/virtio_transport.c| 42 +
 net/vmw_vsock/virtio_transport_common.c |  7 ++
 6 files changed, 109 insertions(+)

-- 
2.7.4



Re: [PATCH v3 2/4] vhost-vsock: add pkt cancel capability

2016-12-12 Thread Peng Tao
On Mon, Dec 12, 2016 at 6:37 PM, Jorgen S. Hansen <jhan...@vmware.com> wrote:
>
>> On Dec 8, 2016, at 6:12 PM, Peng Tao <bergw...@gmail.com> wrote:
>>
>> --- a/include/net/af_vsock.h
>> +++ b/include/net/af_vsock.h
>> @@ -100,6 +100,9 @@ struct vsock_transport {
>>   void (*destruct)(struct vsock_sock *);
>>   void (*release)(struct vsock_sock *);
>>
>> + /* Cancel packets belonging the same vsock */
>
> How about “/* Cancel all pending packets sent on vsock. */“ ?
>
Sure. I'll update it.

Thanks,
Tao


Re: [PATCH v3 0/4] vsock: cancel connect packets when failing to connect

2016-12-12 Thread Peng Tao
On Fri, Dec 9, 2016 at 6:18 PM, Stefan Hajnoczi <stefa...@gmail.com> wrote:
> On Fri, Dec 09, 2016 at 01:12:32AM +0800, Peng Tao wrote:
>> Currently, if a connect call fails on a signal or timeout (e.g., guest is 
>> still
>> in the process of starting up), we'll just return to caller and leave the 
>> connect
>> packet queued and they are sent even though the connection is considered a 
>> failure,
>> which can confuse applications with unwanted false connect attempt.
>>
>> The patchset enables vsock (both host and guest) to cancel queued packets 
>> when
>> a connect attempt is considered to fail.
>>
>> v3 changelog:
>>   - define cancel_pkt callback in struct vsock_transport rather than struct 
>> virtio_transport
>>   - rename virtio_vsock_pkt->vsk to virtio_vsock_pkt->cancel_token
>> v2 changelog:
>>   - fix queued_replies counting and resume tx/rx when necessary
>>
>>
>> Peng Tao (4):
>>   vsock: track pkt owner vsock
>>   vhost-vsock: add pkt cancel capability
>>   vsock: add pkt cancel capability
>>   vsock: cancel packets when failing to connect
>>
>>  drivers/vhost/vsock.c   | 41 
>> 
>>  include/linux/virtio_vsock.h|  2 ++
>>  include/net/af_vsock.h  |  3 +++
>>  net/vmw_vsock/af_vsock.c| 14 +++
>>  net/vmw_vsock/virtio_transport.c| 42 
>> +
>>  net/vmw_vsock/virtio_transport_common.c |  7 ++
>>  6 files changed, 109 insertions(+)
>
> I'm happy although I pointed out two unnecessary (void*) casts.
>
> Please wait for Jorgen to go happy on the af_vsock.c changes before
> applying.
Thanks for reviewing!

Jorgen, would you please see if the changes to af_vsock.c is OK to you?

Cheers,
Tao


[PATCH v3 3/4] vsock: add pkt cancel capability

2016-12-08 Thread Peng Tao
Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/virtio_transport.c | 42 
 1 file changed, 42 insertions(+)

diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 936d7ee..95c1162 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -170,6 +170,47 @@ virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+virtio_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct virtio_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   vsock = virtio_vsock_get();
+   if (!vsock) {
+   return -ENODEV;
+   }
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->cancel_token != (void *)vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
+   new_cnt < virtqueue_get_vring_size(rx_vq))
+   queue_work(virtio_vsock_workqueue, >rx_work);
+   }
+
+   return 0;
+}
+
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
@@ -419,6 +460,7 @@ static struct virtio_transport virtio_transport = {
.release  = virtio_transport_release,
.connect  = virtio_transport_connect,
.shutdown = virtio_transport_shutdown,
+   .cancel_pkt   = virtio_transport_cancel_pkt,
 
.dgram_bind   = virtio_transport_dgram_bind,
.dgram_dequeue= virtio_transport_dgram_dequeue,
-- 
2.7.4



[PATCH v3 2/4] vhost-vsock: add pkt cancel capability

2016-12-08 Thread Peng Tao
To allow canceling all packets of a connection.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 drivers/vhost/vsock.c  | 41 +
 include/net/af_vsock.h |  3 +++
 2 files changed, 44 insertions(+)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index a504e2e0..db64d51 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -218,6 +218,46 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+vhost_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct vhost_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   /* Find the vhost_vsock according to guest context id  */
+   vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
+   if (!vsock)
+   return -ENODEV;
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->cancel_token != (void *)vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct vhost_virtqueue *tx_vq = >vqs[VSOCK_VQ_TX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
+   vhost_poll_queue(_vq->poll);
+   }
+
+   return 0;
+}
+
 static struct virtio_vsock_pkt *
 vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
  unsigned int out, unsigned int in)
@@ -664,6 +704,7 @@ static struct virtio_transport vhost_transport = {
.release  = virtio_transport_release,
.connect  = virtio_transport_connect,
.shutdown = virtio_transport_shutdown,
+   .cancel_pkt   = vhost_transport_cancel_pkt,
 
.dgram_enqueue= virtio_transport_dgram_enqueue,
.dgram_dequeue= virtio_transport_dgram_dequeue,
diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index f275896..ce5f100 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -100,6 +100,9 @@ struct vsock_transport {
void (*destruct)(struct vsock_sock *);
void (*release)(struct vsock_sock *);
 
+   /* Cancel packets belonging the same vsock */
+   int (*cancel_pkt)(struct vsock_sock *vsk);
+
/* Connections. */
int (*connect)(struct vsock_sock *);
 
-- 
2.7.4



[PATCH v3 1/4] vsock: track pkt owner vsock

2016-12-08 Thread Peng Tao
So that we can cancel a queued pkt later if necessary.

Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 include/linux/virtio_vsock.h| 2 ++
 net/vmw_vsock/virtio_transport_common.c | 7 +++
 2 files changed, 9 insertions(+)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 9638bfe..193ad3a 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -48,6 +48,7 @@ struct virtio_vsock_pkt {
struct virtio_vsock_hdr hdr;
struct work_struct work;
struct list_head list;
+   void *cancel_token; /* only used for cancellation */
void *buf;
u32 len;
u32 off;
@@ -56,6 +57,7 @@ struct virtio_vsock_pkt {
 
 struct virtio_vsock_pkt_info {
u32 remote_cid, remote_port;
+   struct vsock_sock *vsk;
struct msghdr *msg;
u32 pkt_len;
u16 type;
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index a53b3a1..ef94eb8 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -57,6 +57,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
pkt->len= len;
pkt->hdr.len= cpu_to_le32(len);
pkt->reply  = info->reply;
+   pkt->cancel_token   = info->vsk;
 
if (info->msg && len > 0) {
pkt->buf = kmalloc(len, GFP_KERNEL);
@@ -180,6 +181,7 @@ static int virtio_transport_send_credit_update(struct 
vsock_sock *vsk,
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
.type = type,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -519,6 +521,7 @@ int virtio_transport_connect(struct vsock_sock *vsk)
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_REQUEST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -534,6 +537,7 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int 
mode)
  VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
 (mode & SEND_SHUTDOWN ?
  VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -560,6 +564,7 @@ virtio_transport_stream_enqueue(struct vsock_sock *vsk,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.msg = msg,
.pkt_len = len,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -581,6 +586,7 @@ static int virtio_transport_reset(struct vsock_sock *vsk,
.op = VIRTIO_VSOCK_OP_RST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.reply = !!pkt,
+   .vsk = vsk,
};
 
/* Send RST only if the original pkt is not a RST pkt */
@@ -826,6 +832,7 @@ virtio_transport_send_response(struct vsock_sock *vsk,
.remote_cid = le32_to_cpu(pkt->hdr.src_cid),
.remote_port = le32_to_cpu(pkt->hdr.src_port),
.reply = true,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
-- 
2.7.4



[PATCH v3 0/4] vsock: cancel connect packets when failing to connect

2016-12-08 Thread Peng Tao
Currently, if a connect call fails on a signal or timeout (e.g., guest is still
in the process of starting up), we'll just return to caller and leave the 
connect
packet queued and they are sent even though the connection is considered a 
failure,
which can confuse applications with unwanted false connect attempt.

The patchset enables vsock (both host and guest) to cancel queued packets when
a connect attempt is considered to fail.

v3 changelog:
  - define cancel_pkt callback in struct vsock_transport rather than struct 
virtio_transport
  - rename virtio_vsock_pkt->vsk to virtio_vsock_pkt->cancel_token
v2 changelog:
  - fix queued_replies counting and resume tx/rx when necessary


Peng Tao (4):
  vsock: track pkt owner vsock
  vhost-vsock: add pkt cancel capability
  vsock: add pkt cancel capability
  vsock: cancel packets when failing to connect

 drivers/vhost/vsock.c   | 41 
 include/linux/virtio_vsock.h|  2 ++
 include/net/af_vsock.h  |  3 +++
 net/vmw_vsock/af_vsock.c| 14 +++
 net/vmw_vsock/virtio_transport.c| 42 +
 net/vmw_vsock/virtio_transport_common.c |  7 ++
 6 files changed, 109 insertions(+)

-- 
2.7.4



[PATCH v3 4/4] vsock: cancel packets when failing to connect

2016-12-08 Thread Peng Tao
Otherwise we'll leave the packets queued until releasing vsock device.
E.g., if guest is slow to start up, resulting ETIMEDOUT on connect, guest
will get the connect requests from failed host sockets.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/af_vsock.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 8a398b3..c73b03a 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1101,10 +1101,19 @@ static const struct proto_ops vsock_dgram_ops = {
.sendpage = sock_no_sendpage,
 };
 
+static int vsock_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   if (!transport->cancel_pkt)
+   return -EOPNOTSUPP;
+
+   return transport->cancel_pkt(vsk);
+}
+
 static void vsock_connect_timeout(struct work_struct *work)
 {
struct sock *sk;
struct vsock_sock *vsk;
+   int cancel = 0;
 
vsk = container_of(work, struct vsock_sock, dwork.work);
sk = sk_vsock(vsk);
@@ -1115,8 +1124,11 @@ static void vsock_connect_timeout(struct work_struct 
*work)
sk->sk_state = SS_UNCONNECTED;
sk->sk_err = ETIMEDOUT;
sk->sk_error_report(sk);
+   cancel = 1;
}
release_sock(sk);
+   if (cancel)
+   vsock_transport_cancel_pkt(vsk);
 
sock_put(sk);
 }
@@ -1223,11 +1235,13 @@ static int vsock_stream_connect(struct socket *sock, 
struct sockaddr *addr,
err = sock_intr_errno(timeout);
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   vsock_transport_cancel_pkt(vsk);
goto out_wait;
} else if (timeout == 0) {
err = -ETIMEDOUT;
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   vsock_transport_cancel_pkt(vsk);
goto out_wait;
}
 
-- 
2.7.4



[PATCH-RESEND] vhost-vsock: fix orphan connection reset

2016-12-08 Thread Peng Tao
local_addr.svm_cid is host cid. We should check guest cid instead,
which is remote_addr.svm_cid. Otherwise we end up resetting all
connections to all guests.

Cc: sta...@vger.kernel.org [4.8+]
Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
resending because the last attempt looks to be dropped by vger.
 drivers/vhost/vsock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index e3b30ea..a504e2e0 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -506,7 +506,7 @@ static void vhost_vsock_reset_orphans(struct sock *sk)
 * executing.
 */
 
-   if (!vhost_vsock_get(vsk->local_addr.svm_cid)) {
+   if (!vhost_vsock_get(vsk->remote_addr.svm_cid)) {
sock_set_flag(sk, SOCK_DONE);
vsk->peer_shutdown = SHUTDOWN_MASK;
sk->sk_state = SS_UNCONNECTED;
-- 
2.7.4



Re: [PATCH v2 4/4] vsock: cancel packets when failing to connect

2016-12-08 Thread Peng Tao
On Thu, Dec 8, 2016 at 5:24 PM, Stefan Hajnoczi <stefa...@gmail.com> wrote:
> On Wed, Dec 07, 2016 at 11:14:12PM +0800, Peng Tao wrote:
>> Otherwise we'll leave the packets queued until releasing vsock device.
>> E.g., if guest is slow to start up, resulting ETIMEDOUT on connect, guest
>> will get the connect requests from failed host sockets.
>>
>> Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
>> Signed-off-by: Peng Tao <bergw...@gmail.com>
>> ---
>>  include/linux/virtio_vsock.h| 7 +++
>>  net/vmw_vsock/af_vsock.c| 7 +++
>>  net/vmw_vsock/virtio_transport_common.c | 7 ---
>>  3 files changed, 14 insertions(+), 7 deletions(-)
>>
>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>> index b92e88d..ff6850a 100644
>> --- a/include/linux/virtio_vsock.h
>> +++ b/include/linux/virtio_vsock.h
>> @@ -156,4 +156,11 @@ void virtio_transport_inc_tx_pkt(struct 
>> virtio_vsock_sock *vvs, struct virtio_vs
>>  u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
>>  void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
>>
>> +static inline const struct virtio_transport *virtio_transport_get_ops(void)
>> +{
>> + const struct vsock_transport *t = vsock_core_get_transport();
>> +
>> + return container_of(t, struct virtio_transport, transport);
>> +}
>> +
>>  #endif /* _LINUX_VIRTIO_VSOCK_H */
>> diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
>> index 8a398b3..ebb50d6 100644
>> --- a/net/vmw_vsock/af_vsock.c
>> +++ b/net/vmw_vsock/af_vsock.c
>> @@ -104,6 +104,7 @@
>>  #include 
>>  #include 
>>  #include 
>> +#include 
>>  #include 
>>  #include 
>>
>> @@ -1105,6 +1106,7 @@ static void vsock_connect_timeout(struct work_struct 
>> *work)
>>  {
>>   struct sock *sk;
>>   struct vsock_sock *vsk;
>> + int cancel = 0;
>>
>>   vsk = container_of(work, struct vsock_sock, dwork.work);
>>   sk = sk_vsock(vsk);
>> @@ -1115,8 +1117,11 @@ static void vsock_connect_timeout(struct work_struct 
>> *work)
>>   sk->sk_state = SS_UNCONNECTED;
>>   sk->sk_err = ETIMEDOUT;
>>   sk->sk_error_report(sk);
>> + cancel = 1;
>>   }
>>   release_sock(sk);
>> + if (cancel)
>> + virtio_transport_get_ops()->cancel_pkt(vsk);
>
> This doesn't work with the VMCI transport.  Remember af_vsock.c is
> common code shared by all transports.
>
> You need to add a struct vsock_transport->cancel_pkt() callback instead
> os a struct virtio_transport->cancel_pkt() callback.  And you need to
> handle the case where cancel_pkt == NULL if you don't implement it for
> VMCI.
>
I see. Thanks for reviewing! I'll fix it and resend later.

Cheers,
Tao


Re: [PATCH v2 1/4] vsock: track pkt owner vsock

2016-12-08 Thread Peng Tao
On Thu, Dec 8, 2016 at 5:30 PM, Stefan Hajnoczi <stefa...@gmail.com> wrote:
> On Wed, Dec 07, 2016 at 11:14:09PM +0800, Peng Tao wrote:
>> So that we can cancel a queued pkt later if necessary.
>>
>> Signed-off-by: Peng Tao <bergw...@gmail.com>
>> ---
>>  include/linux/virtio_vsock.h| 2 ++
>>  net/vmw_vsock/virtio_transport_common.c | 7 +++
>>  2 files changed, 9 insertions(+)
>>
>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>> index 9638bfe..6dd3242 100644
>> --- a/include/linux/virtio_vsock.h
>> +++ b/include/linux/virtio_vsock.h
>> @@ -48,6 +48,7 @@ struct virtio_vsock_pkt {
>>   struct virtio_vsock_hdr hdr;
>>   struct work_struct work;
>>   struct list_head list;
>> + struct vsock_sock *vsk;
>
> To prevent future bugs, please add a comment here:
> /* socket refcnt not held, only use for cancellation */
>
> This field is just an opaque token used for cancellation rather than a
> struct vsock_sock pointer that we are allowed to dereference.  You could
> change this field to void *cancel_token to make the code harder to
> misuse.
Will do. Thanks!

Cheers,
Tao


[PATCH v2 4/4] vsock: cancel packets when failing to connect

2016-12-07 Thread Peng Tao
Otherwise we'll leave the packets queued until releasing vsock device.
E.g., if guest is slow to start up, resulting ETIMEDOUT on connect, guest
will get the connect requests from failed host sockets.

Reviewed-by: Stefan Hajnoczi <stefa...@redhat.com>
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 include/linux/virtio_vsock.h| 7 +++
 net/vmw_vsock/af_vsock.c| 7 +++
 net/vmw_vsock/virtio_transport_common.c | 7 ---
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index b92e88d..ff6850a 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -156,4 +156,11 @@ void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock 
*vvs, struct virtio_vs
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
 
+static inline const struct virtio_transport *virtio_transport_get_ops(void)
+{
+   const struct vsock_transport *t = vsock_core_get_transport();
+
+   return container_of(t, struct virtio_transport, transport);
+}
+
 #endif /* _LINUX_VIRTIO_VSOCK_H */
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 8a398b3..ebb50d6 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -104,6 +104,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -1105,6 +1106,7 @@ static void vsock_connect_timeout(struct work_struct 
*work)
 {
struct sock *sk;
struct vsock_sock *vsk;
+   int cancel = 0;
 
vsk = container_of(work, struct vsock_sock, dwork.work);
sk = sk_vsock(vsk);
@@ -1115,8 +1117,11 @@ static void vsock_connect_timeout(struct work_struct 
*work)
sk->sk_state = SS_UNCONNECTED;
sk->sk_err = ETIMEDOUT;
sk->sk_error_report(sk);
+   cancel = 1;
}
release_sock(sk);
+   if (cancel)
+   virtio_transport_get_ops()->cancel_pkt(vsk);
 
sock_put(sk);
 }
@@ -1223,11 +1228,13 @@ static int vsock_stream_connect(struct socket *sock, 
struct sockaddr *addr,
err = sock_intr_errno(timeout);
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   virtio_transport_get_ops()->cancel_pkt(vsk);
goto out_wait;
} else if (timeout == 0) {
err = -ETIMEDOUT;
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   virtio_transport_get_ops()->cancel_pkt(vsk);
goto out_wait;
}
 
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index cc1eeb5..72c5dff 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -25,13 +25,6 @@
 /* How long to wait for graceful shutdown of a connection */
 #define VSOCK_CLOSE_TIMEOUT (8 * HZ)
 
-static const struct virtio_transport *virtio_transport_get_ops(void)
-{
-   const struct vsock_transport *t = vsock_core_get_transport();
-
-   return container_of(t, struct virtio_transport, transport);
-}
-
 struct virtio_vsock_pkt *
 virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
   size_t len,
-- 
2.7.4



[PATCH v2 1/4] vsock: track pkt owner vsock

2016-12-07 Thread Peng Tao
So that we can cancel a queued pkt later if necessary.

Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 include/linux/virtio_vsock.h| 2 ++
 net/vmw_vsock/virtio_transport_common.c | 7 +++
 2 files changed, 9 insertions(+)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 9638bfe..6dd3242 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -48,6 +48,7 @@ struct virtio_vsock_pkt {
struct virtio_vsock_hdr hdr;
struct work_struct work;
struct list_head list;
+   struct vsock_sock *vsk;
void *buf;
u32 len;
u32 off;
@@ -56,6 +57,7 @@ struct virtio_vsock_pkt {
 
 struct virtio_vsock_pkt_info {
u32 remote_cid, remote_port;
+   struct vsock_sock *vsk;
struct msghdr *msg;
u32 pkt_len;
u16 type;
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index a53b3a1..cc1eeb5 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -57,6 +57,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
pkt->len= len;
pkt->hdr.len= cpu_to_le32(len);
pkt->reply  = info->reply;
+   pkt->vsk= info->vsk;
 
if (info->msg && len > 0) {
pkt->buf = kmalloc(len, GFP_KERNEL);
@@ -180,6 +181,7 @@ static int virtio_transport_send_credit_update(struct 
vsock_sock *vsk,
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
.type = type,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -519,6 +521,7 @@ int virtio_transport_connect(struct vsock_sock *vsk)
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_REQUEST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -534,6 +537,7 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int 
mode)
  VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
 (mode & SEND_SHUTDOWN ?
  VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -560,6 +564,7 @@ virtio_transport_stream_enqueue(struct vsock_sock *vsk,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.msg = msg,
.pkt_len = len,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -581,6 +586,7 @@ static int virtio_transport_reset(struct vsock_sock *vsk,
.op = VIRTIO_VSOCK_OP_RST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.reply = !!pkt,
+   .vsk = vsk,
};
 
/* Send RST only if the original pkt is not a RST pkt */
@@ -826,6 +832,7 @@ virtio_transport_send_response(struct vsock_sock *vsk,
.remote_cid = le32_to_cpu(pkt->hdr.src_cid),
.remote_port = le32_to_cpu(pkt->hdr.src_port),
.reply = true,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
-- 
2.7.4



[PATCH v2 2/4] vhost-vsock: add pkt cancel capability

2016-12-07 Thread Peng Tao
To allow canceling all packets of a connection.

Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 drivers/vhost/vsock.c| 41 +
 include/linux/virtio_vsock.h |  3 +++
 2 files changed, 44 insertions(+)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index a504e2e0..d01e4a4 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -218,6 +218,46 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+vhost_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct vhost_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   /* Find the vhost_vsock according to guest context id  */
+   vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
+   if (!vsock)
+   return -ENODEV;
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->vsk != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct vhost_virtqueue *tx_vq = >vqs[VSOCK_VQ_TX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
+   vhost_poll_queue(_vq->poll);
+   }
+
+   return 0;
+}
+
 static struct virtio_vsock_pkt *
 vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
  unsigned int out, unsigned int in)
@@ -698,6 +738,7 @@ static struct virtio_transport vhost_transport = {
},
 
.send_pkt = vhost_transport_send_pkt,
+   .cancel_pkt = vhost_transport_cancel_pkt,
 };
 
 static int __init vhost_vsock_init(void)
diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 6dd3242..b92e88d 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -72,6 +72,9 @@ struct virtio_transport {
 
/* Takes ownership of the packet */
int (*send_pkt)(struct virtio_vsock_pkt *pkt);
+
+   /* Cancel packets belonging the same vsock */
+   int (*cancel_pkt)(struct vsock_sock *vsk);
 };
 
 ssize_t
-- 
2.7.4



[PATCH v2 3/4] vsock: add pkt cancel capability

2016-12-07 Thread Peng Tao
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/virtio_transport.c | 42 
 1 file changed, 42 insertions(+)

diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 936d7ee..a5f3833 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -170,6 +170,47 @@ virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+virtio_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct virtio_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   vsock = virtio_vsock_get();
+   if (!vsock) {
+   return -ENODEV;
+   }
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->vsk != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   if (cnt) {
+   struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
+   int new_cnt;
+
+   new_cnt = atomic_sub_return(cnt, >queued_replies);
+   if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
+   new_cnt < virtqueue_get_vring_size(rx_vq))
+   queue_work(virtio_vsock_workqueue, >rx_work);
+   }
+
+   return 0;
+}
+
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
@@ -453,6 +494,7 @@ static struct virtio_transport virtio_transport = {
},
 
.send_pkt = virtio_transport_send_pkt,
+   .cancel_pkt = virtio_transport_cancel_pkt,
 };
 
 static int virtio_vsock_probe(struct virtio_device *vdev)
-- 
2.7.4



[PATCH v2 0/4] vsock: cancel connect packets when failing to connect

2016-12-07 Thread Peng Tao
Currently, if a connect call fails on a signal or timeout (e.g., guest is still
in the process of starting up), we'll just return to caller and leave the 
connect
packet queued and they are sent even though the connection is considered a 
failure,
which can confuse applications with unwanted false connect attempt.

The patchset enables vsock (both host and guest) to cancel queued packets when
a connect attempt is considered to fail.

v2 changelog:
  - fix queued_replies counting and resume tx/rx when necessary

Peng Tao (4):
  vsock: track pkt owner vsock
  vhost-vsock: add pkt cancel capability
  vsock: add pkt cancel capability
  vsock: cancel packets when failing to connect

 drivers/vhost/vsock.c   | 41 
 include/linux/virtio_vsock.h| 12 ++
 net/vmw_vsock/af_vsock.c|  7 ++
 net/vmw_vsock/virtio_transport.c| 42 +
 net/vmw_vsock/virtio_transport_common.c | 14 +--
 5 files changed, 109 insertions(+), 7 deletions(-)

-- 
2.7.4



Re: [PATCH 3/4] vsock: add pkt cancel capability

2016-12-07 Thread Peng Tao
On Wed, Dec 7, 2016 at 9:22 PM, Stefan Hajnoczi <stefa...@gmail.com> wrote:
> On Wed, Dec 07, 2016 at 06:00:20PM +0800, Peng Tao wrote:
>> Signed-off-by: Peng Tao <bergw...@gmail.com>
>> ---
>>  net/vmw_vsock/virtio_transport.c | 36 
>>  1 file changed, 36 insertions(+)
>>
>> diff --git a/net/vmw_vsock/virtio_transport.c 
>> b/net/vmw_vsock/virtio_transport.c
>> index 936d7ee..f88b6ed 100644
>> --- a/net/vmw_vsock/virtio_transport.c
>> +++ b/net/vmw_vsock/virtio_transport.c
>> @@ -170,6 +170,41 @@ virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
>>   return len;
>>  }
>>
>> +static int
>> +virtio_transport_cancel_pkt(struct vsock_sock *vsk)
>> +{
>> + struct virtio_vsock *vsock;
>> + struct virtio_vsock_pkt *pkt, *n;
>> + int cnt = 0;
>> + LIST_HEAD(freeme);
>> +
>> + vsock = virtio_vsock_get();
>> + if (!vsock) {
>> + return -ENODEV;
>> + }
>> +
>> + if (pkt->reply)
>
> pkt is uninitialized.  I guess this if statement should be deleted, you
> already take care of counting reply packets below.
>
ah, sorry! I forgot to delete it after handling cnt below...

>> + cnt++;
>> +
>> + spin_lock_bh(>send_pkt_list_lock);
>> + list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
>> + if (pkt->vsk != vsk)
>> + continue;
>> + list_move(>list, );
>> + }
>> + spin_unlock_bh(>send_pkt_list_lock);
>> +
>> + list_for_each_entry_safe(pkt, n, , list) {
>> + if (pkt->reply)
>> + cnt++;
>> + list_del(>list);
>> + virtio_transport_free_pkt(pkt);
>> + }
>> + atomic_sub(cnt, >queued_replies);
>
> If we stopped rx because there were too many replies in flight then we
> might be able to resume rx now:
>
> /* Do we now have resources to resume rx processing? */
> if (old_val >= virtqueue_get_vring_size(rx_vq) &&
> new_val < virtqueue_get_vring_size(rx_vq))
> queue_work(virtio_vsock_workqueue, >rx_work);
Thanks! I totally missed the resume part... I'll send an updated version later.

Cheers,
Tao


[PATCH 3/4] vsock: add pkt cancel capability

2016-12-07 Thread Peng Tao
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 net/vmw_vsock/virtio_transport.c | 36 
 1 file changed, 36 insertions(+)

diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 936d7ee..f88b6ed 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -170,6 +170,41 @@ virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+virtio_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct virtio_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   int cnt = 0;
+   LIST_HEAD(freeme);
+
+   vsock = virtio_vsock_get();
+   if (!vsock) {
+   return -ENODEV;
+   }
+
+   if (pkt->reply)
+   cnt++;
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->vsk != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   if (pkt->reply)
+   cnt++;
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+   atomic_sub(cnt, >queued_replies);
+
+   return 0;
+}
+
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
@@ -453,6 +488,7 @@ static struct virtio_transport virtio_transport = {
},
 
.send_pkt = virtio_transport_send_pkt,
+   .cancel_pkt = virtio_transport_cancel_pkt,
 };
 
 static int virtio_vsock_probe(struct virtio_device *vdev)
-- 
2.7.4



[PATCH 4/4] vsock: cancel packets when failing to connect

2016-12-07 Thread Peng Tao
Otherwise we'll leave the packets queued until releasing vsock device.
E.g., if guest is slow to start up, resulting ETIMEDOUT on connect, guest
will get the connect requests from failed host sockets.

Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 include/linux/virtio_vsock.h| 7 +++
 net/vmw_vsock/af_vsock.c| 7 +++
 net/vmw_vsock/virtio_transport_common.c | 7 ---
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index b92e88d..ff6850a 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -156,4 +156,11 @@ void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock 
*vvs, struct virtio_vs
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
 
+static inline const struct virtio_transport *virtio_transport_get_ops(void)
+{
+   const struct vsock_transport *t = vsock_core_get_transport();
+
+   return container_of(t, struct virtio_transport, transport);
+}
+
 #endif /* _LINUX_VIRTIO_VSOCK_H */
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 8a398b3..ebb50d6 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -104,6 +104,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -1105,6 +1106,7 @@ static void vsock_connect_timeout(struct work_struct 
*work)
 {
struct sock *sk;
struct vsock_sock *vsk;
+   int cancel = 0;
 
vsk = container_of(work, struct vsock_sock, dwork.work);
sk = sk_vsock(vsk);
@@ -1115,8 +1117,11 @@ static void vsock_connect_timeout(struct work_struct 
*work)
sk->sk_state = SS_UNCONNECTED;
sk->sk_err = ETIMEDOUT;
sk->sk_error_report(sk);
+   cancel = 1;
}
release_sock(sk);
+   if (cancel)
+   virtio_transport_get_ops()->cancel_pkt(vsk);
 
sock_put(sk);
 }
@@ -1223,11 +1228,13 @@ static int vsock_stream_connect(struct socket *sock, 
struct sockaddr *addr,
err = sock_intr_errno(timeout);
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   virtio_transport_get_ops()->cancel_pkt(vsk);
goto out_wait;
} else if (timeout == 0) {
err = -ETIMEDOUT;
sk->sk_state = SS_UNCONNECTED;
sock->state = SS_UNCONNECTED;
+   virtio_transport_get_ops()->cancel_pkt(vsk);
goto out_wait;
}
 
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index cc1eeb5..72c5dff 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -25,13 +25,6 @@
 /* How long to wait for graceful shutdown of a connection */
 #define VSOCK_CLOSE_TIMEOUT (8 * HZ)
 
-static const struct virtio_transport *virtio_transport_get_ops(void)
-{
-   const struct vsock_transport *t = vsock_core_get_transport();
-
-   return container_of(t, struct virtio_transport, transport);
-}
-
 struct virtio_vsock_pkt *
 virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
   size_t len,
-- 
2.7.4



[PATCH 1/4] vsock: track pkt owner vsock

2016-12-07 Thread Peng Tao
So that we can cancel a queued pkt later if necessary.

Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 include/linux/virtio_vsock.h| 2 ++
 net/vmw_vsock/virtio_transport_common.c | 7 +++
 2 files changed, 9 insertions(+)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 9638bfe..6dd3242 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -48,6 +48,7 @@ struct virtio_vsock_pkt {
struct virtio_vsock_hdr hdr;
struct work_struct work;
struct list_head list;
+   struct vsock_sock *vsk;
void *buf;
u32 len;
u32 off;
@@ -56,6 +57,7 @@ struct virtio_vsock_pkt {
 
 struct virtio_vsock_pkt_info {
u32 remote_cid, remote_port;
+   struct vsock_sock *vsk;
struct msghdr *msg;
u32 pkt_len;
u16 type;
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index a53b3a1..cc1eeb5 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -57,6 +57,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
pkt->len= len;
pkt->hdr.len= cpu_to_le32(len);
pkt->reply  = info->reply;
+   pkt->vsk= info->vsk;
 
if (info->msg && len > 0) {
pkt->buf = kmalloc(len, GFP_KERNEL);
@@ -180,6 +181,7 @@ static int virtio_transport_send_credit_update(struct 
vsock_sock *vsk,
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
.type = type,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -519,6 +521,7 @@ int virtio_transport_connect(struct vsock_sock *vsk)
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_REQUEST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -534,6 +537,7 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int 
mode)
  VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
 (mode & SEND_SHUTDOWN ?
  VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -560,6 +564,7 @@ virtio_transport_stream_enqueue(struct vsock_sock *vsk,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.msg = msg,
.pkt_len = len,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
@@ -581,6 +586,7 @@ static int virtio_transport_reset(struct vsock_sock *vsk,
.op = VIRTIO_VSOCK_OP_RST,
.type = VIRTIO_VSOCK_TYPE_STREAM,
.reply = !!pkt,
+   .vsk = vsk,
};
 
/* Send RST only if the original pkt is not a RST pkt */
@@ -826,6 +832,7 @@ virtio_transport_send_response(struct vsock_sock *vsk,
.remote_cid = le32_to_cpu(pkt->hdr.src_cid),
.remote_port = le32_to_cpu(pkt->hdr.src_port),
.reply = true,
+   .vsk = vsk,
};
 
return virtio_transport_send_pkt_info(vsk, );
-- 
2.7.4



[PATCH 2/4] vhost-vsock: add pkt cancel capability

2016-12-07 Thread Peng Tao
To allow canceling all packets of a connection.

Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 drivers/vhost/vsock.c| 29 +
 include/linux/virtio_vsock.h |  3 +++
 2 files changed, 32 insertions(+)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index a504e2e0..0c23b55 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -218,6 +218,34 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return len;
 }
 
+static int
+vhost_transport_cancel_pkt(struct vsock_sock *vsk)
+{
+   struct vhost_vsock *vsock;
+   struct virtio_vsock_pkt *pkt, *n;
+   LIST_HEAD(freeme);
+
+   /* Find the vhost_vsock according to guest context id  */
+   vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
+   if (!vsock)
+   return -ENODEV;
+
+   spin_lock_bh(>send_pkt_list_lock);
+   list_for_each_entry_safe(pkt, n, >send_pkt_list, list) {
+   if (pkt->vsk != vsk)
+   continue;
+   list_move(>list, );
+   }
+   spin_unlock_bh(>send_pkt_list_lock);
+
+   list_for_each_entry_safe(pkt, n, , list) {
+   list_del(>list);
+   virtio_transport_free_pkt(pkt);
+   }
+
+   return 0;
+}
+
 static struct virtio_vsock_pkt *
 vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
  unsigned int out, unsigned int in)
@@ -698,6 +726,7 @@ static struct virtio_transport vhost_transport = {
},
 
.send_pkt = vhost_transport_send_pkt,
+   .cancel_pkt = vhost_transport_cancel_pkt,
 };
 
 static int __init vhost_vsock_init(void)
diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 6dd3242..b92e88d 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -72,6 +72,9 @@ struct virtio_transport {
 
/* Takes ownership of the packet */
int (*send_pkt)(struct virtio_vsock_pkt *pkt);
+
+   /* Cancel packets belonging the same vsock */
+   int (*cancel_pkt)(struct vsock_sock *vsk);
 };
 
 ssize_t
-- 
2.7.4



[PATCH 0/4] vsock: cancel connect packets when failing to connect

2016-12-07 Thread Peng Tao
Currently, if a connect call fails on a signal or timeout (e.g., guest is still
in the process of starting up), we'll just return to caller and leave the 
connect
packet queued and they are sent even though the connection is considered a 
failure,
which can confuse applications with unwanted false connect attempt.

The patchset enables vsock (both host and guest) to cancel queued packets when
a connect attempt is considered to fail.

Peng Tao (4):
  vsock: track pkt owner vsock
  vhost-vsock: add pkt cancel capability
  vsock: add pkt cancel capability
  vsock: cancel packets when failing to connect

 drivers/vhost/vsock.c   | 29 ++
 include/linux/virtio_vsock.h| 12 +++
 net/vmw_vsock/af_vsock.c|  7 +++
 net/vmw_vsock/virtio_transport.c| 36 +
 net/vmw_vsock/virtio_transport_common.c | 14 ++---
 5 files changed, 91 insertions(+), 7 deletions(-)

-- 
2.7.4



[PATCH 2/2] vhost: remove unnecessary smp_mb from vhost_work_queue

2016-12-07 Thread Peng Tao
test_and_set_bit() already implies a memory barrier.

Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 drivers/vhost/vhost.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index c6f2d89..2663543 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -261,8 +261,8 @@ void vhost_work_queue(struct vhost_dev *dev, struct 
vhost_work *work)
if (!test_and_set_bit(VHOST_WORK_QUEUED, >flags)) {
/* We can only add the work to the list after we're
 * sure it was not in the list.
+* test_and_set_bit() implies a memory barrier.
 */
-   smp_mb();
llist_add(>node, >work_list);
wake_up_process(dev->worker);
}
-- 
2.7.4



[PATCH 1/2] vhost-vsock: remove unused vq variable

2016-12-07 Thread Peng Tao
Signed-off-by: Peng Tao <bergw...@gmail.com>
---
 drivers/vhost/vsock.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 0c23b55..3e01d58 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -195,7 +195,6 @@ static int
 vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
 {
struct vhost_vsock *vsock;
-   struct vhost_virtqueue *vq;
int len = pkt->len;
 
/* Find the vhost_vsock according to guest context id  */
@@ -205,8 +204,6 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
return -ENODEV;
}
 
-   vq = >vqs[VSOCK_VQ_RX];
-
if (pkt->reply)
atomic_inc(>queued_replies);
 
-- 
2.7.4