From patchwork Thu Aug 29 22:24:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13783968 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 962B3CA0EC8 for ; Thu, 29 Aug 2024 22:24:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B58F6B0088; Thu, 29 Aug 2024 18:24:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 637106B0092; 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 412D96B008A; Thu, 29 Aug 2024 18:24:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1D3FC6B0089 for ; Thu, 29 Aug 2024 18:24:29 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id CD7B8AA16E for ; Thu, 29 Aug 2024 22:24:28 +0000 (UTC) X-FDA: 82506713016.14.677CCDE Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by imf04.hostedemail.com (Postfix) with ESMTP id 9A3EE40005 for ; Thu, 29 Aug 2024 22:24:26 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=fM2sBu6R; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf04.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.168.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724970167; 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=nqQKk6MOQFV+1+R4/XWf2oHD5kh81okffsDtSVIGLYM=; b=mEUacCvqSUbniORCrSxdeIa4kyFeBrpz/bk4TeAk4elbWMMgmphM3sEUQPmxhVsT20NBqt 6jEjTZQxVxp7aTLmKz7BfDYYhAhU3GiFla8n6vDCoMUxVJV3OliqTZWiLwnyGwAa77nwuA etNBXO83OHASYe/i9+1stteBgaxu8Pc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724970167; a=rsa-sha256; cv=none; b=FBNgwHicQUSQLoqTPIvDdQv07IPj8y0muGCMJ7iexV7FvaNTkc5tke1k48xIm7ddgjFlv8 4NPYIr9CczVpL9wU+LTtyg/ilpe2rZhxygoIeWVNtbijpMO2LaBzklXdJn+V5lk/l1Mx7p r8HDI/LTBu8n7ikkw/q2nbIY7vHuY/s= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=fM2sBu6R; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf04.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.168.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com Received: from pps.filterd (m0279867.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 47THw8rh019432; 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= nqQKk6MOQFV+1+R4/XWf2oHD5kh81okffsDtSVIGLYM=; b=fM2sBu6RSAMa+ucj chXJ2MezexBJDEBCe/1qL3cvNUY38wxUpB840yteHKfkLEtg+rmzWTVHaTPZPZDs 9lFDjTsCSD2R9C4P0eLlBMp8QeB2Fk6GFvXj8b8F6RPJ/DTwgrERrbtJejUYUG46 B5rXXJlJmNctWJpmTZYM37VyeP5YEKq+G+Kv6rn8lC0X8fBBkUun/avqj308QRK0 WY9ALgZjDJGr96vtS26rOpacPGJ9OG5q31ntkRPb1d6cqZ6SendYtAMCWMMKvcny XvcnI+HZWGzbxJFZZZ4QGInrsZzV193YMdFePOc0RKx9QhA2zNSecMavVvbTVGwz XcJodA== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 419putpu29-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 29 Aug 2024 22:24:13 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 47TMODPG023594 (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:12 -0700 From: Elliot Berman Date: Thu, 29 Aug 2024 15:24:12 -0700 Subject: [PATCH RFC v2 4/5] mm: guest_memfd: Add ability for userspace to mmap pages MIME-Version: 1.0 Message-ID: <20240829-guest-memfd-lib-v2-4-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-GUID: YoBcGu8s-izQ3-pSoL8-J8hiQQsRjEyp X-Proofpoint-ORIG-GUID: YoBcGu8s-izQ3-pSoL8-J8hiQQsRjEyp 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 clxscore=1015 lowpriorityscore=0 mlxscore=0 phishscore=0 adultscore=0 priorityscore=1501 impostorscore=0 malwarescore=0 suspectscore=0 spamscore=0 bulkscore=0 mlxlogscore=882 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2408290158 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9A3EE40005 X-Stat-Signature: gfcwjom5te9nsqk7c83dcmi9kz1q5mwc X-Rspam-User: X-HE-Tag: 1724970266-117708 X-HE-Meta: U2FsdGVkX1/z9MyESNhFVM1LZSMLtqjVNTrr4tJ1fcYRy2Fre7mGGXbxFPpY79yjNCXJd+gdWmdQNEoApFv4Al0hAjEyAFu+h+9xDnn9wg7swPwWkvWuG4jww3WeyXXuAauiWq+mINPDNaJdrdSQX3RUa0ThIY7d6MaarOL4JHFaK7gg4gPOQx4lcmypWy4G2FV+UoFcVrr+SGGJ9H5Kcvuad+em9pmE3OaIhJ0eOSs1WY507172ovp7tb1ZZ9A199aZLIiDjhcUup5N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2R0apeRdFLYZJvj14bZw== 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: "Inaccessible" and "accessible" state are properly tracked by the guest_memfd. Userspace can now safely access pages to preload binaries in a hypervisor/architecture-agnostic manner. Signed-off-by: Elliot Berman --- mm/guest_memfd.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/mm/guest_memfd.c b/mm/guest_memfd.c index 62cb576248a9d..194b2c3ea1525 100644 --- a/mm/guest_memfd.c +++ b/mm/guest_memfd.c @@ -279,6 +279,51 @@ int guest_memfd_make_inaccessible(struct folio *folio) } EXPORT_SYMBOL_GPL(guest_memfd_make_inaccessible); +static vm_fault_t gmem_fault(struct vm_fault *vmf) +{ + struct file *file = vmf->vma->vm_file; + struct guest_memfd_private *private; + struct folio *folio; + + folio = guest_memfd_grab_folio(file, vmf->pgoff, GUEST_MEMFD_GRAB_ACCESSIBLE); + if (IS_ERR(folio)) + return VM_FAULT_SIGBUS; + + vmf->page = folio_page(folio, vmf->pgoff - folio_index(folio)); + + /** + * Drop the safe and accessible references, the folio refcount will + * be preserved and unmap_mapping_folio() will decrement the + * refcount when converting to inaccessible. + */ + private = folio_get_private(folio); + atomic_dec(&private->accessible); + atomic_dec(&private->safe); + + return VM_FAULT_LOCKED; +} + +static const struct vm_operations_struct gmem_vm_ops = { + .fault = gmem_fault, +}; + +static int gmem_mmap(struct file *file, struct vm_area_struct *vma) +{ + const struct guest_memfd_operations *ops = file_inode(file)->i_private; + + if (!ops->prepare_accessible) + return -EPERM; + + /* No support for private mappings to avoid COW. */ + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) != + (VM_SHARED | VM_MAYSHARE)) + return -EINVAL; + + file_accessed(file); + vma->vm_ops = &gmem_vm_ops; + return 0; +} + static long gmem_punch_hole(struct file *file, loff_t offset, loff_t len) { struct inode *inode = file_inode(file); @@ -390,6 +435,7 @@ static int gmem_release(struct inode *inode, struct file *file) static const struct file_operations gmem_fops = { .open = generic_file_open, .llseek = generic_file_llseek, + .mmap = gmem_mmap, .release = gmem_release, .fallocate = gmem_fallocate, .owner = THIS_MODULE,