From nobody Sun Dec 28 23:13:31 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94FEFC4167B for ; Tue, 5 Dec 2023 08:36:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376355AbjLEIgA (ORCPT ); Tue, 5 Dec 2023 03:36:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235458AbjLEIf2 (ORCPT ); Tue, 5 Dec 2023 03:35:28 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D1E2185 for ; Tue, 5 Dec 2023 00:35:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701765333; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1EF7vfXUKmZ7w1vqKhCPA032G3uF9wOvyUGxj1cxm00=; b=KVWExMcplhPWc61Dx92bdtpNXGgOQcTWjHCMZtpOHIwJ+0JC7ffV0AlU70WT0Mf7mGgEQo 2lZD38dp6kdeDJHFNZhq9TR0ieg9bG8sbs0tdgUBGajtZLDSJPiuJMKghNoJN9yg8RD2S9 GCuJEeDK7mNlQnxxfSSBG12fmVJ3ajI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-645-l76KXkNdNkaAjlgEfj5TDw-1; Tue, 05 Dec 2023 03:35:32 -0500 X-MC-Unique: l76KXkNdNkaAjlgEfj5TDw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BCDB0879843; Tue, 5 Dec 2023 08:35:31 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3C87F3C25; Tue, 5 Dec 2023 08:35:28 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, xieyongji@bytedance.com, linux-kernel@vger.kernel.org, maxime.coquelin@redhat.com Subject: [PATCH v3 5/7] vduse: Add file operation for mmap Date: Tue, 5 Dec 2023 16:34:42 +0800 Message-Id: <20231205083444.3029239-6-lulu@redhat.com> In-Reply-To: <20231205083444.3029239-1-lulu@redhat.com> References: <20231205083444.3029239-1-lulu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add the operation for mmap, This function will be used by the user space application to map the pages to the user space. Signed-off-by: Cindy Lu --- drivers/vdpa/vdpa_user/vduse_dev.c | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 52ccde636406..f55f415629de 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -1381,6 +1381,79 @@ static struct vduse_dev *vduse_dev_get_from_minor(in= t minor) return dev; } =20 +static vm_fault_t vduse_vm_fault(struct vm_fault *vmf) +{ + struct vduse_dev *dev =3D vmf->vma->vm_file->private_data; + struct vm_area_struct *vma =3D vmf->vma; + u16 index =3D vma->vm_pgoff; + struct vduse_virtqueue *vq; + unsigned long vaddr; + + /* index 0 page reserved for vduse status*/ + if (index =3D=3D 0) { + vaddr =3D dev->vdpa_reconnect_vaddr; + } else { + /* index 1+vq_number page reserved for vduse vqs*/ + vq =3D &dev->vqs[index - 1]; + vaddr =3D vq->vdpa_reconnect_vaddr; + } + if (remap_pfn_range(vma, vmf->address & PAGE_MASK, + PFN_DOWN(virt_to_phys((void *)vaddr)), + VDUSE_RECONNCT_MMAP_SIZE, vma->vm_page_prot)) + return VM_FAULT_SIGBUS; + return VM_FAULT_NOPAGE; +} + +static const struct vm_operations_struct vduse_vm_ops =3D { + .fault =3D vduse_vm_fault, +}; + +static int vduse_dev_mmap(struct file *file, struct vm_area_struct *vma) +{ + struct vduse_dev *dev =3D file->private_data; + unsigned long vaddr =3D 0; + unsigned long index =3D vma->vm_pgoff; + struct vduse_virtqueue *vq; + + if (vma->vm_end - vma->vm_start !=3D PAGE_SIZE) + return -EINVAL; + if ((vma->vm_flags & VM_SHARED) =3D=3D 0) + return -EINVAL; + + /*check if Userspace App map the page number larger than kernel allocated= */ + if (index > dev->vq_num + 1) + return -EINVAL; + + /* index 0 page reserved for vduse status*/ + if (index =3D=3D 0) { + vaddr =3D dev->vdpa_reconnect_vaddr; + } else { + /* index 1+vq_number page reserved for vduse vqs*/ + vq =3D &dev->vqs[index - 1]; + vaddr =3D vq->vdpa_reconnect_vaddr; + } + /* Check whether the memory for the mmap was allocated by the kernel. + * If not, this device may not have been created/destroyed correctly. + */ + if (vaddr =3D=3D 0) + return -EOPNOTSUPP; + + /* check if the address is page aligned, if not, + * this address maybe damaged + */ + if (virt_to_phys((void *)vaddr) & (PAGE_SIZE - 1)) + return -EINVAL; + + /* Check if the Userspace App has mapped the correct size */ + if (vma->vm_end - vma->vm_start !=3D VDUSE_RECONNCT_MMAP_SIZE) + return -EOPNOTSUPP; + + vm_flags_set(vma, VM_DONTEXPAND); + vma->vm_ops =3D &vduse_vm_ops; + + return 0; +} + static int vduse_dev_open(struct inode *inode, struct file *file) { int ret; @@ -1413,6 +1486,8 @@ static const struct file_operations vduse_dev_fops = =3D { .unlocked_ioctl =3D vduse_dev_ioctl, .compat_ioctl =3D compat_ptr_ioctl, .llseek =3D noop_llseek, + .mmap =3D vduse_dev_mmap, + }; =20 static struct vduse_dev *vduse_dev_create(void) --=20 2.34.3