Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp5904375imb; Fri, 8 Mar 2019 05:09:48 -0800 (PST) X-Google-Smtp-Source: APXvYqym294F6EfVpx/4SbtmW5zWVOQtYCtyiZ9PjbBzpnARDo46KM8IsbgFw1dZhw1fRXWehhdN X-Received: by 2002:a65:63c8:: with SMTP id n8mr16437479pgv.158.1552050588396; Fri, 08 Mar 2019 05:09:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1552050588; cv=none; d=google.com; s=arc-20160816; b=mLxay16DejCyLv6SwPEgVgsI2Ua0O/TCcwxuZ13KMxpVKTUxHZmqh/gWp9LQLbgV+e ndaUOi6i9ltejS1QLH4ySZSX33ZUd1Czo0OZVHzmgjRgbyez8GKlmzEpc8E+XCYaRCkx 5/gUL3qEnsBT/gqWQYZF97yj7kT0cv6coes2TB6J7PJa2QJivujFV3zPENyYAJ0b3yzJ b8lpjX2aCDf9sa9WIeiKFtuoKdvThl7nrmIVXQ2DEQVyYQv2ld5BtHgIIhZv7r9EEZxp u0bVUnS+0c6EgaJcGoHoHWGyw5mEoOlNWRyx+p7VW4HaMud33HtNs7p1j6ch9YB9Yyvi D3fg== 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=qhOycbuMaQKEhwAFoKLSfu/gQLmfV0yOeRbWOXivWcA=; b=OPL9AVViju1A+XFzKVuLhRTuoS+/zTuBHFbRMlGpJnOmKJHWpws9YHfWqNJ57j+GEY 5wz8VOy+6BoN/M/0dwHvJYKDDYpviqQOlbr+VdnnD9cLDveayG69PGuYJO/1S4/IO3ap ADX0F6/sH2BR2o0xEoNx0IiQgb8if4a6qQDwdybRWsS0W7zSDSbKGvzQY53DPzhI7jDq a6dvprT8xM+4maTUsEyit+9FFFvmAKoVk1JYIuzQ12j8KVABRBhsTG0th9Fq2xOybqkL LcOIScMlQHA9GQr1TDpLcLscelZGpVedvL0QO7Q2WDMY1Nsd6Pvq68iKBsrlZceQUQ9U 0c5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EPXt2I+j; 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 h8si6677741pls.365.2019.03.08.05.09.32; Fri, 08 Mar 2019 05:09:48 -0800 (PST) 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=EPXt2I+j; 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 S1727175AbfCHMxh (ORCPT + 99 others); Fri, 8 Mar 2019 07:53:37 -0500 Received: from mail.kernel.org ([198.145.29.99]:57236 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727167AbfCHMxe (ORCPT ); Fri, 8 Mar 2019 07:53:34 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 EAFF52085A; Fri, 8 Mar 2019 12:53:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552049613; bh=cmRrDCP0yk410vOws96NOfIwetHIayultEdjCOMilEk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EPXt2I+jWgZAVOnbK7xiI5t49M6hmm/v5mfJoxsB7mAS8JZQBCOW8xOU0zL70PcFP 9T2CopolIcNYyrDJxVOdncwTk8Nat6vut+zPuDL2hUkSmBqcRERTQWkyqxRqNPPcAZ pJUEIPRjAhGoTznv82rKWOF2LqgdFT5lTBhJ8MNo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chao Yu , Gao Xiang Subject: [PATCH 4.20 10/76] staging: erofs: fix race of initializing xattrs of a inode at the same time Date: Fri, 8 Mar 2019 13:49:22 +0100 Message-Id: <20190308124915.149162065@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190308124914.789210760@linuxfoundation.org> References: <20190308124914.789210760@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Gao Xiang commit 62dc45979f3f8cb0ea67302a93bff686f0c46c5a upstream. In real scenario, there could be several threads accessing xattrs of the same xattr-uninitialized inode, and init_inode_xattrs() almost at the same time. That's actually an unexpected behavior, this patch closes the race. Fixes: b17500a0fdba ("staging: erofs: introduce xattr & acl support") Cc: # 4.19+ Reviewed-by: Chao Yu Signed-off-by: Gao Xiang Signed-off-by: Greg Kroah-Hartman --- drivers/staging/erofs/internal.h | 11 +++++++--- drivers/staging/erofs/xattr.c | 41 +++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 15 deletions(-) --- a/drivers/staging/erofs/internal.h +++ b/drivers/staging/erofs/internal.h @@ -352,12 +352,17 @@ static inline erofs_off_t iloc(struct er return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits); } -#define inode_set_inited_xattr(inode) (EROFS_V(inode)->flags |= 1) -#define inode_has_inited_xattr(inode) (EROFS_V(inode)->flags & 1) +/* atomic flag definitions */ +#define EROFS_V_EA_INITED_BIT 0 + +/* bitlock definitions (arranged in reverse order) */ +#define EROFS_V_BL_XATTR_BIT (BITS_PER_LONG - 1) struct erofs_vnode { erofs_nid_t nid; - unsigned int flags; + + /* atomic flags (including bitlocks) */ + unsigned long flags; unsigned char data_mapping_mode; /* inline size in bytes */ --- a/drivers/staging/erofs/xattr.c +++ b/drivers/staging/erofs/xattr.c @@ -44,18 +44,25 @@ static inline void xattr_iter_end_final( static int init_inode_xattrs(struct inode *inode) { + struct erofs_vnode *const vi = EROFS_V(inode); struct xattr_iter it; unsigned int i; struct erofs_xattr_ibody_header *ih; struct super_block *sb; struct erofs_sb_info *sbi; - struct erofs_vnode *vi; bool atomic_map; + int ret = 0; - if (likely(inode_has_inited_xattr(inode))) + /* the most case is that xattrs of this inode are initialized. */ + if (test_bit(EROFS_V_EA_INITED_BIT, &vi->flags)) return 0; - vi = EROFS_V(inode); + if (wait_on_bit_lock(&vi->flags, EROFS_V_BL_XATTR_BIT, TASK_KILLABLE)) + return -ERESTARTSYS; + + /* someone has initialized xattrs for us? */ + if (test_bit(EROFS_V_EA_INITED_BIT, &vi->flags)) + goto out_unlock; /* * bypass all xattr operations if ->xattr_isize is not greater than @@ -68,13 +75,16 @@ static int init_inode_xattrs(struct inod if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) { errln("xattr_isize %d of nid %llu is not supported yet", vi->xattr_isize, vi->nid); - return -ENOTSUPP; + ret = -ENOTSUPP; + goto out_unlock; } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) { if (unlikely(vi->xattr_isize)) { DBG_BUGON(1); - return -EIO; /* xattr ondisk layout error */ + ret = -EIO; + goto out_unlock; /* xattr ondisk layout error */ } - return -ENOATTR; + ret = -ENOATTR; + goto out_unlock; } sb = inode->i_sb; @@ -83,8 +93,10 @@ static int init_inode_xattrs(struct inod it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize); it.page = erofs_get_inline_page(inode, it.blkaddr); - if (IS_ERR(it.page)) - return PTR_ERR(it.page); + if (IS_ERR(it.page)) { + ret = PTR_ERR(it.page); + goto out_unlock; + } /* read in shared xattr array (non-atomic, see kmalloc below) */ it.kaddr = kmap(it.page); @@ -97,7 +109,8 @@ static int init_inode_xattrs(struct inod sizeof(uint), GFP_KERNEL); if (vi->xattr_shared_xattrs == NULL) { xattr_iter_end(&it, atomic_map); - return -ENOMEM; + ret = -ENOMEM; + goto out_unlock; } /* let's skip ibody header */ @@ -114,7 +127,8 @@ static int init_inode_xattrs(struct inod if (IS_ERR(it.page)) { kfree(vi->xattr_shared_xattrs); vi->xattr_shared_xattrs = NULL; - return PTR_ERR(it.page); + ret = PTR_ERR(it.page); + goto out_unlock; } it.kaddr = kmap_atomic(it.page); @@ -127,8 +141,11 @@ static int init_inode_xattrs(struct inod } xattr_iter_end(&it, atomic_map); - inode_set_inited_xattr(inode); - return 0; + set_bit(EROFS_V_EA_INITED_BIT, &vi->flags); + +out_unlock: + clear_and_wake_up_bit(EROFS_V_BL_XATTR_BIT, &vi->flags); + return ret; } /*