1# SPDX-License-Identifier: GPL-2.0
2# Makefile for mm selftests
3
4LOCAL_HDRS += $(selfdir)/mm/local_config.h $(top_srcdir)/mm/gup_test.h
5
6include local_config.mk
7
8ifeq ($(ARCH),)
9
10ifeq ($(CROSS_COMPILE),)
11uname_M := $(shell uname -m 2>/dev/null || echo not)
12else
13uname_M := $(shell echo $(CROSS_COMPILE) | grep -o '^[a-z0-9]\+')
14endif
15ARCH ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/' -e 's/ppc64.*/ppc64/')
16endif
17
18# Without this, failed build products remain, with up-to-date timestamps,
19# thus tricking Make (and you!) into believing that All Is Well, in subsequent
20# make invocations:
21.DELETE_ON_ERROR:
22
23# Avoid accidental wrong builds, due to built-in rules working just a little
24# bit too well--but not quite as well as required for our situation here.
25#
26# In other words, "make $SOME_TEST" is supposed to fail to build at all,
27# because this Makefile only supports either "make" (all), or "make /full/path".
28# However,  the built-in rules, if not suppressed, will pick up CFLAGS and the
29# initial LDLIBS (but not the target-specific LDLIBS, because those are only
30# set for the full path target!). This causes it to get pretty far into building
31# things despite using incorrect values such as an *occasionally* incomplete
32# LDLIBS.
33MAKEFLAGS += --no-builtin-rules
34
35CFLAGS = -Wall -I $(top_srcdir) $(EXTRA_CFLAGS) $(KHDR_INCLUDES)
36LDLIBS = -lrt -lpthread
37
38TEST_GEN_PROGS = cow
39TEST_GEN_PROGS += compaction_test
40TEST_GEN_PROGS += gup_longterm
41TEST_GEN_PROGS += gup_test
42TEST_GEN_PROGS += hmm-tests
43TEST_GEN_PROGS += hugetlb-madvise
44TEST_GEN_PROGS += hugepage-mmap
45TEST_GEN_PROGS += hugepage-mremap
46TEST_GEN_PROGS += hugepage-shm
47TEST_GEN_PROGS += hugepage-vmemmap
48TEST_GEN_PROGS += khugepaged
49TEST_GEN_PROGS += madv_populate
50TEST_GEN_PROGS += map_fixed_noreplace
51TEST_GEN_PROGS += map_hugetlb
52TEST_GEN_PROGS += map_populate
53TEST_GEN_PROGS += memfd_secret
54TEST_GEN_PROGS += migration
55TEST_GEN_PROGS += mkdirty
56TEST_GEN_PROGS += mlock-random-test
57TEST_GEN_PROGS += mlock2-tests
58TEST_GEN_PROGS += mrelease_test
59TEST_GEN_PROGS += mremap_dontunmap
60TEST_GEN_PROGS += mremap_test
61TEST_GEN_PROGS += on-fault-limit
62TEST_GEN_PROGS += thuge-gen
63TEST_GEN_PROGS += transhuge-stress
64TEST_GEN_PROGS += uffd-stress
65TEST_GEN_PROGS += uffd-unit-tests
66TEST_GEN_PROGS += soft-dirty
67TEST_GEN_PROGS += split_huge_page_test
68TEST_GEN_PROGS += ksm_tests
69TEST_GEN_PROGS += ksm_functional_tests
70TEST_GEN_PROGS += mdwe_test
71
72ifeq ($(ARCH),x86_64)
73CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_32bit_program.c -m32)
74CAN_BUILD_X86_64 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_64bit_program.c)
75CAN_BUILD_WITH_NOPIE := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_program.c -no-pie)
76
77VMTARGETS := protection_keys
78BINARIES_32 := $(VMTARGETS:%=%_32)
79BINARIES_64 := $(VMTARGETS:%=%_64)
80
81ifeq ($(CAN_BUILD_WITH_NOPIE),1)
82CFLAGS += -no-pie
83endif
84
85ifeq ($(CAN_BUILD_I386),1)
86TEST_GEN_PROGS += $(BINARIES_32)
87endif
88
89ifeq ($(CAN_BUILD_X86_64),1)
90TEST_GEN_PROGS += $(BINARIES_64)
91endif
92else
93
94ifneq (,$(findstring $(ARCH),ppc64))
95TEST_GEN_PROGS += protection_keys
96endif
97
98endif
99
100ifneq (,$(filter $(ARCH),arm64 ia64 mips64 parisc64 ppc64 riscv64 s390x sparc64 x86_64))
101TEST_GEN_PROGS += va_high_addr_switch
102TEST_GEN_PROGS += virtual_address_range
103TEST_GEN_PROGS += write_to_hugetlbfs
104endif
105
106TEST_PROGS := run_vmtests.sh
107
108TEST_FILES := test_vmalloc.sh
109TEST_FILES += test_hmm.sh
110TEST_FILES += va_high_addr_switch.sh
111
112include ../lib.mk
113
114$(TEST_GEN_PROGS): vm_util.c
115
116$(OUTPUT)/uffd-stress: uffd-common.c
117$(OUTPUT)/uffd-unit-tests: uffd-common.c
118
119ifeq ($(ARCH),x86_64)
120BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
121BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))
122
123define gen-target-rule-32
124$(1) $(1)_32: $(OUTPUT)/$(1)_32
125.PHONY: $(1) $(1)_32
126endef
127
128define gen-target-rule-64
129$(1) $(1)_64: $(OUTPUT)/$(1)_64
130.PHONY: $(1) $(1)_64
131endef
132
133ifeq ($(CAN_BUILD_I386),1)
134$(BINARIES_32): CFLAGS += -m32 -mxsave
135$(BINARIES_32): LDLIBS += -lrt -ldl -lm
136$(BINARIES_32): $(OUTPUT)/%_32: %.c
137	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
138$(foreach t,$(VMTARGETS),$(eval $(call gen-target-rule-32,$(t))))
139endif
140
141ifeq ($(CAN_BUILD_X86_64),1)
142$(BINARIES_64): CFLAGS += -m64 -mxsave
143$(BINARIES_64): LDLIBS += -lrt -ldl
144$(BINARIES_64): $(OUTPUT)/%_64: %.c
145	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
146$(foreach t,$(VMTARGETS),$(eval $(call gen-target-rule-64,$(t))))
147endif
148
149# x86_64 users should be encouraged to install 32-bit libraries
150ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),01)
151all: warn_32bit_failure
152
153warn_32bit_failure:
154	@echo "Warning: you seem to have a broken 32-bit build" 2>&1;		\
155	echo  "environment. This will reduce test coverage of 64-bit" 2>&1;	\
156	echo  "kernels. If you are using a Debian-like distribution," 2>&1;	\
157	echo  "try:"; 2>&1;							\
158	echo  "";								\
159	echo  "  apt-get install gcc-multilib libc6-i386 libc6-dev-i386";	\
160	echo  "";								\
161	echo  "If you are using a Fedora-like distribution, try:";		\
162	echo  "";								\
163	echo  "  yum install glibc-devel.*i686";				\
164	exit 0;
165endif
166endif
167
168# IOURING_EXTRA_LIBS may get set in local_config.mk, or it may be left empty.
169$(OUTPUT)/cow: LDLIBS += $(IOURING_EXTRA_LIBS)
170
171$(OUTPUT)/gup_longterm: LDLIBS += $(IOURING_EXTRA_LIBS)
172
173$(OUTPUT)/mlock-random-test $(OUTPUT)/memfd_secret: LDLIBS += -lcap
174
175$(OUTPUT)/ksm_tests: LDLIBS += -lnuma
176
177$(OUTPUT)/migration: LDLIBS += -lnuma
178
179local_config.mk local_config.h: check_config.sh
180	/bin/sh ./check_config.sh $(CC)
181
182EXTRA_CLEAN += local_config.mk local_config.h
183
184ifeq ($(IOURING_EXTRA_LIBS),)
185all: warn_missing_liburing
186
187warn_missing_liburing:
188	@echo ; \
189	echo "Warning: missing liburing support. Some tests will be skipped." ; \
190	echo
191endif
192