Received: by 10.223.185.116 with SMTP id b49csp2780815wrg; Mon, 5 Mar 2018 08:34:34 -0800 (PST) X-Google-Smtp-Source: AG47ELvgNovBSnO9Nrujv9zscrvIaGH+0yfXpSMFqjFcv7CAwenuT1922LHzNmTcg1hhHvstBVrt X-Received: by 10.99.149.24 with SMTP id p24mr12675901pgd.122.1520267674694; Mon, 05 Mar 2018 08:34:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520267674; cv=none; d=google.com; s=arc-20160816; b=bArn0JtycmijAKpVhEueGAcx/0KtRXRFPDmJVsmdiICPxYgvHAvKnsKckt69IiuL/7 BjaKxNNqdXVtuKLpNU7bY+WT16cqp+udu8xbV69LcJ2Oe5jf/TfGTDw2OVgcYKwzki0v Ucvhu0u60NerBvo5wiCSl0cfBe/NeivKwwAPauWJwnmrz+HtvsAqqRgwqA+QkEMaHBHp 6qombRXR4ga5iuBaxHOf8l/qF6TWeUcVULY4aFvg12D6AqsbDcFe8hWK9Rtven1Oh3rC BbXMPiTHs3X12x9aq2GsJ6ULmMt1ngudYnDhUKVV0F9bEaR4a1f9N1v4AAAZzlCc1q7F j2+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=jAGbXuW9EOQocKnMeFwzotm5hyeokMtvrVXa6xWAXvk=; b=u4qhdtg9D9pmPkTWGYmRgGExgc9GHNf/1/UAQVhM8FXjeD6Htq0m6CnvmP0ntI+lJV MmxsWLHHcPAmn4CMTEeaqRKva2Nb+xvdhj2cWIDm8Y8HEVPy3jmqCyb5O0AMm8dBlNgI zLh5nFXUNP1CqN1bFKNUH9A+6yqEinRs4qCllEpKZ+Qz/sJv05g2OTralQCD2u5vm3U4 ojR2faL2ckdo0Mf5MYhUDBaQEZXC2p34YCoR6boG4BqZksqY69H32brgP2eZWgXz/Idq pZQBGKPgAAlq2u1R8UtiDKcePgWoACNfAweQ8+pGdOZrZiB1kPbdpJfMJUxrR/hpP9Zi Bfjg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t18-v6si9335380plo.407.2018.03.05.08.34.20; Mon, 05 Mar 2018 08:34:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752972AbeCEQag (ORCPT + 99 others); Mon, 5 Mar 2018 11:30:36 -0500 Received: from mga04.intel.com ([192.55.52.120]:44459 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752401AbeCEQ01 (ORCPT ); Mon, 5 Mar 2018 11:26:27 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Mar 2018 08:26:27 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,427,1515484800"; d="scan'208";a="23065199" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga006.jf.intel.com with ESMTP; 05 Mar 2018 08:26:24 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 739B1531; Mon, 5 Mar 2018 18:26:20 +0200 (EET) From: "Kirill A. Shutemov" To: Ingo Molnar , x86@kernel.org, Thomas Gleixner , "H. Peter Anvin" , Tom Lendacky Cc: Dave Hansen , Kai Huang , linux-kernel@vger.kernel.org, linux-mm@kvack.org, "Kirill A. Shutemov" Subject: [RFC, PATCH 13/22] mm, rmap: Free encrypted pages once mapcount drops to zero Date: Mon, 5 Mar 2018 19:26:01 +0300 Message-Id: <20180305162610.37510-14-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180305162610.37510-1-kirill.shutemov@linux.intel.com> References: <20180305162610.37510-1-kirill.shutemov@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Freeing encrypted pages may require special treatment such as flush cache to avoid aliasing. Anonymous pages cannot be mapped back once the last mapcount is gone. That's a good place to add hook to free encrypted page. At later point we may not have valid anon_vma around to get KeyID. Signed-off-by: Kirill A. Shutemov --- include/linux/mm.h | 1 + mm/rmap.c | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 7a4285f09c99..7ab5e39e3195 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1981,6 +1981,7 @@ extern void mem_init_print_info(const char *str); extern void reserve_bootmem_region(phys_addr_t start, phys_addr_t end); extern void prep_encrypt_page(struct page *page, gfp_t gfp, unsigned int order); +extern void free_encrypt_page(struct page *page, int keyid, unsigned int order); /* Free the reserved page into the buddy system, so it gets managed. */ static inline void __free_reserved_page(struct page *page) diff --git a/mm/rmap.c b/mm/rmap.c index c0470a69a4c9..4bff992fc106 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -81,6 +81,21 @@ static inline void arch_anon_vma_init(struct anon_vma *anon_vma, } #endif +#ifndef anon_vma_encrypted +static inline bool anon_vma_encrypted(struct anon_vma *anon_vma) +{ + return false; +} +#endif + +#ifndef anon_vma_keyid +static inline int anon_vma_keyid(struct anon_vma *anon_vma) +{ + BUILD_BUG(); + return 0; +} +#endif + static inline struct anon_vma *anon_vma_alloc(struct vm_area_struct *vma) { struct anon_vma *anon_vma; @@ -1258,6 +1273,7 @@ static void page_remove_file_rmap(struct page *page, bool compound) static void page_remove_anon_compound_rmap(struct page *page) { + struct anon_vma *anon_vma; int i, nr; if (!atomic_add_negative(-1, compound_mapcount_ptr(page))) @@ -1292,6 +1308,12 @@ static void page_remove_anon_compound_rmap(struct page *page) __mod_node_page_state(page_pgdat(page), NR_ANON_MAPPED, -nr); deferred_split_huge_page(page); } + + anon_vma = page_anon_vma(page); + if (anon_vma_encrypted(anon_vma)) { + int keyid = anon_vma_keyid(anon_vma); + free_encrypt_page(page, keyid, compound_order(page)); + } } /** @@ -1303,6 +1325,9 @@ static void page_remove_anon_compound_rmap(struct page *page) */ void page_remove_rmap(struct page *page, bool compound) { + struct page *head; + struct anon_vma *anon_vma; + if (!PageAnon(page)) return page_remove_file_rmap(page, compound); @@ -1323,8 +1348,13 @@ void page_remove_rmap(struct page *page, bool compound) if (unlikely(PageMlocked(page))) clear_page_mlock(page); - if (PageTransCompound(page)) - deferred_split_huge_page(compound_head(page)); + head = compound_head(page); + if (PageTransHuge(head)) + deferred_split_huge_page(head); + + anon_vma = page_anon_vma(head); + if (anon_vma_encrypted(anon_vma)) + free_encrypt_page(page, anon_vma_keyid(anon_vma), 0); /* * It would be tidy to reset the PageAnon mapping here, -- 2.16.1