blob: e912b9dc4633e0909d5f8fadf29e4262c78eb884 [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>
Dan Williams063a7d12018-12-28 00:39:46 -080013#include <asm/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 {
40 /* Output 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
45 /* Input flags */
46 HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
47 HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,
48
49 HMM_PFN_FLAGS = HMM_PFN_VALID | HMM_PFN_WRITE | HMM_PFN_ERROR,
Jérôme Glissef88a1e92018-04-10 16:29:06 -070050};
51
52/*
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030053 * hmm_pfn_to_page() - return struct page pointed to by a device entry
Jérôme Glissef88a1e92018-04-10 16:29:06 -070054 *
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030055 * This must be called under the caller 'user_lock' after a successful
56 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
57 * already.
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070058 */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030059static inline struct page *hmm_pfn_to_page(unsigned long hmm_pfn)
60{
61 return pfn_to_page(hmm_pfn & ~HMM_PFN_FLAGS);
62}
Jérôme Glissef88a1e92018-04-10 16:29:06 -070063
64/*
65 * struct hmm_range - track invalidation lock on virtual address range
66 *
Jason Gunthorpea22dd502019-11-12 16:22:30 -040067 * @notifier: a mmu_interval_notifier that includes the start/end
68 * @notifier_seq: result of mmu_interval_read_begin()
Jérôme Glissef88a1e92018-04-10 16:29:06 -070069 * @start: range virtual start address (inclusive)
70 * @end: range virtual end address (exclusive)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030071 * @hmm_pfns: array of pfns (big enough for the range)
Jérôme Glisse023a0192019-05-13 17:20:05 -070072 * @default_flags: default flags for the range (write, read, ... see hmm doc)
73 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
Christoph Hellwig08ddddd2020-03-16 20:32:16 +010074 * @dev_private_owner: owner of device private pages
Jérôme Glissef88a1e92018-04-10 16:29:06 -070075 */
76struct hmm_range {
Jason Gunthorpe04ec32f2019-11-12 16:22:20 -040077 struct mmu_interval_notifier *notifier;
78 unsigned long notifier_seq;
Jérôme Glissef88a1e92018-04-10 16:29:06 -070079 unsigned long start;
80 unsigned long end;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030081 unsigned long *hmm_pfns;
82 unsigned long default_flags;
83 unsigned long pfn_flags_mask;
Christoph Hellwig08ddddd2020-03-16 20:32:16 +010084 void *dev_private_owner;
Jérôme Glissef88a1e92018-04-10 16:29:06 -070085};
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070086
87/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -070088 * Please see Documentation/vm/hmm.rst for how to use the range API.
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070089 */
Jason Gunthorpebe957c82020-05-01 15:20:45 -030090int hmm_range_fault(struct hmm_range *range);
Jérôme Glisse74eee182017-09-08 16:11:35 -070091
92/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -070093 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
Jérôme Glisse74eee182017-09-08 16:11:35 -070094 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -070095 * When waiting for mmu notifiers we need some kind of time out otherwise we
96 * could potentialy wait for ever, 1000ms ie 1s sounds like a long time to
97 * wait already.
Jérôme Glisse74eee182017-09-08 16:11:35 -070098 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -070099#define HMM_RANGE_DEFAULT_TIMEOUT 1000
100
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700101#endif /* LINUX_HMM_H */