All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX
@ 2021-02-05 15:15 Jarkko Sakkinen
  2021-02-05 15:15 ` [PATCH 2/2] x86/sgx: Maintain encl->refcount for each encl->mm_list entry Jarkko Sakkinen
  2021-02-06 13:19 ` [tip: x86/sgx] MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX tip-bot2 for Jarkko Sakkinen
  0 siblings, 2 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2021-02-05 15:15 UTC (permalink / raw)
  To: sgx
  Cc: dave.hansen, Jarkko Sakkinen, Borislav Petkov, Dave Hansen, linux-kernel

Add Dave as reviewer for INTEL SGX patches.

Cc: Borislav Petkov <bp@alien8.de>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 5b66de2097d6..41b78e20bd1f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -9227,6 +9227,7 @@ F:	include/linux/tboot.h
 
 INTEL SGX
 M:	Jarkko Sakkinen <jarkko@kernel.org>
+R:	Dave Hansen <dave.hansen@linux.intel.com>
 L:	linux-sgx@vger.kernel.org
 S:	Supported
 Q:	https://patchwork.kernel.org/project/intel-sgx/list/
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] x86/sgx: Maintain encl->refcount for each encl->mm_list entry
  2021-02-05 15:15 [PATCH 1/2] MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX Jarkko Sakkinen
@ 2021-02-05 15:15 ` Jarkko Sakkinen
  2021-02-06 13:19 ` [tip: x86/sgx] MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX tip-bot2 for Jarkko Sakkinen
  1 sibling, 0 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2021-02-05 15:15 UTC (permalink / raw)
  To: sgx
  Cc: dave.hansen, Jarkko Sakkinen, Dave Hansen, Haitao Huang,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H. Peter Anvin, Jethro Beekman, Sean Christopherson, linux-sgx,
	linux-kernel

This has been shown in tests:

[  +0.000008] WARNING: CPU: 3 PID: 7620 at kernel/rcu/srcutree.c:374 cleanup_srcu_struct+0xed/0x100

There are two functions that drain encl->mm_list:

- sgx_release() (i.e. VFS release) removes the remaining mm_list entries.
- sgx_mmu_notifier_release() removes mm_list entry for the registered
  process, if it still exists.

If encl->refcount is taken only for VFS, this can lead to
sgx_encl_release() being executed before sgx_mmu_notifier_release()
completes, which is exactly what happens in the above klog entry.

Each process also needs its own enclave reference.

In order to fix the race condition, increase encl->refcount when an
entry to encl->mm_list added for a process. Release this reference
when the mm_list entry is cleaned up, either in
sgx_mmu_notifier_release() or sgx_release().

Fixes: 1728ab54b4be ("x86/sgx: Add a page reclaimer")
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Reported-by: Haitao Huang <haitao.huang@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
v7:
- Same as v6 but v6 was missing cc to Dave. Thus, also the
  MAINTAINERS update.
v6:
- Maintain refcount for each encl->mm_list entry.
v5:
- To make sure that the instance does not get deleted use kref_get()
  kref_put(). This also removes the need for additional
  synchronize_srcu().
v4:
- Rewrite the commit message.
- Just change the call order. *_expedited() is out of scope for this
  bug fix.
v3: Fine-tuned tags, and added missing change log for v2.
v2: Switch to synchronize_srcu_expedited().
 arch/x86/kernel/cpu/sgx/driver.c | 6 ++++++
 arch/x86/kernel/cpu/sgx/encl.c   | 8 ++++++++
 2 files changed, 14 insertions(+)

diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/driver.c
index f2eac41bb4ff..8d8fcc91c0d6 100644
--- a/arch/x86/kernel/cpu/sgx/driver.c
+++ b/arch/x86/kernel/cpu/sgx/driver.c
@@ -72,6 +72,12 @@ static int sgx_release(struct inode *inode, struct file *file)
 		synchronize_srcu(&encl->srcu);
 		mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm);
 		kfree(encl_mm);
+
+		/*
+		 * Release the mm_list reference, as sgx_mmu_notifier_release()
+		 * will only do this only, when it grabs encl_mm.
+		 */
+		kref_put(&encl->refcount, sgx_encl_release);
 	}
 
 	kref_put(&encl->refcount, sgx_encl_release);
diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
index ee50a5010277..c1d9c86c0265 100644
--- a/arch/x86/kernel/cpu/sgx/encl.c
+++ b/arch/x86/kernel/cpu/sgx/encl.c
@@ -474,6 +474,7 @@ static void sgx_mmu_notifier_release(struct mmu_notifier *mn,
 	if (tmp == encl_mm) {
 		synchronize_srcu(&encl_mm->encl->srcu);
 		mmu_notifier_put(mn);
+		kref_put(&encl_mm->encl->refcount, sgx_encl_release);
 	}
 }
 
@@ -545,6 +546,13 @@ int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm)
 	}
 
 	spin_lock(&encl->mm_lock);
+
+	/*
+	 * Take a reference to guarantee that the enclave is not destroyed,
+	 * while sgx_mmu_notifier_release() is active.
+	 */
+	kref_get(&encl->refcount);
+
 	list_add_rcu(&encl_mm->list, &encl->mm_list);
 	/* Pairs with smp_rmb() in sgx_reclaimer_block(). */
 	smp_wmb();
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [tip: x86/sgx] MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX
  2021-02-05 15:15 [PATCH 1/2] MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX Jarkko Sakkinen
  2021-02-05 15:15 ` [PATCH 2/2] x86/sgx: Maintain encl->refcount for each encl->mm_list entry Jarkko Sakkinen
@ 2021-02-06 13:19 ` tip-bot2 for Jarkko Sakkinen
  1 sibling, 0 replies; 4+ messages in thread
From: tip-bot2 for Jarkko Sakkinen @ 2021-02-06 13:19 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Jarkko Sakkinen, Borislav Petkov, Dave Hansen, x86, linux-kernel

The following commit has been merged into the x86/sgx branch of tip:

Commit-ID:     848477782bfa2b6aec738045246abd6cd104006c
Gitweb:        https://git.kernel.org/tip/848477782bfa2b6aec738045246abd6cd104006c
Author:        Jarkko Sakkinen <jarkko@kernel.org>
AuthorDate:    Fri, 05 Feb 2021 17:15:44 +02:00
Committer:     Borislav Petkov <bp@suse.de>
CommitterDate: Sat, 06 Feb 2021 14:15:27 +01:00

MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX

Add Dave as reviewer for INTEL SGX patches.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Link: https://lkml.kernel.org/r/20210205151546.144810-1-jarkko@kernel.org
---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 5b66de2..41b78e2 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -9227,6 +9227,7 @@ F:	include/linux/tboot.h
 
 INTEL SGX
 M:	Jarkko Sakkinen <jarkko@kernel.org>
+R:	Dave Hansen <dave.hansen@linux.intel.com>
 L:	linux-sgx@vger.kernel.org
 S:	Supported
 Q:	https://patchwork.kernel.org/project/intel-sgx/list/

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 1/2] MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX
@ 2021-02-05 18:28 Jarkko Sakkinen
  0 siblings, 0 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2021-02-05 18:28 UTC (permalink / raw)
  To: linux-sgx
  Cc: dave.hansen, Jarkko Sakkinen, Borislav Petkov, Dave Hansen, linux-kernel

Add Dave as reviewer for INTEL SGX patches.

Cc: Borislav Petkov <bp@alien8.de>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 5b66de2097d6..41b78e20bd1f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -9227,6 +9227,7 @@ F:	include/linux/tboot.h
 
 INTEL SGX
 M:	Jarkko Sakkinen <jarkko@kernel.org>
+R:	Dave Hansen <dave.hansen@linux.intel.com>
 L:	linux-sgx@vger.kernel.org
 S:	Supported
 Q:	https://patchwork.kernel.org/project/intel-sgx/list/
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-02-06 13:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-05 15:15 [PATCH 1/2] MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX Jarkko Sakkinen
2021-02-05 15:15 ` [PATCH 2/2] x86/sgx: Maintain encl->refcount for each encl->mm_list entry Jarkko Sakkinen
2021-02-06 13:19 ` [tip: x86/sgx] MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX tip-bot2 for Jarkko Sakkinen
2021-02-05 18:28 [PATCH 1/2] " Jarkko Sakkinen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.