From patchwork Wed Jan 23 21:10:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Shilovsky X-Patchwork-Id: 10777881 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9C7406C2 for ; Wed, 23 Jan 2019 21:10:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8BAE12D7B2 for ; Wed, 23 Jan 2019 21:10:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7FEE72D840; Wed, 23 Jan 2019 21:10:55 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 18E252D7B2 for ; Wed, 23 Jan 2019 21:10:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726754AbfAWVKy (ORCPT ); Wed, 23 Jan 2019 16:10:54 -0500 Received: from mail-pg1-f194.google.com ([209.85.215.194]:41391 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726345AbfAWVKy (ORCPT ); Wed, 23 Jan 2019 16:10:54 -0500 Received: by mail-pg1-f194.google.com with SMTP id m1so1625612pgq.8 for ; Wed, 23 Jan 2019 13:10:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=H3O0voJkghLaVDRQq81wH7xi53bB+6WmVAyGNfI6oXc=; b=scczZjCBukypDc48IPPWFEIbOLVszkEbIhK5phsF/eMHENrTDlsEjdpIHg3M6pn3my dNmlVcoBDYX+2PYJTWbjbwy+Y9CxlbzYyNYgi/PeJKgNyThSZ9Uf77DmHcD13qnDh2g+ tEMBrErHsDSaL6yV05gaI1av7eZTjJKbPzwyL3M6ERRnH1SJaiELAkohrCAn2NbLuoSe 3jioSSAdtM+C+txFKFq8UNp4ew9qC986ZTJIViZwor57ISDrBPj17e9NsTZRnRitZdXT m9vg0tUfqGr2ud4XHhwQuivVnbBRbX/jhU5lrA5xGmQ3kZg91lbPyJyCI39YLkDy+/Sq vqfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=H3O0voJkghLaVDRQq81wH7xi53bB+6WmVAyGNfI6oXc=; b=psyn8t3e73c2U5U/TTYEF6+i+vjVMQM/vxM66n3WCcl4xOm35isVB+BpR8ZSf2S+ZH +KmGtQ/WBHKxzU4ZtapUrzZH95R9ONN4rPiofLD7QvMuIeKQb7ZZNEGG/Lz/Ztg3ha14 LtzGjKCt8eU7mlhajG1t7M2/mYlG5qoTGrV/Ya9pm+DWE6njlNMDZuVXaZ/A2km/+9C3 /FDAyVQwII58hxc8kbGZfGaaT59CiL8bWPIfjBOMrqw19MeLKT93NtOrs4OqDDplibaW P/HeR+MfFPK/1v+cAFx3o2ZYghYasUxfU+ngg3c58pyX8juTb/GxQSbXoLC2mn1UM1WW RNUQ== X-Gm-Message-State: AJcUukeRcm039RCTGOFXWQiCUTI6X0lcG0llzoWbqE+9NBg4avVM8WIi U8xyqlFxyEXe9rp1A7O4e67PiTE= X-Google-Smtp-Source: ALg8bN7J7HtUHSHqS1BXDfKXCDLye4zywyl4OaEY3N/xcqICaN9MJBkv3Yw89tz5e1RBzp43dWY98w== X-Received: by 2002:a63:8043:: with SMTP id j64mr3530732pgd.405.1548277853179; Wed, 23 Jan 2019 13:10:53 -0800 (PST) Received: from ubuntu-vm.corp.microsoft.com ([2001:4898:80e8:f:a17f:4e9f:6b7c:507d]) by smtp.gmail.com with ESMTPSA id n73sm28063714pfj.148.2019.01.23.13.10.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 23 Jan 2019 13:10:52 -0800 (PST) From: Pavel Shilovsky X-Google-Original-From: Pavel Shilovsky To: linux-cifs@vger.kernel.org, Steve French , Ronnie Sahlberg Subject: [PATCH 01/15] CIFS: Do not reconnect TCP session in add_credits() Date: Wed, 23 Jan 2019 13:10:31 -0800 Message-Id: <1548277845-6746-2-git-send-email-pshilov@microsoft.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1548277845-6746-1-git-send-email-pshilov@microsoft.com> References: <1548277845-6746-1-git-send-email-pshilov@microsoft.com> Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When executing add_credits() we currently call cifs_reconnect() if the number of credits is zero and there are no requests in flight. In this case we may call cifs_reconnect() recursively twice and cause memory corruption given the following sequence of functions: mid1.callback() -> add_credits() -> cifs_reconnect() -> -> mid2.callback() -> add_credits() -> cifs_reconnect(). Fix this by avoiding to call cifs_reconnect() in add_credits() and checking for zero credits in the demultiplex thread. Cc: Signed-off-by: Pavel Shilovsky --- fs/cifs/connect.c | 21 +++++++++++++++++++++ fs/cifs/smb2ops.c | 29 ++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/fs/cifs/connect.c b/fs/cifs/connect.c index 3d5e308..dc02b6d 100644 --- a/fs/cifs/connect.c +++ b/fs/cifs/connect.c @@ -699,6 +699,21 @@ server_unresponsive(struct TCP_Server_Info *server) return false; } +static inline bool +zero_credits(struct TCP_Server_Info *server) +{ + int val; + + spin_lock(&server->req_lock); + val = server->credits + server->echo_credits + server->oplock_credits; + if (server->in_flight == 0 && val == 0) { + spin_unlock(&server->req_lock); + return true; + } + spin_unlock(&server->req_lock); + return false; +} + static int cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg) { @@ -711,6 +726,12 @@ cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg) for (total_read = 0; msg_data_left(smb_msg); total_read += length) { try_to_freeze(); + /* reconnect if no credits and no requests in flight */ + if (zero_credits(server)) { + cifs_reconnect(server); + return -ECONNABORTED; + } + if (server_unresponsive(server)) return -ECONNABORTED; if (cifs_rdma_enabled(server) && server->smbd_conn) diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c index 82967f6..3b6c95d 100644 --- a/fs/cifs/smb2ops.c +++ b/fs/cifs/smb2ops.c @@ -34,6 +34,7 @@ #include "cifs_ioctl.h" #include "smbdirect.h" +/* Change credits for different ops and return the total number of credits */ static int change_conf(struct TCP_Server_Info *server) { @@ -41,17 +42,15 @@ change_conf(struct TCP_Server_Info *server) server->oplock_credits = server->echo_credits = 0; switch (server->credits) { case 0: - return -1; + return 0; case 1: server->echoes = false; server->oplocks = false; - cifs_dbg(VFS, "disabling echoes and oplocks\n"); break; case 2: server->echoes = true; server->oplocks = false; server->echo_credits = 1; - cifs_dbg(FYI, "disabling oplocks\n"); break; default: server->echoes = true; @@ -64,14 +63,15 @@ change_conf(struct TCP_Server_Info *server) server->echo_credits = 1; } server->credits -= server->echo_credits + server->oplock_credits; - return 0; + return server->credits + server->echo_credits + server->oplock_credits; } static void smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add, const int optype) { - int *val, rc = 0; + int *val, rc = -1; + spin_lock(&server->req_lock); val = server->ops->get_credits_field(server, optype); @@ -101,8 +101,23 @@ smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add, } spin_unlock(&server->req_lock); wake_up(&server->request_q); - if (rc) - cifs_reconnect(server); + + switch (rc) { + case -1: + /* change_conf hasn't been executed */ + break; + case 0: + cifs_dbg(VFS, "Possible client or server bug - zero credits\n"); + break; + case 1: + cifs_dbg(VFS, "disabling echoes and oplocks\n"); + break; + case 2: + cifs_dbg(FYI, "disabling oplocks\n"); + break; + default: + cifs_dbg(FYI, "add %u credits total=%d\n", add, rc); + } } static void