Commit f0bb6dba authored by Carlos Llamas's avatar Carlos Llamas Committed by Andrew Morton
Browse files

selftests/mm: fix division-by-zero in uffd-unit-tests

Commit 4dfd4bba ("selftests/mm/uffd: refactor non-composite global
vars into struct") moved some of the operations previously implemented in
uffd_setup_environment() earlier in the main test loop.

The calculation of nr_pages, which involves a division by page_size, now
occurs before checking that default_huge_page_size() returns a non-zero
This leads to a division-by-zero error on systems with !CONFIG_HUGETLB.

Fix this by relocating the non-zero page_size check before the nr_pages
calculation, as it was originally implemented.

Link: https://lkml.kernel.org/r/20251113034623.3127012-1-cmllamas@google.com


Fixes: 4dfd4bba ("selftests/mm/uffd: refactor non-composite global vars into struct")
Signed-off-by: default avatarCarlos Llamas <cmllamas@google.com>
Acked-by: default avatarDavid Hildenbrand (Red Hat) <david@kernel.org>
Reviewed-by: default avatarLorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: default avatarMike Rapoport (Microsoft) <rppt@kernel.org>
Reviewed-by: default avatarUjwal Kundur <ujwal.kundur@gmail.com>
Cc: Brendan Jackman <jackmanb@google.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 270065f5
Loading
Loading
Loading
Loading
+7 −8
Original line number Diff line number Diff line
@@ -1758,10 +1758,15 @@ int main(int argc, char *argv[])
			uffd_test_ops = mem_type->mem_ops;
			uffd_test_case_ops = test->test_case_ops;

			if (mem_type->mem_flag & (MEM_HUGETLB_PRIVATE | MEM_HUGETLB))
			if (mem_type->mem_flag & (MEM_HUGETLB_PRIVATE | MEM_HUGETLB)) {
				gopts.page_size = default_huge_page_size();
			else
				if (gopts.page_size == 0) {
					uffd_test_skip("huge page size is 0, feature missing?");
					continue;
				}
			} else {
				gopts.page_size = psize();
			}

			/* Ensure we have at least 2 pages */
			gopts.nr_pages = MAX(UFFD_TEST_MEM_SIZE, gopts.page_size * 2)
@@ -1776,12 +1781,6 @@ int main(int argc, char *argv[])
				continue;

			uffd_test_start("%s on %s", test->name, mem_type->name);
			if ((mem_type->mem_flag == MEM_HUGETLB ||
			    mem_type->mem_flag == MEM_HUGETLB_PRIVATE) &&
			    (default_huge_page_size() == 0)) {
				uffd_test_skip("huge page size is 0, feature missing?");
				continue;
			}
			if (!uffd_feature_supported(test)) {
				uffd_test_skip("feature missing");
				continue;