Re: [PATCH] x86/sgx: fix a NULL pointer

From: Haitao Huang
Date: Tue Jul 18 2023 - 17:23:01 EST


On Tue, 18 Jul 2023 15:56:27 -0500, Dave Hansen <dave.hansen@xxxxxxxxx> wrote:

On 7/18/23 13:32, Haitao Huang wrote:
...
Ignore VA pages for now. Say for a system with 10 page EPC, 2 enclaves,
each needs 5 pages non-SECS so total demand would be 12 pages. The ksgxd
would only need to swap out 2 pages at the most to get one enclave fully
loaded with 6 pages, and the other one with 4 pages. There is no chance
the ksgxd would swap any one of two SECS pages.

We would need at least one enclave A of 10 pages total to squeeze out
the other B completely. For that to happen B pretty much has to be
sleeping all the time so the LRU based reclaiming would hit it but not
pages of A. So no chance to hit #PF on pages of B still.

So some minimal pressure is needed to ensure SECS swapped. The higher
the pressure the higher the chance to hit #PF while SECS is swapped.

What would the second-to-last non-SECS page be? A thread control page?
VA page?

As long as *that* page can generate a page fault, then you only need two
pages for this scenario to happen:

1. Reclaimer takes encl->lock
2. #PF occurs from another thread, blocks on encl->lock
3. SECS is reclaimed
4. encl->lock released
5. #PF sees reclaimed SECS


I agree this is the race. But for this to happen, that is at #1 you have only one non-SECS page left so #3 can happen. That means it is already high pressure because reclaimer has swapped all other non-SECS.
In my example of two enclaves of 5 non-EPC pages. #3 won't happen because you don't reach #1 with only one non-SECS left.

Thanks
Haitao