From patchwork Tue Dec 3 00:23:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuanchu Xie X-Patchwork-Id: 13891450 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 1D055E6C5E3 for ; Tue, 3 Dec 2024 00:24:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6558C6B007B; Mon, 2 Dec 2024 19:24:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6046C6B0083; Mon, 2 Dec 2024 19:24:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4A5CA6B0085; Mon, 2 Dec 2024 19:24:04 -0500 (EST) 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 282936B007B for ; Mon, 2 Dec 2024 19:24:04 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CDA7AC0410 for ; Tue, 3 Dec 2024 00:24:03 +0000 (UTC) X-FDA: 82851750282.29.B632962 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf17.hostedemail.com (Postfix) with ESMTP id 6746D40002 for ; Tue, 3 Dec 2024 00:23:52 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=qzWwuD3u; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3oE9OZwcKCLkxtZmbgtfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--yuanchu.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3oE9OZwcKCLkxtZmbgtfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--yuanchu.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733185430; a=rsa-sha256; cv=none; b=iCvCcXvaPvmjh4ZxoVckv1xPgXy8pfSkWI43tkfsv6bOgU1cunQ6MjwHi05jaGAGIFNN60 4IDsi+HiMxMW+pTPrVbWwQ2pnhK/luwnwLyne0yK/H1vhGKjj4vI49iYiXhdC2GwVkCzvt VX8+EWlx27mUyNcc2G6YBM05Drh45tI= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=qzWwuD3u; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3oE9OZwcKCLkxtZmbgtfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--yuanchu.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3oE9OZwcKCLkxtZmbgtfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--yuanchu.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733185430; 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:in-reply-to: references:dkim-signature; bh=3dEkRIysLWaL9ITQw+zsEeXtl+7ibVr1QzCbZ81J1U8=; b=dnywb1TxUIU/bs4N2urLQuCsO5w3q1516SfCB/ZddncOnj2m1UKCWMUt3+EgmodxnUK3Y7 kIKp+KNVNyebyfKI84QeDBHtIuHlskJAbG8W5W55ZzBBaMbe1T9GdhtJOYh7dnypRLJF1p 3fKycL4cWexpqXjVMguK0pKz7FyN9YA= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee4866c419so5114012a91.0 for ; Mon, 02 Dec 2024 16:24:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733185440; x=1733790240; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=3dEkRIysLWaL9ITQw+zsEeXtl+7ibVr1QzCbZ81J1U8=; b=qzWwuD3usSNVkPtPmKZm7JWEwZgN8Nx0iZw9xP+NNc/utSB3E9ecJyxqsLoh6KIG2x 2924piH3s4MkBbVyrxxoPhf1OyAqP9aHa/XQkq+ci9jnkyR2GASCPzzJQG64OPkjKrmS aZZt4RR0Qt4/xBsLp21ByuUUFW//klxwwaN7Kh2HCgIfUF+eBoJn38aUnwgYmKwn9VgS scm2oq/CDZYNtdpRRjSXzjyNdLk9bPerbI7bniUShMZyRNKZrDak9Qi5b7X8M4VOlmSy 45Czn3R5hbXskU2A0ZiigxG5w5NeNfgl1Zz6mGAkCzBpXgwoUmZBNBBbauiLbQDKkzHd WcRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733185440; x=1733790240; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=3dEkRIysLWaL9ITQw+zsEeXtl+7ibVr1QzCbZ81J1U8=; b=wSd9lNnOpZBTSd6OqgUd1quIDVG6xc+nyEUL9LIlXqoZ7jLOK+W7uKGPN/d5J/xWEj 1rQDNxFDy66nIS+5U/LjXw4mOJapmabuUuB3DhTB3v5eqB/70f67Z1SwyMcEHblG/wRW UlDeOe9fLaY77i9NU0OxRd5JIufKg5Yx3+ZfMeSYth/Cel5Sy7Lu6rv0BIP8UcZSRk5d MnsfJ2votJBRkla3tLn1V5EAn1/pIB02FCE3L8sWesR8IZKpIAHjhxjwenA4IFxgrUE5 1CZCAY9DaSbxB2AnR2ypwaszgew8HYKklLk9eI+dV1oXQPdVSDvmeJ3W9cE3dPvQ5dj+ GSjA== X-Forwarded-Encrypted: i=1; AJvYcCUIkyxEFX3MYnZ+4o2fH0c7dZdtvmKekdOGPlyBBbw3NuElIqKQJhAJcq1l0vHLwL4s2OkRZs8MLw==@kvack.org X-Gm-Message-State: AOJu0YzhI5UFEbIIGGgK4E+KHOi6gTCrqf4BkuHUS9ILHHP7OAjhjAxi qMFI6x9SyeI9dI1sPe36kfsbKsvk9vuIy8u8Ur3ziubdKZJByS6+HsRFmnXylvsB5qMfIiUOyze m1GpAAg== X-Google-Smtp-Source: AGHT+IF4PCWpM3k+PFEDD7TgfuNJuV7QWB2xQO7QIlH4mkHyH358LOPJeBKZaCKKEmSLKH5Jo69CY6MsvgRF X-Received: from pjbnc15.prod.google.com ([2002:a17:90b:37cf:b0:2ea:5469:76c2]) (user=yuanchu job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1dc1:b0:2ee:ab11:fab2 with SMTP id 98e67ed59e1d1-2ef01211725mr734840a91.22.1733185440563; Mon, 02 Dec 2024 16:24:00 -0800 (PST) Date: Mon, 2 Dec 2024 16:23:27 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.46.0 Message-ID: <20241203002328.694071-1-yuanchu@google.com> Subject: [PATCH v5 1/2] virt: pvmemcontrol: control guest physical memory properties From: Yuanchu Xie To: Greg Kroah-Hartman Cc: Wei Liu , Rob Bradford , Pasha Tatashin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, virtualization@lists.linux.dev, dev@lists.cloudhypervisor.org, Yuanchu Xie X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 6746D40002 X-Stat-Signature: sa1k9ds5i9neyfyqsec3wdryj5gnsp81 X-Rspam-User: X-HE-Tag: 1733185432-899537 X-HE-Meta: U2FsdGVkX1+Q0kInZ26NC3KAKtl4JdgdLu1Q7SwLnXbQcNaiVPG6JW163uSZKzrqSzARTLI1w5YHcw737aJ3Og4gPvrSq33eG11awQWM40LNTyBlpls6nuqqSmk3yK8hM8wAaPnXc9Tzs/3mP+J8griaRas8K4Bu6DXM+srpAKDVh07RY91MS2duyrmuyqnSeql19s4G9rXKQl2Rp3iOL6apODnNmf9N5jlY+VP5RF/TtGSov4p6YSMhA0laQn26MHmln7NjJQt3oXRZH6497g/35+aqtY6r6r+c3+oaR8rEY0r+glJqUvtJJR8V7BoaEgRJDax7eRb1I/iHLNHo5Onra6xyFAH3OzGnUhkKBsfIbT+jXBNjMvjPtzWW5RTRmT5Xsh5J8ngiaQDY+9IITWOQ6ofu8Gpp6dotpUd5807sPybBfUIcnGgKsflvrmsekg1wQ+C+sWu4xEL3iU/Oa/0cIIHJyBj0cPJndVMLM84panmQ5qg7IqUQVO8DYJtnjANqT/ScAGg21KJ2W4JdHrOJ1dorTMKh4nt4lV6PMcS7V1fpyd3CD0Rxa+Vbj9FdJlZXnTx2LhFaq8c+b0cSuxgMMbP/+Ublr6X/cKblFLVz+xnHsE0ezmhvNn8Fw7sG2lCrp4DViW5XPqaIA69xXpoUGQIW4pO4FcY9Nr9oDFp70ndqNnq5yzBk4CZW0iW4HJCaAC6Kw+7trDwavtf4kG/1W7CfTF1Z2IWbm5W77GNQ73+kqEykY1T//mthtirZnrh8WTwttpYw7Dz1ulzkS1h0PENvis4sHzqTBALYFLZ/aknQqIkUUbdo1M+OyDAEtxoNsnZUf4Vi80HY3fCog51FTp4gEnvKp9ErXG/cJ97hmaoSvzc4HnsimjzkT9+OevBoqS/Avzd8KeW4csgcUKBcxqqWsKlhQCFN7DbWna96Gx13ryc+ij/EtCpNYhEizVkaPerinGTOK3BNTlE M2IAbMXd Xkb1/UBPZd1tIeQpfeyyxMI+G1cRuX2673eqVkpmyEI1wLDQdb3Mhf9bMrrwKYYPYqWYvmUCcH9X+uxTDTl+XmhB9UE4LU2rKW7MWXw617E0h0RV4SnFj/HGh38ld1Gagl0wzbUyX8zM9x5pbqhTsOHUipzdRqZwS3iCMP0cKUhQNXoUsdR1oxbanLGeMBscszftmKpLBqdbR34Vj1ABFipis8XOiJ0mJqY+t1B7Pu6ZsBzPw7gbimYfdA7pjxmsA+Xybqy/yNByDJtHcA7XXTeHo8EAS0EMGCaybqJK5XdXQGeLk+IzVaKUTdsW5cSlSIaqSGS++1SwocOxuubCBsrIZ/jhzKkBqP+HitsEaXBnjuDhapTw4XRbaB17QkP4UVR9LBhwO3pXwY1NZazIQAqGkyZG+mHb41oi8qPVnvK5XVdY6UK4J0Yzw5AMaAa2iTOw/9p5hFvZJB9UO0F3JirYwjqCxjVRfVs81yrSC43TQpXE5lIIZSnTrAY/2WJmaRInIp6/wHOVn34Ah6VOz1/rhd7/ur6CkRmSTXEkVvxU22RaWkJ2ZT65EocRfGl5pr3hrYmYEXbDRsP2Wn41uxkVOcvHFdd4xB7dLc0CNOJMB671Rw72EZHbSe5H+xQEgy+p+X1DM+c3ci5aXy4Fo58QGqwtN2ZFstgXpKj2ce0+EEW0= 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: Pvmemcontrol provides a way for the guest to control its physical memory properties and enables optimizations and security features. For example, the guest can provide information to the host where parts of a hugepage may be unbacked, or sensitive data may not be swapped out, etc. Pvmemcontrol allows guests to manipulate its gPTE entries in the SLAT, and also some other properties of the memory mapping on the host. This is achieved by using the KVM_CAP_SYNC_MMU capability. When this capability is available, the changes in the backing of the memory region on the host are automatically reflected into the guest. For example, an mmap() or madvise() that affects the region will be made visible immediately. There are two components of the implementation: the guest Linux driver and Virtual Machine Monitor (VMM) device. A guest-allocated shared buffer is negotiated per-cpu through a few PCI MMIO registers; the VMM device assigns a unique command for each per-cpu buffer. The guest writes its pvmemcontrol request in the per-cpu buffer, then writes the corresponding command into the command register, calling into the VMM device to perform the pvmemcontrol request. The synchronous per-cpu shared buffer approach avoids the kick and busy waiting that the guest would have to do with virtio virtqueue transport. User API From the userland, the pvmemcontrol guest driver is controlled via the ioctl(2) call. It requires CAP_SYS_ADMIN. ioctl(fd, PVMEMCONTROL_IOCTL, struct pvmemcontrol_buf *buf); Guest userland applications can tag VMAs and guest hugepages, or advise the host on how to handle sensitive guest pages. Supported function codes and their use cases: PVMEMCONTROL_FREE/REMOVE/DONTNEED/PAGEOUT. For the guest. One can reduce the struct page and page table lookup overhead by using hugepages backed by smaller pages on the host. These pvmemcontrol commands can allow for partial freeing of private guest hugepages to save memory. They also allow kernel memory, such as kernel stacks and task_structs to be paravirtualized if we expose kernel APIs. PVMEMCONTROL_MERGEABLE can inform the host KSM to deduplicate VM pages. PVMEMCONTROL_UNMERGEABLE is useful for security, when the VM does not want to share its backing pages. The same with PVMEMCONTROL_DONTDUMP, so sensitive pages are not included in a dump. MLOCK/UNLOCK can advise the host that sensitive information is not swapped out on the host. PVMEMCONTROL_MPROTECT_NONE/R/W/RW. For guest stacks backed by hugepages, stack guard pages can be handled in the host and memory can be saved in the hugepage. PVMEMCONTROL_SET_VMA_ANON_NAME is useful for observability and debugging how guest memory is being mapped on the host. Sample program making use of PVMEMCONTROL_DONTNEED: https://github.com/Dummyc0m/pvmemcontrol-user The VMM implementation is part of Cloud Hypervisor, the feature pvmemcontrol can be enabled and the VMM can then provide the device to a supporting guest. https://github.com/cloud-hypervisor/cloud-hypervisor Signed-off-by: Yuanchu Xie --- PATCH v4 -> v5 - use drvdata and friends to enable multiple devices PATCH v3 -> v4 - changed dev_info to dev_dbg so the driver is quiet when it works properly. - Edited the changelog section to be included in the diffstat. PATCH v2 -> v3 - added PVMEMCONTROL_MERGEABLE for memory dedupe. - updated link to the upstream Cloud Hypervisor repo, and specify the feature required to enable the device. PATCH v1 -> v2 - fixed byte order sparse warning. ioread/write already does little-endian. - add include for linux/percpu.h RFC v1 -> PATCH v1 - renamed memctl to pvmemcontrol - defined device endianness as little endian v1: https://lore.kernel.org/linux-mm/20240518072422.771698-1-yuanchu@google.com/ v2: https://lore.kernel.org/linux-mm/20240612021207.3314369-1-yuanchu@google.com/ v3: https://lore.kernel.org/linux-mm/20241016193947.48534-1-yuanchu@google.com/ v4: https://lore.kernel.org/linux-mm/20241021204849.1580384-1-yuanchu@google.com/ .../userspace-api/ioctl/ioctl-number.rst | 2 + drivers/virt/Kconfig | 2 + drivers/virt/Makefile | 1 + drivers/virt/pvmemcontrol/Kconfig | 10 + drivers/virt/pvmemcontrol/Makefile | 2 + drivers/virt/pvmemcontrol/pvmemcontrol.c | 499 ++++++++++++++++++ include/uapi/linux/pvmemcontrol.h | 76 +++ mm/percpu.c | 1 + 8 files changed, 593 insertions(+) create mode 100644 drivers/virt/pvmemcontrol/Kconfig create mode 100644 drivers/virt/pvmemcontrol/Makefile create mode 100644 drivers/virt/pvmemcontrol/pvmemcontrol.c create mode 100644 include/uapi/linux/pvmemcontrol.h diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst index 243f1f1b554a..09788e8e4f9f 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -378,6 +378,8 @@ Code Seq# Include File Comments 0xCD 01 linux/reiserfs_fs.h Dead since 6.13 0xCE 01-02 uapi/linux/cxl_mem.h Compute Express Link Memory Devices 0xCF 02 fs/smb/client/cifs_ioctl.h +0xDA 00 uapi/linux/pvmemcontrol.h Pvmemcontrol Device + 0xDB 00-0F drivers/char/mwave/mwavepub.h 0xDD 00-3F ZFCP device driver see drivers/s390/scsi/ diff --git a/drivers/virt/Kconfig b/drivers/virt/Kconfig index d8c848cf09a6..454e347a90cf 100644 --- a/drivers/virt/Kconfig +++ b/drivers/virt/Kconfig @@ -49,4 +49,6 @@ source "drivers/virt/acrn/Kconfig" source "drivers/virt/coco/Kconfig" +source "drivers/virt/pvmemcontrol/Kconfig" + endif diff --git a/drivers/virt/Makefile b/drivers/virt/Makefile index f29901bd7820..3a1fd6e076ad 100644 --- a/drivers/virt/Makefile +++ b/drivers/virt/Makefile @@ -10,3 +10,4 @@ obj-y += vboxguest/ obj-$(CONFIG_NITRO_ENCLAVES) += nitro_enclaves/ obj-$(CONFIG_ACRN_HSM) += acrn/ obj-y += coco/ +obj-$(CONFIG_PVMEMCONTROL) += pvmemcontrol/ diff --git a/drivers/virt/pvmemcontrol/Kconfig b/drivers/virt/pvmemcontrol/Kconfig new file mode 100644 index 000000000000..9fe16da23bd8 --- /dev/null +++ b/drivers/virt/pvmemcontrol/Kconfig @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 +config PVMEMCONTROL + tristate "pvmemcontrol Guest Service Module" + depends on KVM_GUEST + help + pvmemcontrol is a guest kernel module that allows to communicate + with hypervisor / VMM and control the guest memory backing. + + To compile as a module, choose M, the module will be called + pvmemcontrol. If unsure, say N. diff --git a/drivers/virt/pvmemcontrol/Makefile b/drivers/virt/pvmemcontrol/Makefile new file mode 100644 index 000000000000..2fc087ef3ef5 --- /dev/null +++ b/drivers/virt/pvmemcontrol/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_PVMEMCONTROL) := pvmemcontrol.o diff --git a/drivers/virt/pvmemcontrol/pvmemcontrol.c b/drivers/virt/pvmemcontrol/pvmemcontrol.c new file mode 100644 index 000000000000..28df8f77c3a7 --- /dev/null +++ b/drivers/virt/pvmemcontrol/pvmemcontrol.c @@ -0,0 +1,499 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Control guest physical memory properties by sending + * madvise-esque requests to the host VMM. + * + * Author: Yuanchu Xie + * Author: Pasha Tatashin + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PCI_VENDOR_ID_GOOGLE 0x1ae0 +#define PCI_DEVICE_ID_GOOGLE_PVMEMCONTROL 0x0087 + +#define PVMEMCONTROL_COMMAND_OFFSET 0x08 +#define PVMEMCONTROL_REQUEST_OFFSET 0x00 +#define PVMEMCONTROL_RESPONSE_OFFSET 0x00 + +static DEFINE_IDA(pvmemcontrol_minors_ida); + +static unsigned int major_num __read_mostly; + +struct pvmemcontrol; + +struct pvmemcontrol_file_private { + struct pvmemcontrol *dev; + struct pvmemcontrol_buf buf; +}; + +/* + * Magic values that perform the action specified when written to + * the command register. + */ +enum pvmemcontrol_transport_command { + PVMEMCONTROL_TRANSPORT_RESET = 0x060FE6D2, + PVMEMCONTROL_TRANSPORT_REGISTER = 0x0E359539, + PVMEMCONTROL_TRANSPORT_READY = 0x0CA8D227, + PVMEMCONTROL_TRANSPORT_DISCONNECT = 0x030F5DA0, + PVMEMCONTROL_TRANSPORT_ACK = 0x03CF5196, + PVMEMCONTROL_TRANSPORT_ERROR = 0x01FBA249, +}; + +/* Contains the function code and arguments for specific function */ +struct pvmemcontrol_vmm_call_le { + __le64 func_code; /* pvmemcontrol set function code */ + __le64 addr; /* hyper. page size aligned guest phys. addr */ + __le64 length; /* hyper. page size aligned length */ + __le64 arg; /* function code specific argument */ +}; + +/* Is filled on return to guest from VMM from most function calls */ +struct pvmemcontrol_vmm_ret_le { + __le32 ret_errno; /* on error, value of errno */ + __le32 ret_code; /* pvmemcontrol internal error code, on success 0 */ + __le64 ret_value; /* return value from the function call */ + __le64 arg0; /* currently unused */ + __le64 arg1; /* currently unused */ +}; + +struct pvmemcontrol_buf_le { + union { + struct pvmemcontrol_vmm_call_le call; + struct pvmemcontrol_vmm_ret_le ret; + }; +}; + +struct pvmemcontrol_percpu_channel { + struct pvmemcontrol_buf_le buf; + u64 buf_phys_addr; + u32 command; +}; + +struct pvmemcontrol { + int minor_number; + void __iomem *base_addr; + struct pci_dev *pci_dev; + struct cdev cdev; + struct device *cdev_device; + /* cache the info call */ + struct pvmemcontrol_vmm_ret pvmemcontrol_vmm_info; + struct pvmemcontrol_percpu_channel __percpu *pcpu_channels; +}; + +static void pvmemcontrol_write_command(void __iomem *base_addr, u32 command) +{ + iowrite32(command, base_addr + PVMEMCONTROL_COMMAND_OFFSET); +} + +static u32 pvmemcontrol_read_command(void __iomem *base_addr) +{ + return ioread32(base_addr + PVMEMCONTROL_COMMAND_OFFSET); +} + +static void pvmemcontrol_write_reg(void __iomem *base_addr, u64 buf_phys_addr) +{ + iowrite64_lo_hi(buf_phys_addr, base_addr + PVMEMCONTROL_REQUEST_OFFSET); +} + +static u32 pvmemcontrol_read_resp(void __iomem *base_addr) +{ + return ioread32(base_addr + PVMEMCONTROL_RESPONSE_OFFSET); +} + +static void pvmemcontrol_buf_call_to_le(struct pvmemcontrol_buf_le *le, + const struct pvmemcontrol_buf *buf) +{ + le->call.func_code = cpu_to_le64(buf->call.func_code); + le->call.addr = cpu_to_le64(buf->call.addr); + le->call.length = cpu_to_le64(buf->call.length); + le->call.arg = cpu_to_le64(buf->call.arg); +} + +static void pvmemcontrol_buf_ret_from_le(struct pvmemcontrol_buf *buf, + const struct pvmemcontrol_buf_le *le) +{ + buf->ret.ret_errno = le32_to_cpu(le->ret.ret_errno); + buf->ret.ret_code = le32_to_cpu(le->ret.ret_code); + buf->ret.ret_value = le64_to_cpu(le->ret.ret_value); + buf->ret.arg0 = le64_to_cpu(le->ret.arg0); + buf->ret.arg1 = le64_to_cpu(le->ret.arg1); +} + +static void pvmemcontrol_send_request(struct pvmemcontrol *pvmemcontrol, + struct pvmemcontrol_buf *buf) +{ + struct pvmemcontrol_percpu_channel *channel; + + preempt_disable(); + channel = this_cpu_ptr(pvmemcontrol->pcpu_channels); + + pvmemcontrol_buf_call_to_le(&channel->buf, buf); + pvmemcontrol_write_command(pvmemcontrol->base_addr, channel->command); + pvmemcontrol_buf_ret_from_le(buf, &channel->buf); + + preempt_enable(); +} + +static void pvmemcontrol_vmm_call(struct pvmemcontrol *pvmemcontrol, + struct pvmemcontrol_buf *buf) +{ + if (buf->call.func_code == PVMEMCONTROL_INFO) { + memcpy(&buf->ret, &pvmemcontrol->pvmemcontrol_vmm_info, + sizeof(buf->ret)); + return; + } + + pvmemcontrol_send_request(pvmemcontrol, buf); +} + +static int pvmemcontrol_init_info(struct pvmemcontrol *dev, + struct pvmemcontrol_buf *buf) +{ + buf->call.func_code = PVMEMCONTROL_INFO; + + pvmemcontrol_send_request(dev, buf); + if (buf->ret.ret_code) + return buf->ret.ret_code; + + /* Initialize global pvmemcontrol_vmm_info */ + memcpy(&dev->pvmemcontrol_vmm_info, &buf->ret, + sizeof(dev->pvmemcontrol_vmm_info)); + dev_dbg(&dev->pci_dev->dev, + "pvmemcontrol_vmm_info.ret_errno = %u\n" + "pvmemcontrol_vmm_info.ret_code = %u\n" + "pvmemcontrol_vmm_info.major_version = %llu\n" + "pvmemcontrol_vmm_info.minor_version = %llu\n" + "pvmemcontrol_vmm_info.page_size = %llu\n", + dev->pvmemcontrol_vmm_info.ret_errno, + dev->pvmemcontrol_vmm_info.ret_code, + dev->pvmemcontrol_vmm_info.arg0, + dev->pvmemcontrol_vmm_info.arg1, + dev->pvmemcontrol_vmm_info.ret_value); + + return 0; +} + +static int pvmemcontrol_open(struct inode *inode, struct file *filp) +{ + struct pvmemcontrol_file_private *priv = NULL; + + if (!capable(CAP_SYS_ADMIN)) + return -EACCES; + + /* Do not allow exclusive open */ + if (filp->f_flags & O_EXCL) + return -EINVAL; + + priv = kzalloc(sizeof(struct pvmemcontrol_file_private), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->dev = container_of(inode->i_cdev, struct pvmemcontrol, cdev); + filp->private_data = priv; + return 0; +} + +static int pvmemcontrol_release(struct inode *inode, struct file *filp) +{ + kfree(filp->private_data); + filp->private_data = NULL; + return 0; +} + +static long pvmemcontrol_ioctl(struct file *filp, unsigned int cmd, + unsigned long ioctl_param) +{ + struct pvmemcontrol_file_private *priv = filp->private_data; + struct pvmemcontrol *p = priv->dev; + struct pvmemcontrol_buf *buf = &priv->buf; + + if (cmd != PVMEMCONTROL_IOCTL_VMM) + return -EINVAL; + + if (copy_from_user(&buf->call, (void __user *)ioctl_param, + sizeof(struct pvmemcontrol_buf))) + return -EFAULT; + + pvmemcontrol_vmm_call(p, buf); + + if (copy_to_user((void __user *)ioctl_param, &buf->ret, + sizeof(struct pvmemcontrol_buf))) + return -EFAULT; + + return 0; +} + +static const struct file_operations pvmemcontrol_fops = { + .owner = THIS_MODULE, + .open = pvmemcontrol_open, + .release = pvmemcontrol_release, + .unlocked_ioctl = pvmemcontrol_ioctl, + .compat_ioctl = compat_ptr_ioctl, +}; + +static int pvmemcontrol_connect(struct pvmemcontrol *pvmemcontrol) +{ + int cpu; + u32 cmd; + + pvmemcontrol_write_command(pvmemcontrol->base_addr, + PVMEMCONTROL_TRANSPORT_RESET); + cmd = pvmemcontrol_read_command(pvmemcontrol->base_addr); + if (cmd != PVMEMCONTROL_TRANSPORT_ACK) { + dev_err(&pvmemcontrol->pci_dev->dev, + "failed to reset device, cmd 0x%x\n", cmd); + return -EINVAL; + } + + for_each_possible_cpu(cpu) { + struct pvmemcontrol_percpu_channel *channel = + per_cpu_ptr(pvmemcontrol->pcpu_channels, cpu); + + pvmemcontrol_write_reg(pvmemcontrol->base_addr, + channel->buf_phys_addr); + pvmemcontrol_write_command(pvmemcontrol->base_addr, + PVMEMCONTROL_TRANSPORT_REGISTER); + + cmd = pvmemcontrol_read_command(pvmemcontrol->base_addr); + if (cmd != PVMEMCONTROL_TRANSPORT_ACK) { + dev_err(&pvmemcontrol->pci_dev->dev, + "failed to register pcpu buf, cmd 0x%x\n", cmd); + return -EINVAL; + } + channel->command = + pvmemcontrol_read_resp(pvmemcontrol->base_addr); + } + + pvmemcontrol_write_command(pvmemcontrol->base_addr, + PVMEMCONTROL_TRANSPORT_READY); + cmd = pvmemcontrol_read_command(pvmemcontrol->base_addr); + if (cmd != PVMEMCONTROL_TRANSPORT_ACK) { + dev_err(&pvmemcontrol->pci_dev->dev, + "failed to ready device, cmd 0x%x\n", cmd); + return -EINVAL; + } + return 0; +} + +static int pvmemcontrol_disconnect(struct pvmemcontrol *pvmemcontrol) +{ + u32 cmd; + + pvmemcontrol_write_command(pvmemcontrol->base_addr, + PVMEMCONTROL_TRANSPORT_DISCONNECT); + + cmd = pvmemcontrol_read_command(pvmemcontrol->base_addr); + if (cmd != PVMEMCONTROL_TRANSPORT_ERROR) { + dev_err(&pvmemcontrol->pci_dev->dev, + "failed to disconnect device, cmd 0x%x\n", cmd); + return -EINVAL; + } + return 0; +} + +static int pvmemcontrol_alloc_percpu_channels(struct pvmemcontrol *pvmemcontrol) +{ + int cpu; + + pvmemcontrol->pcpu_channels = alloc_percpu_gfp( + struct pvmemcontrol_percpu_channel, GFP_ATOMIC | __GFP_ZERO); + if (!pvmemcontrol->pcpu_channels) + return -ENOMEM; + + for_each_possible_cpu(cpu) { + struct pvmemcontrol_percpu_channel *channel = + per_cpu_ptr(pvmemcontrol->pcpu_channels, cpu); + phys_addr_t buf_phys = per_cpu_ptr_to_phys(&channel->buf); + + channel->buf_phys_addr = buf_phys; + } + return 0; +} + +static const struct class pvmemcontrol_class = { + .name = "pvmemcontrol", +}; + +static int pvmemcontrol_init(struct pci_dev *pci_dev, void __iomem *base_addr) +{ + struct pvmemcontrol_buf *buf = NULL; + struct pvmemcontrol *pvmemcontrol = NULL; + int err = 0; + dev_t dev; + int minor; + + pvmemcontrol = kzalloc(sizeof(struct pvmemcontrol), GFP_ATOMIC); + buf = kzalloc(sizeof(struct pvmemcontrol_buf), GFP_ATOMIC); + if (!pvmemcontrol || !buf) { + err = -ENOMEM; + goto fail_free; + } + + minor = ida_alloc_max(&pvmemcontrol_minors_ida, MINORMASK, GFP_KERNEL); + if (minor < 0) { + err = minor; + goto fail_free; + } + pvmemcontrol->minor_number = minor; + pvmemcontrol->base_addr = base_addr; + pvmemcontrol->pci_dev = pci_dev; + + cdev_init(&pvmemcontrol->cdev, &pvmemcontrol_fops); + dev = MKDEV(major_num, minor); + err = cdev_add(&pvmemcontrol->cdev, dev, 1); + if (err) + goto fail_minor; + + pvmemcontrol->cdev_device = device_create( + &pvmemcontrol_class, NULL, dev, NULL, "pvmemcontrol%u", minor); + if (IS_ERR(pvmemcontrol->cdev_device)) + goto fail_cdev; + + err = pvmemcontrol_alloc_percpu_channels(pvmemcontrol); + if (err) + goto fail_dev_create; + + err = pvmemcontrol_connect(pvmemcontrol); + if (err) + goto fail_free_percpu; + + err = pvmemcontrol_init_info(pvmemcontrol, buf); + if (err) + goto fail_disconnect; + + pci_set_drvdata(pci_dev, pvmemcontrol); + kfree(buf); + return 0; + +fail_disconnect: + pvmemcontrol_disconnect(pvmemcontrol); +fail_free_percpu: + free_percpu(pvmemcontrol->pcpu_channels); +fail_dev_create: + device_destroy(&pvmemcontrol_class, dev); +fail_cdev: + cdev_del(&pvmemcontrol->cdev); +fail_minor: + ida_free(&pvmemcontrol_minors_ida, minor); +fail_free: + kfree(pvmemcontrol); + kfree(buf); + return err; +} + +static int pvmemcontrol_pci_probe(struct pci_dev *pci_dev, + const struct pci_device_id *id) +{ + void __iomem *base_addr; + int err; + + err = pcim_enable_device(pci_dev); + if (err < 0) + return err; + + base_addr = pcim_iomap(pci_dev, 0, 0); + if (!base_addr) + return -ENOMEM; + + err = pvmemcontrol_init(pci_dev, base_addr); + return err; +} + +static void pvmemcontrol_pci_remove(struct pci_dev *pci_dev) +{ + int err; + struct pvmemcontrol *dev; + + dev = (struct pvmemcontrol *)pci_get_drvdata(pci_dev); + if (!dev) { + pci_err(pci_dev, "cleanup with no drvdata"); + return; + } + + err = pvmemcontrol_disconnect(dev); + if (err) + dev_err(&pci_dev->dev, "device did not ack disconnect\n"); + /* free percpu channels */ + free_percpu(dev->pcpu_channels); + device_destroy(&pvmemcontrol_class, dev->cdev.dev); + cdev_del(&dev->cdev); + ida_free(&pvmemcontrol_minors_ida, dev->minor_number); + kfree(dev); +} + +static const struct pci_device_id pvmemcontrol_pci_id_tbl[] = { + { PCI_DEVICE(PCI_VENDOR_ID_GOOGLE, PCI_DEVICE_ID_GOOGLE_PVMEMCONTROL) }, + { 0 } +}; +MODULE_DEVICE_TABLE(pci, pvmemcontrol_pci_id_tbl); + +static struct pci_driver pvmemcontrol_pci_driver = { + .name = "pvmemcontrol", + .id_table = pvmemcontrol_pci_id_tbl, + .probe = pvmemcontrol_pci_probe, + .remove = pvmemcontrol_pci_remove, +}; + +static int __init pvmemcontrol_driver_init(void) +{ + int err; + dev_t dev; + + err = class_register(&pvmemcontrol_class); + if (err) + return err; + + err = alloc_chrdev_region(&dev, 0, MINORMASK, "pvmemcontrol"); + if (err) + goto fail_class; + WRITE_ONCE(major_num, MAJOR(dev)); + + err = pci_register_driver(&pvmemcontrol_pci_driver); + if (err) + goto fail_chrdev; + + return 0; +fail_chrdev: + unregister_chrdev_region(major_num, MINORMASK); +fail_class: + class_unregister(&pvmemcontrol_class); + return err; +} + +static void __exit pvmemcontrol_driver_exit(void) +{ + pci_unregister_driver(&pvmemcontrol_pci_driver); + unregister_chrdev_region(major_num, MINORMASK); + class_unregister(&pvmemcontrol_class); +} + +module_init(pvmemcontrol_driver_init); +module_exit(pvmemcontrol_driver_exit); + +MODULE_AUTHOR("Yuanchu Xie "); +MODULE_DESCRIPTION("pvmemcontrol Guest Service Module"); +MODULE_LICENSE("GPL"); diff --git a/include/uapi/linux/pvmemcontrol.h b/include/uapi/linux/pvmemcontrol.h new file mode 100644 index 000000000000..31b366dee796 --- /dev/null +++ b/include/uapi/linux/pvmemcontrol.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Userspace interface for /dev/pvmemcontrol + * pvmemcontrol Guest Memory Service Module + * + * Copyright (c) 2024, Google LLC. + * Yuanchu Xie + * Pasha Tatashin + */ + +#ifndef _UAPI_PVMEMCONTROL_H +#define _UAPI_PVMEMCONTROL_H + +#include +#include +#include + +/* Contains the function code and arguments for specific function */ +struct pvmemcontrol_vmm_call { + __u64 func_code; /* pvmemcontrol set function code */ + __u64 addr; /* hyper. page size aligned guest phys. addr */ + __u64 length; /* hyper. page size aligned length */ + __u64 arg; /* function code specific argument */ +}; + +/* Is filled on return to guest from VMM from most function calls */ +struct pvmemcontrol_vmm_ret { + __u32 ret_errno; /* on error, value of errno */ + __u32 ret_code; /* pvmemcontrol internal error code, on success 0 */ + __u64 ret_value; /* return value from the function call */ + __u64 arg0; /* major version for func_code INFO */ + __u64 arg1; /* minor version for func_code INFO */ +}; + +struct pvmemcontrol_buf { + union { + struct pvmemcontrol_vmm_call call; + struct pvmemcontrol_vmm_ret ret; + }; +}; + +/* The ioctl type, documented in ioctl-number.rst */ +#define PVMEMCONTROL_IOCTL_TYPE 0xDA + +#define PVMEMCONTROL_IOCTL_VMM _IOWR(PVMEMCONTROL_IOCTL_TYPE, 0x00, struct pvmemcontrol_buf) + +/* + * Returns the host page size in ret_value. + * major version in arg0. + * minor version in arg1. + */ +#define PVMEMCONTROL_INFO 0 + +/* Pvmemcontrol calls, pvmemcontrol_vmm_return is returned */ +#define PVMEMCONTROL_DONTNEED 1 /* madvise(addr, len, MADV_DONTNEED); */ +#define PVMEMCONTROL_REMOVE 2 /* madvise(addr, len, MADV_MADV_REMOVE); */ +#define PVMEMCONTROL_FREE 3 /* madvise(addr, len, MADV_FREE); */ +#define PVMEMCONTROL_PAGEOUT 4 /* madvise(addr, len, MADV_PAGEOUT); */ +#define PVMEMCONTROL_DONTDUMP 5 /* madvise(addr, len, MADV_DONTDUMP); */ + +/* prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, addr, len, arg) */ +#define PVMEMCONTROL_SET_VMA_ANON_NAME 6 + +#define PVMEMCONTROL_MLOCK 7 /* mlock2(addr, len, 0) */ +#define PVMEMCONTROL_MUNLOCK 8 /* munlock(addr, len) */ + +#define PVMEMCONTROL_MPROTECT_NONE 9 /* mprotect(addr, len, PROT_NONE) */ +#define PVMEMCONTROL_MPROTECT_R 10 /* mprotect(addr, len, PROT_READ) */ +#define PVMEMCONTROL_MPROTECT_W 11 /* mprotect(addr, len, PROT_WRITE) */ +/* mprotect(addr, len, PROT_READ | PROT_WRITE) */ +#define PVMEMCONTROL_MPROTECT_RW 12 + +#define PVMEMCONTROL_MERGEABLE 13 /* madvise(addr, len, MADV_MERGEABLE); */ +#define PVMEMCONTROL_UNMERGEABLE 14 /* madvise(addr, len, MADV_UNMERGEABLE); */ + +#endif /* _UAPI_PVMEMCONTROL_H */ diff --git a/mm/percpu.c b/mm/percpu.c index d8dd31a2e407..87d69b17d979 100644 --- a/mm/percpu.c +++ b/mm/percpu.c @@ -2384,6 +2384,7 @@ phys_addr_t per_cpu_ptr_to_phys(void *addr) return page_to_phys(pcpu_addr_to_page(addr)) + offset_in_page(addr); } +EXPORT_SYMBOL_GPL(per_cpu_ptr_to_phys); /** * pcpu_alloc_alloc_info - allocate percpu allocation info