Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp213634rdf; Mon, 20 Nov 2023 23:50:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IEgg7SfVoQZUZVJj4VLd7aAP3C+Kta3S4KWeShpeqzy6hoq3vs+8S+htgUFZQo6e0e/Jn4k X-Received: by 2002:a05:6808:1aae:b0:3ae:1675:700e with SMTP id bm46-20020a0568081aae00b003ae1675700emr10720132oib.36.1700553052375; Mon, 20 Nov 2023 23:50:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700553052; cv=none; d=google.com; s=arc-20160816; b=JlvRJqGqF7FvOntNLs1Jw9SDIWPijTOGAXssoyGrKrO/SMMJRT74hm3yeanPM0V9dO uylRJpipDurrz97FTp6Ql+PuU5rVzwqq3cnFAYGyL1O2FNXRBjjQ+u03BJGmHwjqUrdo 26jxmiil4xCEE2JoffvLae9XlRnXnnJS+z7KG5ng2jBJvCN8yQfbEI5K9yx9Vc3oXdcC p7sex2zjVm56lmfNvxXIJYbP6xNViB5tKE9JDJs2zQjODF28AM302nlKg64Gyb5k1DJ2 RPxcjjPm5RvIxa8IAsVQUW/onR7FaDLEW9cAoE+EnL0f0RusOLTteyIfC/rWhI/cEOyi j5WQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=btk5PFJVEM3+qQpJy98Ir5AUr2+WhihqrbyG2lX5mHg=; fh=ir7cOO3TfsXjq7Y5DnAiuq3+k2gzyaCCb4HcXAX8/h4=; b=Quo6DWcq6cE+cFslD/2Oo1bIhKUwKVxGyvouF+hsfQk3K5lW+2La0biQPpA2qT4VPq ecmDk45x0tzp2+ee6B10QUEW8QHb7M+Bz6lwbtPKVOCqCTjkgfr82DlJJGKYE7PcekD/ CRxm1xKzcGtR7/KRKClwCLgX99F+k7HrS84hfbv2T9h3FbEGCHlh/KTON7IIAw5TsoTG iZWvTkuGcu1TOjUxWKVNM7C/+09mW2rVOY/WAiEjxDMZF7hWOL/nt392rq6B8LdEUeM5 CF1m6bKJo8SRkKDTgRaUpxnDWOmx4pfhvkKNiiscsZNykzkGWNbsK+kfWPPt/2m4CfbM 3yqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=M2znojdq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id b11-20020aa78ecb000000b006cb68d85220si5709231pfr.286.2023.11.20.23.50.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 23:50:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=M2znojdq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id B2494806AFE4; Mon, 20 Nov 2023 23:50:49 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229522AbjKUHul (ORCPT + 99 others); Tue, 21 Nov 2023 02:50:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230089AbjKUHkk (ORCPT ); Tue, 21 Nov 2023 02:40:40 -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 C752F116 for ; Mon, 20 Nov 2023 23:40:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700552434; h=from:from: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; bh=btk5PFJVEM3+qQpJy98Ir5AUr2+WhihqrbyG2lX5mHg=; b=M2znojdqoTKr3r0b3EY1U7ns53PzYoRl2LlAz4GK+0v22cMUpIgipls2F8GlAEGX59WjBc fHQXyOB7Op7ztQGmxaYXode07aD4QlBBE5rGH4rKHQEshbwmJsmBnKJRn0xwp7NGNDIUSx 2dvN7/khIvkcGgXuzby7KeplLyECvAk= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-217-Z3NPIQIvNROhyewsTo932Q-1; Tue, 21 Nov 2023 02:40:30 -0500 X-MC-Unique: Z3NPIQIvNROhyewsTo932Q-1 Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2c6f33ee403so48548591fa.2 for ; Mon, 20 Nov 2023 23:40:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700552428; x=1701157228; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=btk5PFJVEM3+qQpJy98Ir5AUr2+WhihqrbyG2lX5mHg=; b=P/pU1yqgjOT8OFjTztfGO/rrOmlyl8LsCMcUZYdfBZLObU1rf1TPv6qpCPVR/6LDrl 8IaudeilhGNPKLxNlgv2PQCS6gKhhRP4ZtLczZUk+p+xT6DMPjIEbUy5NY4G976Kqk7B cnseTCiYvhJtXBkhfwZTkUCHrteIP/YL0EP2E65mE62CG8fr8OaLc0CEN4zE1fDepS/+ igMD40DMrOiz7hAASYD3VBfIe1UkO0eIGbkLZyCARMVXbdJAQQ7yLPaS2S5AN3Y4keQc fFFnXHWma7i4iLn/UvaF4GhTzbNi2fjlXHEIiJzIRtqwDeyld3UkbmzvXm2WEtoVHN+K MIRw== X-Gm-Message-State: AOJu0YwHcu2K/FndLaboOVhUTdNYuZ5B+tShhb5n2i+iqbYa9/InVCgD Z+pGz4TIbPX/UYBHRqexagtkNkz3MEsNfMX8c19apmBHHNYMXd5s6sJhMOlPl4SiX1/gDBaWyyU JvV7OfuEa7WM7wZtixq/2RP998kWMBNmTpvZ8edti X-Received: by 2002:a19:7519:0:b0:503:1913:ed8e with SMTP id y25-20020a197519000000b005031913ed8emr6576763lfe.61.1700552428778; Mon, 20 Nov 2023 23:40:28 -0800 (PST) X-Received: by 2002:a19:7519:0:b0:503:1913:ed8e with SMTP id y25-20020a197519000000b005031913ed8emr6576752lfe.61.1700552428474; Mon, 20 Nov 2023 23:40:28 -0800 (PST) MIME-Version: 1.0 References: <20231121073050.287080-1-lulu@redhat.com> <20231121073050.287080-3-lulu@redhat.com> In-Reply-To: <20231121073050.287080-3-lulu@redhat.com> From: Jason Wang Date: Tue, 21 Nov 2023 15:40:16 +0800 Message-ID: Subject: Re: [PATCH v2 2/5] vduse: Add file operation for mmap To: Cindy Lu Cc: mst@redhat.com, xieyongji@bytedance.com, linux-kernel@vger.kernel.org, maxime.coquelin@redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 20 Nov 2023 23:50:49 -0800 (PST) On Tue, Nov 21, 2023 at 3:31=E2=80=AFPM Cindy Lu wrote: > > Add the operation for mmap, The user space APP will > use this function to map the pages to userspace > > Signed-off-by: Cindy Lu > --- > drivers/vdpa/vdpa_user/vduse_dev.c | 79 ++++++++++++++++++++++++++++++ > 1 file changed, 79 insertions(+) > > diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/= vduse_dev.c > index 6209e2f00278..ccb30e98bab5 100644 > --- a/drivers/vdpa/vdpa_user/vduse_dev.c > +++ b/drivers/vdpa/vdpa_user/vduse_dev.c > @@ -1376,6 +1376,83 @@ static struct vduse_dev *vduse_dev_get_from_minor(= int minor) > return dev; > } > > +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; > + struct vdpa_reconnect_info *info; > + > + /* index 0 page reserved for vduse status*/ > + if (index =3D=3D 0) { > + info =3D &dev->reconnect_info; > + } else { > + /* index 1+vq_number page reserved for vduse vqs*/ > + vq =3D &dev->vqs[index - 1]; > + info =3D &vq->reconnect_info; > + } > + if (remap_pfn_range(vma, vmf->address & PAGE_MASK, > + PFN_DOWN(virt_to_phys((void *)info->vaddr)), > + PAGE_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; > + struct vdpa_reconnect_info *info; > + 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 a= llocated*/ > + if (index > dev->vq_num + 1) > + return -EINVAL; > + > + /* index 0 page reserved for vduse status*/ > + if (index =3D=3D 0) { > + info =3D &dev->reconnect_info; > + } else { > + /* index 1+vq_number page reserved for vduse vqs*/ > + vq =3D &dev->vqs[index - 1]; > + info =3D &vq->reconnect_info; > + } > + /*check if map pages was allocated and inited by kernel */ > + if (info->vaddr =3D=3D 0) > + return -EOPNOTSUPP; > + > + /* check if the address is page aligned, if not, > + * this address maybe damaged > + */ > + if (virt_to_phys((void *)info->vaddr) & (PAGE_SIZE - 1)) > + return -EINVAL; > + > + /* check if Userspace App mapped the correct size > + * the userspace App should map one page each time > + */ > + if (vma->vm_end - vma->vm_start !=3D PAGE_SIZE) > + return -EOPNOTSUPP; > + /* VM_IO: set as a memory-mapped I/O region,This will for vq info= rmation > + * VM_PFNMAP: only need the pure PFN > + * VM_DONTEXPAND: do not need to use mremap() in this function > + * VM_DONTDUMP:Do not include in the core dump > + */ Instead of duplicating the semantic, you need to explain why it is needed. For example I don't see how the following is useful: VM_IO: the page is not backed by any I/O page VM_PFNMAP: we need map it to usespace for sure, for it's not a pure PFN VM_DONTDUMP: including this in core dump may help to debug the userspace process for sure Thanks > + vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP= ); > + vma->vm_ops =3D &vduse_vm_ops; > + > + return 0; > +} > + > static int vduse_dev_open(struct inode *inode, struct file *file) > { > int ret; > @@ -1408,6 +1485,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, > + > }; > > static struct vduse_dev *vduse_dev_create(void) > -- > 2.34.3 >