Skip to content

RFC x86/sgx: implement support for MADV_WILLNEED

Haitao Huang linux-sgx 4 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 RFC Latest4 patches 55 repliesIn review
  2. v3 RFC4 patches 7 repliesSuperseded
  3. v2 RFC4 patches 3 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v1 RFC4 patches4 repliesSuperseded

Changes in v4 from the cover letter

- improve comments for return values of sgx_encl_eaug_page (Jarkko)
- update selftests to set value for the len field of all EACCEPT ops (Jarkko)
- remove unneeded check for 'encl' and reordered input checks and initialization of local vars in sgx_fadvise (Jarkko)

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4x86/sgx: Export sgx_encl_eaug_pagearch/x86/kernel/cpu/sgx/encl.c arch/x86/kernel/cpu/sgx/encl.h+4 -21 reply
2/4x86/sgx: Implement support for MADV_WILLNEEDarch/x86/kernel/cpu/sgx/driver.c arch/x86/kernel/cpu/sgx/encl.c arch/x86/kernel/cpu/sgx/encl.h+111 -2648 replies
3/4selftests/sgx: add len field for EACCEPT optools/testing/selftests/sgx/defines.h tools/testing/selftests/sgx/main.c tools/testing/selftests/sgx/test_encl.c+28 -61 reply
4/4selftests/sgx: Add test for madvise(..., WILLNEED)tools/testing/selftests/sgx/main.c+167 -05 replies

Reviews trailers per patch

All 4 patches have a Reviewed-by or Acked-by.

PatchJarkko Sakkinen
1/4A-b R-b
2/4R-b
3/4T-b R-b
4/4T-b R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v4 thread

Cover letter [RFC PATCH v4 0/4] x86/sgx: implement support for MADV_WILLNEED

V3: https://lore.kernel.org/linux-sgx/[email protected]/#t
Changes since V3:
- improve comments for return values of sgx_encl_eaug_page (Jarkko)
- update selftests to set value for the len field of all EACCEPT ops (Jarkko)
- remove unneeded check for 'encl' and reordered input checks and initialization of local vars in sgx_fadvise (Jarkko)

V2: https://lore.kernel.org/linux-sgx/[email protected]/T/#t
Changes since V2:
- format changes and remove unneeded comments. (Jarkko)
- use PFN_DOWN instead of ">> PAGE_SHIFT". (Jarkko)

V1: https://lore.kernel.org/linux-sgx/[email protected]/T/#t
Changes since V1:
- Separate patch for exporting sgx_encl_eaug_page
- Move return code changes for sgx_encl_eaug_page to the same patch implementing sgx_fadvise
- Small improvement in commit messages and the cover letter

Hi Everybody,

The current SGX2 (EDMM) implementation in the kernel only adds an EPC page when a page fault is triggered on an address without EPC allocated. Although this is adquate for allocations of smaller address ranges or ranges with sparse accessing patterns, it is inefficient for other cases in which large number of EPC pages need be added and accessed immediately afterwards.

Previously we have attempted [1] to address this issue by implementing support for the semantics of MAP_POPULATE flag passed into mmap(). However, some mm maintainers have concerns on adding a new callback in fops [2].

This series is to adopt the MADV_WILLNEED alternative suggested by Dave in previous discussions [3]. The sgx driver implements the fops->fadvise() so that user space will be able to use madvise(..., MADV_WILLNEED) to instruct kernel to EAUG pages as soon as possible for a given range.

Compared to the MAP_POPULATE approach, this alternative requires an additional call to madvise() after mmap() from user space. But it would not need any changes in kernel outside the SGX driver. The separate madvise() call also offers flexibility for user space to specify a subrange to EAUG in an enclosing VMA.

The core implementation is in the second patch while the first patch only exports a function handling EAUG on PF to be reused.

The last two patches are to add a microbenchmark in the sgx selftest to measure the performance difference. Following speedup on various allocation sizes were observed when I ran it on a platform with 4G EPC. It indicates that the change would roughly half the run time until EPC swapping is activated, at which point EAUG for madvise is stopped.

-------------------------
Alloc. size: Speedup
-------------------------

      1 page : 75%
      2 pages: 48%
      4 pages: 55%
      8 pages: 58%
     16 pages: 62%
     32 pages: 62%
     64 pages: 62%
    128 pages: 62%
    256 pages: 73%
    512 pages: 62%

1024 pages: 62%
2048 pages: 62%
4096 pages: 61%
8192 pages: 61%
16384 pages: 61%
32768 pages: 71%
65536 pages: 61%
131072 pages: 62%
262144 pages: 62%
524288 pages: 62%
1048576 pages: 55%
2097152 pages: 19%
-------------------------

Thank you very much for your attention and any comments/feedback.

Haitao

[1]https://lore.kernel.org/all/[email protected]/ [2]https://lore.kernel.org/linux-sgx/[email protected]/ [3]https://lore.kernel.org/linux-sgx/[email protected]/

Haitao Huang (4):
x86/sgx: Export sgx_encl_eaug_page x86/sgx: Implement support for MADV_WILLNEED selftests/sgx: add len field for EACCEPT op selftests/sgx: Add test for madvise(..., WILLNEED)

 arch/x86/kernel/cpu/sgx/driver.c        |  74 ++++++++++
 arch/x86/kernel/cpu/sgx/encl.c          |  59 ++++----
 arch/x86/kernel/cpu/sgx/encl.h          |   2 +
 tools/testing/selftests/sgx/defines.h   |   1 +
 tools/testing/selftests/sgx/main.c      | 182 ++++++++++++++++++++++++
 tools/testing/selftests/sgx/test_encl.c |  18 ++-
 6 files changed, 306 insertions(+), 30 deletions(-)
Signature · 1 line
2.25.1