Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp785604ybe; Fri, 13 Sep 2019 06:22:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqzmsoe6JCWJrndsn4Iea4BwTzsVMfNRcmgXova/SpMN/Vs7rMMUuIWEs0F76dXd6OhRrYuO X-Received: by 2002:a50:e885:: with SMTP id f5mr46677460edn.163.1568380921336; Fri, 13 Sep 2019 06:22:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568380921; cv=none; d=google.com; s=arc-20160816; b=tMcrclhSTCovENEgIBFHeBi13+/lJGv0A075b8YPXE50e43QDKhz935KMH4bfyvJgO DZYVM0L0Dxvn3CwPSD3kDSWhicghGRQW91zSduxV7T9Kv0GzW1ZXDchxVBhODRutz7lX iZsk3Te+Ao+pWtZO3hPKt7lpUv42bDVh6W0AlPy1qiostYv1bH7WL+4Ia7g5LvJMh+k3 hOvfrGGPNM4jmr2ltt4VbmV5WYhOKTb4TM/jk31v8UXGEZyQuume6h1QeixG1UFPpLRH wnLTzZBCyob8AE0/JVvgQr5Zg3SDmNu83fiH/XLfRs1kQpYe3s+wekhEvvTYJxPlYYwv Erjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=TsfML3YppDagW+2eUKKXh2FsBV7HZqxW/9pwU9VCip8=; b=PFzDaTzsfUHXn0FH+AmxGi8DKawf09BSd1Ba3w3kPkbbHHMscZt+5rBlwGGgUtwaLX qTZuqcl8rRqvOZTFwmGqagUYUCep/Um07q/mCt1PD/wOJjxLtFgmYfxUebFuoWa8AmU6 sVgDRdrrlyCUHXwEkjZ73jKMWgeLCb/l78XDn140YNaURDemvgm70hYGptAjDTAFpK/c 6//y9WwoMSulOulcj+vH+rpJSPk9GPW5c009/9W101UeVEZWNR8OlWjU2qwNgbJmyDKC eQHx7NOLqkxUfjs0Vl9saFmK5DE3BU4TjtwUs4fvIbuztxUUi/lzf995b/pftfg1zGN9 1HvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GFBbK+aP; 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 br21si12483557ejb.62.2019.09.13.06.21.38; Fri, 13 Sep 2019 06:22:01 -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; dkim=pass header.i=@kernel.org header.s=default header.b=GFBbK+aP; 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 S2390333AbfIMNS7 (ORCPT + 99 others); Fri, 13 Sep 2019 09:18:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:46748 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390322AbfIMNS4 (ORCPT ); Fri, 13 Sep 2019 09:18:56 -0400 Received: from localhost (unknown [104.132.45.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A7A2E20717; Fri, 13 Sep 2019 13:18:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1568380736; bh=hOxgvApp/1gfEFAkPo+InjWQiHi/zzIKADkmRTtA6vM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GFBbK+aP1pvRXqsOgQEpoPXeI0QTICive7aYjf/sJK/gh75SfjL6PkLgiHeFp2BkY Fxkbtr7PrIAIO1t175XERKouFiLWoPQPgD7c6TXnPxQeqCQ2vFnEy72BRi4jNxGu6J PxdIRBUdt3fa1ozgNiPJ0zj6L1mcdn9hPX2/G3Vs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ronnie Sahlberg , Steve French , Pavel Shilovsky , Sasha Levin Subject: [PATCH 4.19 140/190] cifs: add spinlock for the openFileList to cifsInodeInfo Date: Fri, 13 Sep 2019 14:06:35 +0100 Message-Id: <20190913130611.187003688@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190913130559.669563815@linuxfoundation.org> References: <20190913130559.669563815@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 487317c99477d00f22370625d53be3239febabbe ] We can not depend on the tcon->open_file_lock here since in multiuser mode we may have the same file/inode open via multiple different tcons. The current code is race prone and will crash if one user deletes a file at the same time a different user opens/create the file. To avoid this we need to have a spinlock attached to the inode and not the tcon. RHBZ: 1580165 CC: Stable Signed-off-by: Ronnie Sahlberg Signed-off-by: Steve French Reviewed-by: Pavel Shilovsky Signed-off-by: Sasha Levin --- fs/cifs/cifsfs.c | 1 + fs/cifs/cifsglob.h | 5 +++++ fs/cifs/file.c | 8 ++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/fs/cifs/cifsfs.c b/fs/cifs/cifsfs.c index fb32f3d6925e8..64e3888f30e6d 100644 --- a/fs/cifs/cifsfs.c +++ b/fs/cifs/cifsfs.c @@ -292,6 +292,7 @@ cifs_alloc_inode(struct super_block *sb) cifs_inode->uniqueid = 0; cifs_inode->createtime = 0; cifs_inode->epoch = 0; + spin_lock_init(&cifs_inode->open_file_lock); generate_random_uuid(cifs_inode->lease_key); /* diff --git a/fs/cifs/cifsglob.h b/fs/cifs/cifsglob.h index 0ee0072c1f362..57af9bac0045a 100644 --- a/fs/cifs/cifsglob.h +++ b/fs/cifs/cifsglob.h @@ -1287,6 +1287,7 @@ struct cifsInodeInfo { struct rw_semaphore lock_sem; /* protect the fields above */ /* BB add in lists for dirty pages i.e. write caching info for oplock */ struct list_head openFileList; + spinlock_t open_file_lock; /* protects openFileList */ __u32 cifsAttrs; /* e.g. DOS archive bit, sparse, compressed, system */ unsigned int oplock; /* oplock/lease level we have */ unsigned int epoch; /* used to track lease state changes */ @@ -1687,10 +1688,14 @@ require use of the stronger protocol */ * tcp_ses_lock protects: * list operations on tcp and SMB session lists * tcon->open_file_lock protects the list of open files hanging off the tcon + * inode->open_file_lock protects the openFileList hanging off the inode * cfile->file_info_lock protects counters and fields in cifs file struct * f_owner.lock protects certain per file struct operations * mapping->page_lock protects certain per page operations * + * Note that the cifs_tcon.open_file_lock should be taken before + * not after the cifsInodeInfo.open_file_lock + * * Semaphores * ---------- * sesSem operations on smb session diff --git a/fs/cifs/file.c b/fs/cifs/file.c index 933013543edab..8703b5f26f452 100644 --- a/fs/cifs/file.c +++ b/fs/cifs/file.c @@ -336,10 +336,12 @@ cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, list_add(&cfile->tlist, &tcon->openFileList); /* if readable file instance put first in list*/ + spin_lock(&cinode->open_file_lock); if (file->f_mode & FMODE_READ) list_add(&cfile->flist, &cinode->openFileList); else list_add_tail(&cfile->flist, &cinode->openFileList); + spin_unlock(&cinode->open_file_lock); spin_unlock(&tcon->open_file_lock); if (fid->purge_cache) @@ -411,7 +413,9 @@ void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, bool wait_oplock_handler) cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open); /* remove it from the lists */ + spin_lock(&cifsi->open_file_lock); list_del(&cifs_file->flist); + spin_unlock(&cifsi->open_file_lock); list_del(&cifs_file->tlist); if (list_empty(&cifsi->openFileList)) { @@ -1929,10 +1933,10 @@ refind_writable: if (!rc) return inv_file; else { - spin_lock(&tcon->open_file_lock); + spin_lock(&cifs_inode->open_file_lock); list_move_tail(&inv_file->flist, &cifs_inode->openFileList); - spin_unlock(&tcon->open_file_lock); + spin_unlock(&cifs_inode->open_file_lock); cifsFileInfo_put(inv_file); ++refind; inv_file = NULL; -- 2.20.1