* Re: [PATCH] mm/page_table_check: fix accessing unmapped ptep
2022-05-26 11:33 [PATCH] mm/page_table_check: fix accessing unmapped ptep Miaohe Lin
@ 2022-05-26 13:04 ` Qi Zheng
2022-05-26 13:11 ` Pasha Tatashin
2022-05-26 13:17 ` Pasha Tatashin
2022-05-26 13:34 ` Matthew Wilcox
2 siblings, 1 reply; 10+ messages in thread
From: Qi Zheng @ 2022-05-26 13:04 UTC (permalink / raw)
To: Miaohe Lin, akpm, pasha.tatashin; +Cc: rientjes, linux-mm, linux-kernel
On 2022/5/26 7:33 PM, Miaohe Lin wrote:
> ptep is unmapped too early, so ptep will be accessed while it's unmapped.
> Fix it by deferring pte_unmap() until page table checking is done.
In the beginning, page_table_check only supported x86_64, so there
is no problem. But then the commit 3fee229a8eb9 ("riscv/mm: enable
ARCH_SUPPORTS_PAGE_TABLE_CHECK") added support for riscv-32, it is
indeed a problem in this case.
So:
Reviewed-by: Qi Zheng <zhengqi.arch@bytedance.com>
>
> Fixes: 80110bbfbba6 ("mm/page_table_check: check entries at pmd levels")
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> ---
> mm/page_table_check.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/page_table_check.c b/mm/page_table_check.c
> index 3692bea2ea2c..971c3129b0e3 100644
> --- a/mm/page_table_check.c
> +++ b/mm/page_table_check.c
> @@ -234,11 +234,11 @@ void __page_table_check_pte_clear_range(struct mm_struct *mm,
> pte_t *ptep = pte_offset_map(&pmd, addr);
> unsigned long i;
>
> - pte_unmap(ptep);
> for (i = 0; i < PTRS_PER_PTE; i++) {
> __page_table_check_pte_clear(mm, addr, *ptep);
> addr += PAGE_SIZE;
> ptep++;
> }
> + pte_unmap(ptep);
> }
> }
--
Thanks,
Qi
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm/page_table_check: fix accessing unmapped ptep
2022-05-26 13:04 ` Qi Zheng
@ 2022-05-26 13:11 ` Pasha Tatashin
2022-05-26 13:23 ` Qi Zheng
0 siblings, 1 reply; 10+ messages in thread
From: Pasha Tatashin @ 2022-05-26 13:11 UTC (permalink / raw)
To: Qi Zheng; +Cc: Miaohe Lin, Andrew Morton, David Rientjes, linux-mm, LKML
On Thu, May 26, 2022 at 9:04 AM Qi Zheng <zhengqi.arch@bytedance.com> wrote:
>
>
>
> On 2022/5/26 7:33 PM, Miaohe Lin wrote:
> > ptep is unmapped too early, so ptep will be accessed while it's unmapped.
> > Fix it by deferring pte_unmap() until page table checking is done.
>
> In the beginning, page_table_check only supported x86_64, so there
> is no problem. But then the commit 3fee229a8eb9 ("riscv/mm: enable
> ARCH_SUPPORTS_PAGE_TABLE_CHECK") added support for riscv-32, it is
> indeed a problem in this case.
pte_unmap() is needed only with CONFIG_HIGHPTE. I do not see this
config for riskv-32?
Pasha
>
> So:
>
> Reviewed-by: Qi Zheng <zhengqi.arch@bytedance.com>
>
> >
> > Fixes: 80110bbfbba6 ("mm/page_table_check: check entries at pmd levels")
> > Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> > ---
> > mm/page_table_check.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/mm/page_table_check.c b/mm/page_table_check.c
> > index 3692bea2ea2c..971c3129b0e3 100644
> > --- a/mm/page_table_check.c
> > +++ b/mm/page_table_check.c
> > @@ -234,11 +234,11 @@ void __page_table_check_pte_clear_range(struct mm_struct *mm,
> > pte_t *ptep = pte_offset_map(&pmd, addr);
> > unsigned long i;
> >
> > - pte_unmap(ptep);
> > for (i = 0; i < PTRS_PER_PTE; i++) {
> > __page_table_check_pte_clear(mm, addr, *ptep);
> > addr += PAGE_SIZE;
> > ptep++;
> > }
> > + pte_unmap(ptep);
> > }
> > }
>
> --
> Thanks,
> Qi
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm/page_table_check: fix accessing unmapped ptep
2022-05-26 13:11 ` Pasha Tatashin
@ 2022-05-26 13:23 ` Qi Zheng
0 siblings, 0 replies; 10+ messages in thread
From: Qi Zheng @ 2022-05-26 13:23 UTC (permalink / raw)
To: Pasha Tatashin; +Cc: Miaohe Lin, Andrew Morton, David Rientjes, linux-mm, LKML
On 2022/5/26 9:11 PM, Pasha Tatashin wrote:
> On Thu, May 26, 2022 at 9:04 AM Qi Zheng <zhengqi.arch@bytedance.com> wrote:
>>
>>
>>
>> On 2022/5/26 7:33 PM, Miaohe Lin wrote:
>>> ptep is unmapped too early, so ptep will be accessed while it's unmapped.
>>> Fix it by deferring pte_unmap() until page table checking is done.
>>
>> In the beginning, page_table_check only supported x86_64, so there
>> is no problem. But then the commit 3fee229a8eb9 ("riscv/mm: enable
>> ARCH_SUPPORTS_PAGE_TABLE_CHECK") added support for riscv-32, it is
>> indeed a problem in this case.
>
> pte_unmap() is needed only with CONFIG_HIGHPTE. I do not see this
> config for riskv-32?
My bad, but it's better to call pte_unmap() after the check is done.
>
> Pasha
>
>>
>> So:
>>
>> Reviewed-by: Qi Zheng <zhengqi.arch@bytedance.com>
>>
>>>
>>> Fixes: 80110bbfbba6 ("mm/page_table_check: check entries at pmd levels")
>>> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
>>> ---
>>> mm/page_table_check.c | 2 +-
>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/mm/page_table_check.c b/mm/page_table_check.c
>>> index 3692bea2ea2c..971c3129b0e3 100644
>>> --- a/mm/page_table_check.c
>>> +++ b/mm/page_table_check.c
>>> @@ -234,11 +234,11 @@ void __page_table_check_pte_clear_range(struct mm_struct *mm,
>>> pte_t *ptep = pte_offset_map(&pmd, addr);
>>> unsigned long i;
>>>
>>> - pte_unmap(ptep);
>>> for (i = 0; i < PTRS_PER_PTE; i++) {
>>> __page_table_check_pte_clear(mm, addr, *ptep);
>>> addr += PAGE_SIZE;
>>> ptep++;
>>> }
>>> + pte_unmap(ptep);
>>> }
>>> }
>>
>> --
>> Thanks,
>> Qi
--
Thanks,
Qi
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm/page_table_check: fix accessing unmapped ptep
2022-05-26 11:33 [PATCH] mm/page_table_check: fix accessing unmapped ptep Miaohe Lin
2022-05-26 13:04 ` Qi Zheng
@ 2022-05-26 13:17 ` Pasha Tatashin
2022-05-26 13:34 ` Matthew Wilcox
2 siblings, 0 replies; 10+ messages in thread
From: Pasha Tatashin @ 2022-05-26 13:17 UTC (permalink / raw)
To: Miaohe Lin; +Cc: Andrew Morton, David Rientjes, linux-mm, LKML
On Thu, May 26, 2022 at 7:33 AM Miaohe Lin <linmiaohe@huawei.com> wrote:
>
> ptep is unmapped too early, so ptep will be accessed while it's unmapped.
> Fix it by deferring pte_unmap() until page table checking is done.
I would re-word this as a cleanup. While pte_unmap() is currently
unused, it is still better to call it after we are done with *ptep in
case of future changes in other architectures.
>
> Fixes: 80110bbfbba6 ("mm/page_table_check: check entries at pmd levels")
This is more a clean-up, there is no existing bug, so no need to
backport to stable. Please remove the above.
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
With the above changes:
Acked-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> ---
> mm/page_table_check.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/page_table_check.c b/mm/page_table_check.c
> index 3692bea2ea2c..971c3129b0e3 100644
> --- a/mm/page_table_check.c
> +++ b/mm/page_table_check.c
> @@ -234,11 +234,11 @@ void __page_table_check_pte_clear_range(struct mm_struct *mm,
> pte_t *ptep = pte_offset_map(&pmd, addr);
> unsigned long i;
>
> - pte_unmap(ptep);
> for (i = 0; i < PTRS_PER_PTE; i++) {
> __page_table_check_pte_clear(mm, addr, *ptep);
> addr += PAGE_SIZE;
> ptep++;
> }
> + pte_unmap(ptep);
> }
> }
> --
> 2.23.0
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm/page_table_check: fix accessing unmapped ptep
2022-05-26 11:33 [PATCH] mm/page_table_check: fix accessing unmapped ptep Miaohe Lin
2022-05-26 13:04 ` Qi Zheng
2022-05-26 13:17 ` Pasha Tatashin
@ 2022-05-26 13:34 ` Matthew Wilcox
2022-05-26 13:37 ` Pasha Tatashin
2 siblings, 1 reply; 10+ messages in thread
From: Matthew Wilcox @ 2022-05-26 13:34 UTC (permalink / raw)
To: Miaohe Lin; +Cc: akpm, pasha.tatashin, rientjes, linux-mm, linux-kernel
On Thu, May 26, 2022 at 07:33:50PM +0800, Miaohe Lin wrote:
> ptep is unmapped too early, so ptep will be accessed while it's unmapped.
> Fix it by deferring pte_unmap() until page table checking is done.
>
> Fixes: 80110bbfbba6 ("mm/page_table_check: check entries at pmd levels")
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> ---
> mm/page_table_check.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/page_table_check.c b/mm/page_table_check.c
> index 3692bea2ea2c..971c3129b0e3 100644
> --- a/mm/page_table_check.c
> +++ b/mm/page_table_check.c
> @@ -234,11 +234,11 @@ void __page_table_check_pte_clear_range(struct mm_struct *mm,
> pte_t *ptep = pte_offset_map(&pmd, addr);
> unsigned long i;
>
> - pte_unmap(ptep);
> for (i = 0; i < PTRS_PER_PTE; i++) {
> __page_table_check_pte_clear(mm, addr, *ptep);
> addr += PAGE_SIZE;
> ptep++;
> }
> + pte_unmap(ptep);
But ptep was mutated in the loop. So surely this needs to be:
pte_unmap(ptep - PTRS_PER_PTE);
or you'll be unmapping the wrong page.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm/page_table_check: fix accessing unmapped ptep
2022-05-26 13:34 ` Matthew Wilcox
@ 2022-05-26 13:37 ` Pasha Tatashin
2022-05-26 18:15 ` Andrew Morton
0 siblings, 1 reply; 10+ messages in thread
From: Pasha Tatashin @ 2022-05-26 13:37 UTC (permalink / raw)
To: Matthew Wilcox; +Cc: Miaohe Lin, Andrew Morton, David Rientjes, linux-mm, LKML
On Thu, May 26, 2022 at 9:34 AM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Thu, May 26, 2022 at 07:33:50PM +0800, Miaohe Lin wrote:
> > ptep is unmapped too early, so ptep will be accessed while it's unmapped.
> > Fix it by deferring pte_unmap() until page table checking is done.
> >
> > Fixes: 80110bbfbba6 ("mm/page_table_check: check entries at pmd levels")
> > Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> > ---
> > mm/page_table_check.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/mm/page_table_check.c b/mm/page_table_check.c
> > index 3692bea2ea2c..971c3129b0e3 100644
> > --- a/mm/page_table_check.c
> > +++ b/mm/page_table_check.c
> > @@ -234,11 +234,11 @@ void __page_table_check_pte_clear_range(struct mm_struct *mm,
> > pte_t *ptep = pte_offset_map(&pmd, addr);
> > unsigned long i;
> >
> > - pte_unmap(ptep);
> > for (i = 0; i < PTRS_PER_PTE; i++) {
> > __page_table_check_pte_clear(mm, addr, *ptep);
> > addr += PAGE_SIZE;
> > ptep++;
> > }
> > + pte_unmap(ptep);
>
> But ptep was mutated in the loop. So surely this needs to be:
>
> pte_unmap(ptep - PTRS_PER_PTE);
>
> or you'll be unmapping the wrong page.
Right, thank you Matthew.
Miaohe, please store the ptep, or maybe drop this patch entirely.
Thanks,
Pasha
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm/page_table_check: fix accessing unmapped ptep
2022-05-26 13:37 ` Pasha Tatashin
@ 2022-05-26 18:15 ` Andrew Morton
2022-05-26 18:31 ` Pasha Tatashin
0 siblings, 1 reply; 10+ messages in thread
From: Andrew Morton @ 2022-05-26 18:15 UTC (permalink / raw)
To: Pasha Tatashin; +Cc: Matthew Wilcox, Miaohe Lin, David Rientjes, linux-mm, LKML
On Thu, 26 May 2022 09:37:43 -0400 Pasha Tatashin <pasha.tatashin@soleen.com> wrote:
> On Thu, May 26, 2022 at 9:34 AM Matthew Wilcox <willy@infradead.org> wrote:
> >
> > On Thu, May 26, 2022 at 07:33:50PM +0800, Miaohe Lin wrote:
> > > ptep is unmapped too early, so ptep will be accessed while it's unmapped.
> > > Fix it by deferring pte_unmap() until page table checking is done.
> > >
> > > Fixes: 80110bbfbba6 ("mm/page_table_check: check entries at pmd levels")
> > > Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> > > ---
> > > mm/page_table_check.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/mm/page_table_check.c b/mm/page_table_check.c
> > > index 3692bea2ea2c..971c3129b0e3 100644
> > > --- a/mm/page_table_check.c
> > > +++ b/mm/page_table_check.c
> > > @@ -234,11 +234,11 @@ void __page_table_check_pte_clear_range(struct mm_struct *mm,
> > > pte_t *ptep = pte_offset_map(&pmd, addr);
> > > unsigned long i;
> > >
> > > - pte_unmap(ptep);
> > > for (i = 0; i < PTRS_PER_PTE; i++) {
> > > __page_table_check_pte_clear(mm, addr, *ptep);
> > > addr += PAGE_SIZE;
> > > ptep++;
> > > }
> > > + pte_unmap(ptep);
> >
> > But ptep was mutated in the loop. So surely this needs to be:
> >
> > pte_unmap(ptep - PTRS_PER_PTE);
> >
> > or you'll be unmapping the wrong page.
>
> Right, thank you Matthew.
>
> Miaohe, please store the ptep, or maybe drop this patch entirely.
I think it's best to fix it. I rewrote the changelog as
: ptep is unmapped too early, so ptep could theoretically be accessed while
: it's unmapped. This might become a problem if/when CONFIG_HIGHPTE becomes
: available on riscv.
:
: Fix it by deferring pte_unmap() until page table checking is done.
I'll retain the Fixes:. This doesn't imply cc:stable in MM, and anyone
who backports the original patchset will want to know about this fixup.
And I queued a fixup for the thing Matthew noticed.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm/page_table_check: fix accessing unmapped ptep
2022-05-26 18:15 ` Andrew Morton
@ 2022-05-26 18:31 ` Pasha Tatashin
2022-05-27 1:10 ` Miaohe Lin
0 siblings, 1 reply; 10+ messages in thread
From: Pasha Tatashin @ 2022-05-26 18:31 UTC (permalink / raw)
To: Andrew Morton; +Cc: Matthew Wilcox, Miaohe Lin, David Rientjes, linux-mm, LKML
> > Miaohe, please store the ptep, or maybe drop this patch entirely.
>
> I think it's best to fix it. I rewrote the changelog as
>
> : ptep is unmapped too early, so ptep could theoretically be accessed while
> : it's unmapped. This might become a problem if/when CONFIG_HIGHPTE becomes
> : available on riscv.
> :
> : Fix it by deferring pte_unmap() until page table checking is done.
>
> I'll retain the Fixes:. This doesn't imply cc:stable in MM, and anyone
> who backports the original patchset will want to know about this fixup.
Makes sense.
> And I queued a fixup for the thing Matthew noticed.
Thank you Andrew.
Pasha
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm/page_table_check: fix accessing unmapped ptep
2022-05-26 18:31 ` Pasha Tatashin
@ 2022-05-27 1:10 ` Miaohe Lin
0 siblings, 0 replies; 10+ messages in thread
From: Miaohe Lin @ 2022-05-27 1:10 UTC (permalink / raw)
To: Pasha Tatashin, Andrew Morton, zhengqi.arch, Matthew Wilcox
Cc: Matthew Wilcox, David Rientjes, linux-mm, LKML
On 2022/5/27 2:31, Pasha Tatashin wrote:
>>> Miaohe, please store the ptep, or maybe drop this patch entirely.
>>
>> I think it's best to fix it. I rewrote the changelog as
>>
>> : ptep is unmapped too early, so ptep could theoretically be accessed while
>> : it's unmapped. This might become a problem if/when CONFIG_HIGHPTE becomes
>> : available on riscv.
>> :
>> : Fix it by deferring pte_unmap() until page table checking is done.
>>
>> I'll retain the Fixes:. This doesn't imply cc:stable in MM, and anyone
>> who backports the original patchset will want to know about this fixup.
> Makes sense.
>
>
>> And I queued a fixup for the thing Matthew noticed.
> Thank you Andrew.
>
Many thanks for all of your comments, review and fixup! :)
> Pasha
> .
>
^ permalink raw reply [flat|nested] 10+ messages in thread