From patchwork Tue Jun 30 13:04:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Tao X-Patchwork-Id: 1319863 X-Patchwork-Delegate: richard@nod.at Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=oEOlRJKs; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49x4DQ4k2Rz9sRW for ; Tue, 30 Jun 2020 22:58:46 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-ID:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4f2ye/UtesFtIBnR/eA/h72C11yC0NkhOdt8MFSX7zk=; b=oEOlRJKsMa8ubxW2dAv/tz55l WI+PiE4sLEH1CvuTjD/VlDwDtaLN9mlpyV0L+SxmygKsuX7MsVzF+wP48Hmppfya3V+d9nx4uVVz+ 9qdd+mgoXN3KdRNUVK7o1zrcs+oKhx51QI6q9WwrNgKMn0VSYxjUUqIzyRBX/NQwgS8e7gSCkxERm NVOwwpbOS8382cJUQRfwCdeb3oF73U8xjWIpAgy7+9/Cssn63L5mtKBHjcWM//nozyEaRW5LWYVMe N9ZfoxEKwyN2uK48HftaDIztlnncOv6iHfRmAFEpPI7rXxUDn6xhTFZXoK5CNfZZLra1JMBAW9qYg nQcvVAm6A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jqFpt-0008BW-47; Tue, 30 Jun 2020 12:58:05 +0000 Received: from szxga06-in.huawei.com ([45.249.212.32] helo=huawei.com) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jqFpj-000886-BC for linux-mtd@lists.infradead.org; Tue, 30 Jun 2020 12:57:56 +0000 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id EDF25D4331963481D348; Tue, 30 Jun 2020 20:57:46 +0800 (CST) Received: from huawei.com (10.90.53.225) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.487.0; Tue, 30 Jun 2020 20:57:36 +0800 From: Hou Tao To: Richard Weinberger , Subject: [PATCH 3/3] ubifs: ensure only one in-memory xattr inode is created Date: Tue, 30 Jun 2020 21:04:38 +0800 Message-ID: <20200630130438.141649-4-houtao1@huawei.com> X-Mailer: git-send-email 2.25.0.4.g0ad7144999 In-Reply-To: <20200630130438.141649-1-houtao1@huawei.com> References: <20200630130438.141649-1-houtao1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.53.225] X-CFilter-Loop: Reflected X-Spam-Note: CRM114 invocation failed X-Spam-Score: -2.3 (--) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-2.3 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [45.249.212.32 listed in list.dnswl.org] -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record 0.0 RCVD_IN_MSPIKE_H4 RBL: Very Good reputation (+4) [45.249.212.32 listed in wl.mailspike.net] 0.0 RCVD_IN_MSPIKE_WL Mailspike good senders X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: houtao1@huawei.com Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org ubifs may create two in-memory inodes for one xattr if there are concurrent ubifs_xattr_get() and ubifs_xattr_set(), as show in the following case: ubifs_xattr_get() ubifs_xattr_set() // the first created inode A // fill inode A ubifs_new_inode() ubifs_jnl_update() // mapping xattr name to inum ubifs_tnc_add_nm() // add xattr inode node ubifs_tnc_add() // find inum through xattr name ubifs_tnc_lookup_nm() iget_xattr() ubifs_iget() // not found in hash table // so create a new inode B // and keep it in hash table iget_locked() // find xattr inode node // fill inode B ubifs_tnc_lookup unlock_new_inode // inode A is also inserted into // hash table If we update the xattr value afterwards, only the values in inode A will be updated. So when we ty to remove the xattr name, and in the same time get the xattr name, ubifs_xattr_get() may return the stale value in inode B, as show in the following case: ubifs_xattr_get() ubifs_xattr_remove() // get xattr inum ubifs_tnc_lookup_nm() // return inode A iget_xattr() clear_nlink() remove_xattr() iput() evict() ubifs_evict_inode() remove_inode_hash() // return inode B // return a stale xattr value iget_xattr() Fix it by moving insert_inode_hash() before ubifs_jnl_update(), but after the initialization of inode is completed, so only one inode is created for xattr value. Signed-off-by: Hou Tao --- fs/ubifs/xattr.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/fs/ubifs/xattr.c b/fs/ubifs/xattr.c index 82be2c2d2db5..10fcb454bb01 100644 --- a/fs/ubifs/xattr.c +++ b/fs/ubifs/xattr.c @@ -133,6 +133,15 @@ static int create_xattr(struct ubifs_info *c, struct inode *host, inode->i_size = ui->ui_size = size; ui->data_len = size; + /* + * Ensure iget_xattr() in ubifs_xattr_get() will find the inode + * instead of creating a new one. + * The initialization of xattr inode is completed here, so using + * insert_inode_hash() instead of insert_inode_locked(). The + * latter can lead to iget_xattr() return -ESTALE. + */ + insert_inode_hash(inode); + mutex_lock(&host_ui->ui_mutex); host->i_ctime = current_time(host); host_ui->xattr_cnt += 1; @@ -156,7 +165,6 @@ static int create_xattr(struct ubifs_info *c, struct inode *host, mutex_unlock(&host_ui->ui_mutex); ubifs_release_budget(c, &req); - insert_inode_hash(inode); iput(inode); return 0;