@@ -152,7 +152,7 @@ static int __sgx_encl_eldu(struct sgx_encl_page *encl_page,
152
152
153
153
page_pcmd_off = sgx_encl_get_backing_page_pcmd_offset (encl , page_index );
154
154
155
- ret = sgx_encl_get_backing (encl , page_index , & b );
155
+ ret = sgx_encl_lookup_backing (encl , page_index , & b );
156
156
if (ret )
157
157
return ret ;
158
158
@@ -718,7 +718,7 @@ static struct page *sgx_encl_get_backing_page(struct sgx_encl *encl,
718
718
* 0 on success,
719
719
* -errno otherwise.
720
720
*/
721
- int sgx_encl_get_backing (struct sgx_encl * encl , unsigned long page_index ,
721
+ static int sgx_encl_get_backing (struct sgx_encl * encl , unsigned long page_index ,
722
722
struct sgx_backing * backing )
723
723
{
724
724
pgoff_t page_pcmd_off = sgx_encl_get_backing_page_pcmd_offset (encl , page_index );
@@ -743,6 +743,107 @@ int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index,
743
743
return 0 ;
744
744
}
745
745
746
+ /*
747
+ * When called from ksgxd, returns the mem_cgroup of a struct mm stored
748
+ * in the enclave's mm_list. When not called from ksgxd, just returns
749
+ * the mem_cgroup of the current task.
750
+ */
751
+ static struct mem_cgroup * sgx_encl_get_mem_cgroup (struct sgx_encl * encl )
752
+ {
753
+ struct mem_cgroup * memcg = NULL ;
754
+ struct sgx_encl_mm * encl_mm ;
755
+ int idx ;
756
+
757
+ /*
758
+ * If called from normal task context, return the mem_cgroup
759
+ * of the current task's mm. The remainder of the handling is for
760
+ * ksgxd.
761
+ */
762
+ if (!current_is_ksgxd ())
763
+ return get_mem_cgroup_from_mm (current -> mm );
764
+
765
+ /*
766
+ * Search the enclave's mm_list to find an mm associated with
767
+ * this enclave to charge the allocation to.
768
+ */
769
+ idx = srcu_read_lock (& encl -> srcu );
770
+
771
+ list_for_each_entry_rcu (encl_mm , & encl -> mm_list , list ) {
772
+ if (!mmget_not_zero (encl_mm -> mm ))
773
+ continue ;
774
+
775
+ memcg = get_mem_cgroup_from_mm (encl_mm -> mm );
776
+
777
+ mmput_async (encl_mm -> mm );
778
+
779
+ break ;
780
+ }
781
+
782
+ srcu_read_unlock (& encl -> srcu , idx );
783
+
784
+ /*
785
+ * In the rare case that there isn't an mm associated with
786
+ * the enclave, set memcg to the current active mem_cgroup.
787
+ * This will be the root mem_cgroup if there is no active
788
+ * mem_cgroup.
789
+ */
790
+ if (!memcg )
791
+ return get_mem_cgroup_from_mm (NULL );
792
+
793
+ return memcg ;
794
+ }
795
+
796
+ /**
797
+ * sgx_encl_alloc_backing() - allocate a new backing storage page
798
+ * @encl: an enclave pointer
799
+ * @page_index: enclave page index
800
+ * @backing: data for accessing backing storage for the page
801
+ *
802
+ * When called from ksgxd, sets the active memcg from one of the
803
+ * mms in the enclave's mm_list prior to any backing page allocation,
804
+ * in order to ensure that shmem page allocations are charged to the
805
+ * enclave.
806
+ *
807
+ * Return:
808
+ * 0 on success,
809
+ * -errno otherwise.
810
+ */
811
+ int sgx_encl_alloc_backing (struct sgx_encl * encl , unsigned long page_index ,
812
+ struct sgx_backing * backing )
813
+ {
814
+ struct mem_cgroup * encl_memcg = sgx_encl_get_mem_cgroup (encl );
815
+ struct mem_cgroup * memcg = set_active_memcg (encl_memcg );
816
+ int ret ;
817
+
818
+ ret = sgx_encl_get_backing (encl , page_index , backing );
819
+
820
+ set_active_memcg (memcg );
821
+ mem_cgroup_put (encl_memcg );
822
+
823
+ return ret ;
824
+ }
825
+
826
+ /**
827
+ * sgx_encl_lookup_backing() - retrieve an existing backing storage page
828
+ * @encl: an enclave pointer
829
+ * @page_index: enclave page index
830
+ * @backing: data for accessing backing storage for the page
831
+ *
832
+ * Retrieve a backing page for loading data back into an EPC page with ELDU.
833
+ * It is the caller's responsibility to ensure that it is appropriate to use
834
+ * sgx_encl_lookup_backing() rather than sgx_encl_alloc_backing(). If lookup is
835
+ * not used correctly, this will cause an allocation which is not accounted for.
836
+ *
837
+ * Return:
838
+ * 0 on success,
839
+ * -errno otherwise.
840
+ */
841
+ int sgx_encl_lookup_backing (struct sgx_encl * encl , unsigned long page_index ,
842
+ struct sgx_backing * backing )
843
+ {
844
+ return sgx_encl_get_backing (encl , page_index , backing );
845
+ }
846
+
746
847
/**
747
848
* sgx_encl_put_backing() - Unpin the backing storage
748
849
* @backing: data for accessing backing storage for the page
0 commit comments