From patchwork Mon Sep 16 16:57:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivank Garg X-Patchwork-Id: 13805651 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 64000C3ABB2 for ; Mon, 16 Sep 2024 16:58:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C38456B008C; Mon, 16 Sep 2024 12:58:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE8276B0092; Mon, 16 Sep 2024 12:58:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3B026B0093; Mon, 16 Sep 2024 12:58:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 80BE46B008C for ; Mon, 16 Sep 2024 12:58:35 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 303B7C061D for ; Mon, 16 Sep 2024 16:58:35 +0000 (UTC) X-FDA: 82571210190.03.FA3F55E Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2054.outbound.protection.outlook.com [40.107.220.54]) by imf11.hostedemail.com (Postfix) with ESMTP id 171CF40014 for ; Mon, 16 Sep 2024 16:58:31 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=YkW338mG; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf11.hostedemail.com: domain of shivankg@amd.com designates 40.107.220.54 as permitted sender) smtp.mailfrom=shivankg@amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1726505881; a=rsa-sha256; cv=pass; b=bt9zkYMVcevBv9phkHDNlAYDIdjoA7igfzRYPM1vjYLPuqYROkJseMmuki8IvrahFL9IeQ tnuj8BjhVovxySy5Yo26qDp+ez5DyW6bGRAW7WXZTq4RfgF3O8dXcg5gKWbZCc1h/aXM8R AlSIoOM72MI02B0UTak+axXEY5PQfeg= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=YkW338mG; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf11.hostedemail.com: domain of shivankg@amd.com designates 40.107.220.54 as permitted sender) smtp.mailfrom=shivankg@amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726505881; 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=5wOCBU41KU/9jYAjuxEnaob8uAuZTVT2xJKX1zAfNJ4=; b=zQfKHv7Qdd8dQYHiZO4/NGyZFaw+4OGJh6DuUpWOp+gumq1YSemHG5BEjEerKoQlrJviku FjO+y5OTiKBob+24SgtOY79tXYUBkVC1rrRt6AvnuPDknZSgRbb5bU2kGaGxIwapQNRYaa o5vswdTamV87joTsGlRTE9+dGsUCKy4= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=v5y6xQPiUWn1DkHwR7oPqcS4vfNLDWC0FGJJDw48UDdk/1KWPt2iQeRU9+UoTXqzxdYZSYm8nRKmgRSmIu6nRgFqSepV1MzuZLT6XGer1fUfiaQS0VSP7K4b0QGuegIq0/+vfR5hjnrgK6aalgKM83q7H43Iqme0CDRAh3t7i39Ss7EUXVvDpypyRtQAghverH/WqIEfVgmG8B6DrTVG3qiHKsn3uxtf+KCYkSaP2ViwVR4/ykDNInD8vzFO7Ma+FDWJMmH4DXzZk2ROIxn83KMoXJZdUqaVPU38bNp7/u01g2SX3fJGheGTLyXhPu3EKU6V4ESHG9w9IUoqY90Pqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5wOCBU41KU/9jYAjuxEnaob8uAuZTVT2xJKX1zAfNJ4=; b=DTWFQBBZJO6MXARF6+3FX8YDTifPLsd3rl8u3iTh0MThXxIMSyW+r0mlmYJGrcj+NJ5emH2YtrVYRuYTXu7vLKVKFqGGvsZel59yKa9xCWRtNoMxSSIez30v5VWUvyw6U7d8BJj/s61j/qn8GrZ9Y8xnNNjRpfd+kCqgVLWlOsaig1H9xxLLBtSqq6Vu+y1qaZAxNTCgtVydKq9qZ75CHX19ICE2U+dYh0qzpQqaPKW1RVAWCSnbo8Za47rHGiIRjUWNpPjfdwaAjMkosubbK2Dm6paV0KS2N38xj6V+mtu1HgQBpAX3vizBFOrLvJLfhLMIVE/ytSp1zW/cFSRoHA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5wOCBU41KU/9jYAjuxEnaob8uAuZTVT2xJKX1zAfNJ4=; b=YkW338mGtmM/rP8DoGGhHvxaaWZyPz217gv0nhKYa607H8Xy4KVzPe03YaOw+H/BDKVtp1Y07yERDK+s4LM0RYm1qVch3XeLPumE8o3FsUhrKzBAGjTij9ZS7DtEPLYhoxYx8wwBnYArgHvPcPU5A2lwIJkr+fk4iZB1XGl6WqU= Received: from SN7P222CA0002.NAMP222.PROD.OUTLOOK.COM (2603:10b6:806:124::14) by CH3PR12MB8725.namprd12.prod.outlook.com (2603:10b6:610:170::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Mon, 16 Sep 2024 16:58:26 +0000 Received: from SN1PEPF0002529E.namprd05.prod.outlook.com (2603:10b6:806:124:cafe::8a) by SN7P222CA0002.outlook.office365.com (2603:10b6:806:124::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.30 via Frontend Transport; Mon, 16 Sep 2024 16:58:25 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SN1PEPF0002529E.mail.protection.outlook.com (10.167.242.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Mon, 16 Sep 2024 16:58:25 +0000 Received: from kaveri.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Mon, 16 Sep 2024 11:58:19 -0500 From: Shivank Garg To: , , , CC: , , , , , , , , , , , , , Subject: [PATCH RFC 1/3] KVM: guest_memfd: Extend creation API to support NUMA mempolicy Date: Mon, 16 Sep 2024 16:57:41 +0000 Message-ID: <20240916165743.201087-2-shivankg@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240916165743.201087-1-shivankg@amd.com> References: <20240916165743.201087-1-shivankg@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF0002529E:EE_|CH3PR12MB8725:EE_ X-MS-Office365-Filtering-Correlation-Id: f9f062da-5119-462f-3b16-08dcd670c77f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|36860700013|1800799024|7416014|82310400026; X-Microsoft-Antispam-Message-Info: sZuALtDDyE42Hp6mjhfDVXmW9wXL+1lX5Uq8rwB9yvdceyK1wqkKEghwGTaLtrvdIVuxgxIIUSXVsl9ZPKfe/nCYQV0lHtAUFcmscwNm/6LHip/SHO4TKjLCSIcFcUpJvKpSOlc8B0ya680oyKcaPHVGhpj8DLerjhMo2vYcWqE5kSvkiywSJh2ETgdXnYzSnS5QYO2DI3DkwYNef2ZCsgKP6dPDpKyKnTK4Q67wuh6jTV+YerJSdCqt9tRdDZUEt4ioj5TTuclVgFiygOtiiouhZsh/fF91HRO16drBeVNZG0TSrpXPc/8qAj28TTA2w1hgkzNQBg8M1K0j1QuQ0unvBuM5+Ez9quCiMOVPI2ESXF/m2LOkyPz8Ymu1SQpk0/myqoKeoX3nCCV+P+/1GezdzCvjS5aBb+kWEsDX8b6xzzZTGuzVIRRgGlZkogeQfaXuDi1DX6zf7yVhR1h9xZjW2fmq42SK1MCnv4qJT43ZTOOqH0xkvo1ewgiscEOjCTzzXX1lSorVv/YrWcKQAA4HbwsTVqnmb2ySKgGxXtKGPdO729SWn678wCf8k3ZoBTsYA9gBrgggMjw3RjWlGpsvFEBemPS3GfysMlcLDX93nbG482gZZokSFjk/zVrT1LFAz5l4exT7QPVxHAoyvk0BBM+7G2VtDz1xAhMaEZDDFR0TtqUBEC6pQHGCQfF8wh5VadFrMLxSTN3Hi9/EHcLbvUEUbUMCZfmI9x+gupLhVhw8T1yP5OzymP1ayYD90Rc1IdKdRyAb0zNsgRDO5SkkiLb6oZeJ1Lv9FOUmAouS+Ir+mAVNQj8IqIsiDgolp3d1cmaQxkePFUfKPPnNv2a/YfFESrI3o+jWyn7/RJIpbIhDhZgPEhuGJpNIHBAnCGRryQER3GOmG9Dhw87WPN8XhQ6QsIq6cC2+S3PdNsQJUY/ozA1f4vSt5ALM4DP39jD/aSFspzFa78EZC0QN7YB30e7eQnAk27yjYtMzToWD35yQ7bt6iEYU39U2XyAP8zhXZeX4k/Ae+IxjkgtBCateatGS+sHif8rCGFA9dzHS88Y3L9pjKB+bfpraeNx3Hs0iUwvtK4A+JT8D5RlH77OMW/wg4vdE7+d3eiTG5bBQRyHWrexkHSfjvvJOU2fWzMuJlzWNscK/SBir4dYzigCgLTqTS2E4giIffOkWmmIgEiNRYpYErfFCuufPKyNd0ya037HKzaAMo87w65JkqBb+XPeafNHBRlvmMGey0X3grGRVFjULDzT0Inaox9es7rzYJwWhwkx5vG1jRamD97RJtzhPCIPZSeqrWzSmlPpMSOL22n6us0EzpagR8UfoXRVJ3VYrC4akyTLzV9E3qXntHpwvvc6GwZjKaqD+uR5UUsbQKJnO863Flokl7t7BCH/dxUJFvG1LfXuZzRisSpQdnLSVZUQm5F+/pVOzQO64qVhwQSiF4LnrMUG1H5UH X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(36860700013)(1800799024)(7416014)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Sep 2024 16:58:25.0515 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f9f062da-5119-462f-3b16-08dcd670c77f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF0002529E.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8725 X-Rspam-User: X-Rspamd-Queue-Id: 171CF40014 X-Rspamd-Server: rspam01 X-Stat-Signature: 5mjfupbse5ipgtngrcth9sqpzze7pf3s X-HE-Tag: 1726505911-231818 X-HE-Meta: U2FsdGVkX1/S1SubU9ZMJmH+FFfEr/QMk6YZQHBYp691LDHbWHMjQls4NMyHQqPKiQ6fZoTUUzfQafFgq5KrGC8vtIktP8jCMAYerKHVamqw8rHIhmu9RwPfRfsElrfF/RkbLwJsjPUKddgdcdLcfp+yw7wS3/FCzwxV0dvaOSsoyBigDLgjwoz6yP1VdBOFnf9kPAhyEA4GOoFzQNPmLFe+F2nkRBt+So7Q6DJAg4GsYkDhOjwA9GYx2zQ3DwgeKmUtNPQ+E1+dTQe38TX63Ve4MYOMpXgJmuavcVRAEHbHDBq7k6tVS1yNbtX80UdWF6M4ycKE+TmiFNxxFpa4V5xNh3cIolZsJNCDXrcWd7LuB7HxL1qsjhV/yVyi9h95I7Tz+7fun56iS3s1C2oHGhofBB5vDeNebbYuo1Rqw7nkwmafhf1FgP6/9uc0wtLlib01j1e8tkHbvzJlPqiKjb/RMp+Pq1Nzbo1ygEquXi9/MHfYOz9enuQRzVGsAfFdJOTRXWKzEHTe8g30eSoDcQhCZ2QERZt8e2k/8bOReUn85FdsPcjFIcxrkaHkubdYOCobdUNAW72RwQkEbqDBjVnKcR4uZVj4DB1gp5Kjm3LF62QqfunRtqz+rcnsyfiEnXl3rXPBJgdhlkykmazkkPrLeHRmOGyoWyF/2WevAPJ6Z9g5j+JczAXw92caoEj6PPR8l9jx4ZE4Z+lCQHb/MAHV7TsttkS/wqREsN4WOVIotewBQ1QyvHeNZm2sdZOQzXXax8e0+T6BnAdz5PDhnyGsY8Ut4RnlGYFbwcOE17CXyneOtm50YW2OdSh/vzT2SD/jDv3DeMzTY217GCfFTuqgrlj2ophDIwIlgpXxAUWvVdQE4PnydY+dKOUDtBy2rL1Bqa1nn8UbieXwFg57fbl8q17RVZpuXZVFP1BVQvdpWcPCAHCBWlTalgSxeU+dz1EWAV/+wnSyFiBhgsP AxOP5+DJ h4wauT1AXGft+6+iI5KXBds02X17cIMSBxywjB/0aJr926hP9P7YXHlxJrxNusXQJEqy+g1WCWg49A4pe0Yah1OswIDG2TJ50DYQJL+ZesR2UDrNjGGw3QQKPKYWbHi1JLuiN80apDO4QQiBX81uRRELNtrAbxt9iEHNi/IQ6Sy7mdPzH5cTu9/k12HX4KCZ1MBjdB0b5Oo83miBOR7Qkt5CKWUOoSWkrw0D/I3WIZKXUDhZbKJDH4MgV/k5j9/rsS67kYSWpWv0M13FSmpXvdX60IWYTP5BFfsqigmSZ8GRKmKgkZk15NAUr2SG4zj3FH5c5iAZkh9AuZTOBg9ty9c+tfJvg9Ms6CaJtaIPKBmKtutZiwLbhdw0zQuHnQIAb0j+VTOlUKeyteUBHUxuEKhAC7JNF+qHM+C8EYNzyyeQx4+/JYfVL3lYBF3cGGFySzDltGit8C3pwkcQ= 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: From: Shivansh Dhiman Extend the API of creating guest-memfd to introduce proper NUMA support, allowing VMM to set memory policies effectively. The memory policy defines from which node memory is allocated. The current implementation of KVM guest-memfd does not honor the settings provided by VMM. While mbind() can be used for NUMA policy support in userspace applications, it is not functional for guest-memfd as the memory is not mapped to userspace. Currently, SEV-SNP guest use guest-memfd as a memory backend and would benefit from NUMA support. It enables fine-grained control over memory allocation, optimizing performance for specific workload requirements. To apply memory policy on a guest-memfd, extend the KVM_CREATE_GUEST_MEMFD IOCTL with additional fields related to mempolicy. - mpol_mode represents the policy mode (default, bind, interleave, or preferred). - host_nodes_addr denotes the userspace address of the nodemask, a bit mask of nodes containing up to maxnode bits. - First bit of flags must be set to use mempolicy. Store the mempolicy struct in i_private_data of the memfd's inode, which is currently unused in the context of guest-memfd. Signed-off-by: Shivansh Dhiman Signed-off-by: Shivank Garg --- Documentation/virt/kvm/api.rst | 13 ++++++++- include/linux/mempolicy.h | 4 +++ include/uapi/linux/kvm.h | 5 +++- mm/mempolicy.c | 52 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/kvm.h | 5 +++- virt/kvm/guest_memfd.c | 21 ++++++++++++-- virt/kvm/kvm_mm.h | 3 ++ 7 files changed, 97 insertions(+), 6 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index b3be87489108..dcb61282c773 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6346,7 +6346,10 @@ and cannot be resized (guest_memfd files do however support PUNCH_HOLE). struct kvm_create_guest_memfd { __u64 size; __u64 flags; - __u64 reserved[6]; + __u64 host_nodes_addr; + __u16 maxnode; + __u8 mpol_mode; + __u8 reserved[37]; }; Conceptually, the inode backing a guest_memfd file represents physical memory, @@ -6367,6 +6370,14 @@ a single guest_memfd file, but the bound ranges must not overlap). See KVM_SET_USER_MEMORY_REGION2 for additional details. +NUMA memory policy support for KVM guest_memfd allows the host to specify +memory allocation behavior for guest NUMA nodes, similar to mbind(). If +KVM_GUEST_MEMFD_NUMA_ENABLE flag is set, memory allocations from the guest +will use the specified policy and host-nodes for physical memory. +- mpol_mode refers to the policy mode: default, preferred, bind, interleave, or + preferred. +- host_nodes_addr points to bitmask of nodes containing up to maxnode bits. + 4.143 KVM_PRE_FAULT_MEMORY --------------------------- diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h index 1add16f21612..468eeda2ec2f 100644 --- a/include/linux/mempolicy.h +++ b/include/linux/mempolicy.h @@ -299,4 +299,8 @@ static inline bool mpol_is_preferred_many(struct mempolicy *pol) } #endif /* CONFIG_NUMA */ + +struct mempolicy *create_mpol_from_args(unsigned char mode, + const unsigned long __user *nmask, + unsigned short maxnode); #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 637efc055145..fda6cbef0a1d 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1561,7 +1561,10 @@ struct kvm_memory_attributes { struct kvm_create_guest_memfd { __u64 size; __u64 flags; - __u64 reserved[6]; + __u64 host_nodes_addr; + __u16 maxnode; + __u8 mpol_mode; + __u8 reserved[37]; }; #define KVM_PRE_FAULT_MEMORY _IOWR(KVMIO, 0xd5, struct kvm_pre_fault_memory) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index b858e22b259d..9e9450433fcc 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -3557,3 +3557,55 @@ static int __init mempolicy_sysfs_init(void) late_initcall(mempolicy_sysfs_init); #endif /* CONFIG_SYSFS */ + +#ifdef CONFIG_KVM_PRIVATE_MEM +/** + * create_mpol_from_args - create a mempolicy structure from args + * @mode: NUMA memory policy mode + * @nmask: bitmask of NUMA nodes + * @maxnode: number of bits in the nodes bitmask + * + * Create a mempolicy from given nodemask and memory policy such as + * default, preferred, interleave or bind. + * + * Return: error encoded in a pointer or memory policy on success. + */ +struct mempolicy *create_mpol_from_args(unsigned char mode, + const unsigned long __user *nmask, + unsigned short maxnode) +{ + struct mm_struct *mm = current->mm; + unsigned short mode_flags; + struct mempolicy *mpol; + nodemask_t nodes; + int lmode = mode; + int err = -ENOMEM; + + err = sanitize_mpol_flags(&lmode, &mode_flags); + if (err) + return ERR_PTR(err); + + err = get_nodes(&nodes, nmask, maxnode); + if (err) + return ERR_PTR(err); + + mpol = mpol_new(mode, mode_flags, &nodes); + if (IS_ERR_OR_NULL(mpol)) + return mpol; + + NODEMASK_SCRATCH(scratch); + if (!scratch) + return ERR_PTR(-ENOMEM); + + mmap_write_lock(mm); + err = mpol_set_nodemask(mpol, &nodes, scratch); + mmap_write_unlock(mm); + NODEMASK_SCRATCH_FREE(scratch); + + if (err) + return ERR_PTR(err); + + return mpol; +} +EXPORT_SYMBOL(create_mpol_from_args); +#endif diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h index e5af8c692dc0..e3effcd1e358 100644 --- a/tools/include/uapi/linux/kvm.h +++ b/tools/include/uapi/linux/kvm.h @@ -1546,7 +1546,10 @@ struct kvm_memory_attributes { struct kvm_create_guest_memfd { __u64 size; __u64 flags; - __u64 reserved[6]; + __u64 host_nodes_addr; + __u16 maxnode; + __u8 mpol_mode; + __u8 reserved[37]; }; #define KVM_PRE_FAULT_MEMORY _IOWR(KVMIO, 0xd5, struct kvm_pre_fault_memory) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index e930014b4bdc..8f1877be4976 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "kvm_mm.h" @@ -445,7 +446,8 @@ static const struct inode_operations kvm_gmem_iops = { .setattr = kvm_gmem_setattr, }; -static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) +static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags, + struct mempolicy *pol) { const char *anon_name = "[kvm-gmem]"; struct kvm_gmem *gmem; @@ -478,6 +480,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) inode->i_private = (void *)(unsigned long)flags; inode->i_op = &kvm_gmem_iops; inode->i_mapping->a_ops = &kvm_gmem_aops; + inode->i_mapping->i_private_data = (void *)pol; inode->i_mode |= S_IFREG; inode->i_size = size; mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); @@ -505,7 +508,8 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) { loff_t size = args->size; u64 flags = args->flags; - u64 valid_flags = 0; + u64 valid_flags = GUEST_MEMFD_NUMA_ENABLE; + struct mempolicy *mpol = NULL; if (flags & ~valid_flags) return -EINVAL; @@ -513,7 +517,18 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) if (size <= 0 || !PAGE_ALIGNED(size)) return -EINVAL; - return __kvm_gmem_create(kvm, size, flags); + if (flags & GUEST_MEMFD_NUMA_ENABLE) { + unsigned char mode = args->mpol_mode; + unsigned short maxnode = args->maxnode; + const unsigned long __user *user_nmask = + (const unsigned long *)args->host_nodes_addr; + + mpol = create_mpol_from_args(mode, user_nmask, maxnode); + if (IS_ERR_OR_NULL(mpol)) + return PTR_ERR(mpol); + } + + return __kvm_gmem_create(kvm, size, flags, mpol); } int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index 715f19669d01..3dd8495ae03d 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -36,6 +36,9 @@ static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, #endif /* HAVE_KVM_PFNCACHE */ #ifdef CONFIG_KVM_PRIVATE_MEM +/* Flag to check NUMA policy while creating KVM guest-memfd. */ +#define GUEST_MEMFD_NUMA_ENABLE BIT_ULL(0) + void kvm_gmem_init(struct module *module); int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args); int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, From patchwork Mon Sep 16 16:57:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivank Garg X-Patchwork-Id: 13805652 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 D27C5C3ABB2 for ; Mon, 16 Sep 2024 16:58:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BA7E6B0093; Mon, 16 Sep 2024 12:58:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 66A146B0095; Mon, 16 Sep 2024 12:58:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E3F76B0096; Mon, 16 Sep 2024 12:58:40 -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 2BEBA6B0093 for ; Mon, 16 Sep 2024 12:58:40 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D9D9BA0671 for ; Mon, 16 Sep 2024 16:58:39 +0000 (UTC) X-FDA: 82571210358.07.B13C274 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2089.outbound.protection.outlook.com [40.107.102.89]) by imf30.hostedemail.com (Postfix) with ESMTP id E6EDB8000C for ; Mon, 16 Sep 2024 16:58:36 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=4AItlesE; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf30.hostedemail.com: domain of shivankg@amd.com designates 40.107.102.89 as permitted sender) smtp.mailfrom=shivankg@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1726505908; a=rsa-sha256; cv=pass; b=MwEowqEv+3oRk1wIcT6WeyIHlCcx97GiICq25UIYPl+qERXQkrh7mM78zPlaCbSN4XB73i LH7wkhEq2NCJFG8DWmijP3P0n1G2BGma/f0/I0ObQ+oify1K2WwIRaljavpFAiN8ss/BZ6 wHxBE0Z+juNJ7YotCUJBf0zp9fcXApE= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=4AItlesE; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf30.hostedemail.com: domain of shivankg@amd.com designates 40.107.102.89 as permitted sender) smtp.mailfrom=shivankg@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726505908; 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=IVP9Dii3HPFNi0wU5QvtIqIv1UyuRRkoFQmZgmQjIHI=; b=ok9DjL30yEV4wsJ7UpAnJviKxkZytkrUOIAMoNMmd6rxZhSYvEpjHirm7wZttAJAvMoe1+ C1g6sjoZSCyWnQyz1AlYx42ccJTnKCMY9cFKvjFvMZbmZTOaoJqDcuXODfbQdDslTGMNh0 lfjJaMI/QxQfc62iQZ0nmL4xr6LUguY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=q0CtHGecNJ7MD6op2QX4O9W4yTR2pfAlCQ8Gfdd5zMo6jOgsrhBir8F4tMLjhEGstoZoRrzYDOHRNPfMn4f4V28bpnqeYHDEBg35NQb1iIJnlh7NNlljTATrlY+tbVVK4o3Nv/1fdf6D5SE3dwYs2a1gvDQ77g1zpXS9WNjAibFWWUuwaWrHlJ6LD1sYvkmfjHUtGZWTM8UCBb2po9qHDgr6aNgcStLeBNNQJtmRLBTbSGygajvaHMBplCAUdnWZ6PEH8ac+TkuYZMDk9oEpJZ4lIgtwqoxTqHG25MR5w6+zkt3eirbH8Yu1LNXwnR867l7VE7M+xTVsHFkBRf8mpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=IVP9Dii3HPFNi0wU5QvtIqIv1UyuRRkoFQmZgmQjIHI=; b=idjKFsBt8jWYViusOgUFflYCvL5PbK8I8HKZ6lmsHpMha5/QTL1zeSk1GLw+2xlZyjKR8eQwrwB/EACx4PVjtWF8eiHCWsegCvDmrTqpcIzcwiY2JVFKZpV0EUt5i3FoILMutErcvIHijtJ/ADtZp3ZL0MK1JYGxPoIMxS9WaMpBH7wxZo3vnywb/lS9RLQ4ypRRhjmd63adaQMIFrbBlfXD31xldhhTMjD993kExInUXall71HyOBIs02z+JR9UbJvZQbr+e6LLtdMMtqHrYvjCkmm0b/brdwAj60I+ds7PMRmPoPtL/xN3pdiR0ZcL2UreLJr/m1NICvGl2bbIng== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=IVP9Dii3HPFNi0wU5QvtIqIv1UyuRRkoFQmZgmQjIHI=; b=4AItlesEfdHYwycTxmNZdHOSkZ+q5hDSkVXnU004Yygw6f2kpYYXiMZYrN+vo4fZvaCs+sqZxhbNN1aJsJoO+OlrgywJy7AmwBPqQu29p8yw9UaZylWpBwNEAyWPqmhLbaBXE9Wo7Wu47pkZE5mt5oWdvUfYgTS7xHHMu7TbaEM= Received: from PH7P220CA0052.NAMP220.PROD.OUTLOOK.COM (2603:10b6:510:32b::34) by BY5PR12MB4145.namprd12.prod.outlook.com (2603:10b6:a03:212::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.20; Mon, 16 Sep 2024 16:58:32 +0000 Received: from SN1PEPF000252A0.namprd05.prod.outlook.com (2603:10b6:510:32b:cafe::ac) by PH7P220CA0052.outlook.office365.com (2603:10b6:510:32b::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.30 via Frontend Transport; Mon, 16 Sep 2024 16:58:32 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SN1PEPF000252A0.mail.protection.outlook.com (10.167.242.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Mon, 16 Sep 2024 16:58:32 +0000 Received: from kaveri.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Mon, 16 Sep 2024 11:58:26 -0500 From: Shivank Garg To: , , , CC: , , , , , , , , , , , , , Subject: [PATCH RFC 2/3] mm: Add mempolicy support to the filemap layer Date: Mon, 16 Sep 2024 16:57:42 +0000 Message-ID: <20240916165743.201087-3-shivankg@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240916165743.201087-1-shivankg@amd.com> References: <20240916165743.201087-1-shivankg@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000252A0:EE_|BY5PR12MB4145:EE_ X-MS-Office365-Filtering-Correlation-Id: 57e9d57f-c103-4597-216b-08dcd670cba1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|1800799024|36860700013|82310400026|376014; X-Microsoft-Antispam-Message-Info: mdLdDgGJmLdGTLZl9SRjVrmjYYkLg9fm4+tnslOzLBpKI+RlC3S30zuIdjw0v8lDhmbITO3ps+AKwrveNm5Kx22U6MZ1wtE6+WCplI140s34lqjOeby4i/E6pLVkac7CR+jhjNXYRY5liNCU+QXmvHjaA/aSC0FWQYT5IHHEn1ItUUgDY6YfEIwtALPRXN7qVzqeCGHLFRquS8gtMqk6RuKCeszs67ZuiNMo+kYiKSt64VMrBrep9dOftyP0DtvymGI7Pzx07ecz+k1OdA8ZvBopToRGR1Ps9CzNPE7SuiW/ri6emX81I5w/Trtzy3iINcfQXX117tpE5U/YpUCV1T3kGDAsc8jSNlhmzCEGKzYW1GULzAXJDx+Fqthbct1WqLZrUCSnD+6CSgxPqKK7/DrlvwlvGlvBVItFzMDhg92A4eGXNbeXMFL2HgOMfTiOECVOHgs+f3YYVumXMtBgNkgH9nGI3FA7HhnZnESSgM5kImRvl4dW04jm1wjIrL6gFyx8KOOJWjthU97VP58h7wX/yTGKtBLPXwK2gSJK0Nj5/3NBSJ1tXm9WSulIeOMr0vZAbqgFHzBaWToXq4s/D1uyt/rjuc67RPuVlHTtpp0PgCMkyRAy2aZyeTtKVDXmSbi9s3+nVwZkJpUq/izm5PVt5ywjOpW41yDL5bwb8PqtuK8Rj9CMTgVm/wDRPWmErHpKjarokzV6Y1eyYv7TMQvV+9n8EOKclQm6lzPo7OFmqEzr80StF+Xhe9w8C01fWlqv4uLrVKExooya9MundqXVQuh0kH/OsvPjEFUChYVUfv+CXRJ0oLWsD7rIy+tCQJdFAX0qZZvEtNa46StmYqz88hAyQEPG1z4l58NqlRiVkKftdDjy+nNq36JA8KBhbEFXHsDjQbDUFndFHvu01wkHFzi0ijc2IQthLUFCIoq/ebHUmBzeaKZmvoR9Yrzyb0HIJdGxhXUdLHBtTZgTJ9iOAZ3K2hBG1B9cHETw+8GYNHsSGU8t9ALuNBSl5W6OwtNESsPXBotgE8eVNJXAgbw2Z5K985vQeoyDXHeOGCu7+AslH4BKrqZXLMgNwOGRzu58Bc8wvEum175Ofao8XS9w88ZcIOQuX1WFNYO0+D64WCqXC3UsJDbMV7Hhk8azFvYA9F+pxISfjE3eTmAe9/638WRyjMKWczEwCeTbV0hg40jy6Vzjd0iVaRd8/E+gcyXsJDvARgkv5L6HGjZjiUlIIwEfJ6Cl3iTl1BYUIMziwaoUYPPudk4yt7Pe9ZW/st3q2HirCBLYQvH9F1dHvymmuIOoWd81ApKvqLTJpvCo+CBRmbRtLPCUfGuDq0nVobMjpV9hH90SqMnPkbEYOuXWXpjSVSO2ZVnQvDJP2holbVq5ZYFr5vBi9r7tDcx4xLn/0pbFn5Z2SHwd5CaiAdfeAUKSkq03+uc05aiKGnTAOveShOmqF3OxLSs9wcrf X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(7416014)(1800799024)(36860700013)(82310400026)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Sep 2024 16:58:32.0002 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 57e9d57f-c103-4597-216b-08dcd670cba1 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000252A0.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4145 X-Rspam-User: X-Stat-Signature: 4e8c8frg4759h14yg6ac7worpek8gqew X-Rspamd-Queue-Id: E6EDB8000C X-Rspamd-Server: rspam02 X-HE-Tag: 1726505916-455177 X-HE-Meta: U2FsdGVkX19AXdFQvYql4E9O76WArSW6LIDVsnmHnXh+E9KDfBboNYeCxrZWrRS3S5//mmENejH6zJ6VXfRyIvXlu++RbS+WCh+RDPeCwu+Xvb8jDILjoiMa8Riji/HVuKgKoPkc/JO5ZcfYP42xyFI7Gv1kHIsPaE6FECk4ABPoVR6Gvx7Rk/k36Khx0+4B1wqS4jUMDHzfvufoqzZLwbjsld5m/eCQQlWbVNrgwEpz4OeAU2FDjfDWdACF2tzV98qZHN2rDxFv6lbd1sWK9Pp+BXU77u7HDZwt+K3igJktp8ade5RnmSaSafE/JIDy8h4ND4bnlDLjzlfBeKyI338ENB0L5yCTJF7ATpO5a2ijheeL7tLktkgr1pj+Dbar/12hqpAE+nKHT6KFBH00cShuuxA+fJ7iBRN5Su3KZo1nl8xloBBt0gvoFy0J4JCSRy/uTREMvIvs5esQDSLIVtPCF2eKiNJkFt4Le7t0Ge4H5giuryBzcP0QfYtSYCidcz1k4YoSXptbMHjR0R5+CuzjNjb14mFTOYWACm+nDsz8pJf43o5LqxMKaCvY8wjZmWy/O5sdThZf63BkwkEzAQdqasqqdlSGl6lEDHH8/O/dhmFYSxdlovzNDrwb/KAFDR7YlMZQyQJ69Q4ya6Q2W4WM/RoaMJpr35HNo2oznINd8wLzG0hkRqJmYVxruRQFRtJmuozEkNh8aj5WUL9xv10LZSwAhUwstCo9K6bQdvwr6eQJxHc978bC2KYkdebIXC2zY8GXkuQzHjKwJgq2KKBficRRabqS5GEX0IF8/sshFg6Va1hsMGrFppD5rniDWCCRHJmtsLMDWmScR1UWRc0rdiHiJvGo9W5rJlLt/5hSEel95m6kMWcmG47TwmObcIlno/P3gFaG7WdNB694Jo+BVZyctEMn/HS3wLZvvB2DpxwJEfIwgJuFCuzYg8tqUYAiog1Wq2bvgj8NR/A 5JeZ9r7s v1KRinq0Fv/Jz7E6cUlqrWh6PxJrFO70CsZaPRmhTjEYocMtmy3PwvY9iz7xqeSt5aPiqIN3VrF+soBRmKY2OukYdlCgGTazqigkcRTV+OwnjCMocNX8TgD3HQ1XReTQsDr146Cl9LPz3zVX5b/fXeDHYjOvvPEYx5z38yM0jEVjaD2CAmJTk3Ps7o4cg8xvsduiCRrIsp7VNXI6SrRa8uoAWWr04hgHCp9pfqwwlzseKjmAL3Sk7YFu7J/4qT0bNP/fbR0QFepHZArdyHbsoWzqO/N2UJuz28wcZErDqgYG4Tj6uLLsWtzqg0YQf4Lq1l6uznMvHN6Bl1avFyZwvvVW3Mw== 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: From: Shivansh Dhiman Introduce mempolicy support to the filemap. Add filemap_grab_folio_mpol, filemap_alloc_folio_mpol_noprof() and __filemap_get_folio_mpol() APIs that take mempolicy struct as an argument. The API is required by VMs using KVM guest-memfd memory backends for NUMA mempolicy aware allocations. Signed-off-by: Shivansh Dhiman Signed-off-by: Shivank Garg --- include/linux/pagemap.h | 30 ++++++++++++++++++++++++++++++ mm/filemap.c | 30 +++++++++++++++++++++++++----- mm/mempolicy.c | 1 + 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index d9c7edb6422b..da7e41a45588 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -564,11 +564,19 @@ static inline void *detach_page_private(struct page *page) #ifdef CONFIG_NUMA struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order); +struct folio *filemap_alloc_folio_mpol_noprof(gfp_t gfp, unsigned int order, + struct mempolicy *mpol); #else static inline struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order) { return folio_alloc_noprof(gfp, order); } +static inline struct folio *filemap_alloc_folio_mpol_noprof(gfp_t gfp, + unsigned int order, + struct mempolicy *mpol) +{ + return filemap_alloc_folio_noprof(gfp, order); +} #endif #define filemap_alloc_folio(...) \ @@ -652,6 +660,8 @@ static inline fgf_t fgf_set_order(size_t size) void *filemap_get_entry(struct address_space *mapping, pgoff_t index); struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, fgf_t fgp_flags, gfp_t gfp); +struct folio *__filemap_get_folio_mpol(struct address_space *mapping, + pgoff_t index, fgf_t fgp_flags, gfp_t gfp, struct mempolicy *mpol); struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index, fgf_t fgp_flags, gfp_t gfp); @@ -710,6 +720,26 @@ static inline struct folio *filemap_grab_folio(struct address_space *mapping, mapping_gfp_mask(mapping)); } +/** + * filemap_grab_folio_mpol - grab a folio from the page cache + * @mapping: The address space to search + * @index: The page index + * @mpol: The mempolicy to apply + * + * Same as filemap_grab_folio(), except that it allocates the folio using + * given memory policy. + * + * Return: A found or created folio. ERR_PTR(-ENOMEM) if no folio is found + * and failed to create a folio. + */ +static inline struct folio *filemap_grab_folio_mpol(struct address_space *mapping, + pgoff_t index, struct mempolicy *mpol) +{ + return __filemap_get_folio_mpol(mapping, index, + FGP_LOCK | FGP_ACCESSED | FGP_CREAT, + mapping_gfp_mask(mapping), mpol); +} + /** * find_get_page - find and get a page reference * @mapping: the address_space to search diff --git a/mm/filemap.c b/mm/filemap.c index d62150418b91..a94022e31974 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -990,8 +990,13 @@ int filemap_add_folio(struct address_space *mapping, struct folio *folio, EXPORT_SYMBOL_GPL(filemap_add_folio); #ifdef CONFIG_NUMA -struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order) +struct folio *filemap_alloc_folio_mpol_noprof(gfp_t gfp, unsigned int order, + struct mempolicy *mpol) { + if (mpol) + return folio_alloc_mpol_noprof(gfp, order, mpol, + NO_INTERLEAVE_INDEX, numa_node_id()); + int n; struct folio *folio; @@ -1007,6 +1012,12 @@ struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order) } return folio_alloc_noprof(gfp, order); } +EXPORT_SYMBOL(filemap_alloc_folio_mpol_noprof); + +struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order) +{ + return filemap_alloc_folio_mpol_noprof(gfp, order, NULL); +} EXPORT_SYMBOL(filemap_alloc_folio_noprof); #endif @@ -1861,11 +1872,12 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index) } /** - * __filemap_get_folio - Find and get a reference to a folio. + * __filemap_get_folio_mpol - Find and get a reference to a folio. * @mapping: The address_space to search. * @index: The page index. * @fgp_flags: %FGP flags modify how the folio is returned. * @gfp: Memory allocation flags to use if %FGP_CREAT is specified. + * @mpol: The mempolicy to apply. * * Looks up the page cache entry at @mapping & @index. * @@ -1876,8 +1888,8 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index) * * Return: The found folio or an ERR_PTR() otherwise. */ -struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, - fgf_t fgp_flags, gfp_t gfp) +struct folio *__filemap_get_folio_mpol(struct address_space *mapping, pgoff_t index, + fgf_t fgp_flags, gfp_t gfp, struct mempolicy *mpol) { struct folio *folio; @@ -1947,7 +1959,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, err = -ENOMEM; if (order > 0) alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN; - folio = filemap_alloc_folio(alloc_gfp, order); + folio = filemap_alloc_folio_mpol_noprof(alloc_gfp, order, mpol); if (!folio) continue; @@ -1978,6 +1990,14 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, return ERR_PTR(-ENOENT); return folio; } +EXPORT_SYMBOL(__filemap_get_folio_mpol); + +struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, + fgf_t fgp_flags, gfp_t gfp) +{ + return __filemap_get_folio_mpol(mapping, index, + fgp_flags, gfp, NULL); +} EXPORT_SYMBOL(__filemap_get_folio); static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max, diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 9e9450433fcc..88da732cf2be 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -2281,6 +2281,7 @@ struct folio *folio_alloc_mpol_noprof(gfp_t gfp, unsigned int order, return page_rmappable_folio(alloc_pages_mpol_noprof(gfp | __GFP_COMP, order, pol, ilx, nid)); } +EXPORT_SYMBOL(folio_alloc_mpol_noprof); /** * vma_alloc_folio - Allocate a folio for a VMA. From patchwork Mon Sep 16 16:57:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivank Garg X-Patchwork-Id: 13805653 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 2917EC3ABB2 for ; Mon, 16 Sep 2024 16:58:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B00326B0096; Mon, 16 Sep 2024 12:58:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB0276B0098; Mon, 16 Sep 2024 12:58:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 929B16B0099; Mon, 16 Sep 2024 12:58:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7535B6B0096 for ; Mon, 16 Sep 2024 12:58:50 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id ECF27A0638 for ; Mon, 16 Sep 2024 16:58:49 +0000 (UTC) X-FDA: 82571210778.15.05759FC Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2079.outbound.protection.outlook.com [40.107.94.79]) by imf13.hostedemail.com (Postfix) with ESMTP id ED86620009 for ; Mon, 16 Sep 2024 16:58:46 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=QoxQZ1hH; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf13.hostedemail.com: domain of shivankg@amd.com designates 40.107.94.79 as permitted sender) smtp.mailfrom=shivankg@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1726505846; a=rsa-sha256; cv=pass; b=EwcpbnXrSXFsw8t0Sl2fkILztjZVuvIoiE+2TiR5hSlUY226jP+5kaYcKA5YMQFDJz5/DJ FO0PmmepgFfM76Bu85PX8DhGLo/GfEiEful7rRv7Dn77STdIPB+DrciCo0zWai1I/drLZk HUcdYc5VR6MArGw6TTTN3IKF0nbntUs= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=QoxQZ1hH; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf13.hostedemail.com: domain of shivankg@amd.com designates 40.107.94.79 as permitted sender) smtp.mailfrom=shivankg@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726505846; 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=t3TqfD3ymF6eqT4nlqJNmccBGoGKxBelhDPWPHnJ0q8=; b=OOf4NdlXsWVep6Tn+sZd2i4ctWjT8joLeneT+6HEDKKVvJcAftqXx0sya/Va5Wi3AIuOBl 64abwJQ2I7CoNqbi+/WKhX4chOH8y5v2nm+5iAH5/KYF/eoXixrecPJHHEf2xYg/1LXtkd 6xXOtPlx7R+tCi9khcJkiFnYR6/VIag= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=u5jIR95Kx2fG4Svhmo/jZ57vDaiWopGt1NRUJkPLZ1sr5b6OpvCqNr/b8CLLouCg6/Ngb0hKhGRQkiSXWwe0UFzWbpqitC4nHQwxmvvbeByxzrRP+FS4Ot6p4XD6P9+lfvlJefnGeGnquJ78iCNW4o95u3Xlgje7u+YDEjLEq0USwpyelgfggPN9PPY771lbLnTEcKz33L5GFinI1mlp+CkjODf4u6H+n8kmiDEjVa3VRk7ms1ev/BjBgwigcC65HD1lkXHz9gxg+/a8lk9pMMGlwBM/zGO+NkbsJ5PGbzZNVMsv4ZUs05RQNNqx718ilLeD+tX9ptC5z/XtcOlc7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=t3TqfD3ymF6eqT4nlqJNmccBGoGKxBelhDPWPHnJ0q8=; b=zVJUD3LRylBHVZOH55tgjhN85vVulCoRXyuavhDntkWsEc8fo6yvE561c0aCcbjMkhXDawJAbeptmeXSzbOdIu+EW9hYrEUynwNWGFreUKqHm6JhUF53uW7xG5WEbR5K60exlvXW+K7aaf7p/+RZjDeftDKgfqUG5dQ94DUtOWBTyqitEGSRNEdodY8iWCS574kNKZsGVFNrIzL2aO54JtEFJalLLRGEKtbsL+BY4ybbAULwoN3PP1w2awqE//ygoEgHZu6t+i4pWIgTU/xHd5h2GCLo0GRKcJoJ4r28e2yWZGBnJ1jV677KZVZnm2RkB2uNuX9/owVQmf1QFXkCPw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=t3TqfD3ymF6eqT4nlqJNmccBGoGKxBelhDPWPHnJ0q8=; b=QoxQZ1hHyr14fQxR3BKCC715xWenv4az8sx9Mrzu2+etVof4p+8WXwI4VqWHS0/l4tvjQGubQJvwBJW5UTJjJH81sABWMYbXEgUIpnzXgNOYr0PqZCveenYyXsmahFspj1rAidfg0cunaKZl31ujzyrUgAKS+bJT2pBgwScFflg= Received: from SA0PR11CA0114.namprd11.prod.outlook.com (2603:10b6:806:d1::29) by DM4PR12MB5796.namprd12.prod.outlook.com (2603:10b6:8:63::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.24; Mon, 16 Sep 2024 16:58:40 +0000 Received: from SN1PEPF000252A1.namprd05.prod.outlook.com (2603:10b6:806:d1:cafe::40) by SA0PR11CA0114.outlook.office365.com (2603:10b6:806:d1::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.30 via Frontend Transport; Mon, 16 Sep 2024 16:58:39 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SN1PEPF000252A1.mail.protection.outlook.com (10.167.242.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Mon, 16 Sep 2024 16:58:39 +0000 Received: from kaveri.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Mon, 16 Sep 2024 11:58:32 -0500 From: Shivank Garg To: , , , CC: , , , , , , , , , , , , , Subject: [PATCH RFC 3/3] KVM: guest_memfd: Enforce NUMA mempolicy if available Date: Mon, 16 Sep 2024 16:57:43 +0000 Message-ID: <20240916165743.201087-4-shivankg@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240916165743.201087-1-shivankg@amd.com> References: <20240916165743.201087-1-shivankg@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000252A1:EE_|DM4PR12MB5796:EE_ X-MS-Office365-Filtering-Correlation-Id: f9f75e68-0ee6-488e-5b0f-08dcd670d051 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|82310400026|376014|7416014|1800799024; X-Microsoft-Antispam-Message-Info: +bwEEvAnMVEBX6U9xmGh4Fmg6LC4oZNUd5RW1KE4MImQuwJvVVQSmJStxXfbMohnRy3/t1VO8Xcpk3Yaxrqyk3/FIKBISMYPjLVxVotgX73pFAReY+qKp/lRPMbX8+g1t69fhT1ENYvWZXJrFuAJZFial2bSuH1GFWfmvdT6reG3l1r/8C86beh4BpqRERejjZCAzmy76eh6jVf5JJY5OP5serfVMGlWDwskLYzPbb2pijQCzyZjUfvqrZ/n4IUiVVvGLLCEZ3i5MgiwUf85ekM4mm9rSfLW0hBdstWQXTbUXv0UjIZw98ORE4sn+WWkhZGvgSYZMtvVQMAQIIrUvyfiZZWFub+q4ufUTtcyN32ah2Aq3AUmt4OvhO9yrMvPLZ46IAoMdOECuKgLNlyjGm7xOMfjiA7yu9v8XxDVzPPr1QtTyJQrDbLShEw3opunnNunwkKJPLOO0nsdJD4nPFOKyhnfSS1bGUtHnoah7bteYw5Lf4VEUvo8GY9lXNwSSuEgUywBeR3SJF951SeAspf99ZwZXs2xAh0jJg4Z8/B19pRtbFnemq338Swl1J4t/NMYsVOzvVZ0vYdOZG1oXikA3pmAI85+FYwXiL5uqDbtOVsZpOEKNrepGr7n0kjnJMlZ9R/ugm/OGkfKJPr0NzXDP4X5mIe81zSZxha+sAIhcMAuObmx17m/G3ofWcZr3+lKomLOT4XHDvqPvE8SugY3F9CbUQld1T50ascSTyKCNUIjhfs4CTO/IhSRDgaCHqWyC0zozg7Inmpp16cCuhLGJRi8obLqPt1GmR/DJ0J4AeJl2zui2xcAkf1OFdu2u2wZ6pZI2IC/O3yyuAW1LVm91LSn8KQCsSDzVgpdREu/uWXQJ2sbuqMWEBttO/j3TWJXuCtQ4/Yl9Gnqnl+WjQQC4duXcQpuQnftWPHqt7pZ1tfmrdAuRtrBG5r5Q+uQ6yNzfTYLF0xTemHHzf2XvcwyOzi/UvOGKEUyu+WfyVCx5iV+Hbpl/B5zUaYGDz8sqQgN+25UB3krLmimbG1R8vvDskhXJfNBjx3epTwmG8DpayqXuGccKl0eJDDYAvFxL7YPnD3d5v2+qr+6XL01qmAAqiofjIuByusr/h0fvTIOdhZj9EkdYEVEF95WlgeVsmruzVR05ZYmpRY+ryuAqaSzWGCCmnNe/ICdWHDK9vLUQXXV/Rn+MHYGtpzWgLgp95OPAiVeS7cQ59UOHNzQ9m3URx+7psav6nX9bBm4k0WIxNVfTXKtl1tadZpvMRkvS9+P778cE4NF4yzuQKdH4MScvoH+zHwlJ7tgA1az2Jl1InxY6VnXjkITcGlcRpxSEC1L6ZtpX+4Gp5e0of2ipbDh6RWwTQyebuSz6wYGPlM4NnhsFaffVjUEDbFYie95EVhCtKmjfpO4U0r8qXWsiMEViJAbZEqbhzJCG1kDYfgUKguGmoa7RR/2DKZEydZb X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(82310400026)(376014)(7416014)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Sep 2024 16:58:39.8660 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f9f75e68-0ee6-488e-5b0f-08dcd670d051 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000252A1.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5796 X-Rspamd-Queue-Id: ED86620009 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: snydgt5kuboonns1q8mwe16k8ip6ceoi X-HE-Tag: 1726505926-566734 X-HE-Meta: U2FsdGVkX1/+hUWVQmWifU7giQU9J1m/OtYxze1JSnMYtEVMYHs0SEkBAqVN6UcLDoToYVqKWqrHovCEAR00IwcUI49fMbhP/yYgor7oQyAHXH1Bq+gveWpFPSBXTrUiKk7bSsy0mBIvUBnk4Mv26/gUIffoTuqETxDzm7Vc7yxbfspo9zXE7UvMGXydS+HoimDmTLHKENdhqwgJyOyiseUMJRVqmJJQjeaV5xsRhZ7b1JDoWNaqSqhNZV1qWUHRUMnfZkA8qXhqoKq/FnywIwTBym4LikQAJCGRBB1b+q14nFFbe0Sf8/24k+ylj8Dui/6g9s+X7L/0Y47ExeSppsw+MUUb5lGbiWlxYKi/iJEq/kksiO4sf4wTPESR3QeMXp4bllwZrayMVkXobeUA1fJSt/UlmvtWgJCTVPD0rlGtyyQOFJgw/LmWUA+gBK3QlG9ExELjdVqxsm8NiB65J4RcEmDT8/+oBCZMrNo0hvB+w6nHBAIB0kYo0tIoX//+KHANvQsw+U2/cPD+FovaNDHE881gQLmrZaM8CzJrKKOUqk7D83wJ5rydtislosZIlcXZcd77H4azpHIBN12H73UBVsnAMOZi3/s2evrqWtfNMGJMN1ESBCz0wU1LfALQYvTzShXPhT44118cVNCawYrGj7vwIQhPvYrjUNzo7LsvIyoPANaIEB3DVmd14t2ta15O6HZ52DgTmzIqs23DZq2comlxDZ7h44qUpXicStyQlrAXbJZN1neefC11w2G97ds4Wizquff5tcyqsgS4TnIzNBFvUIgS3Hi1/Sq/55EV+awrresICWjuOIMk+Sc08c4iytN14CzCZoIsG2CFPG53GbsSDLu8ODquR5QZBuqNy+h2rM3hjqldho2aOvUotf9O3iwTBNkmKCcR0UtJN8/p1a64/Z7fM1NhLL6IAYyYMOukADZfOB4LvkBTWe+PzLxF9qT7pYabLfnI59x YWKZaWOg X/e4ubEKC0tEXko8FEBQ6VD7tEvIAJ7EFoxZ7BoLQfhHyJA6jCcePNAa2AtloozwPKOOfIUw4ObmCSNCw3U/QSpjSYXasLMRe+XoT2fPUma52BjW0ueroEskblhMuUEkQdgUO1esw9VR6Fs/fmDLDJv2el92NLlyE4uqgpRXiPl6cyCj5iXIC2SRyKFrX8DQ9LDBEzYfdYnyEXRpPmzIBpmcCam+gGuSYrf8nHmlVEOlGXM/k4+X8PXSe4fSAMK0KiATGvuNJQ+6v7aIX9xR6GGUg2bSEUpych3w6E2sxdgIA4X3Vv1F6K22t0KeygGsUG3uDSpysK9+LW7Ar6OxsSGcvsKM9HzFwkEOTiMKpQr8hGXwlia1pfiqidaOSYvjIwLCy 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: From: Shivansh Dhiman Enforce memory policy on guest-memfd to provide proper NUMA support. Previously, guest-memfd allocations were following local NUMA node id in absence of process mempolicy, resulting in random memory allocation. Moreover, it cannot use mbind() since memory isn't mapped to userspace. To support NUMA policies, retrieve the mempolicy struct from i_private_data part of memfd's inode. Use filemap_grab_folio_mpol() to ensure that allocations follow the specified memory policy. Signed-off-by: Shivansh Dhiman Signed-off-by: Shivank Garg --- virt/kvm/guest_memfd.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 8f1877be4976..8553d7069ba8 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -130,12 +130,15 @@ static struct folio *__kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool allow_huge) { struct folio *folio = NULL; + struct mempolicy *mpol; if (gmem_2m_enabled && allow_huge) folio = kvm_gmem_get_huge_folio(inode, index, PMD_ORDER); - if (!folio) - folio = filemap_grab_folio(inode->i_mapping, index); + if (!folio) { + mpol = (struct mempolicy *)(inode->i_mapping->i_private_data); + folio = filemap_grab_folio_mpol(inode->i_mapping, index, mpol); + } pr_debug("%s: allocate folio with PFN %lx order %d\n", __func__, folio_pfn(folio), folio_order(folio));