Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix locking issues #135

Merged
merged 4 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion driver/linux/dkms.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PACKAGE_NAME="sgx"
PACKAGE_VERSION="1.36"
PACKAGE_VERSION="1.36.1"
BUILT_MODULE_NAME[0]="intel_sgx"
DEST_MODULE_LOCATION[0]="/kernel/drivers/intel/sgx"
AUTOINSTALL="yes"
Expand Down
37 changes: 31 additions & 6 deletions driver/linux/encl.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
unsigned long idx_end = PFN_DOWN(end - 1);
struct sgx_encl_page *page;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0))
unsigned long count = 0;
int ret = 0;
XA_STATE(xas, &encl->page_array, idx_start);
#else
unsigned long idx;
Expand All @@ -385,9 +387,33 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
if (!!(current->personality & READ_IMPLIES_EXEC))
return -EACCES;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0))
xas_for_each(&xas, page, idx_end)
if (!page || (~page->vm_max_prot_bits & vm_prot_bits))
return -EACCES;
mutex_lock(&encl->lock);
xas_lock(&xas);
while (xas.xa_index < idx_end) {
/* Move to the next index. */
page = xas_next(&xas);

if (!page || (~page->vm_max_prot_bits & vm_prot_bits)){
ret = -EACCES;
break;
}
/* Reschedule on every XA_CHECK_SCHED iteration. */
if (!(++count % XA_CHECK_SCHED)) {
xas_pause(&xas);
xas_unlock(&xas);
mutex_unlock(&encl->lock);

cond_resched();

mutex_lock(&encl->lock);
xas_lock(&xas);
}
}
xas_unlock(&xas);
mutex_unlock(&encl->lock);

return ret;

#else
for (idx = idx_start; idx <= idx_end; ++idx) {
mutex_lock(&encl->lock);
Expand All @@ -396,9 +422,8 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
if (!page || (~page->vm_max_prot_bits & vm_prot_bits))
return -EACCES;
}
#endif

return 0;
#endif
}

/* ! Note: Not ported from inkernel patches
Expand Down Expand Up @@ -620,7 +645,7 @@ void sgx_encl_release(struct kref *ref)

if (encl->backing)
fput(encl->backing);

synchronize_srcu(&encl->srcu);
cleanup_srcu_struct(&encl->srcu);

WARN_ON_ONCE(!list_empty(&encl->mm_list));
Expand Down
24 changes: 17 additions & 7 deletions driver/linux/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
// Copyright(c) 2016-17 Intel Corporation.
// Copyright(c) 2016-20 Intel Corporation.

#include <linux/freezer.h>
#include <linux/highmem.h>
Expand Down Expand Up @@ -29,6 +29,9 @@ static struct task_struct *ksgxswapd_tsk;
static DECLARE_WAIT_QUEUE_HEAD(ksgxswapd_waitq);
static LIST_HEAD(sgx_active_page_list);
static DEFINE_SPINLOCK(sgx_active_page_list_lock);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) )
static void (*k_mmput_async)(struct mm_struct* mm);
#endif

/**
* sgx_mark_page_reclaimable() - Mark a page as reclaimable
Expand Down Expand Up @@ -102,8 +105,8 @@ static bool sgx_reclaimer_age(struct sgx_epc_page *epc_page)
up_read(&encl_mm->mm->mmap_sem);
#endif

#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) || LINUX_VERSION_CODE > KERNEL_VERSION(5, 4, 0) )
mmput(encl_mm->mm);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) )
k_mmput_async(encl_mm->mm);
#else
mmput_async(encl_mm->mm);
#endif
Expand Down Expand Up @@ -156,8 +159,8 @@ static void sgx_reclaimer_block(struct sgx_epc_page *epc_page)
up_read(&encl_mm->mm->mmap_sem);
#endif

#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) || LINUX_VERSION_CODE > KERNEL_VERSION(5, 4, 0) )
mmput(encl_mm->mm);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) )
k_mmput_async(encl_mm->mm);
#else
mmput_async(encl_mm->mm);
#endif
Expand Down Expand Up @@ -224,8 +227,8 @@ static const cpumask_t *sgx_encl_ewb_cpumask(struct sgx_encl *encl)

cpumask_or(cpumask, cpumask, mm_cpumask(encl_mm->mm));

#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) || LINUX_VERSION_CODE > KERNEL_VERSION(5, 4, 0) )
mmput(encl_mm->mm);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) )
k_mmput_async(encl_mm->mm);
#else
mmput_async(encl_mm->mm);
#endif
Expand Down Expand Up @@ -800,6 +803,13 @@ static int __init sgx_init(void)
if (!sgx_page_cache_init())
return -EFAULT;

#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) )
k_mmput_async = (void*)kallsyms_lookup_name("mmput_async");
if (!k_mmput_async){
pr_err("intel_sgx: mmput_async support missing from kernel.\n");
return -EFAULT;
}
#endif
if (!sgx_page_reclaimer_init())
goto err_page_cache;

Expand Down
2 changes: 1 addition & 1 deletion driver/linux/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#define _SGX_VERSION_H

#define DRV_DESCRIPTION "Intel SGX DCAP Driver"
#define DRV_VERSION "1.36"
#define DRV_VERSION "1.36.1"

#endif /* _SGX_VERSION_H */