mmu.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/mm/mmu.c
  4. *
  5. * Copyright (C) 1995-2005 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #include <linux/cache.h>
  9. #include <linux/export.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/init.h>
  13. #include <linux/ioport.h>
  14. #include <linux/kexec.h>
  15. #include <linux/libfdt.h>
  16. #include <linux/mman.h>
  17. #include <linux/nodemask.h>
  18. #include <linux/memblock.h>
  19. #include <linux/memremap.h>
  20. #include <linux/memory.h>
  21. #include <linux/fs.h>
  22. #include <linux/io.h>
  23. #include <linux/mm.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/set_memory.h>
  26. #include <linux/kfence.h>
  27. #include <asm/barrier.h>
  28. #include <asm/cputype.h>
  29. #include <asm/fixmap.h>
  30. #include <asm/kasan.h>
  31. #include <asm/kernel-pgtable.h>
  32. #include <asm/sections.h>
  33. #include <asm/setup.h>
  34. #include <linux/sizes.h>
  35. #include <asm/tlb.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/ptdump.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/pgalloc.h>
  40. #include <asm/kfence.h>
  41. #ifdef CONFIG_RKP
  42. #include <linux/rkp.h>
  43. #endif
  44. #define NO_BLOCK_MAPPINGS BIT(0)
  45. #define NO_CONT_MAPPINGS BIT(1)
  46. #define NO_EXEC_MAPPINGS BIT(2) /* assumes FEAT_HPDS is not used */
  47. int idmap_t0sz __ro_after_init;
  48. #if VA_BITS > 48
  49. u64 vabits_actual __ro_after_init = VA_BITS_MIN;
  50. EXPORT_SYMBOL(vabits_actual);
  51. #endif
  52. u64 kimage_vaddr __ro_after_init = (u64)&_text;
  53. EXPORT_SYMBOL(kimage_vaddr);
  54. u64 kimage_voffset __ro_after_init;
  55. EXPORT_SYMBOL(kimage_voffset);
  56. u32 __boot_cpu_mode[] = { BOOT_CPU_MODE_EL2, BOOT_CPU_MODE_EL1 };
  57. /*
  58. * The booting CPU updates the failed status @__early_cpu_boot_status,
  59. * with MMU turned off.
  60. */
  61. long __section(".mmuoff.data.write") __early_cpu_boot_status;
  62. /*
  63. * Empty_zero_page is a special page that is used for zero-initialized data
  64. * and COW.
  65. */
  66. unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
  67. EXPORT_SYMBOL(empty_zero_page);
  68. static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
  69. static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
  70. static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
  71. static DEFINE_SPINLOCK(swapper_pgdir_lock);
  72. static DEFINE_MUTEX(fixmap_lock);
  73. void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
  74. {
  75. pgd_t *fixmap_pgdp;
  76. spin_lock(&swapper_pgdir_lock);
  77. fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
  78. WRITE_ONCE(*fixmap_pgdp, pgd);
  79. /*
  80. * We need dsb(ishst) here to ensure the page-table-walker sees
  81. * our new entry before set_p?d() returns. The fixmap's
  82. * flush_tlb_kernel_range() via clear_fixmap() does this for us.
  83. */
  84. pgd_clear_fixmap();
  85. spin_unlock(&swapper_pgdir_lock);
  86. }
  87. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  88. unsigned long size, pgprot_t vma_prot)
  89. {
  90. if (!pfn_is_map_memory(pfn))
  91. return pgprot_noncached(vma_prot);
  92. else if (file->f_flags & O_SYNC)
  93. return pgprot_writecombine(vma_prot);
  94. return vma_prot;
  95. }
  96. EXPORT_SYMBOL(phys_mem_access_prot);
  97. static phys_addr_t __init early_pgtable_alloc(int shift)
  98. {
  99. phys_addr_t phys;
  100. void *ptr;
  101. phys = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0,
  102. MEMBLOCK_ALLOC_NOLEAKTRACE);
  103. if (!phys)
  104. panic("Failed to allocate page table page\n");
  105. /*
  106. * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
  107. * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
  108. * any level of table.
  109. */
  110. ptr = pte_set_fixmap(phys);
  111. memset(ptr, 0, PAGE_SIZE);
  112. /*
  113. * Implicit barriers also ensure the zeroed page is visible to the page
  114. * table walker
  115. */
  116. pte_clear_fixmap();
  117. return phys;
  118. }
  119. static bool pgattr_change_is_safe(u64 old, u64 new)
  120. {
  121. /*
  122. * The following mapping attributes may be updated in live
  123. * kernel mappings without the need for break-before-make.
  124. */
  125. pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
  126. /* creating or taking down mappings is always safe */
  127. if (old == 0 || new == 0)
  128. return true;
  129. /* live contiguous mappings may not be manipulated at all */
  130. if ((old | new) & PTE_CONT)
  131. return false;
  132. /* Transitioning from Non-Global to Global is unsafe */
  133. if (old & ~new & PTE_NG)
  134. return false;
  135. /*
  136. * Changing the memory type between Normal and Normal-Tagged is safe
  137. * since Tagged is considered a permission attribute from the
  138. * mismatched attribute aliases perspective.
  139. */
  140. if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
  141. (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) &&
  142. ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
  143. (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)))
  144. mask |= PTE_ATTRINDX_MASK;
  145. return ((old ^ new) & ~mask) == 0;
  146. }
  147. static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
  148. phys_addr_t phys, pgprot_t prot)
  149. {
  150. pte_t *ptep;
  151. ptep = pte_set_fixmap_offset(pmdp, addr);
  152. do {
  153. pte_t old_pte = READ_ONCE(*ptep);
  154. set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
  155. /*
  156. * After the PTE entry has been populated once, we
  157. * only allow updates to the permission attributes.
  158. */
  159. BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
  160. READ_ONCE(pte_val(*ptep))));
  161. phys += PAGE_SIZE;
  162. } while (ptep++, addr += PAGE_SIZE, addr != end);
  163. pte_clear_fixmap();
  164. }
  165. static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
  166. unsigned long end, phys_addr_t phys,
  167. pgprot_t prot,
  168. phys_addr_t (*pgtable_alloc)(int),
  169. int flags)
  170. {
  171. unsigned long next;
  172. pmd_t pmd = READ_ONCE(*pmdp);
  173. BUG_ON(pmd_sect(pmd));
  174. if (pmd_none(pmd)) {
  175. pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN;
  176. phys_addr_t pte_phys;
  177. if (flags & NO_EXEC_MAPPINGS)
  178. pmdval |= PMD_TABLE_PXN;
  179. BUG_ON(!pgtable_alloc);
  180. pte_phys = pgtable_alloc(PAGE_SHIFT);
  181. __pmd_populate(pmdp, pte_phys, pmdval);
  182. pmd = READ_ONCE(*pmdp);
  183. }
  184. BUG_ON(pmd_bad(pmd));
  185. do {
  186. pgprot_t __prot = prot;
  187. next = pte_cont_addr_end(addr, end);
  188. /* use a contiguous mapping if the range is suitably aligned */
  189. if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
  190. (flags & NO_CONT_MAPPINGS) == 0)
  191. __prot = __pgprot(pgprot_val(prot) | PTE_CONT);
  192. init_pte(pmdp, addr, next, phys, __prot);
  193. phys += next - addr;
  194. } while (addr = next, addr != end);
  195. }
  196. static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
  197. phys_addr_t phys, pgprot_t prot,
  198. phys_addr_t (*pgtable_alloc)(int), int flags)
  199. {
  200. unsigned long next;
  201. pmd_t *pmdp;
  202. pmdp = pmd_set_fixmap_offset(pudp, addr);
  203. do {
  204. pmd_t old_pmd = READ_ONCE(*pmdp);
  205. next = pmd_addr_end(addr, end);
  206. /* try section mapping first */
  207. if (((addr | next | phys) & ~PMD_MASK) == 0 &&
  208. (flags & NO_BLOCK_MAPPINGS) == 0) {
  209. pmd_set_huge(pmdp, phys, prot);
  210. /*
  211. * After the PMD entry has been populated once, we
  212. * only allow updates to the permission attributes.
  213. */
  214. BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
  215. READ_ONCE(pmd_val(*pmdp))));
  216. } else {
  217. alloc_init_cont_pte(pmdp, addr, next, phys, prot,
  218. pgtable_alloc, flags);
  219. BUG_ON(pmd_val(old_pmd) != 0 &&
  220. pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
  221. }
  222. phys += next - addr;
  223. } while (pmdp++, addr = next, addr != end);
  224. pmd_clear_fixmap();
  225. }
  226. static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
  227. unsigned long end, phys_addr_t phys,
  228. pgprot_t prot,
  229. phys_addr_t (*pgtable_alloc)(int), int flags)
  230. {
  231. unsigned long next;
  232. pud_t pud = READ_ONCE(*pudp);
  233. /*
  234. * Check for initial section mappings in the pgd/pud.
  235. */
  236. BUG_ON(pud_sect(pud));
  237. if (pud_none(pud)) {
  238. pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN;
  239. phys_addr_t pmd_phys;
  240. if (flags & NO_EXEC_MAPPINGS)
  241. pudval |= PUD_TABLE_PXN;
  242. BUG_ON(!pgtable_alloc);
  243. #ifdef CONFIG_RKP
  244. pmd_phys = rkp_ro_alloc_phys(PMD_SHIFT);
  245. if (!pmd_phys)
  246. pmd_phys = pgtable_alloc(PMD_SHIFT);
  247. #else
  248. pmd_phys = pgtable_alloc(PMD_SHIFT);
  249. #endif
  250. __pud_populate(pudp, pmd_phys, pudval);
  251. pud = READ_ONCE(*pudp);
  252. }
  253. BUG_ON(pud_bad(pud));
  254. do {
  255. pgprot_t __prot = prot;
  256. next = pmd_cont_addr_end(addr, end);
  257. /* use a contiguous mapping if the range is suitably aligned */
  258. if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
  259. (flags & NO_CONT_MAPPINGS) == 0)
  260. __prot = __pgprot(pgprot_val(prot) | PTE_CONT);
  261. init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
  262. phys += next - addr;
  263. } while (addr = next, addr != end);
  264. }
  265. static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
  266. phys_addr_t phys, pgprot_t prot,
  267. phys_addr_t (*pgtable_alloc)(int),
  268. int flags)
  269. {
  270. unsigned long next;
  271. pud_t *pudp;
  272. p4d_t *p4dp = p4d_offset(pgdp, addr);
  273. p4d_t p4d = READ_ONCE(*p4dp);
  274. if (p4d_none(p4d)) {
  275. p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN;
  276. phys_addr_t pud_phys;
  277. if (flags & NO_EXEC_MAPPINGS)
  278. p4dval |= P4D_TABLE_PXN;
  279. BUG_ON(!pgtable_alloc);
  280. pud_phys = pgtable_alloc(PUD_SHIFT);
  281. __p4d_populate(p4dp, pud_phys, p4dval);
  282. p4d = READ_ONCE(*p4dp);
  283. }
  284. BUG_ON(p4d_bad(p4d));
  285. pudp = pud_set_fixmap_offset(p4dp, addr);
  286. do {
  287. pud_t old_pud = READ_ONCE(*pudp);
  288. next = pud_addr_end(addr, end);
  289. /*
  290. * For 4K granule only, attempt to put down a 1GB block
  291. */
  292. if (pud_sect_supported() &&
  293. ((addr | next | phys) & ~PUD_MASK) == 0 &&
  294. (flags & NO_BLOCK_MAPPINGS) == 0) {
  295. pud_set_huge(pudp, phys, prot);
  296. /*
  297. * After the PUD entry has been populated once, we
  298. * only allow updates to the permission attributes.
  299. */
  300. BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
  301. READ_ONCE(pud_val(*pudp))));
  302. } else {
  303. alloc_init_cont_pmd(pudp, addr, next, phys, prot,
  304. pgtable_alloc, flags);
  305. BUG_ON(pud_val(old_pud) != 0 &&
  306. pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
  307. }
  308. phys += next - addr;
  309. } while (pudp++, addr = next, addr != end);
  310. pud_clear_fixmap();
  311. }
  312. static void __create_pgd_mapping_locked(pgd_t *pgdir, phys_addr_t phys,
  313. unsigned long virt, phys_addr_t size,
  314. pgprot_t prot,
  315. phys_addr_t (*pgtable_alloc)(int),
  316. int flags)
  317. {
  318. unsigned long addr, end, next;
  319. pgd_t *pgdp = pgd_offset_pgd(pgdir, virt);
  320. /*
  321. * If the virtual and physical address don't have the same offset
  322. * within a page, we cannot map the region as the caller expects.
  323. */
  324. if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
  325. return;
  326. phys &= PAGE_MASK;
  327. addr = virt & PAGE_MASK;
  328. end = PAGE_ALIGN(virt + size);
  329. do {
  330. next = pgd_addr_end(addr, end);
  331. alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
  332. flags);
  333. phys += next - addr;
  334. } while (pgdp++, addr = next, addr != end);
  335. }
  336. static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
  337. unsigned long virt, phys_addr_t size,
  338. pgprot_t prot,
  339. phys_addr_t (*pgtable_alloc)(int),
  340. int flags)
  341. {
  342. mutex_lock(&fixmap_lock);
  343. __create_pgd_mapping_locked(pgdir, phys, virt, size, prot,
  344. pgtable_alloc, flags);
  345. mutex_unlock(&fixmap_lock);
  346. }
  347. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  348. extern __alias(__create_pgd_mapping_locked)
  349. void create_kpti_ng_temp_pgd(pgd_t *pgdir, phys_addr_t phys, unsigned long virt,
  350. phys_addr_t size, pgprot_t prot,
  351. phys_addr_t (*pgtable_alloc)(int), int flags);
  352. #endif
  353. static phys_addr_t __pgd_pgtable_alloc(int shift)
  354. {
  355. void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL);
  356. BUG_ON(!ptr);
  357. /* Ensure the zeroed page is visible to the page table walker */
  358. dsb(ishst);
  359. return __pa(ptr);
  360. }
  361. static phys_addr_t pgd_pgtable_alloc(int shift)
  362. {
  363. phys_addr_t pa = __pgd_pgtable_alloc(shift);
  364. /*
  365. * Call proper page table ctor in case later we need to
  366. * call core mm functions like apply_to_page_range() on
  367. * this pre-allocated page table.
  368. *
  369. * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is
  370. * folded, and if so pgtable_pmd_page_ctor() becomes nop.
  371. */
  372. if (shift == PAGE_SHIFT)
  373. BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa)));
  374. else if (shift == PMD_SHIFT)
  375. BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa)));
  376. return pa;
  377. }
  378. /*
  379. * This function can only be used to modify existing table entries,
  380. * without allocating new levels of table. Note that this permits the
  381. * creation of new section or page entries.
  382. */
  383. static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
  384. phys_addr_t size, pgprot_t prot)
  385. {
  386. if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
  387. pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
  388. &phys, virt);
  389. return;
  390. }
  391. __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
  392. NO_CONT_MAPPINGS);
  393. }
  394. void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
  395. unsigned long virt, phys_addr_t size,
  396. pgprot_t prot, bool page_mappings_only)
  397. {
  398. int flags = 0;
  399. BUG_ON(mm == &init_mm);
  400. if (page_mappings_only)
  401. flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  402. __create_pgd_mapping(mm->pgd, phys, virt, size, prot,
  403. pgd_pgtable_alloc, flags);
  404. }
  405. static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
  406. phys_addr_t size, pgprot_t prot)
  407. {
  408. if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
  409. pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
  410. &phys, virt);
  411. return;
  412. }
  413. __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
  414. NO_CONT_MAPPINGS);
  415. /* flush the TLBs after updating live kernel mappings */
  416. flush_tlb_kernel_range(virt, virt + size);
  417. }
  418. static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
  419. phys_addr_t end, pgprot_t prot, int flags)
  420. {
  421. __create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
  422. prot, early_pgtable_alloc, flags);
  423. }
  424. void __init mark_linear_text_alias_ro(void)
  425. {
  426. /*
  427. * Remove the write permissions from the linear alias of .text/.rodata
  428. */
  429. update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext),
  430. (unsigned long)__init_begin - (unsigned long)_stext,
  431. PAGE_KERNEL_RO);
  432. }
  433. static bool crash_mem_map __initdata;
  434. static int __init enable_crash_mem_map(char *arg)
  435. {
  436. /*
  437. * Proper parameter parsing is done by reserve_crashkernel(). We only
  438. * need to know if the linear map has to avoid block mappings so that
  439. * the crashkernel reservations can be unmapped later.
  440. */
  441. crash_mem_map = true;
  442. return 0;
  443. }
  444. early_param("crashkernel", enable_crash_mem_map);
  445. #ifdef CONFIG_KFENCE
  446. bool __ro_after_init kfence_early_init = !!CONFIG_KFENCE_SAMPLE_INTERVAL;
  447. /* early_param() will be parsed before map_mem() below. */
  448. static int __init parse_kfence_early_init(char *arg)
  449. {
  450. int val;
  451. if (get_option(&arg, &val))
  452. kfence_early_init = !!val;
  453. return 0;
  454. }
  455. early_param("kfence.sample_interval", parse_kfence_early_init);
  456. static phys_addr_t __init arm64_kfence_alloc_pool(void)
  457. {
  458. phys_addr_t kfence_pool;
  459. if (!kfence_early_init)
  460. return 0;
  461. kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
  462. if (!kfence_pool) {
  463. pr_err("failed to allocate kfence pool\n");
  464. kfence_early_init = false;
  465. return 0;
  466. }
  467. /* Temporarily mark as NOMAP. */
  468. memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE);
  469. return kfence_pool;
  470. }
  471. static void __init arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp)
  472. {
  473. if (!kfence_pool)
  474. return;
  475. /* KFENCE pool needs page-level mapping. */
  476. __map_memblock(pgdp, kfence_pool, kfence_pool + KFENCE_POOL_SIZE,
  477. pgprot_tagged(PAGE_KERNEL),
  478. NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
  479. memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE);
  480. __kfence_pool = phys_to_virt(kfence_pool);
  481. }
  482. #else /* CONFIG_KFENCE */
  483. static inline phys_addr_t arm64_kfence_alloc_pool(void) { return 0; }
  484. static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { }
  485. #endif /* CONFIG_KFENCE */
  486. static void __init map_mem(pgd_t *pgdp)
  487. {
  488. static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
  489. phys_addr_t kernel_start = __pa_symbol(_stext);
  490. phys_addr_t kernel_end = __pa_symbol(__init_begin);
  491. phys_addr_t start, end;
  492. phys_addr_t early_kfence_pool;
  493. int flags = NO_EXEC_MAPPINGS;
  494. u64 i;
  495. /*
  496. * Setting hierarchical PXNTable attributes on table entries covering
  497. * the linear region is only possible if it is guaranteed that no table
  498. * entries at any level are being shared between the linear region and
  499. * the vmalloc region. Check whether this is true for the PGD level, in
  500. * which case it is guaranteed to be true for all other levels as well.
  501. */
  502. BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end));
  503. early_kfence_pool = arm64_kfence_alloc_pool();
  504. if (can_set_direct_map())
  505. flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  506. /*
  507. * Take care not to create a writable alias for the
  508. * read-only text and rodata sections of the kernel image.
  509. * So temporarily mark them as NOMAP to skip mappings in
  510. * the following for-loop
  511. */
  512. memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
  513. #ifdef CONFIG_KEXEC_CORE
  514. if (crash_mem_map) {
  515. if (defer_reserve_crashkernel())
  516. flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  517. else if (crashk_res.end)
  518. memblock_mark_nomap(crashk_res.start,
  519. resource_size(&crashk_res));
  520. }
  521. #endif
  522. /* map all the memory banks */
  523. for_each_mem_range(i, &start, &end) {
  524. if (start >= end)
  525. break;
  526. /*
  527. * The linear map must allow allocation tags reading/writing
  528. * if MTE is present. Otherwise, it has the same attributes as
  529. * PAGE_KERNEL.
  530. */
  531. __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL),
  532. flags);
  533. }
  534. /*
  535. * Map the linear alias of the [_stext, __init_begin) interval
  536. * as non-executable now, and remove the write permission in
  537. * mark_linear_text_alias_ro() below (which will be called after
  538. * alternative patching has completed). This makes the contents
  539. * of the region accessible to subsystems such as hibernate,
  540. * but protects it from inadvertent modification or execution.
  541. * Note that contiguous mappings cannot be remapped in this way,
  542. * so we should avoid them here.
  543. */
  544. __map_memblock(pgdp, kernel_start, kernel_end,
  545. PAGE_KERNEL, NO_CONT_MAPPINGS);
  546. memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
  547. /*
  548. * Use page-level mappings here so that we can shrink the region
  549. * in page granularity and put back unused memory to buddy system
  550. * through /sys/kernel/kexec_crash_size interface.
  551. */
  552. #ifdef CONFIG_KEXEC_CORE
  553. if (crash_mem_map && !defer_reserve_crashkernel()) {
  554. if (crashk_res.end) {
  555. __map_memblock(pgdp, crashk_res.start,
  556. crashk_res.end + 1,
  557. PAGE_KERNEL,
  558. NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
  559. memblock_clear_nomap(crashk_res.start,
  560. resource_size(&crashk_res));
  561. }
  562. }
  563. #endif
  564. arm64_kfence_map_pool(early_kfence_pool, pgdp);
  565. }
  566. void mark_rodata_ro(void)
  567. {
  568. unsigned long section_size;
  569. /*
  570. * mark .rodata as read only. Use __init_begin rather than __end_rodata
  571. * to cover NOTES and EXCEPTION_TABLE.
  572. */
  573. section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
  574. update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
  575. section_size, PAGE_KERNEL_RO);
  576. debug_checkwx();
  577. }
  578. static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
  579. pgprot_t prot, struct vm_struct *vma,
  580. int flags, unsigned long vm_flags)
  581. {
  582. phys_addr_t pa_start = __pa_symbol(va_start);
  583. unsigned long size = va_end - va_start;
  584. BUG_ON(!PAGE_ALIGNED(pa_start));
  585. BUG_ON(!PAGE_ALIGNED(size));
  586. __create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
  587. early_pgtable_alloc, flags);
  588. if (!(vm_flags & VM_NO_GUARD))
  589. size += PAGE_SIZE;
  590. vma->addr = va_start;
  591. vma->phys_addr = pa_start;
  592. vma->size = size;
  593. vma->flags = VM_MAP | vm_flags;
  594. vma->caller = __builtin_return_address(0);
  595. vm_area_add_early(vma);
  596. }
  597. #ifdef CONFIG_RKP
  598. static void __init map_kernel_text_segment(pgd_t *pgdp, void *va_start, void *va_end,
  599. pgprot_t prot, struct vm_struct *vma,
  600. int flags, unsigned long vm_flags)
  601. {
  602. phys_addr_t pa_start = __pa_symbol(va_start);
  603. unsigned long size = va_end - va_start;
  604. WARN_ON(!PAGE_ALIGNED(pa_start));
  605. WARN_ON(!PAGE_ALIGNED(size));
  606. __create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
  607. rkp_ro_alloc_phys, flags);
  608. if (!(vm_flags & VM_NO_GUARD))
  609. size += PAGE_SIZE;
  610. vma->addr = (void *)((unsigned long)va_start & PMD_MASK);
  611. vma->phys_addr = (phys_addr_t)((unsigned long)pa_start & PMD_MASK);
  612. vma->size = size + (unsigned long)va_start - (unsigned long)vma->addr;
  613. vma->flags = VM_MAP | vm_flags;
  614. vma->caller = __builtin_return_address(0);
  615. vm_area_add_early(vma);
  616. }
  617. #endif
  618. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  619. static int __init map_entry_trampoline(void)
  620. {
  621. int i;
  622. pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
  623. phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
  624. /* The trampoline is always mapped and can therefore be global */
  625. pgprot_val(prot) &= ~PTE_NG;
  626. /* Map only the text into the trampoline page table */
  627. memset(tramp_pg_dir, 0, PGD_SIZE);
  628. __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS,
  629. entry_tramp_text_size(), prot,
  630. __pgd_pgtable_alloc, NO_BLOCK_MAPPINGS);
  631. /* Map both the text and data into the kernel page table */
  632. for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++)
  633. __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
  634. pa_start + i * PAGE_SIZE, prot);
  635. if (IS_ENABLED(CONFIG_RELOCATABLE))
  636. __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
  637. pa_start + i * PAGE_SIZE, PAGE_KERNEL_RO);
  638. return 0;
  639. }
  640. core_initcall(map_entry_trampoline);
  641. #endif
  642. /*
  643. * Open coded check for BTI, only for use to determine configuration
  644. * for early mappings for before the cpufeature code has run.
  645. */
  646. static bool arm64_early_this_cpu_has_bti(void)
  647. {
  648. u64 pfr1;
  649. if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
  650. return false;
  651. pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
  652. return cpuid_feature_extract_unsigned_field(pfr1,
  653. ID_AA64PFR1_EL1_BT_SHIFT);
  654. }
  655. /*
  656. * Create fine-grained mappings for the kernel.
  657. */
  658. static void __init map_kernel(pgd_t *pgdp)
  659. {
  660. static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
  661. vmlinux_initdata, vmlinux_data;
  662. /*
  663. * External debuggers may need to write directly to the text
  664. * mapping to install SW breakpoints. Allow this (only) when
  665. * explicitly requested with rodata=off.
  666. */
  667. pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
  668. /*
  669. * If we have a CPU that supports BTI and a kernel built for
  670. * BTI then mark the kernel executable text as guarded pages
  671. * now so we don't have to rewrite the page tables later.
  672. */
  673. if (arm64_early_this_cpu_has_bti())
  674. text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP);
  675. /*
  676. * Only rodata will be remapped with different permissions later on,
  677. * all other segments are allowed to use contiguous mappings.
  678. */
  679. #ifdef CONFIG_RKP
  680. map_kernel_text_segment(pgdp, _stext, _etext, text_prot, &vmlinux_text, 0,
  681. VM_NO_GUARD);
  682. #else
  683. map_kernel_segment(pgdp, _stext, _etext, text_prot, &vmlinux_text, 0,
  684. VM_NO_GUARD);
  685. #endif
  686. map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
  687. &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
  688. map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
  689. &vmlinux_inittext, 0, VM_NO_GUARD);
  690. map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
  691. &vmlinux_initdata, 0, VM_NO_GUARD);
  692. map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
  693. if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) {
  694. /*
  695. * The fixmap falls in a separate pgd to the kernel, and doesn't
  696. * live in the carveout for the swapper_pg_dir. We can simply
  697. * re-use the existing dir for the fixmap.
  698. */
  699. set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START),
  700. READ_ONCE(*pgd_offset_k(FIXADDR_START)));
  701. } else if (CONFIG_PGTABLE_LEVELS > 3) {
  702. pgd_t *bm_pgdp;
  703. p4d_t *bm_p4dp;
  704. pud_t *bm_pudp;
  705. /*
  706. * The fixmap shares its top level pgd entry with the kernel
  707. * mapping. This can really only occur when we are running
  708. * with 16k/4 levels, so we can simply reuse the pud level
  709. * entry instead.
  710. */
  711. BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
  712. bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START);
  713. bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START);
  714. bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START);
  715. pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd));
  716. pud_clear_fixmap();
  717. } else {
  718. BUG();
  719. }
  720. kasan_copy_shadow(pgdp);
  721. }
  722. static void __init create_idmap(void)
  723. {
  724. u64 start = __pa_symbol(__idmap_text_start);
  725. u64 size = __pa_symbol(__idmap_text_end) - start;
  726. pgd_t *pgd = idmap_pg_dir;
  727. u64 pgd_phys;
  728. /* check if we need an additional level of translation */
  729. if (VA_BITS < 48 && idmap_t0sz < (64 - VA_BITS_MIN)) {
  730. pgd_phys = early_pgtable_alloc(PAGE_SHIFT);
  731. set_pgd(&idmap_pg_dir[start >> VA_BITS],
  732. __pgd(pgd_phys | P4D_TYPE_TABLE));
  733. pgd = __va(pgd_phys);
  734. }
  735. __create_pgd_mapping(pgd, start, start, size, PAGE_KERNEL_ROX,
  736. early_pgtable_alloc, 0);
  737. if (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0)) {
  738. extern u32 __idmap_kpti_flag;
  739. u64 pa = __pa_symbol(&__idmap_kpti_flag);
  740. /*
  741. * The KPTI G-to-nG conversion code needs a read-write mapping
  742. * of its synchronization flag in the ID map.
  743. */
  744. __create_pgd_mapping(pgd, pa, pa, sizeof(u32), PAGE_KERNEL,
  745. early_pgtable_alloc, 0);
  746. }
  747. }
  748. void __init paging_init(void)
  749. {
  750. pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
  751. extern pgd_t init_idmap_pg_dir[];
  752. idmap_t0sz = 63UL - __fls(__pa_symbol(_end) | GENMASK(VA_BITS_MIN - 1, 0));
  753. #ifdef CONFIG_RKP
  754. rkp_robuffer_init();
  755. #endif
  756. map_kernel(pgdp);
  757. map_mem(pgdp);
  758. pgd_clear_fixmap();
  759. cpu_replace_ttbr1(lm_alias(swapper_pg_dir), init_idmap_pg_dir);
  760. init_mm.pgd = swapper_pg_dir;
  761. memblock_phys_free(__pa_symbol(init_pg_dir),
  762. __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
  763. memblock_allow_resize();
  764. create_idmap();
  765. }
  766. /*
  767. * Check whether a kernel address is valid (derived from arch/x86/).
  768. */
  769. int kern_addr_valid(unsigned long addr)
  770. {
  771. pgd_t *pgdp;
  772. p4d_t *p4dp;
  773. pud_t *pudp, pud;
  774. pmd_t *pmdp, pmd;
  775. pte_t *ptep, pte;
  776. addr = arch_kasan_reset_tag(addr);
  777. if ((((long)addr) >> VA_BITS) != -1UL)
  778. return 0;
  779. pgdp = pgd_offset_k(addr);
  780. if (pgd_none(READ_ONCE(*pgdp)))
  781. return 0;
  782. p4dp = p4d_offset(pgdp, addr);
  783. if (p4d_none(READ_ONCE(*p4dp)))
  784. return 0;
  785. pudp = pud_offset(p4dp, addr);
  786. pud = READ_ONCE(*pudp);
  787. if (pud_none(pud))
  788. return 0;
  789. if (pud_sect(pud))
  790. return pfn_valid(pud_pfn(pud));
  791. pmdp = pmd_offset(pudp, addr);
  792. pmd = READ_ONCE(*pmdp);
  793. if (pmd_none(pmd))
  794. return 0;
  795. if (pmd_sect(pmd))
  796. return pfn_valid(pmd_pfn(pmd));
  797. ptep = pte_offset_kernel(pmdp, addr);
  798. pte = READ_ONCE(*ptep);
  799. if (pte_none(pte))
  800. return 0;
  801. return pfn_valid(pte_pfn(pte));
  802. }
  803. #ifdef CONFIG_MEMORY_HOTPLUG
  804. static void free_hotplug_page_range(struct page *page, size_t size,
  805. struct vmem_altmap *altmap)
  806. {
  807. if (altmap) {
  808. vmem_altmap_free(altmap, size >> PAGE_SHIFT);
  809. } else {
  810. WARN_ON(PageReserved(page));
  811. free_pages((unsigned long)page_address(page), get_order(size));
  812. }
  813. }
  814. static void free_hotplug_pgtable_page(struct page *page)
  815. {
  816. free_hotplug_page_range(page, PAGE_SIZE, NULL);
  817. }
  818. static bool pgtable_range_aligned(unsigned long start, unsigned long end,
  819. unsigned long floor, unsigned long ceiling,
  820. unsigned long mask)
  821. {
  822. start &= mask;
  823. if (start < floor)
  824. return false;
  825. if (ceiling) {
  826. ceiling &= mask;
  827. if (!ceiling)
  828. return false;
  829. }
  830. if (end - 1 > ceiling - 1)
  831. return false;
  832. return true;
  833. }
  834. static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
  835. unsigned long end, bool free_mapped,
  836. struct vmem_altmap *altmap)
  837. {
  838. pte_t *ptep, pte;
  839. do {
  840. ptep = pte_offset_kernel(pmdp, addr);
  841. pte = READ_ONCE(*ptep);
  842. if (pte_none(pte))
  843. continue;
  844. WARN_ON(!pte_present(pte));
  845. pte_clear(&init_mm, addr, ptep);
  846. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  847. if (free_mapped)
  848. free_hotplug_page_range(pte_page(pte),
  849. PAGE_SIZE, altmap);
  850. } while (addr += PAGE_SIZE, addr < end);
  851. }
  852. static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
  853. unsigned long end, bool free_mapped,
  854. struct vmem_altmap *altmap)
  855. {
  856. unsigned long next;
  857. pmd_t *pmdp, pmd;
  858. do {
  859. next = pmd_addr_end(addr, end);
  860. pmdp = pmd_offset(pudp, addr);
  861. pmd = READ_ONCE(*pmdp);
  862. if (pmd_none(pmd))
  863. continue;
  864. WARN_ON(!pmd_present(pmd));
  865. if (pmd_sect(pmd)) {
  866. pmd_clear(pmdp);
  867. /*
  868. * One TLBI should be sufficient here as the PMD_SIZE
  869. * range is mapped with a single block entry.
  870. */
  871. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  872. if (free_mapped)
  873. free_hotplug_page_range(pmd_page(pmd),
  874. PMD_SIZE, altmap);
  875. continue;
  876. }
  877. WARN_ON(!pmd_table(pmd));
  878. unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap);
  879. } while (addr = next, addr < end);
  880. }
  881. static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
  882. unsigned long end, bool free_mapped,
  883. struct vmem_altmap *altmap)
  884. {
  885. unsigned long next;
  886. pud_t *pudp, pud;
  887. do {
  888. next = pud_addr_end(addr, end);
  889. pudp = pud_offset(p4dp, addr);
  890. pud = READ_ONCE(*pudp);
  891. if (pud_none(pud))
  892. continue;
  893. WARN_ON(!pud_present(pud));
  894. if (pud_sect(pud)) {
  895. pud_clear(pudp);
  896. /*
  897. * One TLBI should be sufficient here as the PUD_SIZE
  898. * range is mapped with a single block entry.
  899. */
  900. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  901. if (free_mapped)
  902. free_hotplug_page_range(pud_page(pud),
  903. PUD_SIZE, altmap);
  904. continue;
  905. }
  906. WARN_ON(!pud_table(pud));
  907. unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap);
  908. } while (addr = next, addr < end);
  909. }
  910. static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
  911. unsigned long end, bool free_mapped,
  912. struct vmem_altmap *altmap)
  913. {
  914. unsigned long next;
  915. p4d_t *p4dp, p4d;
  916. do {
  917. next = p4d_addr_end(addr, end);
  918. p4dp = p4d_offset(pgdp, addr);
  919. p4d = READ_ONCE(*p4dp);
  920. if (p4d_none(p4d))
  921. continue;
  922. WARN_ON(!p4d_present(p4d));
  923. unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap);
  924. } while (addr = next, addr < end);
  925. }
  926. static void unmap_hotplug_range(unsigned long addr, unsigned long end,
  927. bool free_mapped, struct vmem_altmap *altmap)
  928. {
  929. unsigned long next;
  930. pgd_t *pgdp, pgd;
  931. /*
  932. * altmap can only be used as vmemmap mapping backing memory.
  933. * In case the backing memory itself is not being freed, then
  934. * altmap is irrelevant. Warn about this inconsistency when
  935. * encountered.
  936. */
  937. WARN_ON(!free_mapped && altmap);
  938. do {
  939. next = pgd_addr_end(addr, end);
  940. pgdp = pgd_offset_k(addr);
  941. pgd = READ_ONCE(*pgdp);
  942. if (pgd_none(pgd))
  943. continue;
  944. WARN_ON(!pgd_present(pgd));
  945. unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
  946. } while (addr = next, addr < end);
  947. }
  948. static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
  949. unsigned long end, unsigned long floor,
  950. unsigned long ceiling)
  951. {
  952. pte_t *ptep, pte;
  953. unsigned long i, start = addr;
  954. do {
  955. ptep = pte_offset_kernel(pmdp, addr);
  956. pte = READ_ONCE(*ptep);
  957. /*
  958. * This is just a sanity check here which verifies that
  959. * pte clearing has been done by earlier unmap loops.
  960. */
  961. WARN_ON(!pte_none(pte));
  962. } while (addr += PAGE_SIZE, addr < end);
  963. if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK))
  964. return;
  965. /*
  966. * Check whether we can free the pte page if the rest of the
  967. * entries are empty. Overlap with other regions have been
  968. * handled by the floor/ceiling check.
  969. */
  970. ptep = pte_offset_kernel(pmdp, 0UL);
  971. for (i = 0; i < PTRS_PER_PTE; i++) {
  972. if (!pte_none(READ_ONCE(ptep[i])))
  973. return;
  974. }
  975. pmd_clear(pmdp);
  976. __flush_tlb_kernel_pgtable(start);
  977. free_hotplug_pgtable_page(virt_to_page(ptep));
  978. }
  979. static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
  980. unsigned long end, unsigned long floor,
  981. unsigned long ceiling)
  982. {
  983. pmd_t *pmdp, pmd;
  984. unsigned long i, next, start = addr;
  985. do {
  986. next = pmd_addr_end(addr, end);
  987. pmdp = pmd_offset(pudp, addr);
  988. pmd = READ_ONCE(*pmdp);
  989. if (pmd_none(pmd))
  990. continue;
  991. WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd));
  992. free_empty_pte_table(pmdp, addr, next, floor, ceiling);
  993. } while (addr = next, addr < end);
  994. if (CONFIG_PGTABLE_LEVELS <= 2)
  995. return;
  996. if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
  997. return;
  998. /*
  999. * Check whether we can free the pmd page if the rest of the
  1000. * entries are empty. Overlap with other regions have been
  1001. * handled by the floor/ceiling check.
  1002. */
  1003. pmdp = pmd_offset(pudp, 0UL);
  1004. for (i = 0; i < PTRS_PER_PMD; i++) {
  1005. if (!pmd_none(READ_ONCE(pmdp[i])))
  1006. return;
  1007. }
  1008. pud_clear(pudp);
  1009. __flush_tlb_kernel_pgtable(start);
  1010. free_hotplug_pgtable_page(virt_to_page(pmdp));
  1011. }
  1012. static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
  1013. unsigned long end, unsigned long floor,
  1014. unsigned long ceiling)
  1015. {
  1016. pud_t *pudp, pud;
  1017. unsigned long i, next, start = addr;
  1018. do {
  1019. next = pud_addr_end(addr, end);
  1020. pudp = pud_offset(p4dp, addr);
  1021. pud = READ_ONCE(*pudp);
  1022. if (pud_none(pud))
  1023. continue;
  1024. WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud));
  1025. free_empty_pmd_table(pudp, addr, next, floor, ceiling);
  1026. } while (addr = next, addr < end);
  1027. if (CONFIG_PGTABLE_LEVELS <= 3)
  1028. return;
  1029. if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
  1030. return;
  1031. /*
  1032. * Check whether we can free the pud page if the rest of the
  1033. * entries are empty. Overlap with other regions have been
  1034. * handled by the floor/ceiling check.
  1035. */
  1036. pudp = pud_offset(p4dp, 0UL);
  1037. for (i = 0; i < PTRS_PER_PUD; i++) {
  1038. if (!pud_none(READ_ONCE(pudp[i])))
  1039. return;
  1040. }
  1041. p4d_clear(p4dp);
  1042. __flush_tlb_kernel_pgtable(start);
  1043. free_hotplug_pgtable_page(virt_to_page(pudp));
  1044. }
  1045. static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
  1046. unsigned long end, unsigned long floor,
  1047. unsigned long ceiling)
  1048. {
  1049. unsigned long next;
  1050. p4d_t *p4dp, p4d;
  1051. do {
  1052. next = p4d_addr_end(addr, end);
  1053. p4dp = p4d_offset(pgdp, addr);
  1054. p4d = READ_ONCE(*p4dp);
  1055. if (p4d_none(p4d))
  1056. continue;
  1057. WARN_ON(!p4d_present(p4d));
  1058. free_empty_pud_table(p4dp, addr, next, floor, ceiling);
  1059. } while (addr = next, addr < end);
  1060. }
  1061. static void free_empty_tables(unsigned long addr, unsigned long end,
  1062. unsigned long floor, unsigned long ceiling)
  1063. {
  1064. unsigned long next;
  1065. pgd_t *pgdp, pgd;
  1066. do {
  1067. next = pgd_addr_end(addr, end);
  1068. pgdp = pgd_offset_k(addr);
  1069. pgd = READ_ONCE(*pgdp);
  1070. if (pgd_none(pgd))
  1071. continue;
  1072. WARN_ON(!pgd_present(pgd));
  1073. free_empty_p4d_table(pgdp, addr, next, floor, ceiling);
  1074. } while (addr = next, addr < end);
  1075. }
  1076. #endif
  1077. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  1078. struct vmem_altmap *altmap)
  1079. {
  1080. unsigned long addr = start;
  1081. unsigned long next;
  1082. pgd_t *pgdp;
  1083. p4d_t *p4dp;
  1084. pud_t *pudp;
  1085. pmd_t *pmdp;
  1086. WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
  1087. if (!ARM64_KERNEL_USES_PMD_MAPS)
  1088. return vmemmap_populate_basepages(start, end, node, altmap);
  1089. do {
  1090. next = pmd_addr_end(addr, end);
  1091. pgdp = vmemmap_pgd_populate(addr, node);
  1092. if (!pgdp)
  1093. return -ENOMEM;
  1094. p4dp = vmemmap_p4d_populate(pgdp, addr, node);
  1095. if (!p4dp)
  1096. return -ENOMEM;
  1097. pudp = vmemmap_pud_populate(p4dp, addr, node);
  1098. if (!pudp)
  1099. return -ENOMEM;
  1100. pmdp = pmd_offset(pudp, addr);
  1101. if (pmd_none(READ_ONCE(*pmdp))) {
  1102. void *p = NULL;
  1103. p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
  1104. if (!p) {
  1105. if (vmemmap_populate_basepages(addr, next, node, altmap))
  1106. return -ENOMEM;
  1107. continue;
  1108. }
  1109. pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
  1110. } else
  1111. vmemmap_verify((pte_t *)pmdp, node, addr, next);
  1112. } while (addr = next, addr != end);
  1113. return 0;
  1114. }
  1115. #ifdef CONFIG_MEMORY_HOTPLUG
  1116. void vmemmap_free(unsigned long start, unsigned long end,
  1117. struct vmem_altmap *altmap)
  1118. {
  1119. WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
  1120. unmap_hotplug_range(start, end, true, altmap);
  1121. free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END);
  1122. }
  1123. #endif /* CONFIG_MEMORY_HOTPLUG */
  1124. static inline pud_t *fixmap_pud(unsigned long addr)
  1125. {
  1126. pgd_t *pgdp = pgd_offset_k(addr);
  1127. p4d_t *p4dp = p4d_offset(pgdp, addr);
  1128. p4d_t p4d = READ_ONCE(*p4dp);
  1129. BUG_ON(p4d_none(p4d) || p4d_bad(p4d));
  1130. return pud_offset_kimg(p4dp, addr);
  1131. }
  1132. static inline pmd_t *fixmap_pmd(unsigned long addr)
  1133. {
  1134. pud_t *pudp = fixmap_pud(addr);
  1135. pud_t pud = READ_ONCE(*pudp);
  1136. BUG_ON(pud_none(pud) || pud_bad(pud));
  1137. return pmd_offset_kimg(pudp, addr);
  1138. }
  1139. static inline pte_t *fixmap_pte(unsigned long addr)
  1140. {
  1141. return &bm_pte[pte_index(addr)];
  1142. }
  1143. /*
  1144. * The p*d_populate functions call virt_to_phys implicitly so they can't be used
  1145. * directly on kernel symbols (bm_p*d). This function is called too early to use
  1146. * lm_alias so __p*d_populate functions must be used to populate with the
  1147. * physical address from __pa_symbol.
  1148. */
  1149. void __init early_fixmap_init(void)
  1150. {
  1151. pgd_t *pgdp;
  1152. p4d_t *p4dp, p4d;
  1153. pud_t *pudp;
  1154. pmd_t *pmdp;
  1155. unsigned long addr = FIXADDR_START;
  1156. pgdp = pgd_offset_k(addr);
  1157. p4dp = p4d_offset(pgdp, addr);
  1158. p4d = READ_ONCE(*p4dp);
  1159. if (CONFIG_PGTABLE_LEVELS > 3 &&
  1160. !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) {
  1161. /*
  1162. * We only end up here if the kernel mapping and the fixmap
  1163. * share the top level pgd entry, which should only happen on
  1164. * 16k/4 levels configurations.
  1165. */
  1166. BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
  1167. pudp = pud_offset_kimg(p4dp, addr);
  1168. } else {
  1169. if (p4d_none(p4d))
  1170. __p4d_populate(p4dp, __pa_symbol(bm_pud), P4D_TYPE_TABLE);
  1171. pudp = fixmap_pud(addr);
  1172. }
  1173. if (pud_none(READ_ONCE(*pudp)))
  1174. __pud_populate(pudp, __pa_symbol(bm_pmd), PUD_TYPE_TABLE);
  1175. pmdp = fixmap_pmd(addr);
  1176. __pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
  1177. /*
  1178. * The boot-ioremap range spans multiple pmds, for which
  1179. * we are not prepared:
  1180. */
  1181. BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
  1182. != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
  1183. if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
  1184. || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  1185. WARN_ON(1);
  1186. pr_warn("pmdp %p != %p, %p\n",
  1187. pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
  1188. fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
  1189. pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  1190. fix_to_virt(FIX_BTMAP_BEGIN));
  1191. pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
  1192. fix_to_virt(FIX_BTMAP_END));
  1193. pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  1194. pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
  1195. }
  1196. }
  1197. /*
  1198. * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
  1199. * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
  1200. */
  1201. void __set_fixmap(enum fixed_addresses idx,
  1202. phys_addr_t phys, pgprot_t flags)
  1203. {
  1204. unsigned long addr = __fix_to_virt(idx);
  1205. pte_t *ptep;
  1206. BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
  1207. ptep = fixmap_pte(addr);
  1208. if (pgprot_val(flags)) {
  1209. set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
  1210. } else {
  1211. pte_clear(&init_mm, addr, ptep);
  1212. flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
  1213. }
  1214. }
  1215. pte_t *__get_fixmap_pte(enum fixed_addresses idx)
  1216. {
  1217. unsigned long addr = __fix_to_virt(idx);
  1218. pte_t *ptep;
  1219. BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
  1220. ptep = fixmap_pte(addr);
  1221. if (!pte_valid(*ptep))
  1222. return NULL;
  1223. return ptep;
  1224. }
  1225. void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
  1226. {
  1227. const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
  1228. int offset;
  1229. void *dt_virt;
  1230. /*
  1231. * Check whether the physical FDT address is set and meets the minimum
  1232. * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
  1233. * at least 8 bytes so that we can always access the magic and size
  1234. * fields of the FDT header after mapping the first chunk, double check
  1235. * here if that is indeed the case.
  1236. */
  1237. BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
  1238. if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
  1239. return NULL;
  1240. /*
  1241. * Make sure that the FDT region can be mapped without the need to
  1242. * allocate additional translation table pages, so that it is safe
  1243. * to call create_mapping_noalloc() this early.
  1244. *
  1245. * On 64k pages, the FDT will be mapped using PTEs, so we need to
  1246. * be in the same PMD as the rest of the fixmap.
  1247. * On 4k pages, we'll use section mappings for the FDT so we only
  1248. * have to be in the same PUD.
  1249. */
  1250. BUILD_BUG_ON(dt_virt_base % SZ_2M);
  1251. BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
  1252. __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
  1253. offset = dt_phys % SWAPPER_BLOCK_SIZE;
  1254. dt_virt = (void *)dt_virt_base + offset;
  1255. /* map the first chunk so we can read the size from the header */
  1256. create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
  1257. dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
  1258. if (fdt_magic(dt_virt) != FDT_MAGIC)
  1259. return NULL;
  1260. *size = fdt_totalsize(dt_virt);
  1261. if (*size > MAX_FDT_SIZE)
  1262. return NULL;
  1263. if (offset + *size > SWAPPER_BLOCK_SIZE)
  1264. create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
  1265. round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
  1266. return dt_virt;
  1267. }
  1268. int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
  1269. {
  1270. pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot));
  1271. /* Only allow permission changes for now */
  1272. if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
  1273. pud_val(new_pud)))
  1274. return 0;
  1275. VM_BUG_ON(phys & ~PUD_MASK);
  1276. set_pud(pudp, new_pud);
  1277. return 1;
  1278. }
  1279. int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
  1280. {
  1281. pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot));
  1282. /* Only allow permission changes for now */
  1283. if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
  1284. pmd_val(new_pmd)))
  1285. return 0;
  1286. VM_BUG_ON(phys & ~PMD_MASK);
  1287. set_pmd(pmdp, new_pmd);
  1288. return 1;
  1289. }
  1290. int pud_clear_huge(pud_t *pudp)
  1291. {
  1292. if (!pud_sect(READ_ONCE(*pudp)))
  1293. return 0;
  1294. pud_clear(pudp);
  1295. return 1;
  1296. }
  1297. int pmd_clear_huge(pmd_t *pmdp)
  1298. {
  1299. if (!pmd_sect(READ_ONCE(*pmdp)))
  1300. return 0;
  1301. pmd_clear(pmdp);
  1302. return 1;
  1303. }
  1304. int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
  1305. {
  1306. pte_t *table;
  1307. pmd_t pmd;
  1308. pmd = READ_ONCE(*pmdp);
  1309. if (!pmd_table(pmd)) {
  1310. VM_WARN_ON(1);
  1311. return 1;
  1312. }
  1313. table = pte_offset_kernel(pmdp, addr);
  1314. pmd_clear(pmdp);
  1315. __flush_tlb_kernel_pgtable(addr);
  1316. pte_free_kernel(NULL, table);
  1317. return 1;
  1318. }
  1319. int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
  1320. {
  1321. pmd_t *table;
  1322. pmd_t *pmdp;
  1323. pud_t pud;
  1324. unsigned long next, end;
  1325. pud = READ_ONCE(*pudp);
  1326. if (!pud_table(pud)) {
  1327. VM_WARN_ON(1);
  1328. return 1;
  1329. }
  1330. table = pmd_offset(pudp, addr);
  1331. pmdp = table;
  1332. next = addr;
  1333. end = addr + PUD_SIZE;
  1334. do {
  1335. pmd_free_pte_page(pmdp, next);
  1336. } while (pmdp++, next += PMD_SIZE, next != end);
  1337. pud_clear(pudp);
  1338. __flush_tlb_kernel_pgtable(addr);
  1339. pmd_free(NULL, table);
  1340. return 1;
  1341. }
  1342. #ifdef CONFIG_MEMORY_HOTPLUG
  1343. static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
  1344. {
  1345. unsigned long end = start + size;
  1346. WARN_ON(pgdir != init_mm.pgd);
  1347. WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END));
  1348. unmap_hotplug_range(start, end, false, NULL);
  1349. free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
  1350. }
  1351. struct range arch_get_mappable_range(void)
  1352. {
  1353. struct range mhp_range;
  1354. u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual));
  1355. u64 end_linear_pa = __pa(PAGE_END - 1);
  1356. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  1357. /*
  1358. * Check for a wrap, it is possible because of randomized linear
  1359. * mapping the start physical address is actually bigger than
  1360. * the end physical address. In this case set start to zero
  1361. * because [0, end_linear_pa] range must still be able to cover
  1362. * all addressable physical addresses.
  1363. */
  1364. if (start_linear_pa > end_linear_pa)
  1365. start_linear_pa = 0;
  1366. }
  1367. WARN_ON(start_linear_pa > end_linear_pa);
  1368. /*
  1369. * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
  1370. * accommodating both its ends but excluding PAGE_END. Max physical
  1371. * range which can be mapped inside this linear mapping range, must
  1372. * also be derived from its end points.
  1373. */
  1374. mhp_range.start = start_linear_pa;
  1375. mhp_range.end = end_linear_pa;
  1376. return mhp_range;
  1377. }
  1378. int arch_add_memory(int nid, u64 start, u64 size,
  1379. struct mhp_params *params)
  1380. {
  1381. int ret, flags = NO_EXEC_MAPPINGS;
  1382. VM_BUG_ON(!mhp_range_allowed(start, size, true));
  1383. if (can_set_direct_map())
  1384. flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  1385. __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
  1386. size, params->pgprot, __pgd_pgtable_alloc,
  1387. flags);
  1388. memblock_clear_nomap(start, size);
  1389. ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
  1390. params);
  1391. if (ret)
  1392. __remove_pgd_mapping(swapper_pg_dir,
  1393. __phys_to_virt(start), size);
  1394. else {
  1395. max_pfn = PFN_UP(start + size);
  1396. max_low_pfn = max_pfn;
  1397. }
  1398. return ret;
  1399. }
  1400. void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
  1401. {
  1402. unsigned long start_pfn = start >> PAGE_SHIFT;
  1403. unsigned long nr_pages = size >> PAGE_SHIFT;
  1404. __remove_pages(start_pfn, nr_pages, altmap);
  1405. __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
  1406. }
  1407. /*
  1408. * This memory hotplug notifier helps prevent boot memory from being
  1409. * inadvertently removed as it blocks pfn range offlining process in
  1410. * __offline_pages(). Hence this prevents both offlining as well as
  1411. * removal process for boot memory which is initially always online.
  1412. * In future if and when boot memory could be removed, this notifier
  1413. * should be dropped and free_hotplug_page_range() should handle any
  1414. * reserved pages allocated during boot.
  1415. */
  1416. static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
  1417. unsigned long action, void *data)
  1418. {
  1419. struct mem_section *ms;
  1420. struct memory_notify *arg = data;
  1421. unsigned long end_pfn = arg->start_pfn + arg->nr_pages;
  1422. unsigned long pfn = arg->start_pfn;
  1423. if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE))
  1424. return NOTIFY_OK;
  1425. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1426. unsigned long start = PFN_PHYS(pfn);
  1427. unsigned long end = start + (1UL << PA_SECTION_SHIFT);
  1428. ms = __pfn_to_section(pfn);
  1429. if (!early_section(ms))
  1430. continue;
  1431. if (action == MEM_GOING_OFFLINE) {
  1432. /*
  1433. * Boot memory removal is not supported. Prevent
  1434. * it via blocking any attempted offline request
  1435. * for the boot memory and just report it.
  1436. */
  1437. pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end);
  1438. return NOTIFY_BAD;
  1439. } else if (action == MEM_OFFLINE) {
  1440. /*
  1441. * This should have never happened. Boot memory
  1442. * offlining should have been prevented by this
  1443. * very notifier. Probably some memory removal
  1444. * procedure might have changed which would then
  1445. * require further debug.
  1446. */
  1447. pr_err("Boot memory [%lx %lx] offlined\n", start, end);
  1448. /*
  1449. * Core memory hotplug does not process a return
  1450. * code from the notifier for MEM_OFFLINE events.
  1451. * The error condition has been reported. Return
  1452. * from here as if ignored.
  1453. */
  1454. return NOTIFY_DONE;
  1455. }
  1456. }
  1457. return NOTIFY_OK;
  1458. }
  1459. static struct notifier_block prevent_bootmem_remove_nb = {
  1460. .notifier_call = prevent_bootmem_remove_notifier,
  1461. };
  1462. /*
  1463. * This ensures that boot memory sections on the platform are online
  1464. * from early boot. Memory sections could not be prevented from being
  1465. * offlined, unless for some reason they are not online to begin with.
  1466. * This helps validate the basic assumption on which the above memory
  1467. * event notifier works to prevent boot memory section offlining and
  1468. * its possible removal.
  1469. */
  1470. static void validate_bootmem_online(void)
  1471. {
  1472. phys_addr_t start, end, addr;
  1473. struct mem_section *ms;
  1474. u64 i;
  1475. /*
  1476. * Scanning across all memblock might be expensive
  1477. * on some big memory systems. Hence enable this
  1478. * validation only with DEBUG_VM.
  1479. */
  1480. if (!IS_ENABLED(CONFIG_DEBUG_VM))
  1481. return;
  1482. for_each_mem_range(i, &start, &end) {
  1483. for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) {
  1484. ms = __pfn_to_section(PHYS_PFN(addr));
  1485. /*
  1486. * All memory ranges in the system at this point
  1487. * should have been marked as early sections.
  1488. */
  1489. WARN_ON(!early_section(ms));
  1490. /*
  1491. * Memory notifier mechanism here to prevent boot
  1492. * memory offlining depends on the fact that each
  1493. * early section memory on the system is initially
  1494. * online. Otherwise a given memory section which
  1495. * is already offline will be overlooked and can
  1496. * be removed completely. Call out such sections.
  1497. */
  1498. if (!online_section(ms))
  1499. pr_err("Boot memory [%llx %llx] is offline, can be removed\n",
  1500. addr, addr + (1UL << PA_SECTION_SHIFT));
  1501. }
  1502. }
  1503. }
  1504. static int __init prevent_bootmem_remove_init(void)
  1505. {
  1506. int ret = 0;
  1507. if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE))
  1508. return ret;
  1509. validate_bootmem_online();
  1510. ret = register_memory_notifier(&prevent_bootmem_remove_nb);
  1511. if (ret)
  1512. pr_err("%s: Notifier registration failed %d\n", __func__, ret);
  1513. return ret;
  1514. }
  1515. early_initcall(prevent_bootmem_remove_init);
  1516. #endif