From: zhangchun <zhang.chuna@h3c.com>
To: <akpm@linux-foundation.org>
Cc: <jiaoxupo@h3c.com>, <linux-kernel@vger.kernel.org>,
<linux-mm@kvack.org>, <shaohaojize@126.com>,
<zhang.chuna@h3c.com>, <zhang.zhansheng@h3c.com>,
<zhang.zhengming@h3c.com>
Subject: [PATCH v2] mm: Give kmap_lock before call flush_tlb_kernel_rang,avoid kmap_high deadlock.
Date: Fri, 12 Jul 2024 15:54:41 +0800 [thread overview]
Message-ID: <1720770881-65312-1-git-send-email-zhang.chuna@h3c.com> (raw)
In-Reply-To: <20240710103611.809895ff809df9ed411bfaa8@linux-foundation.org>
>> >> --- a/mm/highmem.c
>> >> +++ b/mm/highmem.c
>> >> @@ -220,8 +220,11 @@ static void flush_all_zero_pkmaps(void)
>> >> set_page_address(page, NULL);
>> >> need_flush = 1;
>> >> }
>> >> - if (need_flush)
>> >> + if (need_flush) {
>> >> + unlock_kmap();
>> >> flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
>> >> + lock_kmap();
>> >> + }
>> >> }
>>
>> >Why is dropping the lock like this safe? What data is it protecting
>> >and why is it OK to leave that data unprotected here?
>>
>> kmap_lock is used to protect pkmap_count, pkmap_page_table and last_pkmap_nr(static variable).
>> When call flush_tlb_kernel_range(PKMAP_ADDR(0),
>> PKMAP_ADDR(LAST_PKMAP)), flush_tlb_kernel_range will neither modify nor read these variables. Leave that data unprotected here is safe.
>No, the risk here is that when the lock is dropped, other threads will modify the data. And when this thread (the one running
>flush_all_zero_pkmaps()) retakes the lock, that data may now be unexpectedly altered.
map_new_virtual aims to find an usable entry pkmap_count[last_pkmap_nr]. When read and modify the pkmap_count[last_pkmap_nr], the kmap_lock is
not dropped.
"if (!pkmap_count[last_pkmap_nr])" determine pkmap_count[last_pkmap_nr] is usable or not. If unusable, try agin.
Furthermore, the value of static variable last_pkmap_nr is stored in a local variable last_pkmap_nr, when kmap_lock is acquired,
this is thread-safe.
In an extreme case, if Thread A and Thread B access the same last_pkmap_nr, Thread A calls function flush_tlb_kernel_range and release the
kmap_lock, and Thread B then acquires the kmap_lock and modifies the variable pkmap_count[last_pkmap_nr]. After Thread A completes
the execution of function flush_tlb_kernel_range, it will check the variable pkmap_count[last_pkmap_nr].
If pkmap_count[last_pkmap_nr] != 0, Thread A continue to call get_next_pkmap_nr and get next last_pkmap_nr.
static inline unsigned long map_new_virtual(struct page *page)
{
unsigned long vaddr;
int count;
unsigned int last_pkmap_nr; // local variable to store static variable last_pkmap_nr
unsigned int color = get_pkmap_color(page);
start:
...
flush_all_zero_pkmaps();// release kmap_lock, then acquire it
count = get_pkmap_entries_count(color);
}
...
if (!pkmap_count[last_pkmap_nr]) // pkmap_count[last_pkmap_nr] is used or not
break; /* Found a usable entry */
if (--count)
continue;
...
vaddr = PKMAP_ADDR(last_pkmap_nr);
set_pte_at(&init_mm, vaddr,
&(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
pkmap_count[last_pkmap_nr] = 1;
...
return vaddr;
}
--
1.8.3.1
next prev parent reply other threads:[~2024-07-12 8:59 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-10 12:20 [PATCH v2] mm: Give kmap_lock before call flush_tlb_kernel_rang,avoid kmap_high deadlock zhangchun
2024-07-10 17:36 ` Andrew Morton
2024-07-11 7:07 ` zhangchun
2024-07-11 21:13 ` Andrew Morton
2024-07-12 7:54 ` zhangchun [this message]
2024-07-18 16:18 ` zhangchun
2024-07-24 0:26 ` Andrew Morton
2024-08-19 16:10 ` [PATCH v3] " zhangchun
2024-09-03 11:52 ` zhangchun
2024-10-08 3:23 ` zhangchun
2024-10-14 7:41 ` zhangchun
2024-10-31 15:18 ` [PATCH v4] " zhangchun
2025-02-18 6:30 ` zhangchun
-- strict thread matches above, loose matches on Subject: below --
2024-07-10 7:20 [PATCH v2] " zhangchun
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=1720770881-65312-1-git-send-email-zhang.chuna@h3c.com \
--to=zhang.chuna@h3c.com \
--cc=akpm@linux-foundation.org \
--cc=jiaoxupo@h3c.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=shaohaojize@126.com \
--cc=zhang.zhansheng@h3c.com \
--cc=zhang.zhengming@h3c.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;
as well as URLs for NNTP newsgroup(s).