hmm.h 3.3 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0-or-later */
2 3 4
/*
 * Copyright 2013 Red Hat Inc.
 *
J
Jérôme Glisse 已提交
5
 * Authors: Jérôme Glisse <jglisse@redhat.com>
6
 *
7
 * See Documentation/vm/hmm.rst for reasons and overview of what HMM is.
8 9 10 11 12
 */
#ifndef LINUX_HMM_H
#define LINUX_HMM_H

#include <linux/kconfig.h>
13
#include <asm/pgtable.h>
14

15
#include <linux/device.h>
16 17 18
#include <linux/migrate.h>
#include <linux/memremap.h>
#include <linux/completion.h>
19
#include <linux/mmu_notifier.h>
20

21
/*
22 23 24 25 26 27 28 29 30
 * On output:
 * 0             - The page is faultable and a future call with 
 *                 HMM_PFN_REQ_FAULT could succeed.
 * HMM_PFN_VALID - the pfn field points to a valid PFN. This PFN is at
 *                 least readable. If dev_private_owner is !NULL then this could
 *                 point at a DEVICE_PRIVATE page.
 * HMM_PFN_WRITE - if the page memory can be written to (requires HMM_PFN_VALID)
 * HMM_PFN_ERROR - accessing the pfn is impossible and the device should
 *                 fail. ie poisoned memory, special pages, no vma, etc
31
 *
32 33 34 35 36 37
 * On input:
 * 0                 - Return the current state of the page, do not fault it.
 * HMM_PFN_REQ_FAULT - The output must have HMM_PFN_VALID or hmm_range_fault()
 *                     will fail
 * HMM_PFN_REQ_WRITE - The output must have HMM_PFN_WRITE or hmm_range_fault()
 *                     will fail. Must be combined with HMM_PFN_REQ_FAULT.
38
 */
39 40 41 42 43 44 45 46 47 48 49
enum hmm_pfn_flags {
	/* Output flags */
	HMM_PFN_VALID = 1UL << (BITS_PER_LONG - 1),
	HMM_PFN_WRITE = 1UL << (BITS_PER_LONG - 2),
	HMM_PFN_ERROR = 1UL << (BITS_PER_LONG - 3),

	/* Input flags */
	HMM_PFN_REQ_FAULT = HMM_PFN_VALID,
	HMM_PFN_REQ_WRITE = HMM_PFN_WRITE,

	HMM_PFN_FLAGS = HMM_PFN_VALID | HMM_PFN_WRITE | HMM_PFN_ERROR,
50 51 52
};

/*
53
 * hmm_pfn_to_page() - return struct page pointed to by a device entry
54
 *
55 56 57
 * This must be called under the caller 'user_lock' after a successful
 * mmu_interval_read_begin(). The caller must have tested for HMM_PFN_VALID
 * already.
58
 */
59 60 61 62
static inline struct page *hmm_pfn_to_page(unsigned long hmm_pfn)
{
	return pfn_to_page(hmm_pfn & ~HMM_PFN_FLAGS);
}
63 64 65 66

/*
 * struct hmm_range - track invalidation lock on virtual address range
 *
67 68
 * @notifier: a mmu_interval_notifier that includes the start/end
 * @notifier_seq: result of mmu_interval_read_begin()
69 70
 * @start: range virtual start address (inclusive)
 * @end: range virtual end address (exclusive)
71
 * @hmm_pfns: array of pfns (big enough for the range)
72 73
 * @default_flags: default flags for the range (write, read, ... see hmm doc)
 * @pfn_flags_mask: allows to mask pfn flags so that only default_flags matter
74
 * @dev_private_owner: owner of device private pages
75 76
 */
struct hmm_range {
77 78
	struct mmu_interval_notifier *notifier;
	unsigned long		notifier_seq;
79 80
	unsigned long		start;
	unsigned long		end;
81 82 83
	unsigned long		*hmm_pfns;
	unsigned long		default_flags;
	unsigned long		pfn_flags_mask;
84
	void			*dev_private_owner;
85
};
86

87
/*
88
 * Please see Documentation/vm/hmm.rst for how to use the range API.
89
 */
90
int hmm_range_fault(struct hmm_range *range);
91 92

/*
93
 * HMM_RANGE_DEFAULT_TIMEOUT - default timeout (ms) when waiting for a range
94
 *
95 96 97
 * When waiting for mmu notifiers we need some kind of time out otherwise we
 * could potentialy wait for ever, 1000ms ie 1s sounds like a long time to
 * wait already.
98
 */
99 100
#define HMM_RANGE_DEFAULT_TIMEOUT 1000

101
#endif /* LINUX_HMM_H */