Received: by 10.213.65.68 with SMTP id h4csp865925imn; Tue, 27 Mar 2018 10:11:48 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+nFOHSRs1rASOVsXOn09eZCalTfWk9u7AOyEcPOcYo6j6P2uhZyppb5GG6DZn3bezIU18O X-Received: by 2002:a17:902:3f83:: with SMTP id a3-v6mr135499pld.279.1522170708297; Tue, 27 Mar 2018 10:11:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522170708; cv=none; d=google.com; s=arc-20160816; b=PmC5OcTNff2nESk7r+GjhIvK1a30WMnH57sl9f6QI4016QBhIKqNJXpHuC3qBnrXEG sOd+kAxXSVszdlrRZdONYJdltqRu9wH/1Pi3Gkub7Av3A2qtPbOdhvoTKYHn9BodVMw5 CVKVybbNaoULe7SKE5ncZGSc56bjahNfBnLThs56kWsPiqF9UaBZQAEHQYEXjXV9nhd+ jHDya6Y//Q9mDKRvikCXkYFLzh6AnI+kHiMZmE9+9UZ4pPgL7zigsVMcOhTPOqmXhHop yHKswf9FlCrpEPFnWRBx9Sv0NHV+CIM3Jrx+01FYHQ6FxnXd0+C7K4Y1LWeHlrV3ldv4 NVpw== 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=VDCEyCWiheo23NCUClZtQ02hZ8Ip/tj4PWsy+2EMrFk=; b=DE92hzCVW9E+vR+0du9/hEv6+UUHPsZ8pLMP044L5XZd5tuGhZvv29YFLnNcbHJ00x 7VvdseYdYeSFIdlTImjiOxCJOl/z8MOmk++/U2KO3N8FZUznyRUSs4iKEhsLYzcdJKVL APfI/eVT4bvUTAhZ3qYO2/hahQHBhBNr1w5pwhp82/gDcbWft+Jm7za8S6Uo+At2pfap FekqU/2O5BeQWFfrUQHNiWvid79yvYTafHNsSTVVRNMOohuPzT93/OD1fpS46k9mNThC 33AXVbW+4cAQVF60CUG1Zy/wLlMUR3adnfOGBVSyMVecgQyYcOQFNcIDf5NQr47tqvT+ Ip0A== 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 v8-v6si1560641plk.659.2018.03.27.10.11.33; Tue, 27 Mar 2018 10:11:48 -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 S1754957AbeC0RKA (ORCPT + 99 others); Tue, 27 Mar 2018 13:10:00 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:47334 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932312AbeC0Qjk (ORCPT ); Tue, 27 Mar 2018 12:39:40 -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 305601027; Tue, 27 Mar 2018 16:39:40 +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.15 029/105] Drivers: hv: vmbus: Fix ring buffer signaling Date: Tue, 27 Mar 2018 18:27:09 +0200 Message-Id: <20180327162759.139951217@linuxfoundation.org> X-Mailer: git-send-email 2.16.3 In-Reply-To: <20180327162757.813009222@linuxfoundation.org> References: <20180327162757.813009222@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.15-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); }