From patchwork Tue Jul 25 21:22:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Stabellini X-Patchwork-Id: 9863827 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9B4F46038F for ; Tue, 25 Jul 2017 21:24:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8523E2860F for ; Tue, 25 Jul 2017 21:24:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7A4C828708; Tue, 25 Jul 2017 21:24:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0FB9228707 for ; Tue, 25 Jul 2017 21:24:34 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1da7Hb-0004cF-71; Tue, 25 Jul 2017 21:22:23 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1da7Ha-0004Yn-2f for xen-devel@lists.xen.org; Tue, 25 Jul 2017 21:22:22 +0000 Received: from [85.158.139.211] by server-11.bemta-5.messagelabs.com id 20/3E-01729-D86B7795; Tue, 25 Jul 2017 21:22:21 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpkkeJIrShJLcpLzFFi42I5NlE2WbdnW3m kwYp7ehZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bD8x0sBRNVKhY3T2BrYNwo18XIxSEksI5J 4vadt2xdjJwcLAIOEh8eHGHvYuTgYBSIkXjwwxokzCgQJjH58hJWEJtNwFDi75NNYOUiAtIS1 z5fZgSZwywwmVHi9btfzCAJYQELiZkfOqFmqkosvvebBcTmFXCT2L/pNFiNhICcxMljk8GGcg q4S8yfuZ0F4qB2RomPW08zT2DkXcDIsIpRozi1qCy1SNfIUC+pKDM9oyQ3MTNH19DAVC83tbg 4MT01JzGpWC85P3cTIzAg6hkYGHcw3p3sd4hRkoNJSZT3m255pBBfUn5KZUZicUZ8UWlOavEh RhkODiUJ3klbgXKCRanpqRVpmTnA0IRJS3DwKInw3tkClOYtLkjMLc5Mh0idYjTm2LB6/Rcmj lcT/n9jEmLJy89LlRLnXQoySQCkNKM0D24QLGYuMcpKCfMyMjAwCPEUpBblZpagyr9iFOdgVB LmjQSZwpOZVwK37xXQKUxAp8yZUQpySkkiQkqqgdFux/c73et/npiu372kJrHJt8KBVdbU96N n8ArfaccmPr4iar3tXG7BsZv8nnGqMdObAu5sqi5uk7aX1VY/7XFFTPbBhlfzRCQ0OswSlrZ9 OjJpbY/To1qJx2mWb2ewC3+cv2K388aDk2T3Pf7sdL+66t4hT9bApTueXjSTf97jmL0hbNqUf d5KLMUZiYZazEXFiQCiy1FblAIAAA== X-Env-Sender: sstabellini@kernel.org X-Msg-Ref: server-15.tower-206.messagelabs.com!1501017739!90475581!1 X-Originating-IP: [198.145.29.99] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.25; banners=-,-,- X-VirusChecked: Checked Received: (qmail 38595 invoked from network); 25 Jul 2017 21:22:20 -0000 Received: from mail.kernel.org (HELO mail.kernel.org) (198.145.29.99) by server-15.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 25 Jul 2017 21:22:20 -0000 Received: from localhost.localdomain (162-198-228-33.lightspeed.wlfrct.sbcglobal.net [162.198.228.33]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C642822CB1; Tue, 25 Jul 2017 21:22:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C642822CB1 From: Stefano Stabellini To: xen-devel@lists.xen.org Date: Tue, 25 Jul 2017 14:22:05 -0700 Message-Id: <1501017730-12797-8-git-send-email-sstabellini@kernel.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1501017730-12797-1-git-send-email-sstabellini@kernel.org> References: <1501017730-12797-1-git-send-email-sstabellini@kernel.org> Cc: jgross@suse.com, Stefano Stabellini , boris.ostrovsky@oracle.com, sstabellini@kernel.org, linux-kernel@vger.kernel.org Subject: [Xen-devel] [PATCH v2 08/13] xen/pvcalls: implement sendmsg X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Send data to an active socket by copying data to the "out" ring. Take the active socket out_mutex so that only one function can access the ring at any given time. If not enough room is available on the ring, rather than returning immediately or sleep-waiting, spin for up to 5000 cycles. This small optimization turns out to improve performance significantly. Signed-off-by: Stefano Stabellini CC: boris.ostrovsky@oracle.com CC: jgross@suse.com --- drivers/xen/pvcalls-front.c | 109 ++++++++++++++++++++++++++++++++++++++++++++ drivers/xen/pvcalls-front.h | 3 ++ 2 files changed, 112 insertions(+) diff --git a/drivers/xen/pvcalls-front.c b/drivers/xen/pvcalls-front.c index b8c4538..d8ed280 100644 --- a/drivers/xen/pvcalls-front.c +++ b/drivers/xen/pvcalls-front.c @@ -29,6 +29,7 @@ #define PVCALLS_INVALID_ID (UINT_MAX) #define RING_ORDER XENBUS_MAX_RING_GRANT_ORDER #define PVCALLS_NR_REQ_PER_RING __CONST_RING_SIZE(xen_pvcalls, XEN_PAGE_SIZE) +#define PVCALLS_FRONT_MAX_SPIN 5000 struct pvcalls_bedata { struct xen_pvcalls_front_ring ring; @@ -79,6 +80,22 @@ struct sock_mapping { }; }; +static int pvcalls_front_write_todo(struct sock_mapping *map) +{ + struct pvcalls_data_intf *intf = map->active.ring; + RING_IDX cons, prod, size = XEN_FLEX_RING_SIZE(intf->ring_order); + int32_t error; + + cons = intf->out_cons; + prod = intf->out_prod; + error = intf->out_error; + if (error == -ENOTCONN) + return 0; + if (error != 0) + return error; + return size - pvcalls_queued(prod, cons, size); +} + static irqreturn_t pvcalls_front_event_handler(int irq, void *dev_id) { struct xenbus_device *dev = dev_id; @@ -309,6 +326,98 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr, return ret; } +static int __write_ring(struct pvcalls_data_intf *intf, + struct pvcalls_data *data, + struct iov_iter *msg_iter, + size_t len) +{ + RING_IDX cons, prod, size, masked_prod, masked_cons; + RING_IDX array_size = XEN_FLEX_RING_SIZE(intf->ring_order); + int32_t error; + + cons = intf->out_cons; + prod = intf->out_prod; + error = intf->out_error; + /* read indexes before continuing */ + virt_mb(); + + if (error < 0) + return error; + + size = pvcalls_queued(prod, cons, array_size); + if (size >= array_size) + return 0; + if (len > array_size - size) + len = array_size - size; + + masked_prod = pvcalls_mask(prod, array_size); + masked_cons = pvcalls_mask(cons, array_size); + + if (masked_prod < masked_cons) { + copy_from_iter(data->out + masked_prod, len, msg_iter); + } else { + if (len > array_size - masked_prod) { + copy_from_iter(data->out + masked_prod, + array_size - masked_prod, msg_iter); + copy_from_iter(data->out, + len - (array_size - masked_prod), + msg_iter); + } else { + copy_from_iter(data->out + masked_prod, len, msg_iter); + } + } + /* write to ring before updating pointer */ + virt_wmb(); + intf->out_prod += len; + + return len; +} + +int pvcalls_front_sendmsg(struct socket *sock, struct msghdr *msg, + size_t len) +{ + struct pvcalls_bedata *bedata; + struct sock_mapping *map; + int sent = 0, tot_sent = 0; + int count = 0, flags; + + if (!pvcalls_front_dev) + return -ENOTCONN; + bedata = dev_get_drvdata(&pvcalls_front_dev->dev); + + map = (struct sock_mapping *) READ_ONCE(sock->sk->sk_send_head); + if (!map) + return -ENOTSOCK; + + flags = msg->msg_flags; + if (flags & (MSG_CONFIRM|MSG_DONTROUTE|MSG_EOR|MSG_OOB)) + return -EOPNOTSUPP; + + mutex_lock(&map->active.out_mutex); + if ((flags & MSG_DONTWAIT) && !pvcalls_front_write_todo(map)) { + mutex_unlock(&map->active.out_mutex); + return -EAGAIN; + } + +again: + count++; + sent = __write_ring(map->active.ring, + &map->active.data, &msg->msg_iter, + len); + if (sent > 0) { + len -= sent; + tot_sent += sent; + notify_remote_via_irq(map->active.irq); + } + if (sent >= 0 && len > 0 && count < PVCALLS_FRONT_MAX_SPIN) + goto again; + if (sent < 0) + tot_sent = sent; + + mutex_unlock(&map->active.out_mutex); + return tot_sent; +} + int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len) { struct pvcalls_bedata *bedata; diff --git a/drivers/xen/pvcalls-front.h b/drivers/xen/pvcalls-front.h index ab4f1da..d937c24 100644 --- a/drivers/xen/pvcalls-front.h +++ b/drivers/xen/pvcalls-front.h @@ -13,5 +13,8 @@ int pvcalls_front_bind(struct socket *sock, int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags); +int pvcalls_front_sendmsg(struct socket *sock, + struct msghdr *msg, + size_t len); #endif