highmem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_HIGHMEM_H
  3. #define _LINUX_HIGHMEM_H
  4. #include <linux/fs.h>
  5. #include <linux/kernel.h>
  6. #include <linux/bug.h>
  7. #include <linux/cacheflush.h>
  8. #include <linux/kmsan.h>
  9. #include <linux/mm.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/hardirq.h>
  12. #include "highmem-internal.h"
  13. /**
  14. * kmap - Map a page for long term usage
  15. * @page: Pointer to the page to be mapped
  16. *
  17. * Returns: The virtual address of the mapping
  18. *
  19. * Can only be invoked from preemptible task context because on 32bit
  20. * systems with CONFIG_HIGHMEM enabled this function might sleep.
  21. *
  22. * For systems with CONFIG_HIGHMEM=n and for pages in the low memory area
  23. * this returns the virtual address of the direct kernel mapping.
  24. *
  25. * The returned virtual address is globally visible and valid up to the
  26. * point where it is unmapped via kunmap(). The pointer can be handed to
  27. * other contexts.
  28. *
  29. * For highmem pages on 32bit systems this can be slow as the mapping space
  30. * is limited and protected by a global lock. In case that there is no
  31. * mapping slot available the function blocks until a slot is released via
  32. * kunmap().
  33. */
  34. static inline void *kmap(struct page *page);
  35. /**
  36. * kunmap - Unmap the virtual address mapped by kmap()
  37. * @page: Pointer to the page which was mapped by kmap()
  38. *
  39. * Counterpart to kmap(). A NOOP for CONFIG_HIGHMEM=n and for mappings of
  40. * pages in the low memory area.
  41. */
  42. static inline void kunmap(struct page *page);
  43. /**
  44. * kmap_to_page - Get the page for a kmap'ed address
  45. * @addr: The address to look up
  46. *
  47. * Returns: The page which is mapped to @addr.
  48. */
  49. static inline struct page *kmap_to_page(void *addr);
  50. /**
  51. * kmap_flush_unused - Flush all unused kmap mappings in order to
  52. * remove stray mappings
  53. */
  54. static inline void kmap_flush_unused(void);
  55. /**
  56. * kmap_local_page - Map a page for temporary usage
  57. * @page: Pointer to the page to be mapped
  58. *
  59. * Returns: The virtual address of the mapping
  60. *
  61. * Can be invoked from any context, including interrupts.
  62. *
  63. * Requires careful handling when nesting multiple mappings because the map
  64. * management is stack based. The unmap has to be in the reverse order of
  65. * the map operation:
  66. *
  67. * addr1 = kmap_local_page(page1);
  68. * addr2 = kmap_local_page(page2);
  69. * ...
  70. * kunmap_local(addr2);
  71. * kunmap_local(addr1);
  72. *
  73. * Unmapping addr1 before addr2 is invalid and causes malfunction.
  74. *
  75. * Contrary to kmap() mappings the mapping is only valid in the context of
  76. * the caller and cannot be handed to other contexts.
  77. *
  78. * On CONFIG_HIGHMEM=n kernels and for low memory pages this returns the
  79. * virtual address of the direct mapping. Only real highmem pages are
  80. * temporarily mapped.
  81. *
  82. * While it is significantly faster than kmap() for the higmem case it
  83. * comes with restrictions about the pointer validity.
  84. *
  85. * On HIGHMEM enabled systems mapping a highmem page has the side effect of
  86. * disabling migration in order to keep the virtual address stable across
  87. * preemption. No caller of kmap_local_page() can rely on this side effect.
  88. */
  89. static inline void *kmap_local_page(struct page *page);
  90. /**
  91. * kmap_local_folio - Map a page in this folio for temporary usage
  92. * @folio: The folio containing the page.
  93. * @offset: The byte offset within the folio which identifies the page.
  94. *
  95. * Requires careful handling when nesting multiple mappings because the map
  96. * management is stack based. The unmap has to be in the reverse order of
  97. * the map operation::
  98. *
  99. * addr1 = kmap_local_folio(folio1, offset1);
  100. * addr2 = kmap_local_folio(folio2, offset2);
  101. * ...
  102. * kunmap_local(addr2);
  103. * kunmap_local(addr1);
  104. *
  105. * Unmapping addr1 before addr2 is invalid and causes malfunction.
  106. *
  107. * Contrary to kmap() mappings the mapping is only valid in the context of
  108. * the caller and cannot be handed to other contexts.
  109. *
  110. * On CONFIG_HIGHMEM=n kernels and for low memory pages this returns the
  111. * virtual address of the direct mapping. Only real highmem pages are
  112. * temporarily mapped.
  113. *
  114. * While it is significantly faster than kmap() for the higmem case it
  115. * comes with restrictions about the pointer validity. Only use when really
  116. * necessary.
  117. *
  118. * On HIGHMEM enabled systems mapping a highmem page has the side effect of
  119. * disabling migration in order to keep the virtual address stable across
  120. * preemption. No caller of kmap_local_folio() can rely on this side effect.
  121. *
  122. * Context: Can be invoked from any context.
  123. * Return: The virtual address of @offset.
  124. */
  125. static inline void *kmap_local_folio(struct folio *folio, size_t offset);
  126. /**
  127. * kmap_atomic - Atomically map a page for temporary usage - Deprecated!
  128. * @page: Pointer to the page to be mapped
  129. *
  130. * Returns: The virtual address of the mapping
  131. *
  132. * In fact a wrapper around kmap_local_page() which also disables pagefaults
  133. * and, depending on PREEMPT_RT configuration, also CPU migration and
  134. * preemption. Therefore users should not count on the latter two side effects.
  135. *
  136. * Mappings should always be released by kunmap_atomic().
  137. *
  138. * Do not use in new code. Use kmap_local_page() instead.
  139. *
  140. * It is used in atomic context when code wants to access the contents of a
  141. * page that might be allocated from high memory (see __GFP_HIGHMEM), for
  142. * example a page in the pagecache. The API has two functions, and they
  143. * can be used in a manner similar to the following::
  144. *
  145. * // Find the page of interest.
  146. * struct page *page = find_get_page(mapping, offset);
  147. *
  148. * // Gain access to the contents of that page.
  149. * void *vaddr = kmap_atomic(page);
  150. *
  151. * // Do something to the contents of that page.
  152. * memset(vaddr, 0, PAGE_SIZE);
  153. *
  154. * // Unmap that page.
  155. * kunmap_atomic(vaddr);
  156. *
  157. * Note that the kunmap_atomic() call takes the result of the kmap_atomic()
  158. * call, not the argument.
  159. *
  160. * If you need to map two pages because you want to copy from one page to
  161. * another you need to keep the kmap_atomic calls strictly nested, like:
  162. *
  163. * vaddr1 = kmap_atomic(page1);
  164. * vaddr2 = kmap_atomic(page2);
  165. *
  166. * memcpy(vaddr1, vaddr2, PAGE_SIZE);
  167. *
  168. * kunmap_atomic(vaddr2);
  169. * kunmap_atomic(vaddr1);
  170. */
  171. static inline void *kmap_atomic(struct page *page);
  172. /* Highmem related interfaces for management code */
  173. static inline unsigned int nr_free_highpages(void);
  174. static inline unsigned long totalhigh_pages(void);
  175. #ifndef ARCH_HAS_FLUSH_ANON_PAGE
  176. static inline void flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
  177. {
  178. }
  179. #endif
  180. #ifndef ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE
  181. static inline void flush_kernel_vmap_range(void *vaddr, int size)
  182. {
  183. }
  184. static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
  185. {
  186. }
  187. #endif
  188. /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
  189. #ifndef clear_user_highpage
  190. static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
  191. {
  192. void *addr = kmap_local_page(page);
  193. clear_user_page(addr, vaddr, page);
  194. kunmap_local(addr);
  195. }
  196. #endif
  197. #ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE_MOVABLE
  198. /**
  199. * alloc_zeroed_user_highpage_movable - Allocate a zeroed HIGHMEM page for a VMA that the caller knows can move
  200. * @vma: The VMA the page is to be allocated for
  201. * @vaddr: The virtual address the page will be inserted into
  202. *
  203. * Returns: The allocated and zeroed HIGHMEM page
  204. *
  205. * This function will allocate a page for a VMA that the caller knows will
  206. * be able to migrate in the future using move_pages() or reclaimed
  207. *
  208. * An architecture may override this function by defining
  209. * __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE_MOVABLE and providing their own
  210. * implementation.
  211. */
  212. static inline struct page *
  213. alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
  214. unsigned long vaddr)
  215. {
  216. struct page *page = alloc_page_vma(GFP_HIGHUSER_MOVABLE | __GFP_CMA, vma, vaddr);
  217. if (page)
  218. clear_user_highpage(page, vaddr);
  219. return page;
  220. }
  221. #endif
  222. static inline void clear_highpage(struct page *page)
  223. {
  224. void *kaddr = kmap_local_page(page);
  225. clear_page(kaddr);
  226. kunmap_local(kaddr);
  227. }
  228. static inline void clear_highpage_kasan_tagged(struct page *page)
  229. {
  230. u8 tag;
  231. tag = page_kasan_tag(page);
  232. page_kasan_tag_reset(page);
  233. clear_highpage(page);
  234. page_kasan_tag_set(page, tag);
  235. }
  236. #ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
  237. static inline void tag_clear_highpage(struct page *page)
  238. {
  239. }
  240. #endif
  241. /*
  242. * If we pass in a base or tail page, we can zero up to PAGE_SIZE.
  243. * If we pass in a head page, we can zero up to the size of the compound page.
  244. */
  245. #ifdef CONFIG_HIGHMEM
  246. void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
  247. unsigned start2, unsigned end2);
  248. #else
  249. static inline void zero_user_segments(struct page *page,
  250. unsigned start1, unsigned end1,
  251. unsigned start2, unsigned end2)
  252. {
  253. void *kaddr = kmap_local_page(page);
  254. unsigned int i;
  255. BUG_ON(end1 > page_size(page) || end2 > page_size(page));
  256. if (end1 > start1)
  257. memset(kaddr + start1, 0, end1 - start1);
  258. if (end2 > start2)
  259. memset(kaddr + start2, 0, end2 - start2);
  260. kunmap_local(kaddr);
  261. for (i = 0; i < compound_nr(page); i++)
  262. flush_dcache_page(page + i);
  263. }
  264. #endif
  265. static inline void zero_user_segment(struct page *page,
  266. unsigned start, unsigned end)
  267. {
  268. zero_user_segments(page, start, end, 0, 0);
  269. }
  270. static inline void zero_user(struct page *page,
  271. unsigned start, unsigned size)
  272. {
  273. zero_user_segments(page, start, start + size, 0, 0);
  274. }
  275. #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE
  276. static inline void copy_user_highpage(struct page *to, struct page *from,
  277. unsigned long vaddr, struct vm_area_struct *vma)
  278. {
  279. char *vfrom, *vto;
  280. vfrom = kmap_local_page(from);
  281. vto = kmap_local_page(to);
  282. copy_user_page(vto, vfrom, vaddr, to);
  283. kmsan_unpoison_memory(page_address(to), PAGE_SIZE);
  284. kunmap_local(vto);
  285. kunmap_local(vfrom);
  286. }
  287. #endif
  288. #ifdef copy_mc_to_kernel
  289. static inline int copy_mc_user_highpage(struct page *to, struct page *from,
  290. unsigned long vaddr, struct vm_area_struct *vma)
  291. {
  292. unsigned long ret;
  293. char *vfrom, *vto;
  294. vfrom = kmap_local_page(from);
  295. vto = kmap_local_page(to);
  296. ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
  297. if (!ret)
  298. kmsan_unpoison_memory(page_address(to), PAGE_SIZE);
  299. kunmap_local(vto);
  300. kunmap_local(vfrom);
  301. return ret;
  302. }
  303. #else
  304. static inline int copy_mc_user_highpage(struct page *to, struct page *from,
  305. unsigned long vaddr, struct vm_area_struct *vma)
  306. {
  307. copy_user_highpage(to, from, vaddr, vma);
  308. return 0;
  309. }
  310. #endif
  311. #ifndef __HAVE_ARCH_COPY_HIGHPAGE
  312. static inline void copy_highpage(struct page *to, struct page *from)
  313. {
  314. char *vfrom, *vto;
  315. vfrom = kmap_local_page(from);
  316. vto = kmap_local_page(to);
  317. copy_page(vto, vfrom);
  318. kmsan_copy_page_meta(to, from);
  319. kunmap_local(vto);
  320. kunmap_local(vfrom);
  321. }
  322. #endif
  323. static inline void memcpy_page(struct page *dst_page, size_t dst_off,
  324. struct page *src_page, size_t src_off,
  325. size_t len)
  326. {
  327. char *dst = kmap_local_page(dst_page);
  328. char *src = kmap_local_page(src_page);
  329. VM_BUG_ON(dst_off + len > PAGE_SIZE || src_off + len > PAGE_SIZE);
  330. memcpy(dst + dst_off, src + src_off, len);
  331. kunmap_local(src);
  332. kunmap_local(dst);
  333. }
  334. static inline void memset_page(struct page *page, size_t offset, int val,
  335. size_t len)
  336. {
  337. char *addr = kmap_local_page(page);
  338. VM_BUG_ON(offset + len > PAGE_SIZE);
  339. memset(addr + offset, val, len);
  340. kunmap_local(addr);
  341. }
  342. static inline void memcpy_from_page(char *to, struct page *page,
  343. size_t offset, size_t len)
  344. {
  345. char *from = kmap_local_page(page);
  346. VM_BUG_ON(offset + len > PAGE_SIZE);
  347. memcpy(to, from + offset, len);
  348. kunmap_local(from);
  349. }
  350. static inline void memcpy_to_page(struct page *page, size_t offset,
  351. const char *from, size_t len)
  352. {
  353. char *to = kmap_local_page(page);
  354. VM_BUG_ON(offset + len > PAGE_SIZE);
  355. memcpy(to + offset, from, len);
  356. flush_dcache_page(page);
  357. kunmap_local(to);
  358. }
  359. static inline void memzero_page(struct page *page, size_t offset, size_t len)
  360. {
  361. char *addr = kmap_local_page(page);
  362. VM_BUG_ON(offset + len > PAGE_SIZE);
  363. memset(addr + offset, 0, len);
  364. flush_dcache_page(page);
  365. kunmap_local(addr);
  366. }
  367. /**
  368. * folio_zero_segments() - Zero two byte ranges in a folio.
  369. * @folio: The folio to write to.
  370. * @start1: The first byte to zero.
  371. * @xend1: One more than the last byte in the first range.
  372. * @start2: The first byte to zero in the second range.
  373. * @xend2: One more than the last byte in the second range.
  374. */
  375. static inline void folio_zero_segments(struct folio *folio,
  376. size_t start1, size_t xend1, size_t start2, size_t xend2)
  377. {
  378. zero_user_segments(&folio->page, start1, xend1, start2, xend2);
  379. }
  380. /**
  381. * folio_zero_segment() - Zero a byte range in a folio.
  382. * @folio: The folio to write to.
  383. * @start: The first byte to zero.
  384. * @xend: One more than the last byte to zero.
  385. */
  386. static inline void folio_zero_segment(struct folio *folio,
  387. size_t start, size_t xend)
  388. {
  389. zero_user_segments(&folio->page, start, xend, 0, 0);
  390. }
  391. /**
  392. * folio_zero_range() - Zero a byte range in a folio.
  393. * @folio: The folio to write to.
  394. * @start: The first byte to zero.
  395. * @length: The number of bytes to zero.
  396. */
  397. static inline void folio_zero_range(struct folio *folio,
  398. size_t start, size_t length)
  399. {
  400. zero_user_segments(&folio->page, start, start + length, 0, 0);
  401. }
  402. #endif /* _LINUX_HIGHMEM_H */