Received: by 10.213.65.68 with SMTP id h4csp835095imn; Tue, 27 Mar 2018 09:38:51 -0700 (PDT) X-Google-Smtp-Source: AIpwx48lR9D3yB5UJL5HmfgMArbWd2qcEhpmjSAV0VyfxXJ6OPFd7mxeAMG8wENNuEl/CPZ7zXDY X-Received: by 10.99.123.23 with SMTP id w23mr52692pgc.10.1522168730982; Tue, 27 Mar 2018 09:38:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522168730; cv=none; d=google.com; s=arc-20160816; b=CGLRyrxpstxjlpe/JInhAgbV3c6lwRYXXg1Xpu0uZ6JpqFtisK8BY0wdpO8CL0JfAG yupcMvCE40lN51Vuek0KvfvPPfFsvDm4EIEL7TdXkNitb5qM3N8YOso/xEFMYqsBSMxb d7920cBLY+/c3wO9VHd82SNGTrPTLc+ln1tK+AOg3re5AOPPQimG7R+kMSwExkuFb1wF 2JGhOCOQzKOVd/rrbP8TBVr/zgb9KviDyz+h8SdeG1brw0KS739BC+pAOoA7FGYVzryc TRiZ6KdF2on+Bu74NvMgiYj9FBzJ9sxXGj5T3ZeARYEa33CBQOqoEx4i9+Trs++2C8Ta t3UQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=qFtYFxcKZDbK/zauvn1BtMyl1xx7u1OM+1434IrLGWU=; b=r8G2DiyRezX/wn8pDF9Axm6SM/U4zLfdc+2rpeYCz+npWNwKzLeI5f3qTDyDaaBTiO QGBHCK+ArpibLSxPnv+MRgZG20rwj5NGxyZ8ud7WmJZf0xPY68CoGdshiLOAP9uN5VXb kRYnX5s8IIGtuubTOFuYrDY2vgh9n+RUFL20FfqwHTp+2cQ14vktFBZNIH6zyhsxuu9F yXSK3X3yzTdvni+ZI8vqOrxjoewwSR/DbbE1SaPpWKWGHR+KjfiqRlGSadFuoemEb4ki RKCGgMscV6ui2EHaD9Vn5uT0xjXCDfb5pH0CPAFvRARtBwoxmi5VqbgHL8xJv87JgQCG gpSQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t28si1205032pfk.187.2018.03.27.09.38.36; Tue, 27 Mar 2018 09:38:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754745AbeC0Qgi (ORCPT + 99 others); Tue, 27 Mar 2018 12:36:38 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:45804 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754728AbeC0Qgf (ORCPT ); Tue, 27 Mar 2018 12:36:35 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 1FF48DD0; Tue, 27 Mar 2018 16:36:34 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Michael Kelley , "K. Y. Srinivasan" Subject: [PATCH 4.14 029/101] Drivers: hv: vmbus: Fix ring buffer signaling Date: Tue, 27 Mar 2018 18:27:01 +0200 Message-Id: <20180327162751.771629248@linuxfoundation.org> X-Mailer: git-send-email 2.16.3 In-Reply-To: <20180327162749.993880276@linuxfoundation.org> References: <20180327162749.993880276@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Michael Kelley commit 655296c8bbeffcf020558c4455305d597a73bde1 upstream. Fix bugs in signaling the Hyper-V host when freeing space in the host->guest ring buffer: 1. The interrupt_mask must not be used to determine whether to signal on the host->guest ring buffer 2. The ring buffer write_index must be read (via hv_get_bytes_to_write) *after* pending_send_sz is read in order to avoid a race condition 3. Comparisons with pending_send_sz must treat the "equals" case as not-enough-space 4. Don't signal if the pending_send_sz feature is not present. Older versions of Hyper-V that don't implement this feature will poll. Fixes: 03bad714a161 ("vmbus: more host signalling avoidance") Cc: Stable # 4.14 and above Signed-off-by: Michael Kelley Signed-off-by: K. Y. Srinivasan Signed-off-by: Greg Kroah-Hartman --- drivers/hv/ring_buffer.c | 54 +++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 18 deletions(-) --- a/drivers/hv/ring_buffer.c +++ b/drivers/hv/ring_buffer.c @@ -394,13 +394,24 @@ __hv_pkt_iter_next(struct vmbus_channel } EXPORT_SYMBOL_GPL(__hv_pkt_iter_next); +/* How many bytes were read in this iterator cycle */ +static u32 hv_pkt_iter_bytes_read(const struct hv_ring_buffer_info *rbi, + u32 start_read_index) +{ + if (rbi->priv_read_index >= start_read_index) + return rbi->priv_read_index - start_read_index; + else + return rbi->ring_datasize - start_read_index + + rbi->priv_read_index; +} + /* * Update host ring buffer after iterating over packets. */ void hv_pkt_iter_close(struct vmbus_channel *channel) { struct hv_ring_buffer_info *rbi = &channel->inbound; - u32 orig_write_sz = hv_get_bytes_to_write(rbi); + u32 curr_write_sz, pending_sz, bytes_read, start_read_index; /* * Make sure all reads are done before we update the read index since @@ -408,8 +419,12 @@ void hv_pkt_iter_close(struct vmbus_chan * is updated. */ virt_rmb(); + start_read_index = rbi->ring_buffer->read_index; rbi->ring_buffer->read_index = rbi->priv_read_index; + if (!rbi->ring_buffer->feature_bits.feat_pending_send_sz) + return; + /* * Issue a full memory barrier before making the signaling decision. * Here is the reason for having this barrier: @@ -423,26 +438,29 @@ void hv_pkt_iter_close(struct vmbus_chan */ virt_mb(); - /* If host has disabled notifications then skip */ - if (rbi->ring_buffer->interrupt_mask) + pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz); + if (!pending_sz) return; - if (rbi->ring_buffer->feature_bits.feat_pending_send_sz) { - u32 pending_sz = READ_ONCE(rbi->ring_buffer->pending_send_sz); + /* + * Ensure the read of write_index in hv_get_bytes_to_write() + * happens after the read of pending_send_sz. + */ + virt_rmb(); + curr_write_sz = hv_get_bytes_to_write(rbi); + bytes_read = hv_pkt_iter_bytes_read(rbi, start_read_index); + + /* + * If there was space before we began iteration, + * then host was not blocked. + */ - /* - * If there was space before we began iteration, - * then host was not blocked. Also handles case where - * pending_sz is zero then host has nothing pending - * and does not need to be signaled. - */ - if (orig_write_sz > pending_sz) - return; - - /* If pending write will not fit, don't give false hope. */ - if (hv_get_bytes_to_write(rbi) < pending_sz) - return; - } + if (curr_write_sz - bytes_read > pending_sz) + return; + + /* If pending write will not fit, don't give false hope. */ + if (curr_write_sz <= pending_sz) + return; vmbus_setevent(channel); }