From: Yu Kuai <yukuai1@huaweicloud.com>
To: Christoph Hellwig <hch@infradead.org>, Yu Kuai <yukuai1@huaweicloud.com>
Cc: axboe@kernel.dk, kbusch@kernel.org, linux-block@vger.kernel.org,
linux-kernel@vger.kernel.org, yi.zhang@huawei.com,
yangerkun@huawei.com, johnny.chenyi@huawei.com,
linux-mm@kvack.org, "yukuai (C)" <yukuai3@huawei.com>
Subject: Re: [PATCH 2/5] brd: synchronize using page and free page with rcu
Date: Mon, 21 Apr 2025 15:08:57 +0800 [thread overview]
Message-ID: <8de49859-8441-ee02-6114-ec2c9f89ada1@huaweicloud.com> (raw)
In-Reply-To: <aAXWgjauEVvw5183@infradead.org>
Hi,
在 2025/04/21 13:24, Christoph Hellwig 写道:
> On Fri, Apr 18, 2025 at 05:38:23PM +0800, Yu Kuai wrote:
>> From: Yu Kuai <yukuai3@huawei.com>
>>
>> Currently, after fetching the page by xa_load() in IO path, there is no
>> protection and page can be freed concurrently by discard:
>
> Ah, I guess this helps with the race I mentioned in reply to the
> previous patch. Is the rcu_head in struct page available for use
> by subsystems freeing the page?
Take a look at other union fileds int the struct page, in this case,
the page:
- not used for pagecache or anonymous page
- not used for page_pool
- not used for compound page
- not used for zone device page
So, I think it's fine to use the rcu_head.
We may want to avoid the page reference here since it's atomic and will
affect IO performance.
BTW, perhaps this patch should be the first patch in this set. :)
Thanks,
Kuai
>
>>
>> cpu0
>> brd_submit_bio
>> brd_do_bvec
>> page = brd_lookup_page
>> cpu1
>> brd_submit_bio
>> brd_do_discard
>> page = __xa_erase()
>> __free_page()
>> // page UAF
>>
>> Fix the problem by protecting page with rcu.
>>
>> Fixes: 9ead7efc6f3f ("brd: implement discard support")
>> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
>> ---
>> drivers/block/brd.c | 11 ++++++++++-
>> 1 file changed, 10 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/block/brd.c b/drivers/block/brd.c
>> index a6e4f005cb76..740ed13faaff 100644
>> --- a/drivers/block/brd.c
>> +++ b/drivers/block/brd.c
>> @@ -208,6 +208,7 @@ static int brd_do_bvec(struct brd_device *brd, struct page *page,
>> goto out;
>> }
>>
>> + rcu_read_lock();
>> mem = kmap_local_page(page);
>> if (!op_is_write(opf)) {
>> copy_from_brd(mem + off, brd, sector, len);
>> @@ -217,11 +218,19 @@ static int brd_do_bvec(struct brd_device *brd, struct page *page,
>> copy_to_brd(brd, mem + off, sector, len);
>> }
>> kunmap_local(mem);
>> + rcu_read_unlock();
>>
>> out:
>> return err;
>> }
>>
>> +static void brd_free_one_page(struct rcu_head *head)
>> +{
>> + struct page *page = container_of(head, struct page, rcu_head);
>> +
>> + __free_page(page);
>> +}
>> +
>> static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
>> {
>> sector_t aligned_sector = (sector + PAGE_SECTORS) & ~PAGE_SECTORS;
>> @@ -232,7 +241,7 @@ static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
>> while (size >= PAGE_SIZE && aligned_sector < rd_size * 2) {
>> page = __xa_erase(&brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT);
>> if (page) {
>> - __free_page(page);
>> + call_rcu(&page->rcu_head, brd_free_one_page);
>> brd->brd_nr_pages--;
>> }
>> aligned_sector += PAGE_SECTORS;
>> --
>> 2.39.2
>>
>>
> ---end quoted text---
>
> .
>
prev parent reply other threads:[~2025-04-21 7:09 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20250418093826.3149293-1-yukuai1@huaweicloud.com>
[not found] ` <20250418093826.3149293-3-yukuai1@huaweicloud.com>
2025-04-21 5:24 ` Christoph Hellwig
2025-04-21 7:08 ` Yu Kuai [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8de49859-8441-ee02-6114-ec2c9f89ada1@huaweicloud.com \
--to=yukuai1@huaweicloud.com \
--cc=axboe@kernel.dk \
--cc=hch@infradead.org \
--cc=johnny.chenyi@huawei.com \
--cc=kbusch@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=yangerkun@huawei.com \
--cc=yi.zhang@huawei.com \
--cc=yukuai3@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox