Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp396670rdb; Thu, 25 Jan 2024 21:07:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IE0+1arz5SCLR1ZDRHBu5+UIhu5ZEgTLSUPaNelP1fiXPuka2rUW9X6CQYTmbXrpB2K/ZZU X-Received: by 2002:a2e:b8c3:0:b0:2cf:2783:fadd with SMTP id s3-20020a2eb8c3000000b002cf2783faddmr484248ljp.78.1706245626762; Thu, 25 Jan 2024 21:07:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706245626; cv=pass; d=google.com; s=arc-20160816; b=FIO91YnYA1HJFX/LCwtsJmjrJv3TLbE0jrkC3YS0jgd8er3dBe2tXqJai9dwkcj8+O wxhe0GjJzjp9MSff6HOAcG9Dq2fF9+mZiWzSdYd1O0WCE3b/BCpers0UwNI7RmLm3kQY xIJFHq0qrQFWxPe0hPkJB3p5qkMC4iSqQBYq0XxqrYTVbR3wqJ40MKNloP+jE/MpJE1L K1SrE18XweKjRr2lQ6P0yecHr5XH1EWpKpQTvDfawrLn1GxQ4ahiFS6btWra35LU1TsR k+XMkvL8aSwrswQeHwS0G8J+W4RbKr+A9kmGU/RfCA1W5PS08ONgBl9G9EWtX5HfuPuk KPpA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:date:message-id:dkim-signature; bh=8ahEHxaQuhDxEoF78mzNdHbtkh143G48HFp0lRSTWgY=; fh=N7tknogJ6r2Sf4lZrJ66HD8nrnG1ntEKB1cD4mwmVyE=; b=W4441j25W4j8Ag13YGFF9ibTOSjnZTN0xXUEJRlisV2KMlYCnNBm/w+6yOEKzhHtqq U/lpD9Q53LP7AMoFZzhhf+LV4F5idcG19U7cNWeraAOVCbTbcwxkyfFbykJaJ89I/3yh WMcuvmFKndpbiHiP6p8y6FfV8Jwt0iEFIUBGKVGziMQx6o3bwa7Q1ITCik5uUSPHZTBv wtYw3M3YK7wVdXGfbtx6WWcfsTn5SeqAm5s6MkL4WxYqJVLt+7vVpDI6xE9xqzwzbIG5 udSZCxprkYzJm9/GCRLDXtjyQQlvny6UEYiHJc+GWweIEAn4gZvqv9p1QyGDJA4m9r7k QJww== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=UEAF5XIl; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-39631-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39631-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id q14-20020aa7da8e000000b0055c6562b2c3si234007eds.382.2024.01.25.21.07.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 21:07:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-39631-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=UEAF5XIl; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-39631-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39631-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 54EC71F25733 for ; Fri, 26 Jan 2024 05:07:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5A45CBA33; Fri, 26 Jan 2024 05:06:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="UEAF5XIl" Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com [115.124.30.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E683B647 for ; Fri, 26 Jan 2024 05:06:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706245617; cv=none; b=oEfcJ9bPgrEAOgjvff9A7GYkBn7Q5qjJQ1zvo2/HSfE9PaWk/7cqFRzXVaPXXCb6Aq8nxtZ1qM/BvCtWjHCDf/mzzp6zA1xZBcE1twlMLFeO5Dyt2hYDfBvriR5SFmw4Hbr+YTmwKPC3UdFop8NQP44SO/fy/0VdxTPVcXHnuoQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706245617; c=relaxed/simple; bh=I3Kf+k/5eM6GS+3z5wfBL3hZCstg1dLi6k42ARgNxbw=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=DcVzgdb4ATuWvTi9FFWzPDR/3pJCKyzfdfg7okPUdpPO5CBlgkNavLQEzhlBXItnoq9qtrnigk9d8tlkr6oQG5Yag0yzjdJZilZOloApAwZiEosV7MYC0aIQjRZGD6d9ULVW0v1BntYNLsKbByV00D1vRcQuousBIY8bkd8f44E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=UEAF5XIl; arc=none smtp.client-ip=115.124.30.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1706245611; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=8ahEHxaQuhDxEoF78mzNdHbtkh143G48HFp0lRSTWgY=; b=UEAF5XIlSHIivwAx8L6n0VtekmYQf8Vu0nvxB5t4EhXr63MEOUo75WMtrp1yinEcPVF/tkSkVG1WybcCkD8sHuGOXc85MUNCKN4eiiQqA8TPqadvx6trKgahI0E/AqmTueBhCk3wi5l06Gp5cC9YUyjKFRJQYLoD2Bp9Eh7ZoWA= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046051;MF=hsiangkao@linux.alibaba.com;NM=1;PH=DS;RN=4;SR=0;TI=SMTPD_---0W.MaLCP_1706245609; Received: from 30.97.48.207(mailfrom:hsiangkao@linux.alibaba.com fp:SMTPD_---0W.MaLCP_1706245609) by smtp.aliyun-inc.com; Fri, 26 Jan 2024 13:06:50 +0800 Message-ID: Date: Fri, 26 Jan 2024 13:06:49 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] erofs: fix infinite loop due to a race of filling compressed_bvecs To: Sandeep Dhavale Cc: kernel-team@android.com, linux-erofs@lists.ozlabs.org, LKML References: <20240125120039.3228103-1-hsiangkao@linux.alibaba.com> From: Gao Xiang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 2024/1/26 12:56, Sandeep Dhavale via Linux-erofs wrote: > On Thu, Jan 25, 2024 at 4:01 AM Gao Xiang wrote: >> >> I encountered a race issue after lengthy (~594647 sec) stress tests on >> a 64k-page arm64 VM with several 4k-block EROFS images. The timing >> is like below: >> >> z_erofs_try_inplace_io z_erofs_fill_bio_vec >> cmpxchg(&compressed_bvecs[].page, >> NULL, ..) >> [access bufvec] >> compressed_bvecs[] = *bvec; >> >> Previously, z_erofs_submit_queue() just accessed bufvec->page only, so >> other fields in bufvec didn't matter. After the subpage block support >> is landed, .offset and .end can be used too, but filling bufvec isn't >> an atomic operation which can cause inconsistency. >> >> Let's use a spinlock to keep the atomicity of each bufvec. More >> specifically, just reuse the existing spinlock `pcl->obj.lockref.lock` >> since it's rarely used (also it takes a short time if even used) as long >> as the pcluster has a reference. >> >> Fixes: 192351616a9d ("erofs: support I/O submission for sub-page compressed blocks") >> Signed-off-by: Gao Xiang >> --- >> fs/erofs/zdata.c | 74 +++++++++++++++++++++++++----------------------- >> 1 file changed, 38 insertions(+), 36 deletions(-) >> >> diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c >> index 583c062cd0e4..c1c77166b30f 100644 >> --- a/fs/erofs/zdata.c >> +++ b/fs/erofs/zdata.c >> @@ -563,21 +563,19 @@ static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe) >> __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; >> unsigned int i; >> >> - if (i_blocksize(fe->inode) != PAGE_SIZE) >> - return; >> - if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED) >> + if (i_blocksize(fe->inode) != PAGE_SIZE || >> + fe->mode < Z_EROFS_PCLUSTER_FOLLOWED) >> return; >> >> for (i = 0; i < pclusterpages; ++i) { >> struct page *page, *newpage; >> void *t; /* mark pages just found for debugging */ >> >> - /* the compressed page was loaded before */ >> + /* Inaccurate check w/o locking to avoid unneeded lookups */ >> if (READ_ONCE(pcl->compressed_bvecs[i].page)) >> continue; >> >> page = find_get_page(mc, pcl->obj.index + i); >> - >> if (page) { >> t = (void *)((unsigned long)page | 1); >> newpage = NULL; >> @@ -597,9 +595,13 @@ static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe) >> set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE); >> t = (void *)((unsigned long)newpage | 1); >> } >> - >> - if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t)) >> + spin_lock(&pcl->obj.lockref.lock); >> + if (!pcl->compressed_bvecs[i].page) { >> + pcl->compressed_bvecs[i].page = t; >> + spin_unlock(&pcl->obj.lockref.lock); >> continue; >> + } >> + spin_unlock(&pcl->obj.lockref.lock); >> >> if (page) >> put_page(page); >> @@ -718,31 +720,25 @@ int erofs_init_managed_cache(struct super_block *sb) >> return 0; >> } >> >> -static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe, >> - struct z_erofs_bvec *bvec) >> -{ >> - struct z_erofs_pcluster *const pcl = fe->pcl; >> - >> - while (fe->icur > 0) { >> - if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page, >> - NULL, bvec->page)) { >> - pcl->compressed_bvecs[fe->icur] = *bvec; >> - return true; >> - } >> - } >> - return false; >> -} >> - >> /* callers must be with pcluster lock held */ >> static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe, >> struct z_erofs_bvec *bvec, bool exclusive) >> { >> + struct z_erofs_pcluster *pcl = fe->pcl; >> int ret; >> >> if (exclusive) { >> /* give priority for inplaceio to use file pages first */ >> - if (z_erofs_try_inplace_io(fe, bvec)) >> + spin_lock(&pcl->obj.lockref.lock); >> + while (fe->icur > 0) { >> + if (pcl->compressed_bvecs[--fe->icur].page) >> + continue; >> + pcl->compressed_bvecs[fe->icur] = *bvec; >> + spin_unlock(&pcl->obj.lockref.lock); >> return 0; >> + } >> + spin_unlock(&pcl->obj.lockref.lock); >> + >> /* otherwise, check if it can be used as a bvpage */ >> if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED && >> !fe->candidate_bvpage) >> @@ -1423,23 +1419,26 @@ static void z_erofs_fill_bio_vec(struct bio_vec *bvec, >> { >> gfp_t gfp = mapping_gfp_mask(mc); >> bool tocache = false; >> - struct z_erofs_bvec *zbv = pcl->compressed_bvecs + nr; >> + struct z_erofs_bvec zbv; >> struct address_space *mapping; >> - struct page *page, *oldpage; >> + struct page *page; >> int justfound, bs = i_blocksize(f->inode); >> >> /* Except for inplace pages, the entire page can be used for I/Os */ >> bvec->bv_offset = 0; >> bvec->bv_len = PAGE_SIZE; >> repeat: >> - oldpage = READ_ONCE(zbv->page); >> - if (!oldpage) >> + spin_lock(&pcl->obj.lockref.lock); >> + zbv = pcl->compressed_bvecs[nr]; >> + page = zbv.page; >> + justfound = (unsigned long)page & 1UL; >> + page = (struct page *)((unsigned long)page & ~1UL); >> + pcl->compressed_bvecs[nr].page = page; >> + spin_unlock(&pcl->obj.lockref.lock); >> + if (!page) >> goto out_allocpage; >> >> - justfound = (unsigned long)oldpage & 1UL; >> - page = (struct page *)((unsigned long)oldpage & ~1UL); >> bvec->bv_page = page; >> - >> DBG_BUGON(z_erofs_is_shortlived_page(page)); >> /* >> * Handle preallocated cached pages. We tried to allocate such pages >> @@ -1448,7 +1447,6 @@ static void z_erofs_fill_bio_vec(struct bio_vec *bvec, >> */ >> if (page->private == Z_EROFS_PREALLOCATED_PAGE) { >> set_page_private(page, 0); >> - WRITE_ONCE(zbv->page, page); >> tocache = true; >> goto out_tocache; >> } >> @@ -1459,9 +1457,9 @@ static void z_erofs_fill_bio_vec(struct bio_vec *bvec, >> * therefore it is impossible for `mapping` to be NULL. >> */ >> if (mapping && mapping != mc) { >> - if (zbv->offset < 0) >> - bvec->bv_offset = round_up(-zbv->offset, bs); >> - bvec->bv_len = round_up(zbv->end, bs) - bvec->bv_offset; >> + if (zbv.offset < 0) >> + bvec->bv_offset = round_up(-zbv.offset, bs); >> + bvec->bv_len = round_up(zbv.end, bs) - bvec->bv_offset; >> return; >> } >> >> @@ -1471,7 +1469,6 @@ static void z_erofs_fill_bio_vec(struct bio_vec *bvec, >> >> /* the cached page is still in managed cache */ >> if (page->mapping == mc) { >> - WRITE_ONCE(zbv->page, page); >> /* >> * The cached page is still available but without a valid >> * `->private` pcluster hint. Let's reconnect them. >> @@ -1503,11 +1500,15 @@ static void z_erofs_fill_bio_vec(struct bio_vec *bvec, >> put_page(page); >> out_allocpage: >> page = erofs_allocpage(&f->pagepool, gfp | __GFP_NOFAIL); >> - if (oldpage != cmpxchg(&zbv->page, oldpage, page)) { >> + spin_lock(&pcl->obj.lockref.lock); >> + if (pcl->compressed_bvecs[nr].page) { >> erofs_pagepool_add(&f->pagepool, page); >> + spin_unlock(&pcl->obj.lockref.lock); >> cond_resched(); >> goto repeat; >> } >> + pcl->compressed_bvecs[nr].page = page; >> + spin_unlock(&pcl->obj.lockref.lock); >> bvec->bv_page = page; >> out_tocache: >> if (!tocache || bs != PAGE_SIZE || >> @@ -1685,6 +1686,7 @@ static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f, >> >> if (cur + bvec.bv_len > end) >> bvec.bv_len = end - cur; >> + DBG_BUGON(bvec.bv_len < sb->s_blocksize); >> if (!bio_add_page(bio, bvec.bv_page, bvec.bv_len, >> bvec.bv_offset)) >> goto submit_bio_retry; >> -- >> 2.39.3 >> > > LGTM! > > Reviewed-by: Sandeep Dhavale Thanks for the review :-) Thanks, Gao Xiang