Received: by 10.223.185.116 with SMTP id b49csp2203410wrg; Sun, 4 Mar 2018 21:26:07 -0800 (PST) X-Google-Smtp-Source: AG47ELsuZVovyDMFsYuhG07B/YwRp7+Erm01/PH9w3oOrWyKHBGMH5T1GA719QH0QN47BthCXq/L X-Received: by 2002:a17:902:ab91:: with SMTP id f17-v6mr1793115plr.114.1520227567111; Sun, 04 Mar 2018 21:26:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520227567; cv=none; d=google.com; s=arc-20160816; b=xT5D1VesgpPVey41Z0zsD/deIC05BATMMdfJTiq2/YIkMlEyhoRngf94TG3O6VJ6sR mTMglYeJeuH44wEHut+KIm/SLSVslZURnijKzUD/IJFfOifQ5G1XnHx2TQl3+1WWUjGu bXg02fR807ZFsMB1NfTSVa9mhP5Yj/gWInx9ybnYdD86SNjEHBLO4qivq5upJtdlkdng r3nJ4gmtNVXoWPnl6xhlvvhz2WspKRq7GeDMXvqRkNxxMqdDlwp3IWEtPWfNXa9sJaRB LrnGP7lEXNU/E40GXsHvX2nsAzqrmw0h228Bt9QbrqnMYGlmxOCJQl2JcrEBmTqM5awE hNNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:reply-to:message-id:date:subject:cc:to :from:arc-authentication-results; bh=ior/fPETPrpbrtUP9jsoS9FTBw/tWXS1y235v1mfhPc=; b=iSQkIKXfhnHU1RrNKrW0KajMVX1YLpqKy2Xcdo55A+BRN60OKshoT5K0qhh5DKcmLi 4pueimG4QPYwkb7F8ABYpSH7/4dj3EcjIAtAeunxJ830jurYF5D3We38sPPzQob/W1st 4gr4vULrKLmj6CVYSkScxKGYoGDozovAeaswbXyTSbICezr9eEeabxq6LMVbpMgQH/o1 tegk/AoD9AEHZRDhKhnTiuot3LtNPLkAy17v7hA6lfMu5H3RNQERxL82CT95AO23Gl1i m9dFQDOitHPzykY1j4eYY9e+i7qKDVhUxoHwlSTBIpEKMleE6aLf84bhtpLMTKpg8D8P wiQw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=exchange.microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r65si9651070pfa.79.2018.03.04.21.25.52; Sun, 04 Mar 2018 21:26:07 -0800 (PST) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=exchange.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932709AbeCEFZN (ORCPT + 99 others); Mon, 5 Mar 2018 00:25:13 -0500 Received: from a2nlsmtp01-03.prod.iad2.secureserver.net ([198.71.225.37]:38784 "EHLO a2nlsmtp01-03.prod.iad2.secureserver.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932512AbeCEFZL (ORCPT ); Mon, 5 Mar 2018 00:25:11 -0500 Received: from linuxonhyperv2.linuxonhyperv.com ([107.180.71.197]) by : HOSTING RELAY : with SMTP id sibaeOGXmdOycsibaemne8; Sun, 04 Mar 2018 22:24:10 -0700 x-originating-ip: 107.180.71.197 Received: from kys by linuxonhyperv2.linuxonhyperv.com with local (Exim 4.89_1) (envelope-from ) id 1esiba-00054r-0Q; Sun, 04 Mar 2018 22:24:10 -0700 From: kys@exchange.microsoft.com To: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, devel@linuxdriverproject.org, olaf@aepfle.de, apw@canonical.com, vkuznets@redhat.com, jasowang@redhat.com, leann.ogasawara@canonical.com, marcelo.cerri@canonical.com, sthemmin@microsoft.com Cc: Michael Kelley , Stable , "K . Y . Srinivasan" Subject: [PATCH 1/1] Drivers: hv: vmbus: Fix ring buffer signaling Date: Sun, 4 Mar 2018 22:24:08 -0700 Message-Id: <20180305052408.19475-1-kys@exchange.microsoft.com> X-Mailer: git-send-email 2.15.1 Reply-To: kys@microsoft.com X-CMAE-Envelope: MS4wfB/+UNp7qsYPPTccJikU7t/HpZXaFZxKs24hkUywCXVAU+8hrON2HJTah7eBq6N84vvr0xo005sz77Lr5DdPsFK51hMQI+H+tK45zLs2dMx4ANPLYomv L9RhTsY3gmLQzlcT4lBX2r01qXvm07BGsnq16gJjoRiyVzegxylfQT0x72lehzR/DLS71/ZmUfMoTaaUhoSHk8sVa/NHR2NDm/SUnOYwFI+piYYZok5EFBXr X9fWlGSCghhdVAB3DUkCwAC26dIuoxdQbWeO3JoZdHFMjFTvxAHFgqvs2S3Ge1XjeVvJxsmP805/CyMiZW0djAuNlDRZj1orCdzi/NmykOZKfx24sAtyJEZ8 FoPhzgsWYbgAx6pVRLyjsMuODJFJnmG9ODgBDmTAY+on+JWkpsKWvldEZo9+Gfc9LCY7kpLcL6NIbU/krsKXBlRtsfJ71Lq4XecU9j0yL10kizSE7OdFRYO/ EoqxRdvKfoSCL3w/FObomH68tJnCozNeTnLDgDHDEg2Aib6Y0fjFcCGWiqRb1mTrg5nhTt47aagVWmPI8qtNOMvcePDIKNwoFuMF8LzfY9OXcftjVwJ7xPr3 J19Ech26MpDPB1UisnviwGSolKrJi7ifzGMJ6joZCNhKtX/0dnpfCeohLAzy5yD7w3Q= Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Michael Kelley 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 --- drivers/hv/ring_buffer.c | 52 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c index 1aa17795727b..3cd7f29906ae 100644 --- a/drivers/hv/ring_buffer.c +++ b/drivers/hv/ring_buffer.c @@ -376,13 +376,24 @@ __hv_pkt_iter_next(struct vmbus_channel *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 @@ -390,8 +401,12 @@ void hv_pkt_iter_close(struct vmbus_channel *channel) * 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: @@ -405,26 +420,29 @@ void hv_pkt_iter_close(struct vmbus_channel *channel) */ 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. 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 there was space before we began iteration, + * then host was not blocked. + */ - /* 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); } -- 2.15.1