Commit e9c3ca09 authored by Baolin Wang's avatar Baolin Wang Committed by Liu Shixin
Browse files

mm: shmem: fix incorrect aligned index when checking conflicts

mainline inclusion
from mainline-v6.11-rc2
commit 4cbf320b1500fe64fcef8c96ed74dfc1ae2c9e2c
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IAIHPC

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4cbf320b1500fe64fcef8c96ed74dfc1ae2c9e2c

--------------------------------

In the shmem_suitable_orders() function, xa_find() is used to check for
conflicts in the pagecache to select suitable huge orders.  However, when
checking each huge order in every loop, the aligned index is calculated
from the previous iteration, which may cause suitable huge orders to be
missed.

We should use the original index each time in the loop to calculate a new
aligned index for checking conflicts to avoid this issue.

Link: https://lkml.kernel.org/r/07433b0f16a152bffb8cee34934a5c040e8e2ad6.1722404078.git.baolin.wang@linux.alibaba.com


Fixes: e7a2ab7b3bb5 ("mm: shmem: add mTHP support for anonymous shmem")
Signed-off-by: default avatarBaolin Wang <baolin.wang@linux.alibaba.com>
Acked-by: default avatarDavid Hildenbrand <david@redhat.com>
Cc: Barry Song <21cnbao@gmail.com>
Cc: Gavin Shan <gshan@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Lance Yang <ioworker0@gmail.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Zi Yan <ziy@nvidia.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLiu Shixin <liushixin2@huawei.com>
parent e57297e1
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -1698,6 +1698,7 @@ static unsigned long shmem_suitable_orders(struct inode *inode, struct vm_fault
					   unsigned long orders)
{
	struct vm_area_struct *vma = vmf->vma;
	pgoff_t aligned_index;
	unsigned long pages;
	int order;

@@ -1709,9 +1710,9 @@ static unsigned long shmem_suitable_orders(struct inode *inode, struct vm_fault
	order = highest_order(orders);
	while (orders) {
		pages = 1UL << order;
		index = round_down(index, pages);
		if (!xa_find(&mapping->i_pages, &index,
			     index + pages - 1, XA_PRESENT))
		aligned_index = round_down(index, pages);
		if (!xa_find(&mapping->i_pages, &aligned_index,
			     aligned_index + pages - 1, XA_PRESENT))
			break;
		order = next_order(&orders, order);
	}