blob: 2fd2e91d5107c0feca83a9202edb7b9af186419e [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07002/*
3 * Copyright 2013 Red Hat Inc.
4 *
Jérôme Glissef813f212018-10-30 15:04:06 -07005 * Authors: Jérôme Glisse <[email protected]>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07006 *
Jason Gunthorpef970b972020-03-27 17:00:15 -03007 * See Documentation/vm/hmm.rst for reasons and overview of what HMM is.
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07008 */
9#ifndef LINUX_HMM_H
10#define LINUX_HMM_H
11
12#include <linux/kconfig.h>
Mike Rapoportca5999f2020-06-08 21:32:38 -070013#include <linux/pgtable.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070014
Jérôme Glisse858b54d2017-09-08 16:12:02 -070015#include <linux/device.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070016#include <linux/migrate.h>
17#include <linux/memremap.h>
18#include <linux/completion.h>
Jérôme Glissea3e0d412019-05-13 17:20:01 -070019#include <linux/mmu_notifier.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070020
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070021/*
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030022 * 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
Jérôme Glissef88a1e92018-04-10 16:29:06 -070031 *
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030032 * 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.
Jérôme Glissef88a1e92018-04-10 16:29:06 -070038 */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030039enum hmm_pfn_flags {
Ralph Campbell3b50a6e2020-07-01 15:53:49 -070040 /* Output fields and flags */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030041 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),
Ralph Campbell3b50a6e2020-07-01 15:53:49 -070044 HMM_PFN_ORDER_SHIFT = (BITS_PER_LONG - 8),
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030045
46 /* Input flags */
47 HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
48 HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,
49
Ralph Campbell3b50a6e2020-07-01 15:53:49 -070050 HMM_PFN_FLAGS = 0xFFUL << HMM_PFN_ORDER_SHIFT,
Jérôme Glissef88a1e92018-04-10 16:29:06 -070051};
52
53/*
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030054 * hmm_pfn_to_page() - return struct page pointed to by a device entry
Jérôme Glissef88a1e92018-04-10 16:29:06 -070055 *
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030056 * 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.
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070059 */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030060static inline struct page *hmm_pfn_to_page(unsigned long hmm_pfn)
61{
62 return pfn_to_page(hmm_pfn & ~HMM_PFN_FLAGS);
63}
Jérôme Glissef88a1e92018-04-10 16:29:06 -070064
65/*
Ralph Campbell3b50a6e2020-07-01 15:53:49 -070066 * hmm_pfn_to_map_order() - return the CPU mapping size order
67 *
68 * 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().
74 *
75 * 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.
78 */
79static inline unsigned int hmm_pfn_to_map_order(unsigned long hmm_pfn)
80{
81 return (hmm_pfn >> HMM_PFN_ORDER_SHIFT) & 0x1F;
82}
83
84/*
Jérôme Glissef88a1e92018-04-10 16:29:06 -070085 * struct hmm_range - track invalidation lock on virtual address range
86 *
Jason Gunthorpea22dd502019-11-12 16:22:30 -040087 * @notifier: a mmu_interval_notifier that includes the start/end
88 * @notifier_seq: result of mmu_interval_read_begin()
Jérôme Glissef88a1e92018-04-10 16:29:06 -070089 * @start: range virtual start address (inclusive)
90 * @end: range virtual end address (exclusive)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030091 * @hmm_pfns: array of pfns (big enough for the range)
Jérôme Glisse023a0192019-05-13 17:20:05 -070092 * @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
Christoph Hellwig08ddddd2020-03-16 20:32:16 +010094 * @dev_private_owner: owner of device private pages
Jérôme Glissef88a1e92018-04-10 16:29:06 -070095 */
96struct hmm_range {
Jason Gunthorpe04ec32f2019-11-12 16:22:20 -040097 struct mmu_interval_notifier *notifier;
98 unsigned long notifier_seq;
Jérôme Glissef88a1e92018-04-10 16:29:06 -070099 unsigned long start;
100 unsigned long end;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300101 unsigned long *hmm_pfns;
102 unsigned long default_flags;
103 unsigned long pfn_flags_mask;
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100104 void *dev_private_owner;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700105};
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700106
107/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700108 * Please see Documentation/vm/hmm.rst for how to use the range API.
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700109 */
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300110int hmm_range_fault(struct hmm_range *range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700111
112/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700113 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
Jérôme Glisse74eee182017-09-08 16:11:35 -0700114 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700115 * When waiting for mmu notifiers we need some kind of time out otherwise we
Zhen Lei06c88392021-07-07 18:08:19 -0700116 * could potentially wait for ever, 1000ms ie 1s sounds like a long time to
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700117 * wait already.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700118 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700119#define HMM_RANGE_DEFAULT_TIMEOUT 1000
120
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700121#endif /* LINUX_HMM_H */