Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22DBEC282C6 for ; Thu, 24 Jan 2019 06:31:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EE898218A2 for ; Thu, 24 Jan 2019 06:31:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726178AbfAXGbn (ORCPT ); Thu, 24 Jan 2019 01:31:43 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:48884 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726050AbfAXGbn (ORCPT ); Thu, 24 Jan 2019 01:31:43 -0500 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 0BCC411DEC2EE59489D9; Thu, 24 Jan 2019 14:31:38 +0800 (CST) Received: from huawei.com (10.90.53.225) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.408.0; Thu, 24 Jan 2019 14:31:29 +0800 From: Hou Tao To: CC: , , , , , Subject: [PATCH v3 2/2] 9p: use i_lock to protect update of inode->i_blocks under 32-bit Date: Thu, 24 Jan 2019 14:35:14 +0800 Message-ID: <20190124063514.8571-3-houtao1@huawei.com> X-Mailer: git-send-email 2.16.2.dirty In-Reply-To: <20190124063514.8571-1-houtao1@huawei.com> References: <20190124063514.8571-1-houtao1@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.90.53.225] X-CFilter-Loop: Reflected Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org When v9fs_stat2inode() is invoked concurrently under 32-bit case and CONFIG_LBDAF is enabled, multiple updates of inode->i_blocks may corrupt the value of i_blocks because the assignment of 64-bit value under 32-bit host is not atomic. Fix it by using i_lock to protect update of inode->i_blocks. Also Skip the update when it's not requested. Suggested-by: Dominique Martinet Signed-off-by: Hou Tao --- fs/9p/v9fs_vfs.h | 10 ++++++++++ fs/9p/vfs_inode.c | 7 ++++--- fs/9p/vfs_inode_dotl.c | 16 +++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/fs/9p/v9fs_vfs.h b/fs/9p/v9fs_vfs.h index 3d371b9e461a..0bd4acfdb6af 100644 --- a/fs/9p/v9fs_vfs.h +++ b/fs/9p/v9fs_vfs.h @@ -101,4 +101,14 @@ static inline void v9fs_i_size_write(struct inode *inode, loff_t i_size) if (sizeof(i_size) > sizeof(long)) spin_unlock(&inode->i_lock); } + +static inline void v9fs_i_blocks_write(struct inode *inode, blkcnt_t i_blocks) +{ + /* Avoid taking i_lock under 64-bits */ + if (sizeof(i_blocks) > sizeof(long)) + spin_lock(&inode->i_lock); + inode->i_blocks = i_blocks; + if (sizeof(i_blocks) > sizeof(long)) + spin_unlock(&inode->i_lock); +} #endif diff --git a/fs/9p/vfs_inode.c b/fs/9p/vfs_inode.c index 72b779bc0942..f05ff3cfbfe7 100644 --- a/fs/9p/vfs_inode.c +++ b/fs/9p/vfs_inode.c @@ -1218,10 +1218,11 @@ v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode, mode |= inode->i_mode & ~S_IALLUGO; inode->i_mode = mode; - if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE)) + if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE)) { v9fs_i_size_write(inode, stat->length); - /* not real number of blocks, but 512 byte ones ... */ - inode->i_blocks = (stat->length + 512 - 1) >> 9; + /* not real number of blocks, but 512 byte ones ... */ + v9fs_i_blocks_write(inode, (stat->length + 512 - 1) >> 9); + } v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR; } diff --git a/fs/9p/vfs_inode_dotl.c b/fs/9p/vfs_inode_dotl.c index a950a927a626..c1e2416d83c1 100644 --- a/fs/9p/vfs_inode_dotl.c +++ b/fs/9p/vfs_inode_dotl.c @@ -633,9 +633,10 @@ v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode, mode |= inode->i_mode & ~S_IALLUGO; inode->i_mode = mode; - if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE)) + if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE)) { v9fs_i_size_write(inode, stat->st_size); - inode->i_blocks = stat->st_blocks; + v9fs_i_blocks_write(inode, stat->st_blocks); + } } else { if (stat->st_result_mask & P9_STATS_ATIME) { inode->i_atime.tv_sec = stat->st_atime_sec; @@ -664,11 +665,12 @@ v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode, } if (stat->st_result_mask & P9_STATS_RDEV) inode->i_rdev = new_decode_dev(stat->st_rdev); - if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE) && - stat->st_result_mask & P9_STATS_SIZE) - v9fs_i_size_write(inode, stat->st_size); - if (stat->st_result_mask & P9_STATS_BLOCKS) - inode->i_blocks = stat->st_blocks; + if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE)) { + if (stat->st_result_mask & P9_STATS_SIZE) + v9fs_i_size_write(inode, stat->st_size); + if (stat->st_result_mask & P9_STATS_BLOCKS) + v9fs_i_blocks_write(inode, stat->st_blocks); + } } if (stat->st_result_mask & P9_STATS_GEN) inode->i_generation = stat->st_gen; -- 2.16.2.dirty