1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2016-20 Intel Corporation. */
4 #include <linux/file.h>
5 #include <linux/freezer.h>
6 #include <linux/highmem.h>
7 #include <linux/kthread.h>
8 #include <linux/miscdevice.h>
9 #include <linux/pagemap.h>
10 #include <linux/ratelimit.h>
11 #include <linux/sched/mm.h>
12 #include <linux/sched/signal.h>
13 #include <linux/slab.h>
19 struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
20 static int sgx_nr_epc_sections;
21 static struct task_struct *ksgxd_tsk;
22 static DECLARE_WAIT_QUEUE_HEAD(ksgxd_waitq);
25 * These variables are part of the state of the reclaimer, and must be accessed
26 * with sgx_reclaimer_lock acquired.
28 static LIST_HEAD(sgx_active_page_list);
29 static DEFINE_SPINLOCK(sgx_reclaimer_lock);
31 /* The free page list lock protected variables prepend the lock. */
32 static unsigned long sgx_nr_free_pages;
34 /* Nodes with one or more EPC sections. */
35 static nodemask_t sgx_numa_mask;
38 * Array with one list_head for each possible NUMA node. Each
39 * list contains all the sgx_epc_section's which are on that
42 static struct sgx_numa_node *sgx_numa_nodes;
44 static LIST_HEAD(sgx_dirty_page_list);
47 * Reset post-kexec EPC pages to the uninitialized state. The pages are removed
48 * from the input list, and made available for the page allocator. SECS pages
49 * prepending their children in the input list are left intact.
51 static void __sgx_sanitize_pages(struct list_head *dirty_page_list)
53 struct sgx_epc_page *page;
57 /* dirty_page_list is thread-local, no need for a lock: */
58 while (!list_empty(dirty_page_list)) {
59 if (kthread_should_stop())
62 page = list_first_entry(dirty_page_list, struct sgx_epc_page, list);
64 ret = __eremove(sgx_get_epc_virt_addr(page));
67 * page is now sanitized. Make it available via the SGX
70 list_del(&page->list);
71 sgx_free_epc_page(page);
73 /* The page is not yet clean - move to the dirty list. */
74 list_move_tail(&page->list, &dirty);
80 list_splice(&dirty, dirty_page_list);
83 static bool sgx_reclaimer_age(struct sgx_epc_page *epc_page)
85 struct sgx_encl_page *page = epc_page->owner;
86 struct sgx_encl *encl = page->encl;
87 struct sgx_encl_mm *encl_mm;
91 idx = srcu_read_lock(&encl->srcu);
93 list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
94 if (!mmget_not_zero(encl_mm->mm))
97 mmap_read_lock(encl_mm->mm);
98 ret = !sgx_encl_test_and_clear_young(encl_mm->mm, page);
99 mmap_read_unlock(encl_mm->mm);
101 mmput_async(encl_mm->mm);
107 srcu_read_unlock(&encl->srcu, idx);
115 static void sgx_reclaimer_block(struct sgx_epc_page *epc_page)
117 struct sgx_encl_page *page = epc_page->owner;
118 unsigned long addr = page->desc & PAGE_MASK;
119 struct sgx_encl *encl = page->encl;
120 unsigned long mm_list_version;
121 struct sgx_encl_mm *encl_mm;
122 struct vm_area_struct *vma;
126 mm_list_version = encl->mm_list_version;
128 /* Pairs with smp_rmb() in sgx_encl_mm_add(). */
131 idx = srcu_read_lock(&encl->srcu);
133 list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
134 if (!mmget_not_zero(encl_mm->mm))
137 mmap_read_lock(encl_mm->mm);
139 ret = sgx_encl_find(encl_mm->mm, addr, &vma);
140 if (!ret && encl == vma->vm_private_data)
141 zap_vma_ptes(vma, addr, PAGE_SIZE);
143 mmap_read_unlock(encl_mm->mm);
145 mmput_async(encl_mm->mm);
148 srcu_read_unlock(&encl->srcu, idx);
149 } while (unlikely(encl->mm_list_version != mm_list_version));
151 mutex_lock(&encl->lock);
153 ret = __eblock(sgx_get_epc_virt_addr(epc_page));
154 if (encls_failed(ret))
155 ENCLS_WARN(ret, "EBLOCK");
157 mutex_unlock(&encl->lock);
160 static int __sgx_encl_ewb(struct sgx_epc_page *epc_page, void *va_slot,
161 struct sgx_backing *backing)
163 struct sgx_pageinfo pginfo;
169 pginfo.contents = (unsigned long)kmap_atomic(backing->contents);
170 pginfo.metadata = (unsigned long)kmap_atomic(backing->pcmd) +
171 backing->pcmd_offset;
173 ret = __ewb(&pginfo, sgx_get_epc_virt_addr(epc_page), va_slot);
175 kunmap_atomic((void *)(unsigned long)(pginfo.metadata -
176 backing->pcmd_offset));
177 kunmap_atomic((void *)(unsigned long)pginfo.contents);
182 static void sgx_ipi_cb(void *info)
186 static const cpumask_t *sgx_encl_ewb_cpumask(struct sgx_encl *encl)
188 cpumask_t *cpumask = &encl->cpumask;
189 struct sgx_encl_mm *encl_mm;
193 * Can race with sgx_encl_mm_add(), but ETRACK has already been
194 * executed, which means that the CPUs running in the new mm will enter
195 * into the enclave with a fresh epoch.
197 cpumask_clear(cpumask);
199 idx = srcu_read_lock(&encl->srcu);
201 list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
202 if (!mmget_not_zero(encl_mm->mm))
205 cpumask_or(cpumask, cpumask, mm_cpumask(encl_mm->mm));
207 mmput_async(encl_mm->mm);
210 srcu_read_unlock(&encl->srcu, idx);
216 * Swap page to the regular memory transformed to the blocked state by using
217 * EBLOCK, which means that it can no longer be referenced (no new TLB entries).
219 * The first trial just tries to write the page assuming that some other thread
220 * has reset the count for threads inside the enclave by using ETRACK, and
221 * previous thread count has been zeroed out. The second trial calls ETRACK
222 * before EWB. If that fails we kick all the HW threads out, and then do EWB,
223 * which should be guaranteed the succeed.
225 static void sgx_encl_ewb(struct sgx_epc_page *epc_page,
226 struct sgx_backing *backing)
228 struct sgx_encl_page *encl_page = epc_page->owner;
229 struct sgx_encl *encl = encl_page->encl;
230 struct sgx_va_page *va_page;
231 unsigned int va_offset;
235 encl_page->desc &= ~SGX_ENCL_PAGE_BEING_RECLAIMED;
237 va_page = list_first_entry(&encl->va_pages, struct sgx_va_page,
239 va_offset = sgx_alloc_va_slot(va_page);
240 va_slot = sgx_get_epc_virt_addr(va_page->epc_page) + va_offset;
241 if (sgx_va_page_full(va_page))
242 list_move_tail(&va_page->list, &encl->va_pages);
244 ret = __sgx_encl_ewb(epc_page, va_slot, backing);
245 if (ret == SGX_NOT_TRACKED) {
246 ret = __etrack(sgx_get_epc_virt_addr(encl->secs.epc_page));
248 if (encls_failed(ret))
249 ENCLS_WARN(ret, "ETRACK");
252 ret = __sgx_encl_ewb(epc_page, va_slot, backing);
253 if (ret == SGX_NOT_TRACKED) {
255 * Slow path, send IPIs to kick cpus out of the
256 * enclave. Note, it's imperative that the cpu
257 * mask is generated *after* ETRACK, else we'll
258 * miss cpus that entered the enclave between
259 * generating the mask and incrementing epoch.
261 on_each_cpu_mask(sgx_encl_ewb_cpumask(encl),
262 sgx_ipi_cb, NULL, 1);
263 ret = __sgx_encl_ewb(epc_page, va_slot, backing);
268 if (encls_failed(ret))
269 ENCLS_WARN(ret, "EWB");
271 sgx_free_va_slot(va_page, va_offset);
273 encl_page->desc |= va_offset;
274 encl_page->va_page = va_page;
278 static void sgx_reclaimer_write(struct sgx_epc_page *epc_page,
279 struct sgx_backing *backing)
281 struct sgx_encl_page *encl_page = epc_page->owner;
282 struct sgx_encl *encl = encl_page->encl;
283 struct sgx_backing secs_backing;
286 mutex_lock(&encl->lock);
288 sgx_encl_ewb(epc_page, backing);
289 encl_page->epc_page = NULL;
290 encl->secs_child_cnt--;
292 if (!encl->secs_child_cnt && test_bit(SGX_ENCL_INITIALIZED, &encl->flags)) {
293 ret = sgx_encl_get_backing(encl, PFN_DOWN(encl->size),
298 sgx_encl_ewb(encl->secs.epc_page, &secs_backing);
300 sgx_encl_free_epc_page(encl->secs.epc_page);
301 encl->secs.epc_page = NULL;
303 sgx_encl_put_backing(&secs_backing, true);
307 mutex_unlock(&encl->lock);
311 * Take a fixed number of pages from the head of the active page pool and
312 * reclaim them to the enclave's private shmem files. Skip the pages, which have
313 * been accessed since the last scan. Move those pages to the tail of active
314 * page pool so that the pages get scanned in LRU like fashion.
316 * Batch process a chunk of pages (at the moment 16) in order to degrade amount
317 * of IPI's and ETRACK's potentially required. sgx_encl_ewb() does degrade a bit
318 * among the HW threads with three stage EWB pipeline (EWB, ETRACK + EWB and IPI
319 * + EWB) but not sufficiently. Reclaiming one page at a time would also be
320 * problematic as it would increase the lock contention too much, which would
321 * halt forward progress.
323 static void sgx_reclaim_pages(void)
325 struct sgx_epc_page *chunk[SGX_NR_TO_SCAN];
326 struct sgx_backing backing[SGX_NR_TO_SCAN];
327 struct sgx_epc_section *section;
328 struct sgx_encl_page *encl_page;
329 struct sgx_epc_page *epc_page;
330 struct sgx_numa_node *node;
336 spin_lock(&sgx_reclaimer_lock);
337 for (i = 0; i < SGX_NR_TO_SCAN; i++) {
338 if (list_empty(&sgx_active_page_list))
341 epc_page = list_first_entry(&sgx_active_page_list,
342 struct sgx_epc_page, list);
343 list_del_init(&epc_page->list);
344 encl_page = epc_page->owner;
346 if (kref_get_unless_zero(&encl_page->encl->refcount) != 0)
347 chunk[cnt++] = epc_page;
349 /* The owner is freeing the page. No need to add the
350 * page back to the list of reclaimable pages.
352 epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED;
354 spin_unlock(&sgx_reclaimer_lock);
356 for (i = 0; i < cnt; i++) {
358 encl_page = epc_page->owner;
360 if (!sgx_reclaimer_age(epc_page))
363 page_index = PFN_DOWN(encl_page->desc - encl_page->encl->base);
364 ret = sgx_encl_get_backing(encl_page->encl, page_index, &backing[i]);
368 mutex_lock(&encl_page->encl->lock);
369 encl_page->desc |= SGX_ENCL_PAGE_BEING_RECLAIMED;
370 mutex_unlock(&encl_page->encl->lock);
374 spin_lock(&sgx_reclaimer_lock);
375 list_add_tail(&epc_page->list, &sgx_active_page_list);
376 spin_unlock(&sgx_reclaimer_lock);
378 kref_put(&encl_page->encl->refcount, sgx_encl_release);
383 for (i = 0; i < cnt; i++) {
386 sgx_reclaimer_block(epc_page);
389 for (i = 0; i < cnt; i++) {
394 encl_page = epc_page->owner;
395 sgx_reclaimer_write(epc_page, &backing[i]);
396 sgx_encl_put_backing(&backing[i], true);
398 kref_put(&encl_page->encl->refcount, sgx_encl_release);
399 epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED;
401 section = &sgx_epc_sections[epc_page->section];
402 node = section->node;
404 spin_lock(&node->lock);
405 list_add_tail(&epc_page->list, &node->free_page_list);
407 spin_unlock(&node->lock);
411 static bool sgx_should_reclaim(unsigned long watermark)
413 return sgx_nr_free_pages < watermark && !list_empty(&sgx_active_page_list);
416 static int ksgxd(void *p)
421 * Sanitize pages in order to recover from kexec(). The 2nd pass is
422 * required for SECS pages, whose child pages blocked EREMOVE.
424 __sgx_sanitize_pages(&sgx_dirty_page_list);
425 __sgx_sanitize_pages(&sgx_dirty_page_list);
428 WARN_ON(!list_empty(&sgx_dirty_page_list));
430 while (!kthread_should_stop()) {
434 wait_event_freezable(ksgxd_waitq,
435 kthread_should_stop() ||
436 sgx_should_reclaim(SGX_NR_HIGH_PAGES));
438 if (sgx_should_reclaim(SGX_NR_HIGH_PAGES))
447 static bool __init sgx_page_reclaimer_init(void)
449 struct task_struct *tsk;
451 tsk = kthread_run(ksgxd, NULL, "ksgxd");
460 static struct sgx_epc_page *__sgx_alloc_epc_page_from_node(int nid)
462 struct sgx_numa_node *node = &sgx_numa_nodes[nid];
463 struct sgx_epc_page *page = NULL;
465 spin_lock(&node->lock);
467 if (list_empty(&node->free_page_list)) {
468 spin_unlock(&node->lock);
472 page = list_first_entry(&node->free_page_list, struct sgx_epc_page, list);
473 list_del_init(&page->list);
476 spin_unlock(&node->lock);
482 * __sgx_alloc_epc_page() - Allocate an EPC page
484 * Iterate through NUMA nodes and reserve ia free EPC page to the caller. Start
485 * from the NUMA node, where the caller is executing.
488 * - an EPC page: A borrowed EPC pages were available.
489 * - NULL: Out of EPC pages.
491 struct sgx_epc_page *__sgx_alloc_epc_page(void)
493 struct sgx_epc_page *page;
494 int nid_of_current = numa_node_id();
495 int nid = nid_of_current;
497 if (node_isset(nid_of_current, sgx_numa_mask)) {
498 page = __sgx_alloc_epc_page_from_node(nid_of_current);
503 /* Fall back to the non-local NUMA nodes: */
505 nid = next_node_in(nid, sgx_numa_mask);
506 if (nid == nid_of_current)
509 page = __sgx_alloc_epc_page_from_node(nid);
514 return ERR_PTR(-ENOMEM);
518 * sgx_mark_page_reclaimable() - Mark a page as reclaimable
521 * Mark a page as reclaimable and add it to the active page list. Pages
522 * are automatically removed from the active list when freed.
524 void sgx_mark_page_reclaimable(struct sgx_epc_page *page)
526 spin_lock(&sgx_reclaimer_lock);
527 page->flags |= SGX_EPC_PAGE_RECLAIMER_TRACKED;
528 list_add_tail(&page->list, &sgx_active_page_list);
529 spin_unlock(&sgx_reclaimer_lock);
533 * sgx_unmark_page_reclaimable() - Remove a page from the reclaim list
536 * Clear the reclaimable flag and remove the page from the active page list.
540 * -EBUSY if the page is in the process of being reclaimed
542 int sgx_unmark_page_reclaimable(struct sgx_epc_page *page)
544 spin_lock(&sgx_reclaimer_lock);
545 if (page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) {
546 /* The page is being reclaimed. */
547 if (list_empty(&page->list)) {
548 spin_unlock(&sgx_reclaimer_lock);
552 list_del(&page->list);
553 page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED;
555 spin_unlock(&sgx_reclaimer_lock);
561 * sgx_alloc_epc_page() - Allocate an EPC page
562 * @owner: the owner of the EPC page
563 * @reclaim: reclaim pages if necessary
565 * Iterate through EPC sections and borrow a free EPC page to the caller. When a
566 * page is no longer needed it must be released with sgx_free_epc_page(). If
567 * @reclaim is set to true, directly reclaim pages when we are out of pages. No
568 * mm's can be locked when @reclaim is set to true.
570 * Finally, wake up ksgxd when the number of pages goes below the watermark
571 * before returning back to the caller.
577 struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim)
579 struct sgx_epc_page *page;
582 page = __sgx_alloc_epc_page();
588 if (list_empty(&sgx_active_page_list))
589 return ERR_PTR(-ENOMEM);
592 page = ERR_PTR(-EBUSY);
596 if (signal_pending(current)) {
597 page = ERR_PTR(-ERESTARTSYS);
605 if (sgx_should_reclaim(SGX_NR_LOW_PAGES))
606 wake_up(&ksgxd_waitq);
612 * sgx_free_epc_page() - Free an EPC page
615 * Put the EPC page back to the list of free pages. It's the caller's
616 * responsibility to make sure that the page is in uninitialized state. In other
617 * words, do EREMOVE, EWB or whatever operation is necessary before calling
620 void sgx_free_epc_page(struct sgx_epc_page *page)
622 struct sgx_epc_section *section = &sgx_epc_sections[page->section];
623 struct sgx_numa_node *node = section->node;
625 spin_lock(&node->lock);
627 list_add_tail(&page->list, &node->free_page_list);
630 spin_unlock(&node->lock);
633 static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
635 struct sgx_epc_section *section)
637 unsigned long nr_pages = size >> PAGE_SHIFT;
640 section->virt_addr = memremap(phys_addr, size, MEMREMAP_WB);
641 if (!section->virt_addr)
644 section->pages = vmalloc(nr_pages * sizeof(struct sgx_epc_page));
645 if (!section->pages) {
646 memunmap(section->virt_addr);
650 section->phys_addr = phys_addr;
652 for (i = 0; i < nr_pages; i++) {
653 section->pages[i].section = index;
654 section->pages[i].flags = 0;
655 section->pages[i].owner = NULL;
656 list_add_tail(§ion->pages[i].list, &sgx_dirty_page_list);
663 * A section metric is concatenated in a way that @low bits 12-31 define the
664 * bits 12-31 of the metric and @high bits 0-19 define the bits 32-51 of the
667 static inline u64 __init sgx_calc_section_metric(u64 low, u64 high)
669 return (low & GENMASK_ULL(31, 12)) +
670 ((high & GENMASK_ULL(19, 0)) << 32);
673 static bool __init sgx_page_cache_init(void)
675 u32 eax, ebx, ecx, edx, type;
680 sgx_numa_nodes = kmalloc_array(num_possible_nodes(), sizeof(*sgx_numa_nodes), GFP_KERNEL);
684 for (i = 0; i < ARRAY_SIZE(sgx_epc_sections); i++) {
685 cpuid_count(SGX_CPUID, i + SGX_CPUID_EPC, &eax, &ebx, &ecx, &edx);
687 type = eax & SGX_CPUID_EPC_MASK;
688 if (type == SGX_CPUID_EPC_INVALID)
691 if (type != SGX_CPUID_EPC_SECTION) {
692 pr_err_once("Unknown EPC section type: %u\n", type);
696 pa = sgx_calc_section_metric(eax, ebx);
697 size = sgx_calc_section_metric(ecx, edx);
699 pr_info("EPC section 0x%llx-0x%llx\n", pa, pa + size - 1);
701 if (!sgx_setup_epc_section(pa, size, i, &sgx_epc_sections[i])) {
702 pr_err("No free memory for an EPC section\n");
706 nid = numa_map_to_online_node(phys_to_target_node(pa));
707 if (nid == NUMA_NO_NODE) {
708 /* The physical address is already printed above. */
709 pr_warn(FW_BUG "Unable to map EPC section to online node. Fallback to the NUMA node 0.\n");
713 if (!node_isset(nid, sgx_numa_mask)) {
714 spin_lock_init(&sgx_numa_nodes[nid].lock);
715 INIT_LIST_HEAD(&sgx_numa_nodes[nid].free_page_list);
716 node_set(nid, sgx_numa_mask);
719 sgx_epc_sections[i].node = &sgx_numa_nodes[nid];
721 sgx_nr_epc_sections++;
724 if (!sgx_nr_epc_sections) {
725 pr_err("There are zero EPC sections.\n");
733 * Update the SGX_LEPUBKEYHASH MSRs to the values specified by caller.
734 * Bare-metal driver requires to update them to hash of enclave's signer
735 * before EINIT. KVM needs to update them to guest's virtual MSR values
736 * before doing EINIT from guest.
738 void sgx_update_lepubkeyhash(u64 *lepubkeyhash)
742 WARN_ON_ONCE(preemptible());
744 for (i = 0; i < 4; i++)
745 wrmsrl(MSR_IA32_SGXLEPUBKEYHASH0 + i, lepubkeyhash[i]);
748 const struct file_operations sgx_provision_fops = {
749 .owner = THIS_MODULE,
752 static struct miscdevice sgx_dev_provision = {
753 .minor = MISC_DYNAMIC_MINOR,
754 .name = "sgx_provision",
755 .nodename = "sgx_provision",
756 .fops = &sgx_provision_fops,
760 * sgx_set_attribute() - Update allowed attributes given file descriptor
761 * @allowed_attributes: Pointer to allowed enclave attributes
762 * @attribute_fd: File descriptor for specific attribute
764 * Append enclave attribute indicated by file descriptor to allowed
765 * attributes. Currently only SGX_ATTR_PROVISIONKEY indicated by
766 * /dev/sgx_provision is supported.
769 * -0: SGX_ATTR_PROVISIONKEY is appended to allowed_attributes
770 * -EINVAL: Invalid, or not supported file descriptor
772 int sgx_set_attribute(unsigned long *allowed_attributes,
773 unsigned int attribute_fd)
777 file = fget(attribute_fd);
781 if (file->f_op != &sgx_provision_fops) {
786 *allowed_attributes |= SGX_ATTR_PROVISIONKEY;
791 EXPORT_SYMBOL_GPL(sgx_set_attribute);
793 static int __init sgx_init(void)
798 if (!cpu_feature_enabled(X86_FEATURE_SGX))
801 if (!sgx_page_cache_init())
804 if (!sgx_page_reclaimer_init()) {
809 ret = misc_register(&sgx_dev_provision);
814 * Always try to initialize the native *and* KVM drivers.
815 * The KVM driver is less picky than the native one and
816 * can function if the native one is not supported on the
817 * current system or fails to initialize.
819 * Error out only if both fail to initialize.
821 ret = sgx_drv_init();
823 if (sgx_vepc_init() && ret)
829 misc_deregister(&sgx_dev_provision);
832 kthread_stop(ksgxd_tsk);
835 for (i = 0; i < sgx_nr_epc_sections; i++) {
836 vfree(sgx_epc_sections[i].pages);
837 memunmap(sgx_epc_sections[i].virt_addr);
843 device_initcall(sgx_init);