From patchwork Tue Oct 20 06:18:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 11845777 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2DF5A14B4 for ; Tue, 20 Oct 2020 06:19:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BFAAB22282 for ; Tue, 20 Oct 2020 06:19:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=shutemov-name.20150623.gappssmtp.com header.i=@shutemov-name.20150623.gappssmtp.com header.b="lMVJEorZ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BFAAB22282 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 507DF6B0080; Tue, 20 Oct 2020 02:19:15 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4641E6B0081; Tue, 20 Oct 2020 02:19:15 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 188BF6B0083; Tue, 20 Oct 2020 02:19:15 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0251.hostedemail.com [216.40.44.251]) by kanga.kvack.org (Postfix) with ESMTP id C32A06B0081 for ; Tue, 20 Oct 2020 02:19:14 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 6CF43180AD807 for ; Tue, 20 Oct 2020 06:19:14 +0000 (UTC) X-FDA: 77391301428.14.spy43_5d059752723d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 4F50218229818 for ; Tue, 20 Oct 2020 06:19:14 +0000 (UTC) X-Spam-Summary: 1,0,0,b1f544171418659c,d41d8cd98f00b204,kirill@shutemov.name,,RULES_HIT:2:41:355:379:541:960:968:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1606:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3355:3369:3865:3867:3868:3871:3872:4119:4250:4321:4605:5007:6119:6261:6653:6742:7903:8957:10004:11026:11473:11658:11914:12043:12048:12291:12296:12297:12438:12517:12519:12555:12679:12895:12986:13894:14096:21080:21324:21444:21451:21627:21966:21987:21990:30003:30054:30070,0,RBL:209.85.167.67:@shutemov.name:.lbl8.mailshell.net-62.8.0.100 66.201.201.201;04yrt5ozbuamiw91gzewkcid3rnxpyp4bmfwgwu3agy6jkmpyqs65krd9g3zxar.1k3p61qyniwexfr8iaja5r6u48shwkk88m1pjz6woxhi4j7h33qb73aky5iytk6.g-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: spy43_5d059752723d X-Filterd-Recvd-Size: 8970 Received: from mail-lf1-f67.google.com (mail-lf1-f67.google.com [209.85.167.67]) by imf04.hostedemail.com (Postfix) with ESMTP for ; Tue, 20 Oct 2020 06:19:13 +0000 (UTC) Received: by mail-lf1-f67.google.com with SMTP id c141so669564lfg.5 for ; Mon, 19 Oct 2020 23:19:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KIAVbPzuFo0SQGjh1AZh62RNTXjbtTvj2q2tSqP5u5A=; b=lMVJEorZcBNsU+WKL2FowmkbJxB6ruG+LU7quJ9vFJVwuQlbsEYX1tQ83ACqgxFanj 4ggBJcvVBmrTiDeUgBFYqEv57Ou/jSYAqNFrrAVcorcH/JToj5lI/C0UYX3yS0S8rCl6 zaL5WY6luVVaXlJq3jMMf1kO2wjLAfZv8Tqu+zy4VbuCreEYfz5QDzVEf9hkVMUKAoaf ljmDOeYwiFfb15HQs4So/fZYbIIRRobF9J0oUy9bS+4I4TGYW7GisLUf3CbZwP2zbgQZ eygBtilf124JiAcgeTqYXIt+33gQNYAMrILdV2OAyHpTiAwPYUyu+8Qm6iDPuDE5nrur 7bSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KIAVbPzuFo0SQGjh1AZh62RNTXjbtTvj2q2tSqP5u5A=; b=LrIxrjls5nOoK2l39+ORw/OM0L16eq8AEEygW2GQxreRsxJ6Z/MzycNup6EaXBmYU4 Vj0EVUHVgXzrh4h0R2CZrSwn6Y9PdpUA2b1vOIdjkQuHP4SVs0UvXP4ABHtsOf6tEQy/ 15qC1PawXkvNp0ezvY0/oGqFL7LAZBsh9xVObU1TiYhckXH1Z77ozbIpG8nTHjMnivLB 60C2wy8ZZOYts0MJNlTlLdZQYtr5WUjkTDEOiDd+uKndA7NoWNM7UBEAxBpR3LQ2fDr7 KtKbBJ3lyFzEujogdVdkatDk5m39QIlPjq0M+yZN3ANYM+cTXESo2My91s7AOhFN233B fHxw== X-Gm-Message-State: AOAM530C37RzMCm6URFlLbMJPNwDCUoo2ag6pFj6N7w3eHa2/gNnDR3A GUA/KOYqTZrPI6vYRigZkIyEkQ== X-Google-Smtp-Source: ABdhPJyJBolsoJfELSO4r+qt6ijjZjPzBQXOcPFq5Dp6mn7pNXbaxYjhYpHuOQpmW8XwEU+vrLpZBw== X-Received: by 2002:a05:6512:41e:: with SMTP id u30mr463605lfk.204.1603174752407; Mon, 19 Oct 2020 23:19:12 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id x28sm140002lfd.4.2020.10.19.23.19.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Oct 2020 23:19:09 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id 344C2102F6E; Tue, 20 Oct 2020 09:19:02 +0300 (+03) To: Dave Hansen , Andy Lutomirski , Peter Zijlstra , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Cc: David Rientjes , Andrea Arcangeli , Kees Cook , Will Drewry , "Edgecombe, Rick P" , "Kleen, Andi" , Liran Alon , Mike Rapoport , x86@kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [RFCv2 15/16] KVM: Unmap protected pages from direct mapping Date: Tue, 20 Oct 2020 09:18:58 +0300 Message-Id: <20201020061859.18385-16-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201020061859.18385-1-kirill.shutemov@linux.intel.com> References: <20201020061859.18385-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: If the protected memory feature enabled, unmap guest memory from kernel's direct mappings. Migration and KSM is disabled for protected memory as it would require a special treatment. Signed-off-by: Kirill A. Shutemov --- include/linux/mm.h | 3 +++ mm/huge_memory.c | 8 ++++++++ mm/ksm.c | 2 ++ mm/memory.c | 12 ++++++++++++ mm/rmap.c | 4 ++++ virt/lib/mem_protected.c | 21 +++++++++++++++++++++ 6 files changed, 50 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index ee274d27e764..74efc51e63f0 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -671,6 +671,9 @@ static inline bool vma_is_kvm_protected(struct vm_area_struct *vma) return vma->vm_flags & VM_KVM_PROTECTED; } +void kvm_map_page(struct page *page, int nr_pages); +void kvm_unmap_page(struct page *page, int nr_pages); + #ifdef CONFIG_SHMEM /* * The vma_is_shmem is not inline because it is used only by slow diff --git a/mm/huge_memory.c b/mm/huge_memory.c index ec8cf9a40cfd..40974656cb43 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -627,6 +627,10 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf, spin_unlock(vmf->ptl); count_vm_event(THP_FAULT_ALLOC); count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC); + + /* Unmap page from direct mapping */ + if (vma_is_kvm_protected(vma)) + kvm_unmap_page(page, HPAGE_PMD_NR); } return 0; @@ -1689,6 +1693,10 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, page_remove_rmap(page, true); VM_BUG_ON_PAGE(page_mapcount(page) < 0, page); VM_BUG_ON_PAGE(!PageHead(page), page); + + /* Map the page back to the direct mapping */ + if (vma_is_kvm_protected(vma)) + kvm_map_page(page, HPAGE_PMD_NR); } else if (thp_migration_supported()) { swp_entry_t entry; diff --git a/mm/ksm.c b/mm/ksm.c index 9afccc36dbd2..c720e271448f 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -528,6 +528,8 @@ static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm, return NULL; if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) return NULL; + if (vma_is_kvm_protected(vma)) + return NULL; return vma; } diff --git a/mm/memory.c b/mm/memory.c index 2c9756b4e52f..e28bd5f902a7 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1245,6 +1245,11 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, likely(!(vma->vm_flags & VM_SEQ_READ))) mark_page_accessed(page); } + + /* Map the page back to the direct mapping */ + if (vma_is_anonymous(vma) && vma_is_kvm_protected(vma)) + kvm_map_page(page, 1); + rss[mm_counter(page)]--; page_remove_rmap(page, false); if (unlikely(page_mapcount(page) < 0)) @@ -3466,6 +3471,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) struct page *page; vm_fault_t ret = 0; pte_t entry; + bool set = false; /* File mapping without ->vm_ops ? */ if (vma->vm_flags & VM_SHARED) @@ -3554,6 +3560,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); page_add_new_anon_rmap(page, vma, vmf->address, false); lru_cache_add_inactive_or_unevictable(page, vma); + set = true; setpte: set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); @@ -3561,6 +3568,11 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) update_mmu_cache(vma, vmf->address, vmf->pte); unlock: pte_unmap_unlock(vmf->pte, vmf->ptl); + + /* Unmap page from direct mapping */ + if (vma_is_kvm_protected(vma) && set) + kvm_unmap_page(page, 1); + return ret; release: put_page(page); diff --git a/mm/rmap.c b/mm/rmap.c index 9425260774a1..247548d6d24b 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1725,6 +1725,10 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, static bool invalid_migration_vma(struct vm_area_struct *vma, void *arg) { + /* TODO */ + if (vma_is_kvm_protected(vma)) + return true; + return vma_is_temporary_stack(vma); } diff --git a/virt/lib/mem_protected.c b/virt/lib/mem_protected.c index 1dfe82534242..9d2ef99285e5 100644 --- a/virt/lib/mem_protected.c +++ b/virt/lib/mem_protected.c @@ -30,6 +30,27 @@ void kvm_unmap_page_atomic(void *vaddr) } EXPORT_SYMBOL_GPL(kvm_unmap_page_atomic); +void kvm_map_page(struct page *page, int nr_pages) +{ + int i; + + /* Clear page before returning it to the direct mapping */ + for (i = 0; i < nr_pages; i++) { + void *p = kvm_map_page_atomic(page + i); + memset(p, 0, PAGE_SIZE); + kvm_unmap_page_atomic(p); + } + + kernel_map_pages(page, nr_pages, 1); +} +EXPORT_SYMBOL_GPL(kvm_map_page); + +void kvm_unmap_page(struct page *page, int nr_pages) +{ + kernel_map_pages(page, nr_pages, 0); +} +EXPORT_SYMBOL_GPL(kvm_unmap_page); + int kvm_init_protected_memory(void) { guest_map_ptes = kmalloc_array(num_possible_cpus(),