From patchwork Thu Aug 29 22:24:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13783969 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D0A5CA0EC0 for ; Thu, 29 Aug 2024 22:24:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C79796B0089; Thu, 29 Aug 2024 18:24:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C02DF6B008A; Thu, 29 Aug 2024 18:24:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7D286B0093; Thu, 29 Aug 2024 18:24:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7E0616B0089 for ; Thu, 29 Aug 2024 18:24:29 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 263DC804F3 for ; Thu, 29 Aug 2024 22:24:29 +0000 (UTC) X-FDA: 82506713058.20.DF5A466 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by imf30.hostedemail.com (Postfix) with ESMTP id D734480019 for ; Thu, 29 Aug 2024 22:24:26 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=eah9mlFU; spf=pass (imf30.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.168.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724970148; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OikWV/zxhCjbqlu1U3SrGEvR6lLB0Wn4slbdklJYTbQ=; b=K66h8Qns4j1maK2/E/UkIJ162u8vQRaWNZI8INc+cBmjhxL71PEEoeuraaPvDCoYcCXt5o jRewOD8Fzn/v6FDSJ47C2+Y3APpG43GvrXC93cXPzQksB/qoGQjaZbnyDoZBe6EFRvwwHr dDKAa8bOFoqmkoZnHpr+5pIBI9bZD00= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=eah9mlFU; spf=pass (imf30.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.168.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724970148; a=rsa-sha256; cv=none; b=EXqdlCk1/qrwMePJR8Gs9KjPLHOFCoa4jCcNNcw7xQpz6NLhSq6m+ijcDQlJE4A31oVjMS XQvwbA1X3EiUWzlD1u8FwZpcO6UuP2f1wESbcaFlNg/xfto7BuLKqQHpHwYTPAwX1szC4F 97da/3+Mf/L+RPOJH9ZB5UD+/7yoha4= Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 47THWFOG026564; Thu, 29 Aug 2024 22:24:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= OikWV/zxhCjbqlu1U3SrGEvR6lLB0Wn4slbdklJYTbQ=; b=eah9mlFUiP8U2VPm C2fRIGHbALCXiJ2dg5iiSTWWGrrJG4hcOVmBlzZQgz9WSEhILxnJNru1yrxSn/N+ xGK7RThKNRyd17la0q+19UxJSgOgSUyq5gNFlqQknwFlNRs9AvBVj8NmN4av+7Qp iuZzrb6HZ5WDmEhtpI2G5/R8Y4SNFfloKCL9byLSHyI7Zz7lTJOfcYn9jjg/3lXk FWJ4eDnf5LeiXzF1rLop15EAggNmJNqP68Tlg83dpshOqp83pqH1PQqx/UBtaI5o im5wW4b9CGjV6n8QVowjQtqOq5GaczfOntucqC085rm0NnZRr6azGqK4ixGlZA/X NM6uuA== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 419puvesp0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 29 Aug 2024 22:24:14 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA05.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 47TMOD6e014638 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 29 Aug 2024 22:24:13 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Thu, 29 Aug 2024 15:24:13 -0700 From: Elliot Berman Date: Thu, 29 Aug 2024 15:24:13 -0700 Subject: [PATCH RFC v2 5/5] mm: guest_memfd: Add option to remove inaccessible memory from direct map MIME-Version: 1.0 Message-ID: <20240829-guest-memfd-lib-v2-5-b9afc1ff3656@quicinc.com> References: <20240829-guest-memfd-lib-v2-0-b9afc1ff3656@quicinc.com> In-Reply-To: <20240829-guest-memfd-lib-v2-0-b9afc1ff3656@quicinc.com> To: Andrew Morton , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Fuad Tabba , David Hildenbrand , Patrick Roy , , Ackerley Tng , Mike Rapoport , , "H. Peter Anvin" CC: , , , , , Elliot Berman X-Mailer: b4 0.14.1 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: VKocc11VXrfwDcfhSZyfG1SnIgFpRfUU X-Proofpoint-GUID: VKocc11VXrfwDcfhSZyfG1SnIgFpRfUU X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-29_06,2024-08-29_02,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 mlxlogscore=721 priorityscore=1501 bulkscore=0 impostorscore=0 adultscore=0 malwarescore=0 phishscore=0 lowpriorityscore=0 mlxscore=0 spamscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2408290158 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D734480019 X-Stat-Signature: bmjfbewozg4rwuisy8b1bnnj1u7iy9sr X-Rspam-User: X-HE-Tag: 1724970266-139550 X-HE-Meta: U2FsdGVkX1+Xfy4C8uCL1dlbyu9fsR+Q5xsuX+QQpWC6PkYB1FEUDxTykNYQiNz1gscaMKfaajf1+Za3Xrx2XTnPtGDGefKwN5xEy5nOjVsCe1/H1++MZfzjCnKzOsH/QyQH3Xypc+mHTgoW38sB2wvFqxBjRdVuo6N46FWNySw4H3S9l2iP2U6Y1GIGsZMkAJ6xl1tfRbZvW84OhO1xh5Qc7j0+oFmv3iN2NP9NkYIUMLhGcaoaghHtUpBfyZDeSsu1d1z7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nn7XjVoVG 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: List-Subscribe: List-Unsubscribe: When memory is made inaccessible to the host, Linux may still speculatively access the folio if a load_unaligned_zeropad is performed at the end of the prior page. To ensure Linux itself catches such errors without hypervisor crashing Linux, unmap the guest-inaccessible pages from the direct map. This feature is made optional because arm64 pKVM can provide a special, detectable fault which can be fixed up directly. Signed-off-by: Elliot Berman --- include/linux/guest_memfd.h | 1 + mm/guest_memfd.c | 79 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/include/linux/guest_memfd.h b/include/linux/guest_memfd.h index 66e5d3ab42613..de53bce15db99 100644 --- a/include/linux/guest_memfd.h +++ b/include/linux/guest_memfd.h @@ -33,6 +33,7 @@ enum guest_memfd_grab_flags { enum guest_memfd_create_flags { GUEST_MEMFD_FLAG_CLEAR_INACCESSIBLE = (1UL << 0), + GUEST_MEMFD_FLAG_REMOVE_DIRECT_MAP = (1UL << 1), }; struct folio *guest_memfd_grab_folio(struct file *file, pgoff_t index, u32 flags); diff --git a/mm/guest_memfd.c b/mm/guest_memfd.c index 194b2c3ea1525..d4232739d4c5b 100644 --- a/mm/guest_memfd.c +++ b/mm/guest_memfd.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "internal.h" @@ -26,6 +27,45 @@ struct guest_memfd_private { atomic_t safe; }; +static inline int folio_set_direct_map_invalid_noflush(struct folio *folio) +{ + unsigned long i, nr = folio_nr_pages(folio); + int r; + + for (i = 0; i < nr; i++) { + struct page *page = folio_page(folio, i); + + r = set_direct_map_invalid_noflush(page); + if (r) + goto out_remap; + } + /** + * Currently no need to flush as hypervisor will also be flushing + * tlb when giving the folio to guest. + */ + + return 0; +out_remap: + for (; i > 0; i--) { + struct page *page = folio_page(folio, i - 1); + + BUG_ON(set_direct_map_default_noflush(page)); + } + + return r; +} + +static inline void folio_set_direct_map_default_noflush(struct folio *folio) +{ + unsigned long i, nr = folio_nr_pages(folio); + + for (i = 0; i < nr; i++) { + struct page *page = folio_page(folio, i); + + BUG_ON(set_direct_map_default_noflush(page)); + } +} + static inline int base_safe_refs(struct folio *folio) { /* 1 for filemap */ @@ -131,6 +171,12 @@ struct folio *guest_memfd_grab_folio(struct file *file, pgoff_t index, u32 flags goto out_free; } } else { + if (gmem_flags & GUEST_MEMFD_FLAG_REMOVE_DIRECT_MAP) { + r = folio_set_direct_map_invalid_noflush(folio); + if (r < 0) + goto out_free; + } + if (ops->prepare_inaccessible) { r = ops->prepare_inaccessible(inode, folio); if (r < 0) @@ -203,6 +249,7 @@ int guest_memfd_make_accessible(struct folio *folio) struct guest_memfd_private *private = folio_get_private(folio); struct inode *inode = folio_inode(folio); struct guest_memfd_operations *ops = inode->i_private; + unsigned long gmem_flags; int r; /* @@ -218,6 +265,10 @@ int guest_memfd_make_accessible(struct folio *folio) if (!r) return -EBUSY; + gmem_flags = (unsigned long)inode->i_mapping->i_private_data; + if (gmem_flags & GUEST_MEMFD_FLAG_REMOVE_DIRECT_MAP) + folio_set_direct_map_default_noflush(folio); + if (ops->prepare_accessible) { r = ops->prepare_accessible(inode, folio); if (r) @@ -248,6 +299,7 @@ int guest_memfd_make_inaccessible(struct folio *folio) struct guest_memfd_private *private = folio_get_private(folio); struct inode *inode = folio_inode(folio); struct guest_memfd_operations *ops = inode->i_private; + unsigned long gmem_flags; int r; r = atomic_dec_if_positive(&private->accessible); @@ -266,6 +318,13 @@ int guest_memfd_make_inaccessible(struct folio *folio) goto err; } + gmem_flags = (unsigned long)inode->i_mapping->i_private_data; + if (gmem_flags & GUEST_MEMFD_FLAG_REMOVE_DIRECT_MAP) { + r = folio_set_direct_map_invalid_noflush(folio); + if (r) + goto err; + } + if (ops->prepare_inaccessible) { r = ops->prepare_inaccessible(inode, folio); if (r) @@ -454,6 +513,7 @@ static int gmem_error_folio(struct address_space *mapping, struct folio *folio) struct guest_memfd_operations *ops = inode->i_private; off_t offset = folio->index; size_t nr = folio_nr_pages(folio); + unsigned long gmem_flags; int ret; filemap_invalidate_lock_shared(mapping); @@ -464,6 +524,10 @@ static int gmem_error_folio(struct address_space *mapping, struct folio *folio) filemap_invalidate_unlock_shared(mapping); + gmem_flags = (unsigned long)inode->i_mapping->i_private_data; + if (gmem_flags & GUEST_MEMFD_FLAG_REMOVE_DIRECT_MAP) + folio_set_direct_map_default_noflush(folio); + return ret; } @@ -474,7 +538,7 @@ static bool gmem_release_folio(struct folio *folio, gfp_t gfp) struct guest_memfd_operations *ops = inode->i_private; off_t offset = folio->index; size_t nr = folio_nr_pages(folio); - unsigned long val, expected; + unsigned long val, expected, gmem_flags; int ret; ret = ops->invalidate_begin(inode, offset, nr); @@ -483,6 +547,10 @@ static bool gmem_release_folio(struct folio *folio, gfp_t gfp) if (ops->invalidate_end) ops->invalidate_end(inode, offset, nr); + gmem_flags = (unsigned long)inode->i_mapping->i_private_data; + if (gmem_flags & GUEST_MEMFD_FLAG_REMOVE_DIRECT_MAP) + folio_set_direct_map_default_noflush(folio); + expected = base_safe_refs(folio); val = atomic_read(&private->safe); WARN_ONCE(val != expected, "folio[%x] safe ref: %d != expected %d\n", @@ -518,7 +586,14 @@ static inline bool guest_memfd_check_ops(const struct guest_memfd_operations *op static inline unsigned long guest_memfd_valid_flags(void) { - return GUEST_MEMFD_FLAG_CLEAR_INACCESSIBLE; + unsigned long flags = GUEST_MEMFD_FLAG_CLEAR_INACCESSIBLE; + +#ifdef CONFIG_ARCH_HAS_SET_DIRECT_MAP + if (can_set_direct_map()) + flags |= GUEST_MEMFD_FLAG_REMOVE_DIRECT_MAP; +#endif + + return flags; } /**