From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id DCCC0A0561; Mon, 20 Apr 2020 13:10:04 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 1A30E1C230; Mon, 20 Apr 2020 13:10:04 +0200 (CEST) Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by dpdk.org (Postfix) with ESMTP id 7D0081C22F for ; Mon, 20 Apr 2020 13:10:02 +0200 (CEST) Received: by mail-pf1-f193.google.com with SMTP id y25so4775791pfn.5 for ; Mon, 20 Apr 2020 04:10:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20150623.gappssmtp.com; s=20150623; h=mime-version:from:to:cc:subject:date:message-id :content-transfer-encoding; bh=R7zjHFWiS7NyTsQ8QnnsWT2KL02aWnP6kXjSdlYLW28=; b=SBYHaN1TmpwOp5spEb2RF0RR3T+oGewewQridMImE+P2mBXknNPX9rgMRu+e8OErZn ODMxnw95IW6NTrruTFBv1I7Jtt9vJ900huDT5RMJCxWIzwW3Rd+wdEWav/yOM7LmaxDA hfbOozLXgVIQEePQGnTA3Y9Mtkse0PvzPQopS2qAjrltuDnXU5N0MKX3yUKQoYV9812M GDNy378gn3K1687ologkzU9psASIiqiFoYykYQwlwCYHfP+sVUyQyHS4av1pPyNMGiFi nwY991LH/IQNvQ8sbBnCufcr81pW/h5l6lv2McICyXLUNAXSsO68TCTxPvwYmJ+bDnEO 3QyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :content-transfer-encoding; bh=R7zjHFWiS7NyTsQ8QnnsWT2KL02aWnP6kXjSdlYLW28=; b=rxfDqfO//g23ss0p/+XeWgX1fXQh/ncURdB6W8oDTeAhUn6uOcV5OHgWjPNO06fsmx GgvUdlTgnBtpxg++4EPFVHUEUbQvvoaBXPqX6GoNYQVBXCS6onQx6k24fNEGqbyKj8+D YnjMJYlul2tcvWxEx6SaUOgPUqnuP3yunJXQenBkbM7YRGzoWcjLlxm7g/2s8DC1KOKK nY+7oD44ApwUpzKWS5myVp/XzgIIENInbLRt2gc7jw61ygwSxfxOUFLxTFBGthMlm85c B7uhnVK3BHDKloEm8k9kpSXtKmiZWo9bCkji5CCbGjDYSJ7mgx2YPVL9ZAjz9YZY4ggx LACQ== MIME-Version: 1.0 X-Gm-Message-State: AGi0Puaap9/P0/WhZizyQ0kGvNgJMC/I0sfTBX6GT4zP637RPqWZyV95 ImSBPRt4PGr2S0Zfb6cS/H4VFT0cWK6YTHaV9xfazhgdZndDrf1izCxAC9rukQjvxm/2Qd3i X-Google-Smtp-Source: APiQypIfOKcQIKf4VhVnUYe+p0r4vrktlCwrpoGw1VoFNP/8qPNc5UYq72YnSeDnxA4mNEHQdjrw7w== X-Received: by 2002:a63:1c6:: with SMTP id 189mr15741903pgb.254.1587381001279; Mon, 20 Apr 2020 04:10:01 -0700 (PDT) Received: from 31_216.localdomain ([47.240.167.159]) by smtp.gmail.com with ESMTPSA id b2sm866799pgg.77.2020.04.20.04.09.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Apr 2020 04:10:00 -0700 (PDT) From: Li Feng To: Anatoly Burakov , Bruce Richardson Cc: lifeng1519@gmail.com, dev@dpdk.org, Li Feng Date: Mon, 20 Apr 2020 19:09:53 +0800 Message-Id: <20200420110953.959884-1-fengli@smartx.com> X-Mailer: git-send-email 2.11.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: [dpdk-dev] [PATCH] librte_eal: add APIs to speedup virt2iova/phys X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Using pread to replace lseek + read. And add new APIs to reduce open/close/lseek system call frequency when the user needs to convert a large range of virtual address space. - rte_mem_virt2iova_with_fd - rte_mem_virt2phy_with_fd Currently it will be used by spdk in spdk_mem_register. Signed-off-by: Li Feng --- lib/librte_eal/freebsd/eal_memory.c | 18 ++++++++++++++ lib/librte_eal/include/rte_memory.h | 36 +++++++++++++++++++++++++++ lib/librte_eal/linux/eal_memory.c | 49 +++++++++++++++++++++++----------= ---- lib/librte_eal/rte_eal_version.map | 3 +++ 4 files changed, 88 insertions(+), 18 deletions(-) diff --git a/lib/librte_eal/freebsd/eal_memory.c b/lib/librte_eal/freebsd/e= al_memory.c index a97d8f0f0..fc0debf23 100644 --- a/lib/librte_eal/freebsd/eal_memory.c +++ b/lib/librte_eal/freebsd/eal_memory.c @@ -44,12 +44,30 @@ rte_mem_virt2phy(const void *virtaddr) (void)virtaddr; return RTE_BAD_IOVA; } + rte_iova_t rte_mem_virt2iova(const void *virtaddr) { return rte_mem_virt2phy(virtaddr); } =20 +phys_addr_t +rte_mem_virt2phy_with_fd(int fd, const void *virtaddr) +{ + /* + * XXX not implemented. This function is only used by + * rte_mempool_virt2iova_with_fd() when hugepages are disabled. + */ + (void)virtaddr; + return RTE_BAD_IOVA; +} + +rte_iova_t +rte_mem_virt2iova_with_fd(int fd, const void *virtaddr) +{ + return rte_mem_virt2phy_with_fd(fd, virtaddr); +} + int rte_eal_hugepage_init(void) { diff --git a/lib/librte_eal/include/rte_memory.h b/lib/librte_eal/include/r= te_memory.h index 3d8d0bd69..c75782fa7 100644 --- a/lib/librte_eal/include/rte_memory.h +++ b/lib/librte_eal/include/rte_memory.h @@ -108,6 +108,23 @@ int rte_mem_lock_page(const void *virt); phys_addr_t rte_mem_virt2phy(const void *virt); =20 /** + * Get physical address of any mapped virtual address in the current proce= ss. + * It is found by reading fd which is the opened /proc/self/pagemap specia= l file + * descriptor. This is a optimization of rte_mem_virt2phy when the + * rte_mem_virt2phy is needed to be called many times. + * The page must be locked. + * + * @param fd + * The opened fd of /proc/self/pagemap. + * @param virt + * The virtual address. + * @return + * The physical address or RTE_BAD_IOVA on error. + */ +__rte_experimental +phys_addr_t rte_mem_virt2phy_with_fd(int fd, const void *virt); + +/** * Get IO virtual address of any mapped virtual address in the current pro= cess. * * @note This function will not check internal page table. Instead, in IOV= A as @@ -123,6 +140,25 @@ phys_addr_t rte_mem_virt2phy(const void *virt); rte_iova_t rte_mem_virt2iova(const void *virt); =20 /** + * Get IO virtual address of any mapped virtual address in the current pro= cess. + * + * @note This function will not check internal page table. Instead, in IOV= A as + * PA mode, it will fall back to getting real physical address (whic= h may + * not match the expected IOVA, such as what was specified for exter= nal + * memory). + * + * @param virt + * The virtual address. + * @param fd + * The opened fd of /proc/self/pagemap. + * @return + * The IO address or RTE_BAD_IOVA on error. + */ +__rte_experimental +rte_iova_t rte_mem_virt2iova_with_fd(int fd, const void *virt); + + +/** * Get virtual memory address corresponding to iova address. * * @note This function read-locks the memory hotplug subsystem, and thus c= annot diff --git a/lib/librte_eal/linux/eal_memory.c b/lib/librte_eal/linux/eal_m= emory.c index 7a9c97ff8..918796700 100644 --- a/lib/librte_eal/linux/eal_memory.c +++ b/lib/librte_eal/linux/eal_memory.c @@ -91,11 +91,11 @@ uint64_t eal_get_baseaddr(void) =20 /* * Get physical address of any mapped virtual address in the current proce= ss. + * fd is used to avoid open/close pagemap repeatly. */ phys_addr_t -rte_mem_virt2phy(const void *virtaddr) -{ - int fd, retval; +rte_mem_virt2phy_with_fd(int fd, const void *virtaddr) { + int retval; uint64_t page, physaddr; unsigned long virt_pfn; int page_size; @@ -107,24 +107,10 @@ rte_mem_virt2phy(const void *virtaddr) /* standard page size */ page_size =3D getpagesize(); =20 - fd =3D open("/proc/self/pagemap", O_RDONLY); - if (fd < 0) { - RTE_LOG(INFO, EAL, "%s(): cannot open /proc/self/pagemap: %s\n", - __func__, strerror(errno)); - return RTE_BAD_IOVA; - } - virt_pfn =3D (unsigned long)virtaddr / page_size; offset =3D sizeof(uint64_t) * virt_pfn; - if (lseek(fd, offset, SEEK_SET) =3D=3D (off_t) -1) { - RTE_LOG(INFO, EAL, "%s(): seek error in /proc/self/pagemap: %s\n", - __func__, strerror(errno)); - close(fd); - return RTE_BAD_IOVA; - } =20 - retval =3D read(fd, &page, PFN_MASK_SIZE); - close(fd); + retval =3D pread(fd, &page, PFN_MASK_SIZE, offset); if (retval < 0) { RTE_LOG(INFO, EAL, "%s(): cannot read /proc/self/pagemap: %s\n", __func__, strerror(errno)); @@ -149,6 +135,33 @@ rte_mem_virt2phy(const void *virtaddr) return physaddr; } =20 +/* + * Get physical address of any mapped virtual address in the current proce= ss. + */ +phys_addr_t +rte_mem_virt2phy(const void *virtaddr) +{ + uint64_t physaddr; + int fd; + fd =3D open("/proc/self/pagemap", O_RDONLY); + if (fd < 0) { + RTE_LOG(INFO, EAL, "%s(): cannot open /proc/self/pagemap: %s\n", + __func__, strerror(errno)); + return RTE_BAD_IOVA; + } + physaddr =3D rte_mem_virt2phy_with_fd(fd, virtaddr); + close(fd); + return physaddr; +} + +rte_iova_t +rte_mem_virt2iova_with_fd(int fd, const void *virtaddr) +{ + if (rte_eal_iova_mode() =3D=3D RTE_IOVA_VA) + return (uintptr_t)virtaddr; + return rte_mem_virt2phy_with_fd(fd, virtaddr); +} + rte_iova_t rte_mem_virt2iova(const void *virtaddr) { diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_ve= rsion.map index f9ede5b41..fc3a436e7 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -338,4 +338,7 @@ EXPERIMENTAL { =20 # added in 20.05 rte_log_can_log; + + rte_mem_virt2iova_with_fd; + rte_mem_virt2phy_with_fd; }; --=20 2.11.0 --=20 The SmartX email address is only for business purpose. Any sent message=20 that is not related to the business is not authorized or permitted by=20 SmartX. =E6=9C=AC=E9=82=AE=E7=AE=B1=E4=B8=BA=E5=8C=97=E4=BA=AC=E5=BF=97=E5=87=8C=E6= =B5=B7=E7=BA=B3=E7=A7=91=E6=8A=80=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8=EF=BC= =88SmartX=EF=BC=89=E5=B7=A5=E4=BD=9C=E9=82=AE=E7=AE=B1. =E5=A6=82=E6=9C=AC= =E9=82=AE=E7=AE=B1=E5=8F=91=E5=87=BA=E7=9A=84=E9=82=AE=E4=BB=B6=E4=B8=8E=E5= =B7=A5=E4=BD=9C=E6=97=A0=E5=85=B3,=E8=AF=A5=E9=82=AE=E4=BB=B6=E6=9C=AA=E5= =BE=97=E5=88=B0=E6=9C=AC=E5=85=AC=E5=8F=B8=E4=BB=BB=E4=BD=95=E7=9A=84=E6=98= =8E=E7=A4=BA=E6=88=96=E9=BB=98=E7=A4=BA=E7=9A=84=E6=8E=88=E6=9D=83.