From patchwork Mon Dec 5 21:11:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Shilovskiy X-Patchwork-Id: 9461509 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 1703160231 for ; Mon, 5 Dec 2016 21:11:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0E3AB28066 for ; Mon, 5 Dec 2016 21:11:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 031CA2807E; Mon, 5 Dec 2016 21:11:54 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 7B23A28066 for ; Mon, 5 Dec 2016 21:11:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752330AbcLEVLx (ORCPT ); Mon, 5 Dec 2016 16:11:53 -0500 Received: from mail-by2nam01on0102.outbound.protection.outlook.com ([104.47.34.102]:50720 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752327AbcLEVLv (ORCPT ); Mon, 5 Dec 2016 16:11:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=lFmiJ4p6CYu0D442bRwHIIMGiwiZ77QN8Bltdj4KxIk=; b=LLLbcvUGpFil7zTLBoWiPCy7WxLVJx9mRzoldJ4pKItwADhP+bIqM0ZpvlR69S19ylRaqQhWrQARTRVjR2ON87ot40kxfs63WxO8B3XPhi8MRYmhwj3uZ9u2tOgb9JwlZhrtZJEAQZUTT8EUSg2AYR449UcbdIXOZZwFZLxlKQg= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pshilov@microsoft.com; Received: from ubuntu-vm.corp.microsoft.com (2001:4898:80e8:a::63b) by MWHPR03MB2557.namprd03.prod.outlook.com (10.168.206.19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.747.13; Mon, 5 Dec 2016 21:11:41 +0000 From: Pavel Shilovsky To: linux-cifs@vger.kernel.org Subject: [PATCH v2 5/5] CIFS: Fix a possible double locking of mutex during reconnect Date: Mon, 5 Dec 2016 13:11:11 -0800 Message-Id: <1480972271-57692-6-git-send-email-pshilov@microsoft.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1480972271-57692-1-git-send-email-pshilov@microsoft.com> References: <1480972271-57692-1-git-send-email-pshilov@microsoft.com> MIME-Version: 1.0 X-Originating-IP: [2001:4898:80e8:a::63b] X-ClientProxiedBy: BY2PR06CA0069.namprd06.prod.outlook.com (10.166.106.165) To MWHPR03MB2557.namprd03.prod.outlook.com (10.168.206.19) X-MS-Office365-Filtering-Correlation-Id: 7c57d294-d038-4ccc-1d16-08d41d534f48 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:MWHPR03MB2557; X-Microsoft-Exchange-Diagnostics: 1; MWHPR03MB2557; 3:O+EfBkW0yG/VVKPDjjeT48s3wyZMhLcF2NzEAKTzcrHZYX/VICyD3f2kRAGiI+knCNVsi2Wn42OXOSn0Hf6yjsspM2j3NOhibwF1JMTkQ3KOGCGYMWZBj+gvbSExgY9lwlHEJjwY26tR5aPu2y+zB6baa7OBG+JuztE5MQX6FTvEwRcky7rPc7qoe+w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UDsaIKzYIIT1fxe1BtmlscYTB/CdzKPDUthda9QPqmla5WMrqhkSNqrOufD6E+Ul4D6I0ADsbYPmwtf9ekv+629E7Bj7ieHn/9ASOeZRWWd36dimfYPdd2U2SL+3iOkjYow2dpIRKGeXnUH44BcA1BYd5X1SoY6wRHUuXcFB/Z4gRtN8cY8Q71prpTV89JtsjUvjstDumFGOFqxjVH9mbQPpEtnyM8UqBa+O2rEc2lvx9EkUUiAXZHbcMEXeHTSalmSdFABMM707S5jDJUxl5Ku//w/nDKhgMnzltR599gzBz+pptvqP3CQBD64CgHsJT/V8TDQoGT X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(158342451672863)(9452136761055); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6055026)(6041248)(20161123558021)(20161123562025)(20161123564025)(20161123560025)(20161123555025)(6047074)(6072148); SRVR:MWHPR03MB2557; BCL:0; PCL:0; RULEID:; SRVR:MWHPR03MB2557; X-Microsoft-Exchange-Diagnostics: 1; MWHPR03MB2557; 4:VZp8qHZ02H5Dhu3w8I5npe4I4BTVLaVn7NYu6pn9fCWeIDFb6cZjTkhcRuH/D/gowRjvrAk+wmfZo71QFIX/Q85wfgh6XIkXKMnh+L7YSXgbaY+H+VnWthGXnYLPr4i8dqSi0JFXlW0bMJi65R7rYdpF32Cd8nEwNDnqHD5gEL0+3R6G3/FoleUzfQSZnYiSx8IftZBp5HoMmW/W1F7JujOmjwPR6MiTau0LN9QXEvgyLTocoiKQBDY86rXM94zkR7cbXngjUiRJBGBB3LdJP142KkImQEVyloUzZPveBC7ARE4/2HOcoX78/XMfcjhcSWiMLzb1uHX4PU6G41wAbRoBRPCQypiCXXruPzAOYv9g/QzLkihBnSXc8MaWCoQhPGrs+G9iX5aYQqapC6W0n1LX7+9vvI+Lpt9retcRQBZPlRIcsdwdooZiC96MWt7SDqf97q/gvj2zNy4Zi6X2RlfWXdeXKw7vt4NPkX+EomWfEDg3WkO2LYmXsBTLzDjvgHiXyRYxi7BT6F5cewGa7QuW68ycqXr9GKnwpMzOocLTmgIgj/DzTpGk+OkB8oMIM06+jU2LUkE+wZN/dkayjqHgnLFDafngf5GKLJvRDC3wxmMnSE5ZK6h8sT/sS7yvtyYUfG7Nfu/yT4AOXJC69dT/cOij3BGcLKpq4q0wOwuTYBZJQ1vNqiGneVHdXb+w5iV7dZwjfWuYK0dWP4rt3A== X-Forefront-PRVS: 0147E151B5 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(7916002)(189002)(199003)(69234005)(5003940100001)(36756003)(5005710100001)(10290500002)(6116002)(92566002)(450100001)(42186005)(47776003)(5660300001)(105586002)(50986999)(76176999)(68736007)(33646002)(106356001)(39450400002)(50226002)(107886002)(189998001)(10090500001)(2906002)(39850400001)(39840400001)(38730400001)(39860400001)(733004)(8676002)(6486002)(81166006)(2361001)(39410400001)(81156014)(86362001)(305945005)(6916009)(2351001)(48376002)(86612001)(575784001)(97736004)(101416001)(7736002)(6666003)(7846002)(2950100002)(110136003)(50466002); DIR:OUT; SFP:1102; SCL:1; SRVR:MWHPR03MB2557; H:ubuntu-vm.corp.microsoft.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; MWHPR03MB2557; 23:iUZGfolasJsEZPOZYQu56+MybodVcrUj3YBmo5OpI?= =?us-ascii?Q?sTAXrdVC2S5J96Zj40gck2KHNoahfWNmmL0mC9aYWoSVduh7DpBGc2TMwzu7?= =?us-ascii?Q?swcmYctRUHy5W8bcClevuZZVtifgx8af3QozayHz54ZaPXegqLbC05+OhExb?= =?us-ascii?Q?EzmxWWHADqk0RPwGJEtpZTOAx6z/nnjPXgIAQC4vMtX/ajBgtUPDfM8fzRRH?= =?us-ascii?Q?vgH4iK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Tv0c= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; MWHPR03MB2557; 7:jBgRD+0mzB2c6HxmP0i43/pW0EnXoMAmefHFa1NkNITG7HgIiK/tmXPR07Eshq5EU2nljIxlwEybJu04wxGA1HLmVf+RUf535aRl3UFu8XITUEpK5MGzcuW0s7m4iuQ2kcjV0JT/E4rLe90ERpLAj0LcQCFjDoOjZeIRVJcrMU/g1bdwr4XtfpL7j41+guJmVQNN8/0BqVdnt2O4vcvjjam1QgIG6CarnwlUVgDtFdv+DcSZHR9JX29ulJI14nykzPVV2ebExuo/kLb3f9HpJ9cpH+0eVS+rI0x/ncE8NhouWyDXFkHo4yvIiJ2n5f9y+aprJL+DhNITGafkb1sK0jgx0mnK5d7z9C48TRDEeiww6xJf7LGaBjCzMroXKDgXPdkNLOMT5ZtMqpo+QBKfMm7spfV7StdM79B8H8OAmCED9uJEUfiVoCgFAhElf2wC30oxwjAf8K9KP9X9ZHfudQ== X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Dec 2016 21:11:41.4358 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR03MB2557 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 With the current code it is possible to lock a mutex twice when a subsequent reconnects are triggered. On the 1st reconnect we reconnect sessions and tcons and then persistent file handles. If the 2nd reconnect happens during the reconnecting of persistent file handles then the following sequence of calls is observed: cifs_reopen_file -> SMB2_open -> small_smb2_init -> smb2_reconnect -> cifs_reopen_persistent_file_handles -> cifs_reopen_file (again!). So, we are trying to acquire the same cfile->fh_mutex twice which is wrong. Fix this by moving reconnecting of persistent handles to the delayed work (smb2_reconnect_server) and submitting this work every time we reconnect tcon in SMB2 commands handling codepath. This can also lead to corruption of a temporary file list in cifs_reopen_persistent_file_handles() because we can recursively call this function twice. Cc: Stable # v4.9+ Signed-off-by: Pavel Shilovsky --- fs/cifs/cifsglob.h | 1 + fs/cifs/file.c | 8 +++++++- fs/cifs/smb2pdu.c | 14 +++++++++----- fs/cifs/smb2pdu.h | 2 ++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/fs/cifs/cifsglob.h b/fs/cifs/cifsglob.h index 89a0d7f..59ab5a2 100644 --- a/fs/cifs/cifsglob.h +++ b/fs/cifs/cifsglob.h @@ -926,6 +926,7 @@ struct cifs_tcon { bool broken_posix_open; /* e.g. Samba server versions < 3.3.2, 3.2.9 */ bool broken_sparse_sup; /* if server or share does not support sparse */ bool need_reconnect:1; /* connection reset, tid now invalid */ + bool need_reopen_files:1; /* need to reopen tcon file handles */ bool use_resilient:1; /* use resilient instead of durable handles */ bool use_persistent:1; /* use persistent instead of durable handles */ #ifdef CONFIG_CIFS_SMB2 diff --git a/fs/cifs/file.c b/fs/cifs/file.c index 7f5f617..18a1e1d 100644 --- a/fs/cifs/file.c +++ b/fs/cifs/file.c @@ -777,6 +777,11 @@ cifs_reopen_persistent_handles(struct cifs_tcon *tcon) struct list_head *tmp1; struct list_head tmp_list; + if (!tcon->use_persistent || !tcon->need_reopen_files) + return; + + tcon->need_reopen_files = false; + cifs_dbg(FYI, "Reopen persistent handles"); INIT_LIST_HEAD(&tmp_list); @@ -793,7 +798,8 @@ cifs_reopen_persistent_handles(struct cifs_tcon *tcon) list_for_each_safe(tmp, tmp1, &tmp_list) { open_file = list_entry(tmp, struct cifsFileInfo, rlist); - cifs_reopen_file(open_file, false /* do not flush */); + if (cifs_reopen_file(open_file, false /* do not flush */)) + tcon->need_reopen_files = true; list_del_init(&open_file->rlist); cifsFileInfo_put(open_file); } diff --git a/fs/cifs/smb2pdu.c b/fs/cifs/smb2pdu.c index 4ba3f68..8745722 100644 --- a/fs/cifs/smb2pdu.c +++ b/fs/cifs/smb2pdu.c @@ -250,16 +250,19 @@ smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon) } cifs_mark_open_files_invalid(tcon); + if (tcon->use_persistent) + tcon->need_reopen_files = true; rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage); mutex_unlock(&tcon->ses->session_mutex); - if (tcon->use_persistent) - cifs_reopen_persistent_handles(tcon); - cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc); if (rc) goto out; + + if (smb2_command != SMB2_INTERNAL_CMD) + queue_delayed_work(cifsiod_wq, &server->reconnect, 0); + atomic_inc(&tconInfoReconnectCount); out: /* @@ -1990,7 +1993,7 @@ void smb2_reconnect_server(struct work_struct *work) spin_lock(&cifs_tcp_ses_lock); list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { - if (tcon->need_reconnect) { + if (tcon->need_reconnect || tcon->need_reopen_files) { tcon->tc_count++; list_add_tail(&tcon->rlist, &tmp_list); tcon_exist = true; @@ -2007,7 +2010,8 @@ void smb2_reconnect_server(struct work_struct *work) spin_unlock(&cifs_tcp_ses_lock); list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) { - smb2_reconnect(SMB2_ECHO, tcon); + if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon)) + cifs_reopen_persistent_handles(tcon); list_del_init(&tcon->rlist); cifs_put_tcon(tcon); } diff --git a/fs/cifs/smb2pdu.h b/fs/cifs/smb2pdu.h index fd3709e..dc0d141 100644 --- a/fs/cifs/smb2pdu.h +++ b/fs/cifs/smb2pdu.h @@ -80,6 +80,8 @@ #define SMB2_SET_INFO cpu_to_le16(SMB2_SET_INFO_HE) #define SMB2_OPLOCK_BREAK cpu_to_le16(SMB2_OPLOCK_BREAK_HE) +#define SMB2_INTERNAL_CMD cpu_to_le16(0xFFFF) + #define NUMBER_OF_SMB2_COMMANDS 0x0013 /* BB FIXME - analyze following length BB */