blob: 866a0fa104c42a167ee542599cea49ac93671cf9 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright 2013 Red Hat Inc.
4 *
David Brazdil0f672f62019-12-10 10:32:29 +00005 * Authors: Jérôme Glisse <jglisse@redhat.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 *
Olivier Deprez157378f2022-04-04 15:47:50 +02007 * See Documentation/vm/hmm.rst for reasons and overview of what HMM is.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
9#ifndef LINUX_HMM_H
10#define LINUX_HMM_H
11
12#include <linux/kconfig.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020013#include <linux/pgtable.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014
15#include <linux/device.h>
16#include <linux/migrate.h>
17#include <linux/memremap.h>
18#include <linux/completion.h>
David Brazdil0f672f62019-12-10 10:32:29 +000019#include <linux/mmu_notifier.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
David Brazdil0f672f62019-12-10 10:32:29 +000021/*
Olivier Deprez157378f2022-04-04 15:47:50 +020022 * On output:
23 * 0 - The page is faultable and a future call with
24 * HMM_PFN_REQ_FAULT could succeed.
25 * HMM_PFN_VALID - the pfn field points to a valid PFN. This PFN is at
26 * least readable. If dev_private_owner is !NULL then this could
27 * point at a DEVICE_PRIVATE page.
28 * HMM_PFN_WRITE - if the page memory can be written to (requires HMM_PFN_VALID)
29 * HMM_PFN_ERROR - accessing the pfn is impossible and the device should
30 * fail. ie poisoned memory, special pages, no vma, etc
David Brazdil0f672f62019-12-10 10:32:29 +000031 *
Olivier Deprez157378f2022-04-04 15:47:50 +020032 * On input:
33 * 0 - Return the current state of the page, do not fault it.
34 * HMM_PFN_REQ_FAULT - The output must have HMM_PFN_VALID or hmm_range_fault()
35 * will fail
36 * HMM_PFN_REQ_WRITE - The output must have HMM_PFN_WRITE or hmm_range_fault()
37 * will fail. Must be combined with HMM_PFN_REQ_FAULT.
David Brazdil0f672f62019-12-10 10:32:29 +000038 */
Olivier Deprez157378f2022-04-04 15:47:50 +020039enum hmm_pfn_flags {
40 /* Output fields and flags */
41 HMM_PFN_VALID = 1UL << (BITS_PER_LONG - 1),
42 HMM_PFN_WRITE = 1UL << (BITS_PER_LONG - 2),
43 HMM_PFN_ERROR = 1UL << (BITS_PER_LONG - 3),
44 HMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 8),
45
46 /* Input flags */
47 HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
48 HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,
49
50 HMM_PFN_FLAGS = 0xFFUL << HMM_PFN_ORDER_SHIFT,
David Brazdil0f672f62019-12-10 10:32:29 +000051};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052
53/*
Olivier Deprez157378f2022-04-04 15:47:50 +020054 * hmm_pfn_to_page() - return struct page pointed to by a device entry
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 *
Olivier Deprez157378f2022-04-04 15:47:50 +020056 * This must be called under the caller 'user_lock' after a successful
57 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
58 * already.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059 */
Olivier Deprez157378f2022-04-04 15:47:50 +020060static inline struct page *hmm_pfn_to_page(unsigned long hmm_pfn)
61{
62 return pfn_to_page(hmm_pfn & ~HMM_PFN_FLAGS);
63}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064
65/*
Olivier Deprez157378f2022-04-04 15:47:50 +020066 * hmm_pfn_to_map_order() - return the CPU mapping size order
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 *
Olivier Deprez157378f2022-04-04 15:47:50 +020068 * This is optionally useful to optimize processing of the pfn result
69 * array. It indicates that the page starts at the order aligned VA and is
70 * 1<<order bytes long. Every pfn within an high order page will have the
71 * same pfn flags, both access protections and the map_order. The caller must
72 * be careful with edge cases as the start and end VA of the given page may
73 * extend past the range used with hmm_range_fault().
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074 *
Olivier Deprez157378f2022-04-04 15:47:50 +020075 * This must be called under the caller 'user_lock' after a successful
76 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
77 * already.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078 */
Olivier Deprez157378f2022-04-04 15:47:50 +020079static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)
80{
81 return (hmm_pfn >> HMM_PFN_ORDER_SHIFT) & 0x1F;
82}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083
84/*
85 * struct hmm_range - track invalidation lock on virtual address range
86 *
Olivier Deprez157378f2022-04-04 15:47:50 +020087 * @notifier: a mmu_interval_notifier that includes the start/end
88 * @notifier_seq: result of mmu_interval_read_begin()
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089 * @start: range virtual start address (inclusive)
90 * @end: range virtual end address (exclusive)
Olivier Deprez157378f2022-04-04 15:47:50 +020091 * @hmm_pfns: array of pfns (big enough for the range)
David Brazdil0f672f62019-12-10 10:32:29 +000092 * @default_flags: default flags for the range (write, read, ... see hmm doc)
93 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
Olivier Deprez157378f2022-04-04 15:47:50 +020094 * @dev_private_owner: owner of device private pages
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095 */
96struct hmm_range {
Olivier Deprez157378f2022-04-04 15:47:50 +020097 struct mmu_interval_notifier *notifier;
98 unsigned long notifier_seq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099 unsigned long start;
100 unsigned long end;
Olivier Deprez157378f2022-04-04 15:47:50 +0200101 unsigned long *hmm_pfns;
102 unsigned long default_flags;
103 unsigned long pfn_flags_mask;
104 void *dev_private_owner;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105};
106
107/*
David Brazdil0f672f62019-12-10 10:32:29 +0000108 * Please see Documentation/vm/hmm.rst for how to use the range API.
109 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200110int hmm_range_fault(struct hmm_range *range);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111
112/*
David Brazdil0f672f62019-12-10 10:32:29 +0000113 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114 *
David Brazdil0f672f62019-12-10 10:32:29 +0000115 * When waiting for mmu notifiers we need some kind of time out otherwise we
116 * could potentialy wait for ever, 1000ms ie 1s sounds like a long time to
117 * wait already.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118 */
David Brazdil0f672f62019-12-10 10:32:29 +0000119#define HMM_RANGE_DEFAULT_TIMEOUT 1000
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121#endif /* LINUX_HMM_H */