From patchwork Thu Feb 22 23:16:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13568250 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 2E3EFC47DD9 for ; Thu, 22 Feb 2024 23:17:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6916A6B00A6; Thu, 22 Feb 2024 18:16:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 61E1D6B00AC; Thu, 22 Feb 2024 18:16:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 279156B00A8; Thu, 22 Feb 2024 18:16:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 01F586B00A7 for ; Thu, 22 Feb 2024 18:16:56 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D174E161037 for ; Thu, 22 Feb 2024 23:16:55 +0000 (UTC) X-FDA: 81821001990.16.FB58E47 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by imf26.hostedemail.com (Postfix) with ESMTP id 99021140007 for ; Thu, 22 Feb 2024 23:16:53 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b="H/wcZInY"; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf26.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.168.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708643813; 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=TYpwdT9rGpGU+Krn2HEFIYzazdkjNtusHFYwxcfsZAg=; b=qobTI0iVHBar9x/trjlBoPNawL9WJuEui8nTV1P0YQ3FxCWeXAwPdj1SVgYwqVjy0fMH9G 2CCemuJPOZadCGnir4l0YWGPGqWTYwkEidHkBEHenU8kAWBXZ7uhbRL13e5u3VOT7X8H/S vOnvKqrmnF0D13we3NgvZIjh6ZlCwqk= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b="H/wcZInY"; dmarc=pass (policy=none) header.from=quicinc.com; spf=pass (imf26.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.168.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708643813; a=rsa-sha256; cv=none; b=hREWGCsVC2imXD9Ur9/BCq0+nJP8yasWQNkQg3lEBfQpMNh9IwWs7o5qDyIBnMatXNgy0Q LAg/1DptgeBD8022vEFz4wUQwOcJRfBvRknWlX0lNl8BDS6YwZY4+ydNfMEkm1Gy6kJN+6 Lh5lAXmu++jDf2AUB33EFO41Tv3mIpI= Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 41MLxFWt026010; Thu, 22 Feb 2024 23:16:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:date:subject:mime-version:content-type :content-transfer-encoding:message-id:references:in-reply-to:to :cc; s=qcppdkim1; bh=TYpwdT9rGpGU+Krn2HEFIYzazdkjNtusHFYwxcfsZAg =; b=H/wcZInYw9DqA1TKF4h0M7BlAGWNbQ/KfRny3iN9QUXCd6MjSpbRIVxfHfV lpWPKSJJGOieAjX02AuK0oeVlSf+DVbKSnX0/8hdRsnj0WjcRFHQrC7hrV6mp3a0 g+SSrCh2RhbLPB7o+ArRjNDoPVOTP7BkLo6ZnF1Nmy7hsRv6kHbZW3UZVHk32BCy cDyBcSE0AW4zE/H+XUDLf8Y0RPzRQUjb5yh3exulGyN+mZ8MM574OiXn4S1CFvzA fKUsURJwYneSWnFIsN8Tj3lYAZey8o4mTjPFu3nu+3bp99l+PH808lPkEZ250J7n IEMyWYcLRTaUBLVgL4+67118wHg== Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3we24ajumj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 22 Feb 2024 23:16:46 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA02.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 41MNGj5M028219 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 22 Feb 2024 23:16:45 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Thu, 22 Feb 2024 15:16:44 -0800 From: Elliot Berman Date: Thu, 22 Feb 2024 15:16:55 -0800 Subject: [PATCH v17 32/35] virt: gunyah: Add irqfd interface MIME-Version: 1.0 Message-ID: <20240222-gunyah-v17-32-1e9da6763d38@quicinc.com> References: <20240222-gunyah-v17-0-1e9da6763d38@quicinc.com> In-Reply-To: <20240222-gunyah-v17-0-1e9da6763d38@quicinc.com> To: Alex Elder , Srinivas Kandagatla , Murali Nalajal , Trilok Soni , Srivatsa Vaddagiri , Carl van Schaik , Philip Derrin , Prakruthi Deepak Heragu , Jonathan Corbet , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Catalin Marinas , Will Deacon , Konrad Dybcio , Bjorn Andersson , Dmitry Baryshkov , "Fuad Tabba" , Sean Christopherson , "Andrew Morton" CC: , , , , , , Elliot Berman X-Mailer: b4 0.12.4 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01c.na.qualcomm.com (10.47.97.35) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: 4M5Sctox9093hyrqv1fhYVdp-QAefRVI X-Proofpoint-ORIG-GUID: 4M5Sctox9093hyrqv1fhYVdp-QAefRVI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-22_15,2024-02-22_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 suspectscore=0 clxscore=1015 lowpriorityscore=0 priorityscore=1501 adultscore=0 mlxlogscore=999 phishscore=0 spamscore=0 mlxscore=0 bulkscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2402120000 definitions=main-2402220179 X-Rspamd-Queue-Id: 99021140007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: wxg3idcw7543mgo6s7whx9dfg4bstf95 X-HE-Tag: 1708643813-323691 X-HE-Meta: U2FsdGVkX18Ot/oG6ViMHX7qKqUulRZue9yihuxbuXxs/krysd+to/APuFNW6OGnro4/P2h6IYGTeTNc53xZGcwDApzz8NBa+f52nUPBvIdNdnWchhudKthZ5jyPv2a4zTzsqWMq2SGsIX116u+T4azexFz11yH3r9WonjAXcY4mATyRB9dd1P6ffI8F5eoYhWEodWGrHGyapHirUNvihN8CHbRAT9NYEAmot1uCSCLZaNIjtmJo2Iz0Rz40PXv3WGSFwkuCtmyuaJCzdbPwz7Ee7GhjOdqMhKJa0+4ZZRobEhDL5yX9NU0UTT7rw/cXz9IDAsObf4aa2ecRO6X2X0/xkDL2t3jXv1sGx0X47KqlSsMxtg7Ke8NAdKJ77RVKG2YcMcU2oAwonky5muHo9FXVjK06q7CSW4jzasJC81ye1OUBLt30VoKxUpfFHSTiyscHk74QYZi+otgnqjZNPIVpGIAEVGLj4+ybewSGCwuJYhR2yLmE8SkK6wAqRma/pYLnhRG9Sddh/AcP/acU0xE7c/4RE4S3l4m26eMzuevEhatj0mn0fJCM//RqixibNSeBERFF7/jvgVddyZp0pVZrGiX4RB8TfIPmtWCLY2qdfFrWaLul1oPbUlZmPgA3TDukqiH/f3vqyOiiIo+XUiRdDGepkSTEtSKIaLoXI9TzKht7bgRa7u0jCkru4KM3Zss/HXVoyKKmItDWEdl2tYSR1uQKPS4M3BIN9Vkshc//m962el9IzwUwsbFyAOV+cVCwjoN+3EnPz9KV2ud8Zrz9WfXQ55A/7JaeLclTGdO94YzPMjz0C48o5o6y+UjTAA6Mw60VitNonwf+D8gqUQsdCZCpxJaqXgrISdTOryPqfeKAwW81+MKKmw68hK+BEYRoP/A6+RNW9UFBUIupUtVDTgMPvSC2hNVSbMGZXBH0fTmF9vXzUgPUfSa6uOxhRCOJgN/2zIs= 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: Enable support for creating irqfds which can raise an interrupt on a Gunyah virtual machine. irqfds are exposed to userspace as a Gunyah VM function with the name "irqfd". If the VM devicetree is not configured to create a doorbell with the corresponding label, userspace will still be able to assert the eventfd but no interrupt will be raised on the guest. Acked-by: Alex Elder Co-developed-by: Prakruthi Deepak Heragu Signed-off-by: Prakruthi Deepak Heragu Signed-off-by: Elliot Berman --- drivers/virt/gunyah/Kconfig | 9 ++ drivers/virt/gunyah/Makefile | 1 + drivers/virt/gunyah/gunyah_irqfd.c | 187 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/gunyah.h | 35 +++++++ 4 files changed, 232 insertions(+) diff --git a/drivers/virt/gunyah/Kconfig b/drivers/virt/gunyah/Kconfig index fe2823dc48bac..1685b75fb77a0 100644 --- a/drivers/virt/gunyah/Kconfig +++ b/drivers/virt/gunyah/Kconfig @@ -27,3 +27,12 @@ config GUNYAH_QCOM_PLATFORM extra platform-specific support. Say Y/M here to use Gunyah on Qualcomm platforms. + +config GUNYAH_IRQFD + tristate "Gunyah irqfd interface" + depends on GUNYAH + help + Enable kernel support for creating irqfds which can raise an interrupt + on Gunyah virtual machine. + + Say Y/M here if unsure and you want to support Gunyah VMMs. diff --git a/drivers/virt/gunyah/Makefile b/drivers/virt/gunyah/Makefile index c4505fce177dd..b41b02792921c 100644 --- a/drivers/virt/gunyah/Makefile +++ b/drivers/virt/gunyah/Makefile @@ -5,3 +5,4 @@ gunyah_rsc_mgr-y += rsc_mgr.o rsc_mgr_rpc.o vm_mgr.o vm_mgr_mem.o guest_memfd.o obj-$(CONFIG_GUNYAH) += gunyah.o gunyah_rsc_mgr.o gunyah_vcpu.o obj-$(CONFIG_GUNYAH_PLATFORM_HOOKS) += gunyah_platform_hooks.o obj-$(CONFIG_GUNYAH_QCOM_PLATFORM) += gunyah_qcom.o +obj-$(CONFIG_GUNYAH_IRQFD) += gunyah_irqfd.o diff --git a/drivers/virt/gunyah/gunyah_irqfd.c b/drivers/virt/gunyah/gunyah_irqfd.c new file mode 100644 index 0000000000000..951da6e8d321e --- /dev/null +++ b/drivers/virt/gunyah/gunyah_irqfd.c @@ -0,0 +1,187 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +struct gunyah_irqfd { + struct gunyah_resource *ghrsc; + struct gunyah_vm_resource_ticket ticket; + struct gunyah_vm_function_instance *f; + + bool level; + + struct eventfd_ctx *ctx; + wait_queue_entry_t wait; + poll_table pt; +}; + +static int irqfd_wakeup(wait_queue_entry_t *wait, unsigned int mode, int sync, + void *key) +{ + struct gunyah_irqfd *irqfd = + container_of(wait, struct gunyah_irqfd, wait); + __poll_t flags = key_to_poll(key); + int ret = 0; + + if (flags & EPOLLIN) { + if (irqfd->ghrsc) { + ret = gunyah_hypercall_bell_send(irqfd->ghrsc->capid, 1, + NULL); + if (ret) + pr_err_ratelimited( + "Failed to inject interrupt %d: %d\n", + irqfd->ticket.label, ret); + } else + pr_err_ratelimited( + "Premature injection of interrupt\n"); + } + + return 0; +} + +static void irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh, + poll_table *pt) +{ + struct gunyah_irqfd *irq_ctx = + container_of(pt, struct gunyah_irqfd, pt); + + add_wait_queue(wqh, &irq_ctx->wait); +} + +static bool gunyah_irqfd_populate(struct gunyah_vm_resource_ticket *ticket, + struct gunyah_resource *ghrsc) +{ + struct gunyah_irqfd *irqfd = + container_of(ticket, struct gunyah_irqfd, ticket); + int ret; + + if (irqfd->ghrsc) { + pr_warn("irqfd%d already got a Gunyah resource. Check if multiple resources with same label were configured.\n", + irqfd->ticket.label); + return false; + } + + irqfd->ghrsc = ghrsc; + if (irqfd->level) { + /* Configure the bell to trigger when bit 0 is asserted (see + * irq_wakeup) and for bell to automatically clear bit 0 once + * received by the VM (ack_mask). need to make sure bit 0 is cleared right away, + * otherwise the line will never be deasserted. Emulating edge + * trigger interrupt does not need to set either mask + * because irq is listed only once per gunyah_hypercall_bell_send + */ + ret = gunyah_hypercall_bell_set_mask(irqfd->ghrsc->capid, 1, 1); + if (ret) + pr_warn("irq %d couldn't be set as level triggered. Might cause IRQ storm if asserted\n", + irqfd->ticket.label); + } + + return true; +} + +static void gunyah_irqfd_unpopulate(struct gunyah_vm_resource_ticket *ticket, + struct gunyah_resource *ghrsc) +{ +} + +static long gunyah_irqfd_bind(struct gunyah_vm_function_instance *f) +{ + struct gunyah_fn_irqfd_arg *args = f->argp; + struct gunyah_irqfd *irqfd; + __poll_t events; + struct fd fd; + long r; + + if (f->arg_size != sizeof(*args)) + return -EINVAL; + + /* All other flag bits are reserved for future use */ + if (args->flags & ~GUNYAH_IRQFD_FLAGS_LEVEL) + return -EINVAL; + + irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL); + if (!irqfd) + return -ENOMEM; + + irqfd->f = f; + f->data = irqfd; + + fd = fdget(args->fd); + if (!fd.file) { + kfree(irqfd); + return -EBADF; + } + + irqfd->ctx = eventfd_ctx_fileget(fd.file); + if (IS_ERR(irqfd->ctx)) { + r = PTR_ERR(irqfd->ctx); + goto err_fdput; + } + + if (args->flags & GUNYAH_IRQFD_FLAGS_LEVEL) + irqfd->level = true; + + init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); + init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc); + + irqfd->ticket.resource_type = GUNYAH_RESOURCE_TYPE_BELL_TX; + irqfd->ticket.label = args->label; + irqfd->ticket.owner = THIS_MODULE; + irqfd->ticket.populate = gunyah_irqfd_populate; + irqfd->ticket.unpopulate = gunyah_irqfd_unpopulate; + + r = gunyah_vm_add_resource_ticket(f->ghvm, &irqfd->ticket); + if (r) + goto err_ctx; + + events = vfs_poll(fd.file, &irqfd->pt); + if (events & EPOLLIN) + pr_warn("Premature injection of interrupt\n"); + fdput(fd); + + return 0; +err_ctx: + eventfd_ctx_put(irqfd->ctx); +err_fdput: + fdput(fd); + kfree(irqfd); + return r; +} + +static void gunyah_irqfd_unbind(struct gunyah_vm_function_instance *f) +{ + struct gunyah_irqfd *irqfd = f->data; + u64 cnt; + + eventfd_ctx_remove_wait_queue(irqfd->ctx, &irqfd->wait, &cnt); + gunyah_vm_remove_resource_ticket(irqfd->f->ghvm, &irqfd->ticket); + eventfd_ctx_put(irqfd->ctx); + kfree(irqfd); +} + +static bool gunyah_irqfd_compare(const struct gunyah_vm_function_instance *f, + const void *arg, size_t size) +{ + const struct gunyah_fn_irqfd_arg *instance = f->argp, *other = arg; + + if (sizeof(*other) != size) + return false; + + return instance->label == other->label; +} + +DECLARE_GUNYAH_VM_FUNCTION_INIT(irqfd, GUNYAH_FN_IRQFD, 2, gunyah_irqfd_bind, + gunyah_irqfd_unbind, gunyah_irqfd_compare); +MODULE_DESCRIPTION("Gunyah irqfd VM Function"); +MODULE_LICENSE("GPL"); diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h index 574116f544722..cb7b0bb9bef38 100644 --- a/include/uapi/linux/gunyah.h +++ b/include/uapi/linux/gunyah.h @@ -63,9 +63,12 @@ struct gunyah_vm_dtb_config { * @GUNYAH_FN_VCPU: create a vCPU instance to control a vCPU * &struct gunyah_fn_desc.arg is a pointer to &struct gunyah_fn_vcpu_arg * Return: file descriptor to manipulate the vcpu. + * @GUNYAH_FN_IRQFD: register eventfd to assert a Gunyah doorbell + * &struct gunyah_fn_desc.arg is a pointer to &struct gunyah_fn_irqfd_arg */ enum gunyah_fn_type { GUNYAH_FN_VCPU = 1, + GUNYAH_FN_IRQFD, }; #define GUNYAH_FN_MAX_ARG_SIZE 256 @@ -85,6 +88,38 @@ struct gunyah_fn_vcpu_arg { __u32 id; }; +/** + * enum gunyah_irqfd_flags - flags for use in gunyah_fn_irqfd_arg + * @GUNYAH_IRQFD_FLAGS_LEVEL: make the interrupt operate like a level triggered + * interrupt on guest side. Triggering IRQFD before + * guest handles the interrupt causes interrupt to + * stay asserted. + */ +enum gunyah_irqfd_flags { + GUNYAH_IRQFD_FLAGS_LEVEL = 1UL << 0, +}; + +/** + * struct gunyah_fn_irqfd_arg - Arguments to create an irqfd function. + * + * Create this function with &GUNYAH_VM_ADD_FUNCTION using type &GUNYAH_FN_IRQFD. + * + * Allows setting an eventfd to directly trigger a guest interrupt. + * irqfd.fd specifies the file descriptor to use as the eventfd. + * irqfd.label corresponds to the doorbell label used in the guest VM's devicetree. + * + * @fd: an eventfd which when written to will raise a doorbell + * @label: Label of the doorbell created on the guest VM + * @flags: see &enum gunyah_irqfd_flags + * @padding: padding bytes + */ +struct gunyah_fn_irqfd_arg { + __u32 fd; + __u32 label; + __u32 flags; + __u32 padding; +}; + /** * struct gunyah_fn_desc - Arguments to create a VM function * @type: Type of the function. See &enum gunyah_fn_type.