From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 877D6C433E6 for ; Tue, 16 Feb 2021 20:41:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E66064E7A for ; Tue, 16 Feb 2021 20:41:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229628AbhBPUkt (ORCPT ); Tue, 16 Feb 2021 15:40:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229767AbhBPUkt (ORCPT ); Tue, 16 Feb 2021 15:40:49 -0500 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBBCCC06174A for ; Tue, 16 Feb 2021 12:40:08 -0800 (PST) Received: by mail-ed1-x536.google.com with SMTP id q10so13902740edt.7 for ; Tue, 16 Feb 2021 12:40:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qsbkrWZGkD91kMd27lJYVWXYLDx45J5nDBTki3Poz/Y=; b=GYlUAEcxf+YrKRO9EV5o4/Z+28j3rfdzii+w6iCzZUbQExjtVDl60KciVFUspFaevd xQSimjJ/cKCSAjpA2c6+1IwGP+b9RsJeFUgaGgIQI9CHFXTR045ViXPxwRMKJbmKzBsQ dAYOvturlAhwMrFBnspAhiHq5WWA2Hkdic64X8Rcert/bP/eE1q1Jv7U4+KFDK5bEbTc /CSI3/tdjWwHApgQq7qDVuwhFwQXKrhmaTsx+j7jU6+/8SJ3r9U8+h3wqTu3CVO4gC9b /RRgmqX0ARNglqqE3GjZ6Ihebj0PnyzmcQHZB0bQ0S4W5F+0TaIpoP+exgxw3GtFqYl4 am7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qsbkrWZGkD91kMd27lJYVWXYLDx45J5nDBTki3Poz/Y=; b=Y576dEwiNJGwMgwqG1Efpysu8ZZ+yibEdVfE1OvDcaTJu9x+wEe0AEmsJVTjJmOeQy q38sEVRKWQP3zvvGY9NBcb2p5V5FvEJuZdQ8iPRYQPLCsq5AXzMSuFDsMdU1Gg+6wls0 jHiU8rnbZBMdtbL87lQgKr7FgqN2HVnIxI3L2FVWdSQgRrX2T/ytJT12rULKeg28puaI 03J26uUrdwu9yLTCdMU1NBy1zPtAbMT2LLtTg/24w1a9yw4pyFqWwxzrRTMYAX6Sxd4y mFck+cWpNPDmSURFmRSOY69TiH3ywRyM38sIryykobcejSzHxyfdVSFWj02KEvYJ357N nH5w== X-Gm-Message-State: AOAM531uWFxy/Har5Wd77lXInFmEt7tRTomkJiAekKLp7VXJS9OvGyeE nOZ1dyHCJMGitqvvSWcexu/jmw+h7APwW4D4Au0Ghw== X-Google-Smtp-Source: ABdhPJzItTBAgOKzcio391IOuVIAv/Zadl6m2XXBki/3fUYt940zA/MAddwv1fJXGyV5MfPeAf92HQRO8cYR7bKO5J8= X-Received: by 2002:a05:6402:559:: with SMTP id i25mr15504185edx.300.1613508006214; Tue, 16 Feb 2021 12:40:06 -0800 (PST) MIME-Version: 1.0 References: <161255810396.339900.7646244556839438765.stgit@djiang5-desk3.ch.intel.com> <161255840486.339900.5478922203128287192.stgit@djiang5-desk3.ch.intel.com> <20210210235924.GJ4247@nvidia.com> <8ff16d76-6b36-0da6-03ee-aebec2d1a731@intel.com> In-Reply-To: <8ff16d76-6b36-0da6-03ee-aebec2d1a731@intel.com> From: Dan Williams Date: Tue, 16 Feb 2021 12:39:56 -0800 Message-ID: Subject: Re: [PATCH v5 05/14] vfio/mdev: idxd: add basic mdev registration and helper functions To: Dave Jiang Cc: Jason Gunthorpe , Alex Williamson , kwankhede@nvidia.com, Thomas Gleixner , Vinod Koul , "Dey, Megha" , Jacob jun Pan , "Raj, Ashok" , Yi L Liu , Baolu Lu , "Tian, Kevin" , Sanjay K Kumar , "Luck, Tony" , eric.auger@redhat.com, Parav Pandit , netanelg@mellanox.com, shahafs@mellanox.com, Paolo Bonzini , dmaengine@vger.kernel.org, Linux Kernel Mailing List , KVM list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: dmaengine@vger.kernel.org On Tue, Feb 16, 2021 at 11:05 AM Dave Jiang wrote: > > > On 2/10/2021 4:59 PM, Jason Gunthorpe wrote: > > On Fri, Feb 05, 2021 at 01:53:24PM -0700, Dave Jiang wrote: > > > >> +static int check_vma(struct idxd_wq *wq, struct vm_area_struct *vma) > >> { > >> - /* FIXME: Fill in later */ > >> + if (vma->vm_end < vma->vm_start) > >> + return -EINVAL; > > These checks are redundant > > Thanks. Will remove. > > > > >> -static int idxd_mdev_host_release(struct idxd_device *idxd) > >> +static int idxd_vdcm_mmap(struct mdev_device *mdev, struct vm_area_struct *vma) > >> +{ > >> + unsigned int wq_idx, rc; > >> + unsigned long req_size, pgoff = 0, offset; > >> + pgprot_t pg_prot; > >> + struct vdcm_idxd *vidxd = mdev_get_drvdata(mdev); > >> + struct idxd_wq *wq = vidxd->wq; > >> + struct idxd_device *idxd = vidxd->idxd; > >> + enum idxd_portal_prot virt_portal, phys_portal; > >> + phys_addr_t base = pci_resource_start(idxd->pdev, IDXD_WQ_BAR); > >> + struct device *dev = mdev_dev(mdev); > >> + > >> + rc = check_vma(wq, vma); > >> + if (rc) > >> + return rc; > >> + > >> + pg_prot = vma->vm_page_prot; > >> + req_size = vma->vm_end - vma->vm_start; > >> + vma->vm_flags |= VM_DONTCOPY; > >> + > >> + offset = (vma->vm_pgoff << PAGE_SHIFT) & > >> + ((1ULL << VFIO_PCI_OFFSET_SHIFT) - 1); > >> + > >> + wq_idx = offset >> (PAGE_SHIFT + 2); > >> + if (wq_idx >= 1) { > >> + dev_err(dev, "mapping invalid wq %d off %lx\n", > >> + wq_idx, offset); > >> + return -EINVAL; > >> + } > >> + > >> + /* > >> + * Check and see if the guest wants to map to the limited or unlimited portal. > >> + * The driver will allow mapping to unlimited portal only if the the wq is a > >> + * dedicated wq. Otherwise, it goes to limited. > >> + */ > >> + virt_portal = ((offset >> PAGE_SHIFT) & 0x3) == 1; > >> + phys_portal = IDXD_PORTAL_LIMITED; > >> + if (virt_portal == IDXD_PORTAL_UNLIMITED && wq_dedicated(wq)) > >> + phys_portal = IDXD_PORTAL_UNLIMITED; > >> + > >> + /* We always map IMS portals to the guest */ > >> + pgoff = (base + idxd_get_wq_portal_full_offset(wq->id, phys_portal, > >> + IDXD_IRQ_IMS)) >> PAGE_SHIFT; > >> + dev_dbg(dev, "mmap %lx %lx %lx %lx\n", vma->vm_start, pgoff, req_size, > >> + pgprot_val(pg_prot)); > >> + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); > >> + vma->vm_private_data = mdev; > > What ensures the mdev pointer is valid strictly longer than the VMA? > > This needs refcounting. > > Going to take a kref at open and then put_device at close. Does that > sound reasonable or should I be calling get_device() in mmap() and then > register a notifier for when vma is released? Where does this enabling ever look at vm_private_data again? It seems to me it should be reasonable for the mdev to die out from underneath a vma, just need some tracking to block future uses of the vma->vm_private_data from being attempted.