Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / include / linux / hmm.h
CommitLineData
c942fddf 1/* SPDX-License-Identifier: GPL-2.0-or-later */
133ff0ea
JG
2/*
3 * Copyright 2013 Red Hat Inc.
4 *
f813f219 5 * Authors: Jérôme Glisse <jglisse@redhat.com>
133ff0ea 6 *
ee65728e 7 * See Documentation/mm/hmm.rst for reasons and overview of what HMM is.
133ff0ea
JG
8 */
9#ifndef LINUX_HMM_H
10#define LINUX_HMM_H
11
730ff521 12#include <linux/mm.h>
133ff0ea 13
730ff521 14struct mmu_interval_notifier;
4ef589dc 15
133ff0ea 16/*
2733ea14
JG
17 * On output:
18 * 0 - The page is faultable and a future call with
19 * HMM_PFN_REQ_FAULT could succeed.
20 * HMM_PFN_VALID - the pfn field points to a valid PFN. This PFN is at
21 * least readable. If dev_private_owner is !NULL then this could
22 * point at a DEVICE_PRIVATE page.
23 * HMM_PFN_WRITE - if the page memory can be written to (requires HMM_PFN_VALID)
24 * HMM_PFN_ERROR - accessing the pfn is impossible and the device should
25 * fail. ie poisoned memory, special pages, no vma, etc
8cad4713
LR
26 * HMM_PFN_P2PDMA - P2P page
27 * HMM_PFN_P2PDMA_BUS - Bus mapped P2P transfer
285e8718
LR
28 * HMM_PFN_DMA_MAPPED - Flag preserved on input-to-output transformation
29 * to mark that page is already DMA mapped
f88a1e90 30 *
2733ea14
JG
31 * On input:
32 * 0 - Return the current state of the page, do not fault it.
33 * HMM_PFN_REQ_FAULT - The output must have HMM_PFN_VALID or hmm_range_fault()
34 * will fail
35 * HMM_PFN_REQ_WRITE - The output must have HMM_PFN_WRITE or hmm_range_fault()
36 * will fail. Must be combined with HMM_PFN_REQ_FAULT.
f88a1e90 37 */
2733ea14 38enum hmm_pfn_flags {
3b50a6e5 39 /* Output fields and flags */
2733ea14
JG
40 HMM_PFN_VALID = 1UL << (BITS_PER_LONG - 1),
41 HMM_PFN_WRITE = 1UL << (BITS_PER_LONG - 2),
42 HMM_PFN_ERROR = 1UL << (BITS_PER_LONG - 3),
285e8718
LR
43 /*
44 * Sticky flags, carried from input to output,
45 * don't forget to update HMM_PFN_INOUT_FLAGS
46 */
47 HMM_PFN_DMA_MAPPED = 1UL << (BITS_PER_LONG - 4),
8cad4713
LR
48 HMM_PFN_P2PDMA = 1UL << (BITS_PER_LONG - 5),
49 HMM_PFN_P2PDMA_BUS = 1UL << (BITS_PER_LONG - 6),
285e8718 50
8cad4713 51 HMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 11),
2733ea14
JG
52
53 /* Input flags */
54 HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
55 HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,
56
285e8718 57 HMM_PFN_FLAGS = ~((1UL << HMM_PFN_ORDER_SHIFT) - 1),
f88a1e90
JG
58};
59
60/*
2733ea14 61 * hmm_pfn_to_page() - return struct page pointed to by a device entry
f88a1e90 62 *
2733ea14
JG
63 * This must be called under the caller 'user_lock' after a successful
64 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
65 * already.
133ff0ea 66 */
2733ea14
JG
67static inline struct page *hmm_pfn_to_page(unsigned long hmm_pfn)
68{
69 return pfn_to_page(hmm_pfn & ~HMM_PFN_FLAGS);
70}
f88a1e90 71
285e8718
LR
72/*
73 * hmm_pfn_to_phys() - return physical address pointed to by a device entry
74 */
75static inline phys_addr_t hmm_pfn_to_phys(unsigned long hmm_pfn)
76{
77 return __pfn_to_phys(hmm_pfn & ~HMM_PFN_FLAGS);
78}
79
3b50a6e5
RC
80/*
81 * hmm_pfn_to_map_order() - return the CPU mapping size order
82 *
83 * This is optionally useful to optimize processing of the pfn result
84 * array. It indicates that the page starts at the order aligned VA and is
85 * 1<<order bytes long. Every pfn within an high order page will have the
86 * same pfn flags, both access protections and the map_order. The caller must
87 * be careful with edge cases as the start and end VA of the given page may
88 * extend past the range used with hmm_range_fault().
89 *
90 * This must be called under the caller 'user_lock' after a successful
91 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
92 * already.
93 */
94static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)
95{
96 return (hmm_pfn >> HMM_PFN_ORDER_SHIFT) & 0x1F;
97}
98
f88a1e90
JG
99/*
100 * struct hmm_range - track invalidation lock on virtual address range
101 *
a22dd506
JG
102 * @notifier: a mmu_interval_notifier that includes the start/end
103 * @notifier_seq: result of mmu_interval_read_begin()
f88a1e90
JG
104 * @start: range virtual start address (inclusive)
105 * @end: range virtual end address (exclusive)
2733ea14 106 * @hmm_pfns: array of pfns (big enough for the range)
023a019a
JG
107 * @default_flags: default flags for the range (write, read, ... see hmm doc)
108 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
08ddddda 109 * @dev_private_owner: owner of device private pages
f88a1e90
JG
110 */
111struct hmm_range {
04ec32fb
JG
112 struct mmu_interval_notifier *notifier;
113 unsigned long notifier_seq;
f88a1e90
JG
114 unsigned long start;
115 unsigned long end;
2733ea14
JG
116 unsigned long *hmm_pfns;
117 unsigned long default_flags;
118 unsigned long pfn_flags_mask;
08ddddda 119 void *dev_private_owner;
f88a1e90 120};
133ff0ea 121
da4c3c73 122/*
ee65728e 123 * Please see Documentation/mm/hmm.rst for how to use the range API.
da4c3c73 124 */
be957c88 125int hmm_range_fault(struct hmm_range *range);
74eee180
JG
126
127/*
a3e0d41c 128 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
74eee180 129 *
a3e0d41c 130 * When waiting for mmu notifiers we need some kind of time out otherwise we
06c88398 131 * could potentially wait for ever, 1000ms ie 1s sounds like a long time to
a3e0d41c 132 * wait already.
74eee180 133 */
a3e0d41c
JG
134#define HMM_RANGE_DEFAULT_TIMEOUT 1000
135
133ff0ea 136#endif /* LINUX_HMM_H */