From patchwork Thu Mar 7 17:42:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13586091 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13F59C54E49 for ; Thu, 7 Mar 2024 17:42:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A8556B024A; Thu, 7 Mar 2024 12:42:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 857CA6B024B; Thu, 7 Mar 2024 12:42:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 71F636B024C; Thu, 7 Mar 2024 12:42:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 635296B024A for ; Thu, 7 Mar 2024 12:42:36 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3C65112052B for ; Thu, 7 Mar 2024 17:42:36 +0000 (UTC) X-FDA: 81870962712.16.60AE6D2 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 9C28E4001E for ; Thu, 7 Mar 2024 17:42:33 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YmvVsLmj; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709833353; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=KxcAy9WNvkNJRzy7bt1FHi2MeZVtNJQxQcatjk/mVUc=; b=04GwVyCAhVikkb33BH3rQLMHjAFLRQ9xzRszgtqwcrWN/c+HXy8unuVR1YHkg5B4Xk0+1M CrhOCMS8AF4oBzvdmx2Un+UjJgNcPTSQJwMVmyDplpf2wYt8h/ipC/vh71sU6kBP7N0Pwm 1otecQFv++hzvzfxT1BtAFuhVk+xLUc= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YmvVsLmj; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709833353; a=rsa-sha256; cv=none; b=kNwYQYynP/W3kYUcsfs9bzXktk+oAx+Wp04osbviYF9PXxFHjFVK0p833uOVHbLzHr8Boh rbUiw68nG+UjjWaObo1h8ewU7z561QgTHBVdCNKcGTQP21h+rwi2mcQVJFcg2kRp2uINjU LZGaxsSg20mXM8QmpXEB1LVx7Wn4l30= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id C679F617E9; Thu, 7 Mar 2024 17:42:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 463D0C433C7; Thu, 7 Mar 2024 17:42:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709833352; bh=jIQ/4k8vSjnqNzNcPnEy4vVGjsz9OnbEy/pl9GkP+mE=; h=From:To:Cc:Subject:Date:From; b=YmvVsLmjo7y7QXkOgQvpAZZadKHZzDTUOL3YqOlrbTbhuIRgZr/0ij2x8bw3i1xMQ g6LOrx1rgC/s5mln95igvva1+0jrlJV9oS5v7lJZ+xX61pNcjiyCgC61Dm1Q2aJ+mt I2flh2fS+nkydQHvf4F7g+ZSAdjTZ9nHSvTDfXTKrICVH/HL1jbdjmmN5TeB0z8yUG AqlxYDMkKc1OeWem4v+UoSCszOqFTQXbPgGBpTN7n2H39Tig6Miicw5wLwPUl26fzA QRaBKxHZBp8+RS1LWdcRM1c0t4d+Lg7rOBEnRBws2gqQgwC4KttEf6Sx/uN/JB2KIo QKpVc0awCYwOA== From: cem@kernel.org To: hughd@google.com Cc: jack@suse.cz, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Subject: [PATCH] tmpfs: Fix race on handling dquot rbtree Date: Thu, 7 Mar 2024 18:42:10 +0100 Message-ID: <20240307174226.627962-1-cem@kernel.org> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 9C28E4001E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: rtuxq5hjq8b95ukj6oifaxue596pcb3t X-HE-Tag: 1709833353-706150 X-HE-Meta: U2FsdGVkX19JBa85vErDBMVcigYGxQivVWlmGBIeuwdhV6yIPWis7ptmTPxCeSezWGZuEBsoDQ5px406SP1I9xMVcNQTPQerz+FO52IFbomJjt3ftdksv53IupqFa49rPPKNWy/V1HLG5OdUu2UxgNAZ/CzHrrhHyFOuMKtdttxOujJwGBqeC1iX3hC08pmHM+Dzpku6iUOvo/spmI9MHXBp49vIkWVFtsF3rDHPIB7Gma+LN5w+U32yKin73NIpiHsSa1MvECgKQR5WJTnK5WhtO/8rgi61V5pFp/65DIDjp0xDgNFoE5yyDXT+wriLyULC3GHJc53yE8nvNWT8rWU1ocglM0Kn00l9RoSoueIXJnlBZJg73D5O4rWJEqQxpB+PWLstxXUfJEdpw3YXorF9la9WPu8BtegFP9jkSHq9LsWHn2YWs68Py6n29ltc6uqFQnMjbidokYqPS4EpKP/sbP+T37e2/60X/7qKwxTbdsDR2LGQqRjcn9MstifZxLmX0OK9uPyVIJpYOwBA9wHx4lU4VSli+OGFiy4wvIyk2OnQDOKJE3oKua+hHsjAfWxBSmTE/PF5Wb9rAjQY4yAJdGVdBTZ0Sr42JA3YSvt7j/hzzP+gGAJe69XGEXbU+Cb932iM3QU3gnGIDsGnbg6SdEg+IjXocRQ9LTtKm5OLq7u+KT5AcDHKb14A9i0EKudRkABciMDgvWH7Dxx+pNRpmFnb3zGdg+e3lRxv6bVdnSZbyTcdMCUy9Tsjhs41hEDHD8tsZhrkGdYUAHo+UrLPU317mf9uhqPsPQGI9qYE/W821bjTiQnj+vqMEORIdox+BuERu6zK9/U4GCaGf262xTGvLiuO4kOdeag6nVOtEfGTSOkDXfKpa/x84l0MEEM+E7gPkvgnLzFX69rOeIrCdm9WNKHHaELcPmmREnA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Carlos Maiolino A syzkaller reproducer found a race while attempting to remove dquot information from the rb tree. Fetching the rb_tree root node must also be protected by the dqopt->dqio_sem, otherwise, giving the right timing, shmem_release_dquot() will trigger a warning because it couldn't find a node in the tree, when the real reason was the root node changing before the search starts: Thread 1 Thread 2 - shmem_release_dquot() - shmem_{acquire,release}_dquot() - fetch ROOT - Fetch ROOT - acquire dqio_sem - wait dqio_sem - do something, triger a tree rebalance - release dqio_sem - acquire dqio_sem - start searching for the node, but from the wrong location, missing the node, and triggering a warning. Fixes: eafc474e202978 ("shmem: prepare shmem quota infrastructure") Reported-by: Ubisectech Sirius Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- I had a chat with Aristeu Rozanski and Jan Kara about this issue, which made me stop pursuing the wrong direction and reach the root cause faster, thanks guys. mm/shmem_quota.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/shmem_quota.c b/mm/shmem_quota.c index 062d1c1097ae3..ce514e700d2f6 100644 --- a/mm/shmem_quota.c +++ b/mm/shmem_quota.c @@ -116,7 +116,7 @@ static int shmem_free_file_info(struct super_block *sb, int type) static int shmem_get_next_id(struct super_block *sb, struct kqid *qid) { struct mem_dqinfo *info = sb_dqinfo(sb, qid->type); - struct rb_node *node = ((struct rb_root *)info->dqi_priv)->rb_node; + struct rb_node *node; qid_t id = from_kqid(&init_user_ns, *qid); struct quota_info *dqopt = sb_dqopt(sb); struct quota_id *entry = NULL; @@ -126,6 +126,7 @@ static int shmem_get_next_id(struct super_block *sb, struct kqid *qid) return -ESRCH; down_read(&dqopt->dqio_sem); + node = ((struct rb_root *)info->dqi_priv)->rb_node; while (node) { entry = rb_entry(node, struct quota_id, node); @@ -165,7 +166,7 @@ static int shmem_get_next_id(struct super_block *sb, struct kqid *qid) static int shmem_acquire_dquot(struct dquot *dquot) { struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); - struct rb_node **n = &((struct rb_root *)info->dqi_priv)->rb_node; + struct rb_node **n; struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info; struct rb_node *parent = NULL, *new_node = NULL; struct quota_id *new_entry, *entry; @@ -176,6 +177,8 @@ static int shmem_acquire_dquot(struct dquot *dquot) mutex_lock(&dquot->dq_lock); down_write(&dqopt->dqio_sem); + n = &((struct rb_root *)info->dqi_priv)->rb_node; + while (*n) { parent = *n; entry = rb_entry(parent, struct quota_id, node); @@ -264,7 +267,7 @@ static bool shmem_is_empty_dquot(struct dquot *dquot) static int shmem_release_dquot(struct dquot *dquot) { struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); - struct rb_node *node = ((struct rb_root *)info->dqi_priv)->rb_node; + struct rb_node *node; qid_t id = from_kqid(&init_user_ns, dquot->dq_id); struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); struct quota_id *entry = NULL; @@ -275,6 +278,7 @@ static int shmem_release_dquot(struct dquot *dquot) goto out_dqlock; down_write(&dqopt->dqio_sem); + node = ((struct rb_root *)info->dqi_priv)->rb_node; while (node) { entry = rb_entry(node, struct quota_id, node);