Message ID | 20200416025034.29780-1-songmuchun@bytedance.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | [v4] mm/ksm: Fix NULL pointer dereference when KSM zero page is enabled | expand |
On Thu, 16 Apr 2020 10:50:34 +0800 Muchun Song <songmuchun@bytedance.com> wrote: > The find_mergeable_vma can return NULL. In this case, it leads > to a crash when we access vm_mm(its offset is 0x40) later in > write_protect_page. And this case did happen on our server. The > following call trace is captured in kernel 4.19 with the following > patch applied and KSM zero page enabled on our server. > > commit e86c59b1b12d ("mm/ksm: improve deduplication of zero pages with colouring") > > So add a vma check to fix it. > > ... > > --- a/mm/ksm.c > +++ b/mm/ksm.c > @@ -2112,8 +2112,15 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) > > down_read(&mm->mmap_sem); > vma = find_mergeable_vma(mm, rmap_item->address); > - err = try_to_merge_one_page(vma, page, > - ZERO_PAGE(rmap_item->address)); > + if (vma) > + err = try_to_merge_one_page(vma, page, > + ZERO_PAGE(rmap_item->address)); > + else > + /** > + * If the vma is out of date, we do not need to > + * continue. > + */ > + err = 0; > up_read(&mm->mmap_sem); > /* > * In case of failure, the page was not really empty, so we Thanks. It's conventional to put braces around multi-line blocks such as this. Also, /** is specifically used to introduce kerneldoc comments. This comment is not a kerneldoc one so use /*. --- a/mm/ksm.c~mm-ksm-fix-null-pointer-dereference-when-ksm-zero-page-is-enabled-v4-fix +++ a/mm/ksm.c @@ -2112,15 +2112,16 @@ static void cmp_and_merge_page(struct pa down_read(&mm->mmap_sem); vma = find_mergeable_vma(mm, rmap_item->address); - if (vma) + if (vma) { err = try_to_merge_one_page(vma, page, ZERO_PAGE(rmap_item->address)); - else - /** + } else { + /* * If the vma is out of date, we do not need to * continue. */ err = 0; + } up_read(&mm->mmap_sem); /* * In case of failure, the page was not really empty, so we
> to a crash when we access vm_mm(its offset is 0x40) later in Would the text variant “… vm_mm (its …” be a bit nicer? … > +++ b/mm/ksm.c > @@ -2112,8 +2112,15 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) … > + if (vma) > + err = try_to_merge_one_page(vma, page, > + ZERO_PAGE(rmap_item->address)); Can the parameter alignment trigger further software development considerations for such a function call? Regards, Markus
… >> +++ b/mm/ksm.c >> @@ -2112,8 +2112,15 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) … >> + if (vma) >> + err = try_to_merge_one_page(vma, page, >> + ZERO_PAGE(rmap_item->address)); >> + else >> + /** >> + * If the vma is out of date, we do not need to >> + * continue. >> + */ >> + err = 0; >> up_read(&mm->mmap_sem); … > It's conventional to put braces around multi-line blocks such as this. Are there different views to consider around the usage of single statements together with curly brackets in if branches? Regards, Markus
On 16.04.2020 09:14, Markus Elfring wrote: > … >>> +++ b/mm/ksm.c >>> @@ -2112,8 +2112,15 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) > … >>> + if (vma) >>> + err = try_to_merge_one_page(vma, page, >>> + ZERO_PAGE(rmap_item->address)); >>> + else >>> + /** >>> + * If the vma is out of date, we do not need to >>> + * continue. >>> + */ >>> + err = 0; >>> up_read(&mm->mmap_sem); > … >> It's conventional to put braces around multi-line blocks such as this. > > Are there different views to consider around the usage of single statements > together with curly brackets in if branches? For me Andrew's conversion is the best readable. I try to comment the code the same way myself. I even thought it's kernel default style :) Kirill
On Thu, Apr 16, 2020 at 10:50:34AM +0800, Muchun Song wrote: > + /** > + * If the vma is out of date, we do not need to > + * continue. > + */ This comment is not in kernel-doc format and should not start with '/**'.
On Thu, 2020-04-16 at 14:20 +0300, Kirill Tkhai wrote: > On 16.04.2020 09:14, Markus Elfring wrote: > > … > > > > +++ b/mm/ksm.c > > > > @@ -2112,8 +2112,15 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) > > … > > > > + if (vma) > > > > + err = try_to_merge_one_page(vma, page, > > > > + ZERO_PAGE(rmap_item->address)); > > > > + else > > > > + /** > > > > + * If the vma is out of date, we do not need to > > > > + * continue. trivia: It's generally better to not use "/**" as that's used for kernel-doc and this could be a single line like + /* If the vma is out of date, no need to continue */ > > > It's conventional to put braces around multi-line blocks such as this. true > > Are there different views to consider around the usage of single statements > > together with curly brackets in if branches? no
diff --git a/mm/ksm.c b/mm/ksm.c index a558da9e71770..15339538da299 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2112,8 +2112,15 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) down_read(&mm->mmap_sem); vma = find_mergeable_vma(mm, rmap_item->address); - err = try_to_merge_one_page(vma, page, - ZERO_PAGE(rmap_item->address)); + if (vma) + err = try_to_merge_one_page(vma, page, + ZERO_PAGE(rmap_item->address)); + else + /** + * If the vma is out of date, we do not need to + * continue. + */ + err = 0; up_read(&mm->mmap_sem); /* * In case of failure, the page was not really empty, so we