From patchwork Wed Apr 3 14:01:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vineeth Remanan Pillai X-Patchwork-Id: 13616214 Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BED2D148FE2 for ; Wed, 3 Apr 2024 14:01:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152886; cv=none; b=UD+4oMrbYvlRPhWbfdmx1oWuuJEBq1/2CkCB1ty4nvvKKOtHJGvPnILaItzPpxdEgEF/zHNlWqLv8x2KqCKUznbpza7mMPa9KIBQBD8HzNvy7u7idcwoGjzjfSJs7FuwauqCc5OLxTF1dcx8qvYbJMIkA37Fw+F+ExNH4gOyltQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152886; c=relaxed/simple; bh=lpEab+19m9Mx7BY7SwMTKJYnA5tlJh+0D6EDugugn/0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TbQX4K/e2/8cG61Cs3HfwpLW9BSMweLSCqqqNddj3O6ZrO9DG1f0O0Z6Xx/fQFjKUI30Xv89EomVvKpBLt/W2RtKtKoEvZJisKpL3qWo6vGoVWx3BNDk+9wPajlX9w23tgzSLrQvermeixPzaKftdanViKgDyLCW6tgPygfWi9U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org; spf=pass smtp.mailfrom=bitbyteword.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b=TZOnuuqC; arc=none smtp.client-ip=209.85.222.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b="TZOnuuqC" Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-78d33e20cbfso80406685a.0 for ; Wed, 03 Apr 2024 07:01:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bitbyteword.org; s=google; t=1712152884; x=1712757684; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UONGPslpRt306kqZsCmGTE/b37ydkUsAJesjvT1tJpY=; b=TZOnuuqCWQeykS4xx0VCry+S8i7MXpSO9vMoFgCzqKiZyCp/I8S2Um7vg0thOCZgJf 7UaNcUxP3yUXZZT6TQ8O5f8N/jalmIxY1FnTf1Uhkw0FUeP2Y13+EBpKchrPPJWorJdW I3HrlKMeGHvl7cziEfxEMhY+RMAEYlSuCFwUP3iUzd/05ziPD/hWiwjohlkAnzWJfDGx Jbkg1na5dkHQLAHjVqBbTtG8fibHn2zZTQicobvWq2GG/DrCUT8lSZGf31GjTJ5m6RkQ O6Cofmxlwr/ey490UI32Vb7SRyJBjHS4FzaQ72EGEFKltjhfFbAKar07t4Cyspz5Plsh UAQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712152884; x=1712757684; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UONGPslpRt306kqZsCmGTE/b37ydkUsAJesjvT1tJpY=; b=Zh5nDCJ5B14jvt4bElKvnc4MRjdsP8t+3ZCKFGJWFW8HsFGDln8XlTwZvstnm0VLXB yIGNNgSa9cSg7cpex0sIJPCxtP3V4U9yIdpW2qi9guSV2bzRGk8+MzuAkwYRbOt5ueVq 4maoXWFBUMbegvZexE8WbFiTi1chojJdXvCA6ZioDOAVeeDVfZq1MyD4cE53N9cTCuJR LxaoLGiozKuU5Rn82FSmiobt4BY4LomB0KBwVvqNC8Svjw4Ch+ysGJu6ZUgkJbWhPa3r XrLVrSRiQ82DkbRz629cFK92bedjGhWBr2/M8epXcfuh7XWrQGd8nhtOIOe1IOd6bYNh c04g== X-Forwarded-Encrypted: i=1; AJvYcCWNO5j5YXsD4lw5YYfzNikzUkb93ByOEU2N5v9/zb1x+vsE7k1WOq0zzjWrPq1mPLfsa+KqF82kRPuaJs37a2a31Nog X-Gm-Message-State: AOJu0Yy3UV0/487ysTH1S0WynAR/k8HzLHsxEHqkSn6VkXABanxEbRKR 9crdtwJ+sMR+YkMfDOxpbHmqFRW5KxwfZGJRn+JD5+QL1kTl5XH+X0hUAqv6o4A= X-Google-Smtp-Source: AGHT+IHyvTEQNqSIQcjpHeGqJ7SNEPZRIYtalkAiLrynTgcv4Dtsm9x9wS8IfPIQOAxIX49l9lI3dg== X-Received: by 2002:a05:6214:8f2:b0:699:2ad4:3dc4 with SMTP id dr18-20020a05621408f200b006992ad43dc4mr1251372qvb.42.1712152883304; Wed, 03 Apr 2024 07:01:23 -0700 (PDT) Received: from vinbuntup3.lan (c-73-143-21-186.hsd1.vt.comcast.net. [73.143.21.186]) by smtp.gmail.com with ESMTPSA id gf12-20020a056214250c00b00698d06df322sm5945706qvb.122.2024.04.03.07.01.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 07:01:22 -0700 (PDT) From: "Vineeth Pillai (Google)" To: Ben Segall , Borislav Petkov , Daniel Bristot de Oliveira , Dave Hansen , Dietmar Eggemann , "H . Peter Anvin" , Ingo Molnar , Juri Lelli , Mel Gorman , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , Thomas Gleixner , Valentin Schneider , Vincent Guittot , Vitaly Kuznetsov , Wanpeng Li Cc: "Vineeth Pillai (Google)" , Steven Rostedt , Joel Fernandes , Suleiman Souhlal , Masami Hiramatsu , himadrics@inria.fr, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH v2 1/5] pvsched: paravirt scheduling framework Date: Wed, 3 Apr 2024 10:01:12 -0400 Message-Id: <20240403140116.3002809-2-vineeth@bitbyteword.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240403140116.3002809-1-vineeth@bitbyteword.org> References: <20240403140116.3002809-1-vineeth@bitbyteword.org> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Implement a paravirt scheduling framework for linux kernel. The framework allows for pvsched driver to register to the kernel and receive callbacks from hypervisor(eg: kvm) for interested vcpu events like VMENTER, VMEXIT etc. The framework also allows hypervisor to select a pvsched driver (from the available list of registered drivers) for each guest. Also implement a sysctl for listing the available pvsched drivers. Signed-off-by: Vineeth Pillai (Google) Signed-off-by: Joel Fernandes (Google) --- Kconfig | 2 + include/linux/pvsched.h | 102 +++++++++++++++++++ kernel/sysctl.c | 27 +++++ virt/Makefile | 2 +- virt/pvsched/Kconfig | 12 +++ virt/pvsched/Makefile | 2 + virt/pvsched/pvsched.c | 215 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 361 insertions(+), 1 deletion(-) create mode 100644 include/linux/pvsched.h create mode 100644 virt/pvsched/Kconfig create mode 100644 virt/pvsched/Makefile create mode 100644 virt/pvsched/pvsched.c diff --git a/Kconfig b/Kconfig index 745bc773f567..4a52eaa21166 100644 --- a/Kconfig +++ b/Kconfig @@ -29,4 +29,6 @@ source "lib/Kconfig" source "lib/Kconfig.debug" +source "virt/pvsched/Kconfig" + source "Documentation/Kconfig" diff --git a/include/linux/pvsched.h b/include/linux/pvsched.h new file mode 100644 index 000000000000..59df6b44aacb --- /dev/null +++ b/include/linux/pvsched.h @@ -0,0 +1,102 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2024 Google */ + +#ifndef _LINUX_PVSCHED_H +#define _LINUX_PVSCHED_H 1 + +/* + * List of events for which hypervisor calls back into pvsched driver. + * Driver can specify the events it is interested in. + */ +enum pvsched_vcpu_events { + PVSCHED_VCPU_VMENTER = 0x1, + PVSCHED_VCPU_VMEXIT = 0x2, + PVSCHED_VCPU_HALT = 0x4, + PVSCHED_VCPU_INTR_INJ = 0x8, +}; + +#define PVSCHED_NAME_MAX 32 +#define PVSCHED_MAX 8 +#define PVSCHED_DRV_BUF_MAX (PVSCHED_NAME_MAX * PVSCHED_MAX + PVSCHED_MAX) + +/* + * pvsched driver callbacks. + * TODO: versioning support for better compatibility with the guest + * component implementing this feature. + */ +struct pvsched_vcpu_ops { + /* + * pvsched_vcpu_register() - Register the vcpu with pvsched driver. + * @pid: pid of the vcpu task. + * + * pvsched driver can store the pid internally and initialize + * itself to prepare for receiving callbacks from thsi vcpu. + */ + int (*pvsched_vcpu_register)(struct pid *pid); + + /* + * pvsched_vcpu_unregister() - Un-register the vcpu with pvsched driver. + * @pid: pid of the vcpu task. + */ + void (*pvsched_vcpu_unregister)(struct pid *pid); + + /* + * pvsched_vcpu_notify_event() - Callback for pvsched events + * @addr: Address of the memory region shared with guest + * @pid: pid of the vcpu task. + * @events: bit mask of the events that hypervisor wants to notify. + */ + void (*pvsched_vcpu_notify_event)(void *addr, struct pid *pid, u32 event); + + char name[PVSCHED_NAME_MAX]; + struct module *owner; + struct list_head list; + u32 events; + u32 key; +}; + +#ifdef CONFIG_PARAVIRT_SCHED_HOST +int pvsched_get_available_drivers(char *buf, size_t maxlen); + +int pvsched_register_vcpu_ops(struct pvsched_vcpu_ops *ops); +void pvsched_unregister_vcpu_ops(struct pvsched_vcpu_ops *ops); + +struct pvsched_vcpu_ops *pvsched_get_vcpu_ops(char *name); +void pvsched_put_vcpu_ops(struct pvsched_vcpu_ops *ops); + +static inline int pvsched_validate_vcpu_ops(struct pvsched_vcpu_ops *ops) +{ + /* + * All callbacks are mandatory. + */ + if (!ops->pvsched_vcpu_register || !ops->pvsched_vcpu_unregister || + !ops->pvsched_vcpu_notify_event) + return -EINVAL; + + return 0; +} +#else +static inline void pvsched_get_available_drivers(char *buf, size_t maxlen) +{ +} + +static inline int pvsched_register_vcpu_ops(struct pvsched_vcpu_ops *ops) +{ + return -ENOTSUPP; +} + +static inline void pvsched_unregister_vcpu_ops(struct pvsched_vcpu_ops *ops) +{ +} + +static inline struct pvsched_vcpu_ops *pvsched_get_vcpu_ops(char *name) +{ + return NULL; +} + +static inline void pvsched_put_vcpu_ops(struct pvsched_vcpu_ops *ops) +{ +} +#endif + +#endif diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 157f7ce2942d..10a18a791b4f 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -63,6 +63,7 @@ #include #include #include +#include #include "../lib/kstrtox.h" @@ -1615,6 +1616,24 @@ int proc_do_static_key(struct ctl_table *table, int write, return ret; } +#ifdef CONFIG_PARAVIRT_SCHED_HOST +static int proc_pvsched_available_drivers(struct ctl_table *ctl, + int write, void *buffer, + size_t *lenp, loff_t *ppos) +{ + struct ctl_table tbl = { .maxlen = PVSCHED_DRV_BUF_MAX, }; + int ret; + + tbl.data = kmalloc(tbl.maxlen, GFP_USER); + if (!tbl.data) + return -ENOMEM; + pvsched_get_available_drivers(tbl.data, PVSCHED_DRV_BUF_MAX); + ret = proc_dostring(&tbl, write, buffer, lenp, ppos); + kfree(tbl.data); + return ret; +} +#endif + static struct ctl_table kern_table[] = { { .procname = "panic", @@ -2033,6 +2052,14 @@ static struct ctl_table kern_table[] = { .extra1 = SYSCTL_ONE, .extra2 = SYSCTL_INT_MAX, }, +#endif +#ifdef CONFIG_PARAVIRT_SCHED_HOST + { + .procname = "pvsched_available_drivers", + .maxlen = PVSCHED_DRV_BUF_MAX, + .mode = 0444, + .proc_handler = proc_pvsched_available_drivers, + }, #endif { } }; diff --git a/virt/Makefile b/virt/Makefile index 1cfea9436af9..9d0f32d775a1 100644 --- a/virt/Makefile +++ b/virt/Makefile @@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y += lib/ +obj-y += lib/ pvsched/ diff --git a/virt/pvsched/Kconfig b/virt/pvsched/Kconfig new file mode 100644 index 000000000000..5ca2669060cb --- /dev/null +++ b/virt/pvsched/Kconfig @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-only +config PARAVIRT_SCHED_HOST + bool "Paravirt scheduling framework in the host kernel" + default n + help + Paravirtualized scheduling facilitates the exchange of scheduling + related information between the host and guest through shared memory, + enhancing the efficiency of vCPU thread scheduling by the hypervisor. + An illustrative use case involves dynamically boosting the priority of + a vCPU thread when the guest is executing a latency-sensitive workload + on that specific vCPU. + This config enables paravirt scheduling framework in the host kernel. diff --git a/virt/pvsched/Makefile b/virt/pvsched/Makefile new file mode 100644 index 000000000000..4ca38e30479b --- /dev/null +++ b/virt/pvsched/Makefile @@ -0,0 +1,2 @@ + +obj-$(CONFIG_PARAVIRT_SCHED_HOST) += pvsched.o diff --git a/virt/pvsched/pvsched.c b/virt/pvsched/pvsched.c new file mode 100644 index 000000000000..610c85cf90d2 --- /dev/null +++ b/virt/pvsched/pvsched.c @@ -0,0 +1,215 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024 Google */ + +/* + * Paravirt scheduling framework + * + */ + +/* + * Heavily inspired from tcp congestion avoidance implementation. + * (net/ipv4/tcp_cong.c) + */ + +#define pr_fmt(fmt) "PVSCHED: " fmt + +#include +#include +#include +#include +#include +#include +#include + +static DEFINE_SPINLOCK(pvsched_drv_list_lock); +static int nr_pvsched_drivers = 0; +static LIST_HEAD(pvsched_drv_list); + +/* + * Retrieve pvsched_vcpu_ops given the name. + */ +static struct pvsched_vcpu_ops *pvsched_find_vcpu_ops_name(char *name) +{ + struct pvsched_vcpu_ops *ops; + + list_for_each_entry_rcu(ops, &pvsched_drv_list, list) { + if (strcmp(ops->name, name) == 0) + return ops; + } + + return NULL; +} + +/* + * Retrieve pvsched_vcpu_ops given the hash key. + */ +static struct pvsched_vcpu_ops *pvsched_find_vcpu_ops_key(u32 key) +{ + struct pvsched_vcpu_ops *ops; + + list_for_each_entry_rcu(ops, &pvsched_drv_list, list) { + if (ops->key == key) + return ops; + } + + return NULL; +} + +/* + * pvsched_get_available_drivers() - Copy space separated list of pvsched + * driver names. + * @buf: buffer to store the list of driver names + * @maxlen: size of the buffer + * + * Return: 0 on success, negative value on error. + */ +int pvsched_get_available_drivers(char *buf, size_t maxlen) +{ + struct pvsched_vcpu_ops *ops; + size_t offs = 0; + + if (!buf) + return -EINVAL; + + if (maxlen > PVSCHED_DRV_BUF_MAX) + maxlen = PVSCHED_DRV_BUF_MAX; + + rcu_read_lock(); + list_for_each_entry_rcu(ops, &pvsched_drv_list, list) { + offs += snprintf(buf + offs, maxlen - offs, + "%s%s", + offs == 0 ? "" : " ", ops->name); + + if (WARN_ON_ONCE(offs >= maxlen)) + break; + } + rcu_read_unlock(); + + return 0; +} +EXPORT_SYMBOL_GPL(pvsched_get_available_drivers); + +/* + * pvsched_register_vcpu_ops() - Register the driver in the kernel. + * @ops: Driver data(callbacks) + * + * After the registration, driver will be exposed to the hypervisor + * for assignment to the guest VMs. + * + * Return: 0 on success, negative value on error. + */ +int pvsched_register_vcpu_ops(struct pvsched_vcpu_ops *ops) +{ + int ret = 0; + + ops->key = jhash(ops->name, sizeof(ops->name), strlen(ops->name)); + spin_lock(&pvsched_drv_list_lock); + if (nr_pvsched_drivers > PVSCHED_MAX) { + ret = -ENOSPC; + } if (pvsched_find_vcpu_ops_key(ops->key)) { + ret = -EEXIST; + } else if (!(ret = pvsched_validate_vcpu_ops(ops))) { + list_add_tail_rcu(&ops->list, &pvsched_drv_list); + nr_pvsched_drivers++; + } + spin_unlock(&pvsched_drv_list_lock); + + return ret; +} +EXPORT_SYMBOL_GPL(pvsched_register_vcpu_ops); + +/* + * pvsched_register_vcpu_ops() - Un-register the driver from the kernel. + * @ops: Driver data(callbacks) + * + * After un-registration, driver will not be visible to hypervisor. + */ +void pvsched_unregister_vcpu_ops(struct pvsched_vcpu_ops *ops) +{ + spin_lock(&pvsched_drv_list_lock); + list_del_rcu(&ops->list); + nr_pvsched_drivers--; + spin_unlock(&pvsched_drv_list_lock); + + synchronize_rcu(); +} +EXPORT_SYMBOL_GPL(pvsched_unregister_vcpu_ops); + +/* + * pvsched_get_vcpu_ops: Acquire the driver. + * @name: Name of the driver to be acquired. + * + * Hypervisor can use this API to get the driver structure for + * assigning it to guest VMs. This API takes a reference on the + * module/bpf program so that driver doesn't vanish under the + * hypervisor. + * + * Return: driver structure if found, else NULL. + */ +struct pvsched_vcpu_ops *pvsched_get_vcpu_ops(char *name) +{ + struct pvsched_vcpu_ops *ops; + + if (!name || (strlen(name) >= PVSCHED_NAME_MAX)) + return NULL; + + rcu_read_lock(); + ops = pvsched_find_vcpu_ops_name(name); + if (!ops) + goto out; + + if (unlikely(!bpf_try_module_get(ops, ops->owner))) { + ops = NULL; + goto out; + } + +out: + rcu_read_unlock(); + return ops; +} +EXPORT_SYMBOL_GPL(pvsched_get_vcpu_ops); + +/* + * pvsched_put_vcpu_ops: Release the driver. + * @name: Name of the driver to be releases. + * + * Hypervisor can use this API to release the driver. + */ +void pvsched_put_vcpu_ops(struct pvsched_vcpu_ops *ops) +{ + bpf_module_put(ops, ops->owner); +} +EXPORT_SYMBOL_GPL(pvsched_put_vcpu_ops); + +/* + * NOP vm_ops Sample implementation. + * This driver doesn't do anything other than registering itself. + * Placeholder for adding some default logic when the feature is + * complete. + */ +static int nop_pvsched_vcpu_register(struct pid *pid) +{ + return 0; +} +static void nop_pvsched_vcpu_unregister(struct pid *pid) +{ +} +static void nop_pvsched_notify_event(void *addr, struct pid *pid, u32 event) +{ +} + +struct pvsched_vcpu_ops nop_vcpu_ops = { + .events = PVSCHED_VCPU_VMENTER | PVSCHED_VCPU_VMEXIT | PVSCHED_VCPU_HALT, + .pvsched_vcpu_register = nop_pvsched_vcpu_register, + .pvsched_vcpu_unregister = nop_pvsched_vcpu_unregister, + .pvsched_vcpu_notify_event = nop_pvsched_notify_event, + .name = "pvsched_nop", + .owner = THIS_MODULE, +}; + +static int __init pvsched_init(void) +{ + return WARN_ON(pvsched_register_vcpu_ops(&nop_vcpu_ops)); +} + +late_initcall(pvsched_init); From patchwork Wed Apr 3 14:01:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vineeth Remanan Pillai X-Patchwork-Id: 13616215 Received: from mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26503148305 for ; Wed, 3 Apr 2024 14:01:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152887; cv=none; b=NIYI9LO4S9gAYPkWBtGWFo1nSruO/lvUaHHtMsSWsdxgcI8nWlaus2TZKHCD3E6ZYLr4SZk2X3sbj5PCR/QGcxEfOH5ta2ENJ6ZNevHnlUVbUZEf44eVXe+soGxY0XOW8Qp1Hwwk5DcqZm/X9SnQvY8fw8vhCjnBSutwAwRXDfU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152887; c=relaxed/simple; bh=TDctzrjvOyJsGkgtTdTPKD3E/sGdBBWNU2PAEkT4FWw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RCUk/Lus4MjD40IIHqDksfZZv9TLSl93sflWRBCGmerrbThu0O+Lzc2mzbCL1CkhYgu/yLttr2ftlZVwwFG9nMl1cSiQPijgsRzDdkHpmPlTdN6uULnkBmyqLz1xttzCckto9NgzuqZr6AfRNfAtMSU12ilgkL7zbdH4pSYXsbw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org; spf=pass smtp.mailfrom=bitbyteword.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b=JH9KYukU; arc=none smtp.client-ip=209.85.219.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b="JH9KYukU" Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-6992ec4bbf1so261186d6.2 for ; Wed, 03 Apr 2024 07:01:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bitbyteword.org; s=google; t=1712152885; x=1712757685; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/BPGVzMI1+eo9yvIAgnlTxqk+J0LKPxHXNZYDNRJA58=; b=JH9KYukU0oTCHZDEXfYpMIVEVyCtwpFQMM6lm1YY/9JXIcKNCh/Y4EHvwHXbqt7R/4 fJ8avagTe7AKzsl5yQxW/IRyoPOqTWhnNwznhHn/ooov6AULZBak0k68dKKa58jJ0vYc cy+3FI7DQjSnugPZsk1ilrIeoBJdbRRq7JjS0p+OBCYP75NikQ9NfnWSNfDv6kbRm0V6 ERYOR9M6GuYi3KiJk958KYx1NxHYX5GIkk0MMcm5h6C/t6l937gvr/slczv6dioileq8 i6eHTCmYstWhbg25ZLXSro6urFSxJrJY0xnavry+h9hZH4LjRFZqpt6uy7mN1MYbyL3J aOZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712152885; x=1712757685; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/BPGVzMI1+eo9yvIAgnlTxqk+J0LKPxHXNZYDNRJA58=; b=DNIldTQtbX0LxmwpMM3GnnewNH+Kp1tTHC6t3xoWNARgi5qj5JcftWfBrYdrg28mDe oV8LesRN93z1dVakeo1C5bjNDUiT8nUZySY6PoegpCBWOVig0Yj0lP1qEGupaEOFf+92 ZYiAgplk9Cp5z10reqpCVzhR43JPPaqBblZ9P1M2E5IRd1pGNq15a+ltgsxw8VzzhfhV 2/an3vTViUBWk6gchj04aA6TuJwYVCblXD19c7OoREzLkKb52DNZlQfkYC8RdHz1Je5l 0p76Iinq9i1zMb7rUSBRA0DogZVJvNdthSLIq1F/RQPtQEFFQG+6uNTJmuUe8vdAyY6w YsUA== X-Forwarded-Encrypted: i=1; AJvYcCX4eIUkumNySKwTBXUOjZ24E84z3YwoqYqAbWjd6Ru8WXV4ROl8Zyp/wW1vh9x8tZBoimdR6+Mf7cNWxzGPz1Jr8PkZ X-Gm-Message-State: AOJu0YyC7mPg8isnF7FnPgTJpGQQRRa8y5OZD4OBrPFc8l/W/1klvzkD ho95hE9O9o8Ez8uNFD9bNqSorEwHAe2iCw5uf2OIEEBdknlLBharoV9795Id/2I= X-Google-Smtp-Source: AGHT+IGFZcEaZ2LTTabAkPrVqPjWtdW/Cz0Il+zhLsM9yKyyKZpea5NsINjyFBJZ577Vil8whyw/9g== X-Received: by 2002:a0c:c249:0:b0:699:1fd3:95a3 with SMTP id w9-20020a0cc249000000b006991fd395a3mr3804251qvh.24.1712152884922; Wed, 03 Apr 2024 07:01:24 -0700 (PDT) Received: from vinbuntup3.lan (c-73-143-21-186.hsd1.vt.comcast.net. [73.143.21.186]) by smtp.gmail.com with ESMTPSA id gf12-20020a056214250c00b00698d06df322sm5945706qvb.122.2024.04.03.07.01.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 07:01:24 -0700 (PDT) From: "Vineeth Pillai (Google)" To: Ben Segall , Borislav Petkov , Daniel Bristot de Oliveira , Dave Hansen , Dietmar Eggemann , "H . Peter Anvin" , Ingo Molnar , Juri Lelli , Mel Gorman , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , Thomas Gleixner , Valentin Schneider , Vincent Guittot , Vitaly Kuznetsov , Wanpeng Li Cc: "Vineeth Pillai (Google)" , Steven Rostedt , Joel Fernandes , Suleiman Souhlal , Masami Hiramatsu , himadrics@inria.fr, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH v2 2/5] kvm: Implement the paravirt sched framework for kvm Date: Wed, 3 Apr 2024 10:01:13 -0400 Message-Id: <20240403140116.3002809-3-vineeth@bitbyteword.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240403140116.3002809-1-vineeth@bitbyteword.org> References: <20240403140116.3002809-1-vineeth@bitbyteword.org> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 kvm uses the kernel's paravirt sched framework to assign an available pvsched driver for a guest. guest vcpus registers with the pvsched driver and calls into the driver callback to notify the events that the driver is interested in. This PoC doesn't do the callback on interrupt injection yet. Will be implemented in subsequent iterations. Signed-off-by: Vineeth Pillai (Google) Signed-off-by: Joel Fernandes (Google) --- arch/x86/kvm/Kconfig | 13 ++++ arch/x86/kvm/x86.c | 3 + include/linux/kvm_host.h | 32 +++++++++ virt/kvm/kvm_main.c | 148 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 196 insertions(+) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 65ed14b6540b..c1776cdb5b65 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -189,4 +189,17 @@ config KVM_MAX_NR_VCPUS the memory footprint of each KVM guest, regardless of how many vCPUs are created for a given VM. +config PARAVIRT_SCHED_KVM + bool "Enable paravirt scheduling capability for kvm" + depends on KVM + default n + help + Paravirtualized scheduling facilitates the exchange of scheduling + related information between the host and guest through shared memory, + enhancing the efficiency of vCPU thread scheduling by the hypervisor. + An illustrative use case involves dynamically boosting the priority of + a vCPU thread when the guest is executing a latency-sensitive workload + on that specific vCPU. + This config enables paravirt scheduling in the kvm hypervisor. + endif # VIRTUALIZATION diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ffe580169c93..d0abc2c64d47 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10896,6 +10896,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) preempt_disable(); + kvm_vcpu_pvsched_notify(vcpu, PVSCHED_VCPU_VMENTER); + static_call(kvm_x86_prepare_switch_to_guest)(vcpu); /* @@ -11059,6 +11061,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) guest_timing_exit_irqoff(); local_irq_enable(); + kvm_vcpu_pvsched_notify(vcpu, PVSCHED_VCPU_VMEXIT); preempt_enable(); kvm_vcpu_srcu_read_lock(vcpu); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 179df96b20f8..6381569f3de8 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -45,6 +45,8 @@ #include #include +#include + #ifndef KVM_MAX_VCPU_IDS #define KVM_MAX_VCPU_IDS KVM_MAX_VCPUS #endif @@ -832,6 +834,11 @@ struct kvm { bool vm_bugged; bool vm_dead; +#ifdef CONFIG_PARAVIRT_SCHED_KVM + spinlock_t pvsched_ops_lock; + struct pvsched_vcpu_ops __rcu *pvsched_ops; +#endif + #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; #endif @@ -2413,4 +2420,29 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, } #endif /* CONFIG_KVM_PRIVATE_MEM */ +#ifdef CONFIG_PARAVIRT_SCHED_KVM +int kvm_vcpu_pvsched_notify(struct kvm_vcpu *vcpu, u32 events); +int kvm_vcpu_pvsched_register(struct kvm_vcpu *vcpu); +void kvm_vcpu_pvsched_unregister(struct kvm_vcpu *vcpu); + +int kvm_replace_pvsched_ops(struct kvm *kvm, char *name); +#else +static inline int kvm_vcpu_pvsched_notify(struct kvm_vcpu *vcpu, u32 events) +{ + return 0; +} +static inline int kvm_vcpu_pvsched_register(struct kvm_vcpu *vcpu) +{ + return 0; +} +static inline void kvm_vcpu_pvsched_unregister(struct kvm_vcpu *vcpu) +{ +} + +static inline int kvm_replace_pvsched_ops(struct kvm *kvm, char *name) +{ + return 0; +} +#endif + #endif diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0f50960b0e3a..0546814e4db7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -170,6 +170,142 @@ bool kvm_is_zone_device_page(struct page *page) return is_zone_device_page(page); } +#ifdef CONFIG_PARAVIRT_SCHED_KVM +typedef enum { + PVSCHED_CB_REGISTER = 1, + PVSCHED_CB_UNREGISTER = 2, + PVSCHED_CB_NOTIFY = 3 +} pvsched_vcpu_callback_t; + +/* + * Helper function to invoke the pvsched driver callback. + */ +static int __vcpu_pvsched_callback(struct kvm_vcpu *vcpu, u32 events, + pvsched_vcpu_callback_t action) +{ + int ret = 0; + struct pid *pid; + struct pvsched_vcpu_ops *ops; + + rcu_read_lock(); + ops = rcu_dereference(vcpu->kvm->pvsched_ops); + if (!ops) { + ret = -ENOENT; + goto out; + } + + pid = rcu_dereference(vcpu->pid); + if (WARN_ON_ONCE(!pid)) { + ret = -EINVAL; + goto out; + } + get_pid(pid); + switch(action) { + case PVSCHED_CB_REGISTER: + ops->pvsched_vcpu_register(pid); + break; + case PVSCHED_CB_UNREGISTER: + ops->pvsched_vcpu_unregister(pid); + break; + case PVSCHED_CB_NOTIFY: + if (ops->events & events) { + ops->pvsched_vcpu_notify_event( + NULL, /* TODO: Pass guest allocated sharedmem addr */ + pid, + ops->events & events); + } + break; + default: + WARN_ON_ONCE(1); + } + put_pid(pid); + +out: + rcu_read_unlock(); + return ret; +} + +int kvm_vcpu_pvsched_notify(struct kvm_vcpu *vcpu, u32 events) +{ + return __vcpu_pvsched_callback(vcpu, events, PVSCHED_CB_NOTIFY); +} + +int kvm_vcpu_pvsched_register(struct kvm_vcpu *vcpu) +{ + return __vcpu_pvsched_callback(vcpu, 0, PVSCHED_CB_REGISTER); + /* + * TODO: Action if the registration fails? + */ +} + +void kvm_vcpu_pvsched_unregister(struct kvm_vcpu *vcpu) +{ + __vcpu_pvsched_callback(vcpu, 0, PVSCHED_CB_UNREGISTER); +} + +/* + * Replaces the VM's current pvsched driver. + * if name is NULL or empty string, unassign the + * current driver. + */ +int kvm_replace_pvsched_ops(struct kvm *kvm, char *name) +{ + int ret = 0; + unsigned long i; + struct kvm_vcpu *vcpu = NULL; + struct pvsched_vcpu_ops *ops = NULL, *prev_ops; + + + spin_lock(&kvm->pvsched_ops_lock); + + prev_ops = rcu_dereference(kvm->pvsched_ops); + + /* + * Unassign operation if the passed in value is + * NULL or an empty string. + */ + if (name && *name) { + ops = pvsched_get_vcpu_ops(name); + if (!ops) { + ret = -EINVAL; + goto out; + } + } + + if (prev_ops) { + /* + * Unregister current pvsched driver. + */ + kvm_for_each_vcpu(i, vcpu, kvm) { + kvm_vcpu_pvsched_unregister(vcpu); + } + + pvsched_put_vcpu_ops(prev_ops); + } + + + rcu_assign_pointer(kvm->pvsched_ops, ops); + if (ops) { + /* + * Register new pvsched driver. + */ + kvm_for_each_vcpu(i, vcpu, kvm) { + WARN_ON_ONCE(kvm_vcpu_pvsched_register(vcpu)); + } + } + +out: + spin_unlock(&kvm->pvsched_ops_lock); + + if (ret) + return ret; + + synchronize_rcu(); + + return 0; +} +#endif + /* * Returns a 'struct page' if the pfn is "valid" and backed by a refcounted * page, NULL otherwise. Note, the list of refcounted PG_reserved page types @@ -508,6 +644,8 @@ static void kvm_vcpu_destroy(struct kvm_vcpu *vcpu) kvm_arch_vcpu_destroy(vcpu); kvm_dirty_ring_free(&vcpu->dirty_ring); + kvm_vcpu_pvsched_unregister(vcpu); + /* * No need for rcu_read_lock as VCPU_RUN is the only place that changes * the vcpu->pid pointer, and at destruction time all file descriptors @@ -1221,6 +1359,10 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); +#ifdef CONFIG_PARAVIRT_SCHED_KVM + spin_lock_init(&kvm->pvsched_ops_lock); +#endif + /* * Force subsequent debugfs file creations to fail if the VM directory * is not created (by kvm_create_vm_debugfs()). @@ -1343,6 +1485,8 @@ static void kvm_destroy_vm(struct kvm *kvm) int i; struct mm_struct *mm = kvm->mm; + kvm_replace_pvsched_ops(kvm, NULL); + kvm_destroy_pm_notifier(kvm); kvm_uevent_notify_change(KVM_EVENT_DESTROY_VM, kvm); kvm_destroy_vm_debugfs(kvm); @@ -3779,6 +3923,8 @@ bool kvm_vcpu_block(struct kvm_vcpu *vcpu) if (kvm_vcpu_check_block(vcpu) < 0) break; + kvm_vcpu_pvsched_notify(vcpu, PVSCHED_VCPU_HALT); + waited = true; schedule(); } @@ -4434,6 +4580,7 @@ static long kvm_vcpu_ioctl(struct file *filp, /* The thread running this VCPU changed. */ struct pid *newpid; + kvm_vcpu_pvsched_unregister(vcpu); r = kvm_arch_vcpu_run_pid_change(vcpu); if (r) break; @@ -4442,6 +4589,7 @@ static long kvm_vcpu_ioctl(struct file *filp, rcu_assign_pointer(vcpu->pid, newpid); if (oldpid) synchronize_rcu(); + kvm_vcpu_pvsched_register(vcpu); put_pid(oldpid); } r = kvm_arch_vcpu_ioctl_run(vcpu); From patchwork Wed Apr 3 14:01:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vineeth Remanan Pillai X-Patchwork-Id: 13616216 Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A8CF14882F for ; Wed, 3 Apr 2024 14:01:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152900; cv=none; b=GTmM/qtlAbfnkCQcR7ZfSRkAhu52zttC4By1APcQn+csboqwgd653QE9Qj7rhlTQOzA7pQtXqc+rQIDnSTMUOLUwMRHbG+9kY9gMhAPOwrO9RCYaYmjFeJdxp6Yq7R/WCBpWV0q3GnjBTCd9E8hVOidgKqKSDTSgkcuCL95I89E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152900; c=relaxed/simple; bh=FDYnvGX3jppgjt0ms5JcBF+Jc7zw8R2BAEn2b+ABJPI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rNDyYqp0ZaR7EnSr6BBqS8lGzTeoiPiH+u9OLCAZRpbFM+A4e5L/FUIrbdwzxVykNikKl8Y+pawAzh90e+psAmNW7qilOPnyi29DJloP5dZLEa8awGcRlqhj53CTzgIGTAc0S2uanl23WS+/meKmPZalqE6RfB2TprESa1A+djg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org; spf=pass smtp.mailfrom=bitbyteword.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b=bZzaMlBF; arc=none smtp.client-ip=209.85.219.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b="bZzaMlBF" Received: by mail-qv1-f44.google.com with SMTP id 6a1803df08f44-6991fb8f31dso8922216d6.1 for ; Wed, 03 Apr 2024 07:01:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bitbyteword.org; s=google; t=1712152897; x=1712757697; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RAl5yGyZgzeKDHOgPfwgCPdv3gtVirZpx2WY36IzrFA=; b=bZzaMlBFrw/elIW7JgN4qv6+Iv1CiHxkvUozSuzRHwiCmJpMQl9mIeP/VTN8Okgfp3 o84RvGxZ6SuHf+Ll+7aOCl3Zq18WftiqazJqNT3d4K7EzRIHV7YDL2QyRXypudqCzCDY NTKpxJpuYPvbEJzC3InYC69ja2aacA/TssJy09anMpRkKtq9KuF4JJALfUcnjmU4nX3M F8VgTTbyqArV5hI8XrSfZ72sIN1QrdVsKydhOvck3DOBIeIwRgfH497GP0RMOm9m8vx6 o6UDB4erVNpB30TZRWfJtpn7nfzRxSyGUK6AN7u12sKXa4wrvReucbWdm7UhSKOk7ahy xwvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712152897; x=1712757697; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RAl5yGyZgzeKDHOgPfwgCPdv3gtVirZpx2WY36IzrFA=; b=mP5LxkqyFIf+i28xEVAPR3titA3o8ecnEYxga5CTo54JAOUAZ5it4//UxDgSNhJEwO WdhBZc03UUtkgUaJJdIetrbBuMGngKb+dIDWgF1i1qna2aTR0W3I6CJRBVsczsyuYD6C QtjuELpu+zIuSEmukI12QFvs7L1rjrK1Im50EP6nDeRHTc75fSYUvGeNq7j0GSjxh4d/ ulTHoNtAXbC5aIddSbpgofmi51CfEKJlcXTpxe9SUkQCqDs3xta8inSEox/C+MaY/mZR gLQuzU2pB6BBjBnwdLKBFKiaxVlkOHemwCW2q2NGu5NgeTIoDwvI+ulYNgkeBY5jFq2x q3Hw== X-Forwarded-Encrypted: i=1; AJvYcCUR8baky1VPiO9azCCW5/CCoUdiMejsI+tq5gHhZOHnQYn2uPwgoerqC27B/mxpxzUkzf05DPGkHV0a5HiroKgFKilu X-Gm-Message-State: AOJu0YzNf/478HB/Ye5OG2pSGrh4WqJJnIJHyvKi/eezfwgU5EVcUDMo ExuWVK9ZATdqGzdFqOu2q3TXHj9Kf3eTokvuy2k/uGNEZJUr31LCbL/Fa5ww3lM= X-Google-Smtp-Source: AGHT+IHvc+Z8rAGYdV18GoHSVhAuEJhixIxtB7Sprh49Rl10nkC3+gtuPFpYpMsM5sh5ZkIdR/76QA== X-Received: by 2002:a0c:ed4e:0:b0:699:1b6b:82ae with SMTP id v14-20020a0ced4e000000b006991b6b82aemr4264059qvq.17.1712152896489; Wed, 03 Apr 2024 07:01:36 -0700 (PDT) Received: from vinbuntup3.lan (c-73-143-21-186.hsd1.vt.comcast.net. [73.143.21.186]) by smtp.gmail.com with ESMTPSA id gf12-20020a056214250c00b00698d06df322sm5945706qvb.122.2024.04.03.07.01.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 07:01:26 -0700 (PDT) From: "Vineeth Pillai (Google)" To: Ben Segall , Borislav Petkov , Daniel Bristot de Oliveira , Dave Hansen , Dietmar Eggemann , "H . Peter Anvin" , Ingo Molnar , Juri Lelli , Mel Gorman , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , Thomas Gleixner , Valentin Schneider , Vincent Guittot , Vitaly Kuznetsov , Wanpeng Li Cc: "Vineeth Pillai (Google)" , Steven Rostedt , Joel Fernandes , Suleiman Souhlal , Masami Hiramatsu , himadrics@inria.fr, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH v2 3/5] kvm: interface for managing pvsched driver for guest VMs Date: Wed, 3 Apr 2024 10:01:14 -0400 Message-Id: <20240403140116.3002809-4-vineeth@bitbyteword.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240403140116.3002809-1-vineeth@bitbyteword.org> References: <20240403140116.3002809-1-vineeth@bitbyteword.org> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Implement ioctl for assigning and unassigning pvsched driver for a guest. VMMs would need to adopt this ioctls for supporting the feature. Also add a temporary debugfs interface for managing this. Ideally, the hypervisor would be able to determine the pvsched driver based on the information received from the guest. Guest VMs with the feature enabled would request hypervisor to select a pvsched driver. ioctl api is an override mechanism to give more control to the admin. Signed-off-by: Vineeth Pillai (Google) Signed-off-by: Joel Fernandes (Google) --- include/uapi/linux/kvm.h | 6 ++ virt/kvm/kvm_main.c | 117 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index c3308536482b..4b29bdad4188 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2227,4 +2227,10 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +struct kvm_pvsched_ops { + __u8 ops_name[32]; /* PVSCHED_NAME_MAX */ +}; + +#define KVM_GET_PVSCHED_OPS _IOR(KVMIO, 0xe4, struct kvm_pvsched_ops) +#define KVM_REPLACE_PVSCHED_OPS _IOWR(KVMIO, 0xe5, struct kvm_pvsched_ops) #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0546814e4db7..b3d9c362d2e3 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1223,6 +1223,79 @@ static void kvm_destroy_vm_debugfs(struct kvm *kvm) } } +#ifdef CONFIG_PARAVIRT_SCHED_KVM +static int pvsched_vcpu_ops_show(struct seq_file *m, void *data) +{ + char ops_name[PVSCHED_NAME_MAX]; + struct pvsched_vcpu_ops *ops; + struct kvm *kvm = (struct kvm *) m->private; + + rcu_read_lock(); + ops = rcu_dereference(kvm->pvsched_ops); + if (ops) + strncpy(ops_name, ops->name, PVSCHED_NAME_MAX); + rcu_read_unlock(); + + seq_printf(m, "%s\n", ops_name); + + return 0; +} + +static ssize_t +pvsched_vcpu_ops_write(struct file *filp, const char __user *ubuf, + size_t cnt, loff_t *ppos) +{ + int ret; + char *cmp; + char buf[PVSCHED_NAME_MAX]; + struct inode *inode; + struct kvm *kvm; + + if (cnt > PVSCHED_NAME_MAX) + return -EINVAL; + + if (copy_from_user(&buf, ubuf, cnt)) + return -EFAULT; + + cmp = strstrip(buf); + + inode = file_inode(filp); + inode_lock(inode); + kvm = (struct kvm *)inode->i_private; + ret = kvm_replace_pvsched_ops(kvm, cmp); + inode_unlock(inode); + + if (ret) + return ret; + + *ppos += cnt; + return cnt; +} + +static int pvsched_vcpu_ops_open(struct inode *inode, struct file *filp) +{ + return single_open(filp, pvsched_vcpu_ops_show, inode->i_private); +} + +static const struct file_operations pvsched_vcpu_ops_fops = { + .open = pvsched_vcpu_ops_open, + .write = pvsched_vcpu_ops_write, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static void kvm_create_vm_pvsched_debugfs(struct kvm *kvm) +{ + debugfs_create_file("pvsched_vcpu_ops", 0644, kvm->debugfs_dentry, kvm, + &pvsched_vcpu_ops_fops); +} +#else +static void kvm_create_vm_pvsched_debugfs(struct kvm *kvm) +{ +} +#endif + static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname) { static DEFINE_MUTEX(kvm_debugfs_lock); @@ -1288,6 +1361,8 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname) &stat_fops_per_vm); } + kvm_create_vm_pvsched_debugfs(kvm); + ret = kvm_arch_create_vm_debugfs(kvm); if (ret) goto out_err; @@ -5474,6 +5549,48 @@ static long kvm_vm_ioctl(struct file *filp, r = kvm_gmem_create(kvm, &guest_memfd); break; } +#endif +#ifdef CONFIG_PARAVIRT_SCHED_KVM + case KVM_REPLACE_PVSCHED_OPS: + struct pvsched_vcpu_ops *ops; + struct kvm_pvsched_ops in_ops, out_ops; + + r = -EFAULT; + if (copy_from_user(&in_ops, argp, sizeof(in_ops))) + goto out; + + out_ops.ops_name[0] = 0; + + rcu_read_lock(); + ops = rcu_dereference(kvm->pvsched_ops); + if (ops) + strncpy(out_ops.ops_name, ops->name, PVSCHED_NAME_MAX); + rcu_read_unlock(); + + r = kvm_replace_pvsched_ops(kvm, (char *)in_ops.ops_name); + if (r) + goto out; + + r = -EFAULT; + if (copy_to_user(argp, &out_ops, sizeof(out_ops))) + goto out; + + r = 0; + break; + case KVM_GET_PVSCHED_OPS: + out_ops.ops_name[0] = 0; + rcu_read_lock(); + ops = rcu_dereference(kvm->pvsched_ops); + if (ops) + strncpy(out_ops.ops_name, ops->name, PVSCHED_NAME_MAX); + rcu_read_unlock(); + + r = -EFAULT; + if (copy_to_user(argp, &out_ops, sizeof(out_ops))) + goto out; + + r = 0; + break; #endif default: r = kvm_arch_vm_ioctl(filp, ioctl, arg); From patchwork Wed Apr 3 14:01:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vineeth Remanan Pillai X-Patchwork-Id: 13616217 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C8DF1487F1 for ; Wed, 3 Apr 2024 14:01:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152902; cv=none; b=IxZaCjA6EqZy34w666fAv7YkDyLCv4RESKopXgYdXFqy0oX6a/KmDnlwiUDgyGxssKHtQbZY79akwcLptlpPlWjOJWpHUnZKF5Cj+3o7NuIXTGzqoa4JeFXpevaAD55tKk+aOvKfcCHO7oPn/k7OreLbSEdQLhgGrHr5s1yvUBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152902; c=relaxed/simple; bh=OKezwxiOEiM9+urpB8402BC4SYCwdxDMf8Df/ISZUg0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Dv1z8AuLnwwjrFW/+Xf454fALGYNvRKpqqBglMj+5RE1CBATboPuL2UsivmnpshSNbzq1ilQXG7Ief67BXH6tTukZvIt1XU/bjjti8xUaEQ2moHTlndW4tfJxg2jUIckdSuHXPqs1l1gG7On2mp3M69ZNuvtfSR7xjE/GaY3Wsk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org; spf=pass smtp.mailfrom=bitbyteword.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b=WMp4O7B5; arc=none smtp.client-ip=209.85.210.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b="WMp4O7B5" Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6e675181ceaso4143302a34.2 for ; Wed, 03 Apr 2024 07:01:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bitbyteword.org; s=google; t=1712152898; x=1712757698; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7VOPM+Isn3puOfkfBMYmv3GVLrqplGzQJegeaxwHn/Q=; b=WMp4O7B5HDNF80hOnwY5oOBZPer+2V1WRXTDTQT+cwY/5Cp1SOsxpk21d9uLih02gC i3HfmYuFYntSuHtC6fOXuoghrDCsR/ezPt2osU/0qs0+CrLfVRKFBQaFcC2ubd3Cv0j0 OTAk+xd8LkK+LnurhzJ9SHITRLBcizvh+kpaaEEDYFSP5+KmFWikp7rBE+xXCFFJHzq5 YNL1dfIr5zpiO3vMAjMka2haB7oOhSztdMHPW51sLHhEcXe2yPb1gOEV494VBQ7k5oTM BeR+WgPLfBFEdcsw0oKznFwshw8vIPxXpG3etMx1YOvEU4wB84e9Q+VpV9aFtgiuwXSS /iSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712152898; x=1712757698; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7VOPM+Isn3puOfkfBMYmv3GVLrqplGzQJegeaxwHn/Q=; b=aw5KiYqpe1TYpUirGlJNOxcS/u154qmBijnwNOdjkBA4SqfTL1GDuJNsFL0PiCfo4d XQfvSE0SJuWMZwYEtN7puUM0A7AyueJFN0i6tuIfALG5eWWvqICO2HkgtPlPgXEB7niU j8vuPyV6U1K7B8PGht2HhY0CIM4d+x+OBOqJd/NWK/NycXL2a3Y0+PUqy8yuO0tcSq4k 7e9fM7G6D/da2FwVG+0Nqag7ytDwMO2Gy5+DMxvMsdOwR8UTOGWzDjq8a3Dv+oBMNHx7 8gme9SbIJKPEmmqXzlDrfvI5BdDgT44BoNzz/p36+RP3CmNBE4GE8oF1mNQEX5Md77Od x1cg== X-Forwarded-Encrypted: i=1; AJvYcCXHMsC402GrisXfsRjEr4DMRN2BDDYQ/cjFBOJ8/01T6Y39rCiF4PosCNAuBB+YVu5EvdngbU1RGZBBMuK9XUmYC+vr X-Gm-Message-State: AOJu0Yy+z+ktbXiR8uffzD8d2+bzXZvRpg3vnQhaXtiD3YPXvCWa80Gq JTr+z0tNiPeMIU4GfI7vGp0TaU/qoXwgt7MDALexLrZtEJJ2kF9n1M76kHGOPSY= X-Google-Smtp-Source: AGHT+IFVHhEadwy+vYVbpR80h1N15BwjLDnooutKSgc1bq6Ce0yAUhvCgcTFJsTJXfmfWJbdINes4g== X-Received: by 2002:a05:6830:a44:b0:6e6:d1ac:c989 with SMTP id g4-20020a0568300a4400b006e6d1acc989mr15963532otu.6.1712152898445; Wed, 03 Apr 2024 07:01:38 -0700 (PDT) Received: from vinbuntup3.lan (c-73-143-21-186.hsd1.vt.comcast.net. [73.143.21.186]) by smtp.gmail.com with ESMTPSA id gf12-20020a056214250c00b00698d06df322sm5945706qvb.122.2024.04.03.07.01.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 07:01:38 -0700 (PDT) From: "Vineeth Pillai (Google)" To: Ben Segall , Borislav Petkov , Daniel Bristot de Oliveira , Dave Hansen , Dietmar Eggemann , "H . Peter Anvin" , Ingo Molnar , Juri Lelli , Mel Gorman , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , Thomas Gleixner , Valentin Schneider , Vincent Guittot , Vitaly Kuznetsov , Wanpeng Li Cc: "Vineeth Pillai (Google)" , Steven Rostedt , Joel Fernandes , Suleiman Souhlal , Masami Hiramatsu , himadrics@inria.fr, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH v2 4/5] pvsched: bpf support for pvsched Date: Wed, 3 Apr 2024 10:01:15 -0400 Message-Id: <20240403140116.3002809-5-vineeth@bitbyteword.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240403140116.3002809-1-vineeth@bitbyteword.org> References: <20240403140116.3002809-1-vineeth@bitbyteword.org> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add support for implementing bpf pvsched drivers. bpf programs can use the struct_ops to define the callbacks of pvsched drivers. This is only a skeleton of the bpf framework for pvsched. Some verification details are not implemented yet. Signed-off-by: Vineeth Pillai (Google) Signed-off-by: Joel Fernandes (Google) --- kernel/bpf/bpf_struct_ops_types.h | 4 + virt/pvsched/Makefile | 2 +- virt/pvsched/pvsched_bpf.c | 141 ++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 virt/pvsched/pvsched_bpf.c diff --git a/kernel/bpf/bpf_struct_ops_types.h b/kernel/bpf/bpf_struct_ops_types.h index 5678a9ddf817..9d5e4d1a331a 100644 --- a/kernel/bpf/bpf_struct_ops_types.h +++ b/kernel/bpf/bpf_struct_ops_types.h @@ -9,4 +9,8 @@ BPF_STRUCT_OPS_TYPE(bpf_dummy_ops) #include BPF_STRUCT_OPS_TYPE(tcp_congestion_ops) #endif +#ifdef CONFIG_PARAVIRT_SCHED_HOST +#include +BPF_STRUCT_OPS_TYPE(pvsched_vcpu_ops) +#endif #endif diff --git a/virt/pvsched/Makefile b/virt/pvsched/Makefile index 4ca38e30479b..02bc072cd806 100644 --- a/virt/pvsched/Makefile +++ b/virt/pvsched/Makefile @@ -1,2 +1,2 @@ -obj-$(CONFIG_PARAVIRT_SCHED_HOST) += pvsched.o +obj-$(CONFIG_PARAVIRT_SCHED_HOST) += pvsched.o pvsched_bpf.o diff --git a/virt/pvsched/pvsched_bpf.c b/virt/pvsched/pvsched_bpf.c new file mode 100644 index 000000000000..b125089abc3b --- /dev/null +++ b/virt/pvsched/pvsched_bpf.c @@ -0,0 +1,141 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Google */ + +#include +#include +#include +#include +#include +#include + + +/* "extern" is to avoid sparse warning. It is only used in bpf_struct_ops.c. */ +extern struct bpf_struct_ops bpf_pvsched_vcpu_ops; + +static int bpf_pvsched_vcpu_init(struct btf *btf) +{ + return 0; +} + +static bool bpf_pvsched_vcpu_is_valid_access(int off, int size, + enum bpf_access_type type, + const struct bpf_prog *prog, + struct bpf_insn_access_aux *info) +{ + if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) + return false; + if (type != BPF_READ) + return false; + if (off % size != 0) + return false; + + if (!btf_ctx_access(off, size, type, prog, info)) + return false; + + return true; +} + +static int bpf_pvsched_vcpu_btf_struct_access(struct bpf_verifier_log *log, + const struct bpf_reg_state *reg, + int off, int size) +{ + /* + * TODO: Enable write access to Guest shared mem. + */ + return -EACCES; +} + +static const struct bpf_func_proto * +bpf_pvsched_vcpu_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) +{ + return bpf_base_func_proto(func_id); +} + +static const struct bpf_verifier_ops bpf_pvsched_vcpu_verifier_ops = { + .get_func_proto = bpf_pvsched_vcpu_get_func_proto, + .is_valid_access = bpf_pvsched_vcpu_is_valid_access, + .btf_struct_access = bpf_pvsched_vcpu_btf_struct_access, +}; + +static int bpf_pvsched_vcpu_init_member(const struct btf_type *t, + const struct btf_member *member, + void *kdata, const void *udata) +{ + const struct pvsched_vcpu_ops *uvm_ops; + struct pvsched_vcpu_ops *vm_ops; + u32 moff; + + uvm_ops = (const struct pvsched_vcpu_ops *)udata; + vm_ops = (struct pvsched_vcpu_ops *)kdata; + + moff = __btf_member_bit_offset(t, member) / 8; + switch (moff) { + case offsetof(struct pvsched_vcpu_ops, events): + vm_ops->events = *(u32 *)(udata + moff); + return 1; + case offsetof(struct pvsched_vcpu_ops, name): + if (bpf_obj_name_cpy(vm_ops->name, uvm_ops->name, + sizeof(vm_ops->name)) <= 0) + return -EINVAL; + return 1; + } + + return 0; +} + +static int bpf_pvsched_vcpu_check_member(const struct btf_type *t, + const struct btf_member *member, + const struct bpf_prog *prog) +{ + return 0; +} + +static int bpf_pvsched_vcpu_reg(void *kdata) +{ + return pvsched_register_vcpu_ops((struct pvsched_vcpu_ops *)kdata); +} + +static void bpf_pvsched_vcpu_unreg(void *kdata) +{ + pvsched_unregister_vcpu_ops((struct pvsched_vcpu_ops *)kdata); +} + +static int bpf_pvsched_vcpu_validate(void *kdata) +{ + return pvsched_validate_vcpu_ops((struct pvsched_vcpu_ops *)kdata); +} + +static int bpf_pvsched_vcpu_update(void *kdata, void *old_kdata) +{ + return -EOPNOTSUPP; +} + +static int __pvsched_vcpu_register(struct pid *pid) +{ + return 0; +} +static void __pvsched_vcpu_unregister(struct pid *pid) +{ +} +static void __pvsched_notify_event(void *addr, struct pid *pid, u32 event) +{ +} + +static struct pvsched_vcpu_ops __bpf_ops_pvsched_vcpu_ops = { + .pvsched_vcpu_register = __pvsched_vcpu_register, + .pvsched_vcpu_unregister = __pvsched_vcpu_unregister, + .pvsched_vcpu_notify_event = __pvsched_notify_event, +}; + +struct bpf_struct_ops bpf_pvsched_vcpu_ops = { + .init = &bpf_pvsched_vcpu_init, + .validate = bpf_pvsched_vcpu_validate, + .update = bpf_pvsched_vcpu_update, + .verifier_ops = &bpf_pvsched_vcpu_verifier_ops, + .reg = bpf_pvsched_vcpu_reg, + .unreg = bpf_pvsched_vcpu_unreg, + .check_member = bpf_pvsched_vcpu_check_member, + .init_member = bpf_pvsched_vcpu_init_member, + .name = "pvsched_vcpu_ops", + .cfi_stubs = &__bpf_ops_pvsched_vcpu_ops, +}; From patchwork Wed Apr 3 14:01:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vineeth Remanan Pillai X-Patchwork-Id: 13616218 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56E1A149E14 for ; Wed, 3 Apr 2024 14:01:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152902; cv=none; b=bfHN9L8/kjRsun38lDg+zdYjMhKDj/kBRBUhquOgfDo9bHuxrNLbm6hu993Hj0Xwci/7SBXFn5Wcv8NVOuqYCyiOKrxjCzW+U/MrZ8rGPaRPProIBKIlsf0VkmL6lsf1OX7kBbEW7SBw6nFlM7szS+4itDdYYrDCuC+/e8RDOpo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712152902; c=relaxed/simple; bh=+HycdZGuj5OU1W/c5yOM/8/+whc2oRxO0Toug1yf4G8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ur/fwE5u5YnOZRnF4P2eg6c0w4VfbM6ah1r3NvvB+mp7VD03iq4rgaHXBnSdhBLVayWY+Fqgg4m605vGlDzGudKpHJkPk4w6AcySAAEXk0I29sA3R2LO2e5Y0Yvu4ZKS5LZ9t2FiGNbotbrh6x8tM72t8Xzp1tMHRDvKSR1sMRo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org; spf=pass smtp.mailfrom=bitbyteword.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b=Xn8IhRa6; arc=none smtp.client-ip=209.85.222.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bitbyteword.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bitbyteword.org header.i=@bitbyteword.org header.b="Xn8IhRa6" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-789e6f7f748so335060185a.3 for ; Wed, 03 Apr 2024 07:01:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bitbyteword.org; s=google; t=1712152900; x=1712757700; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QaWEC4wEGQ+YlZ6ti6+WrczlEY+i0roOx8Ci2YqikeE=; b=Xn8IhRa6Q1hH8JPkpsL76ia39K74etBsnq86QvXDJzGkDlu4UQISCgwi31tudYRt83 jPhWkuvW0WWDPNO5GqkZlkamSdX5m/51kSnDSQgYHV4I90SE59QgGnExdChoF6Hyxsy/ DEBPORJpE0l965/MP4/PLQef9cZTT3H12g9caRcNBv7jcQ5dxHGDpDKbBGyAdCqr7UmK ii2GkcFQTeoXQ0TKmt2dtFu9E3LqK/cgIBT1TdNEm8K+8WXd1kr3P+HKnrm3VftvFPtB iodG5/lerUuBzTCWeouSem4Ps7N6uKHKXjVY9g1NsXICLR9FA3KpMZ6aSUbRhyMslQY4 5ZNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712152900; x=1712757700; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QaWEC4wEGQ+YlZ6ti6+WrczlEY+i0roOx8Ci2YqikeE=; b=lbriih+Wr33nhrg70yNAN42Zkwf5eghZtJSt6u6sPhpAq3fas6AODJzl2khfhgSsgj q7y3sdY5jDBCLCNFct+qPeuoOFkxvRMSvCdP7Y5HwUDvGum+sG/QbY1HATVae+HthCAm 0opiS5Bg7IFvRVPhqKxRSxX6Ezxm8Hvcv0OmWPx/Xmua0TpGLuVUX6Ivvoq5E3b/5teK 188AAx0j3G+J2WHd+eByIpzMTOjSe0zw7Xs2O192AW5m/Q0G1fU5ywOAcWMuG0geYOC0 WudG7k6BWv3AqRVfpnRdlDDkiSxCLFOElTA9k6JJJOrHsHl4AaivvMXuKfFTip4V17b/ G7Cg== X-Forwarded-Encrypted: i=1; AJvYcCVW8/NSRVm8RrCClWPms7H3dVGRbuPGBMclgo1Pem0sO0eAG45rC5bfzD3lP6oszqc0ZDgjJ8xEJ429vw/TifSoKzp0 X-Gm-Message-State: AOJu0YzoGLZ70OWowLXcjJ2VQ+s0hHBRXWoEioId91oeqQhlqrwhc97o bg9k1BSwJY0Td5BVUx5/GEDEBMijlaRNIG++Q187h5Ej7Xv99Az79bKHXJ5H74I= X-Google-Smtp-Source: AGHT+IGXzY5eGF4cfLmjKD8WCxnHKVScVbM/F99svOb+MTbMgCl4GsOWSudI3dDOqx11Q2sEA8gtCA== X-Received: by 2002:a05:6214:1384:b0:699:1ad9:259 with SMTP id pp4-20020a056214138400b006991ad90259mr2834018qvb.31.1712152900149; Wed, 03 Apr 2024 07:01:40 -0700 (PDT) Received: from vinbuntup3.lan (c-73-143-21-186.hsd1.vt.comcast.net. [73.143.21.186]) by smtp.gmail.com with ESMTPSA id gf12-20020a056214250c00b00698d06df322sm5945706qvb.122.2024.04.03.07.01.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 07:01:39 -0700 (PDT) From: "Vineeth Pillai (Google)" To: Ben Segall , Borislav Petkov , Daniel Bristot de Oliveira , Dave Hansen , Dietmar Eggemann , "H . Peter Anvin" , Ingo Molnar , Juri Lelli , Mel Gorman , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , Thomas Gleixner , Valentin Schneider , Vincent Guittot , Vitaly Kuznetsov , Wanpeng Li Cc: "Vineeth Pillai (Google)" , Steven Rostedt , Joel Fernandes , Suleiman Souhlal , Masami Hiramatsu , himadrics@inria.fr, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH v2 5/5] selftests/bpf: sample implementation of a bpf pvsched driver. Date: Wed, 3 Apr 2024 10:01:16 -0400 Message-Id: <20240403140116.3002809-6-vineeth@bitbyteword.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240403140116.3002809-1-vineeth@bitbyteword.org> References: <20240403140116.3002809-1-vineeth@bitbyteword.org> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A dummy skeleton of a bpf pvsched driver. This is just for demonstration purpose and would need more work to be included as a test for this feature. Not-Signed-off-by: Vineeth Pillai (Google) --- .../testing/selftests/bpf/progs/bpf_pvsched.c | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_pvsched.c diff --git a/tools/testing/selftests/bpf/progs/bpf_pvsched.c b/tools/testing/selftests/bpf/progs/bpf_pvsched.c new file mode 100644 index 000000000000..a653baa3034b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_pvsched.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Facebook */ + +#include "vmlinux.h" +#include "bpf_tracing_net.h" +#include +#include + +char _license[] SEC("license") = "GPL"; + +SEC("struct_ops/pvsched_vcpu_reg") +int BPF_PROG(pvsched_vcpu_reg, struct pid *pid) +{ + bpf_printk("pvsched_vcpu_reg: pid: %p", pid); + return 0; +} + +SEC("struct_ops/pvsched_vcpu_unreg") +void BPF_PROG(pvsched_vcpu_unreg, struct pid *pid) +{ + bpf_printk("pvsched_vcpu_unreg: pid: %p", pid); +} + +SEC("struct_ops/pvsched_vcpu_notify_event") +void BPF_PROG(pvsched_vcpu_notify_event, void *addr, struct pid *pid, __u32 event) +{ + bpf_printk("pvsched_vcpu_notify: pid: %p, event:%u", pid, event); +} + +SEC(".struct_ops") +struct pvsched_vcpu_ops pvsched_ops = { + .pvsched_vcpu_register = (void *)pvsched_vcpu_reg, + .pvsched_vcpu_unregister = (void *)pvsched_vcpu_unreg, + .pvsched_vcpu_notify_event = (void *)pvsched_vcpu_notify_event, + .events = 0x6, + .name = "bpf_pvsched_ops", +};