Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp881091pxb; Wed, 3 Mar 2021 19:32:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJzNWnlc86vJnHhNE6RLryVIfVgY3Bh6r3BsQTiAu0rcv1o169hxwHoh1pQ9UkTNs86W0N3Z X-Received: by 2002:aa7:d4d2:: with SMTP id t18mr2192486edr.342.1614828755768; Wed, 03 Mar 2021 19:32:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614828755; cv=none; d=google.com; s=arc-20160816; b=XSBDOvlrYA1wquP4UmpQBy77pAvu4NS6U19sLm+S1uUgMjfjl0twxDBN3lC/nNVai/ Sl9TEUKQ1Qx1oiqSFTkLaNMX4gviIMMDuehWHXF816D/8egVEuLMNxVlOcfRArinkLQp WnWTOxNjUFmQFJLNokRErKmMs3IU8JJARVrc6UZE8/RQ1wC9947d0tW+6eAqM9Fox7Pf v/gWzi1VPC630d6hkASE2oghBPzWUCN5PRutgIt0mEiXoRxVVhopw0AyL9TGWXl2pSlh D2TM/5k/fRodFZmk54AFIdvDM0Kdby/OqCXIoSZeLcNI2sIJoTUTSzk6WA9dczY8PPJK kZ3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dVrErrLrbIvCSAt5stUEnecnCAlcF0ZzdPGJwWEDNqc=; b=uoljrqkfhHndJLiUHiorlSxo6CCp0+gChP7dzJkHDFjcumQuc6EMnL8t3zr4O7WZ1w aCwtSOA8ZUI+OZtMmrCEOeFLW0svGwwnWzPJtu5O7kGBGMWoZU2qa6ka0AEjlJriKrhX dyf1+5dSsh9EAxCc0aIiJnTjaHplUEFJugZwUvysuzNqSzZgq3R6KGDqDKs+6rVRyT3c jKbxMqFApXYbWYaiyG5XBD7Dhws6YrpKOLjR8LetPZ7fpsDzGfIOEgTHsC6fNGSBgOrG GxMqou3Mwv8WIraSmCaHiyRFBDmoAKoBaRZbH1cLSKNv/7gc9qtZEYbKq8jdoYnWFNvG oe1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KqH2vZK5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q4si16932279edh.345.2021.03.03.19.32.13; Wed, 03 Mar 2021 19:32:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KqH2vZK5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379407AbhCBB20 (ORCPT + 99 others); Mon, 1 Mar 2021 20:28:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:42626 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241272AbhCATPh (ORCPT ); Mon, 1 Mar 2021 14:15:37 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 11FD1650EA; Mon, 1 Mar 2021 17:00:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614618056; bh=fXtFIMZtE9I6C5BeRdh6N29j8VYPPOpgMmyNRXa+uu8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KqH2vZK51N2+NXZN2dlkSvQEnjvx6o48xS7b604N9zFLsDoHuUlLQElq6gKUMvzvs TWLSDs9IPP2tqccBT5mKrQMyNC5WukWAvrcstsSbsU4DiSJA8ig5zdzwyEblAz5btJ wLOLjaEy7A6rEOSsTVMaaOJHOk9JTMDKz8u2YioY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Huang Jianan , Chao Yu , Gao Xiang Subject: [PATCH 5.4 261/340] erofs: initialized fields can only be observed after bit is set Date: Mon, 1 Mar 2021 17:13:25 +0100 Message-Id: <20210301161101.139901797@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161048.294656001@linuxfoundation.org> References: <20210301161048.294656001@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Gao Xiang commit ce063129181312f8781a047a50be439c5859747b upstream. Currently, although set_bit() & test_bit() pairs are used as a fast- path for initialized configurations. However, these atomic ops are actually relaxed forms. Instead, load-acquire & store-release form is needed to make sure uninitialized fields won't be observed in advance here (yet no such corresponding bitops so use full barriers instead.) Link: https://lore.kernel.org/r/20210209130618.15838-1-hsiangkao@aol.com Fixes: 62dc45979f3f ("staging: erofs: fix race of initializing xattrs of a inode at the same time") Fixes: 152a333a5895 ("staging: erofs: add compacted compression indexes support") Cc: # 5.3+ Reported-by: Huang Jianan Reviewed-by: Chao Yu Signed-off-by: Gao Xiang Signed-off-by: Greg Kroah-Hartman --- fs/erofs/xattr.c | 10 +++++++++- fs/erofs/zmap.c | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -48,8 +48,14 @@ static int init_inode_xattrs(struct inod int ret = 0; /* the most case is that xattrs of this inode are initialized. */ - if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) + if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) { + /* + * paired with smp_mb() at the end of the function to ensure + * fields will only be observed after the bit is set. + */ + smp_mb(); return 0; + } if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE)) return -ERESTARTSYS; @@ -137,6 +143,8 @@ static int init_inode_xattrs(struct inod } xattr_iter_end(&it, atomic_map); + /* paired with smp_mb() at the beginning of the function. */ + smp_mb(); set_bit(EROFS_I_EA_INITED_BIT, &vi->flags); out_unlock: --- a/fs/erofs/zmap.c +++ b/fs/erofs/zmap.c @@ -36,8 +36,14 @@ static int fill_inode_lazy(struct inode void *kaddr; struct z_erofs_map_header *h; - if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) + if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) { + /* + * paired with smp_mb() at the end of the function to ensure + * fields will only be observed after the bit is set. + */ + smp_mb(); return 0; + } if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE)) return -ERESTARTSYS; @@ -83,6 +89,8 @@ static int fill_inode_lazy(struct inode vi->z_physical_clusterbits[1] = vi->z_logical_clusterbits + ((h->h_clusterbits >> 5) & 7); + /* paired with smp_mb() at the beginning of the function */ + smp_mb(); set_bit(EROFS_I_Z_INITED_BIT, &vi->flags); unmap_done: kunmap_atomic(kaddr);