From patchwork Thu Oct 24 12:10:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Krawczyk X-Patchwork-Id: 61895 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 960921E9C7; Thu, 24 Oct 2019 14:11:01 +0200 (CEST) Received: from mail-lj1-f193.google.com (mail-lj1-f193.google.com [209.85.208.193]) by dpdk.org (Postfix) with ESMTP id 7964E1E9C7 for ; Thu, 24 Oct 2019 14:11:00 +0200 (CEST) Received: by mail-lj1-f193.google.com with SMTP id c4so9646575lja.11 for ; Thu, 24 Oct 2019 05:11:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AqlpZOr139OB1unsh2SClg8WVN5Fa/eQjiAu6KjjAk4=; b=0WQjtWG+POhcY/O3gO0UxgL5/qSeSK/2R5Xme/p6aBC0M9gq7rOw+ZfV5e+xC+aYFY WlFgVY6cPcDbebbZ+IUfbiwMMaA5rsKtD9/41kPOkDhZpvnhTEIb5B/iAMHYDC1mu9mC 32PHmfcb436RfXuG++erl0Dice7GDMYVXkSC866C1p8GpITwVZGxko8MowKvrCrTjGMY s/+LqRFX2uw/WZNbISttHcaI89Hbn2SiHjpf1CWGHIYPP8zkpdBJOtc+6L8NlpDjFs0k 0w60bpdpzPRG9WF8rCEriai0+bfOCDHqBZxVGLFNzdURZVB9wyUATsMaWlcJitVFl+rS KHUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AqlpZOr139OB1unsh2SClg8WVN5Fa/eQjiAu6KjjAk4=; b=GcuKefDWfw5UIUleSorRF02SEZQoRvRh3WWwA6320UMLw9W61jP1ozTFp1wxaBGvML 7FFTSwMoYTV+OQNyHDGc5AyWeuDUhrBCetaB09EghOA6Xho7vE1l6UrqatRj4yQ3BtOm BaaWdK7WuABDo4sBrX2/tvXySWOlO0I9X9tP5wID9UE8l8KyinV+PfPMLZyiJKGeQPln E2Lv8Ip1ZaGIuZXW1qeRocmKzKWdrwpthXMfCBEVl3EQ0PFjUbHMaeB6HGcMxyHHKWkz RnvS69WK30j0cN4IVzPY5ZnyjUmlAG41EK31XoSy6vDWu29zKnPjO/HW4sX0DMwMhKGz 9gBg== X-Gm-Message-State: APjAAAWbQZfM2r0saGgBMsEorOuUiyV+U8r18MkcX9IkMPlsQfsXUoNb QyVGaouUcNFnhHt8u4+MsyyJyw== X-Google-Smtp-Source: APXvYqxKLrYoS7VLQ/tho/8d+NGGpjdXBRRw+Tiipe0mpIrvJQy21W0NWhdhNEamg48gWJitPb4eXA== X-Received: by 2002:a2e:9890:: with SMTP id b16mr25828149ljj.181.1571919058497; Thu, 24 Oct 2019 05:10:58 -0700 (PDT) Received: from mkPC.semihalf.local (31-172-191-173.noc.fibertech.net.pl. [31.172.191.173]) by smtp.gmail.com with ESMTPSA id x17sm9961845lji.62.2019.10.24.05.10.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2019 05:10:57 -0700 (PDT) From: Michal Krawczyk To: Anatoly Burakov Cc: dev@dpdk.org, igorch@amazon.com, gtzalik@amazon.com, Michal Krawczyk , rahul.lakkireddy@chelsio.com, stable@dpdk.org Date: Thu, 24 Oct 2019 14:10:46 +0200 Message-Id: <20191024121046.30244-1-mk@semihalf.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH] vfio: fix BAR offset type for 32-bit app 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" When 32-bit application is built on 64-bit system it is possible that the offset of the resource is outside of the 32-bit value. The problem with the unsigned long is, that it is 32-bit and not 64-bit when using armhf compiler. Although the system is returning u64 value, we are losing it's value if it's higher than 32-bit in the conversion process. It can further cause mmap to fail due to offset being 0 or to map not intended memory region. To make it more portable, the uint64_t value is now being used for storing offset instead of unsigned long. The size of being 32-bit seems to be fine as the 32-bit application won't be able to access bigger memory and it is further converted to size_t anyway. But for better readability and to be consistent, it's type was changed to size_t as well. Fixes: 0205f873557c ("vfio: fix overflow of BAR region offset and size") Cc: rahul.lakkireddy@chelsio.com Cc: stable@dpdk.org Signed-off-by: Michal Krawczyk Acked-by: Anatoly Burakov --- drivers/bus/pci/linux/pci_vfio.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c index faf2990a7..b8faa23f8 100644 --- a/drivers/bus/pci/linux/pci_vfio.c +++ b/drivers/bus/pci/linux/pci_vfio.c @@ -451,7 +451,8 @@ pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res, int bar_index, int additional_flags) { struct memreg { - unsigned long offset, size; + uint64_t offset; + size_t size; } memreg[2] = {}; void *bar_addr; struct pci_msix_table *msix_table = &vfio_res->msix_table; @@ -504,7 +505,8 @@ pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res, RTE_LOG(DEBUG, EAL, "Trying to map BAR%d that contains the MSI-X " "table. Trying offsets: " - "0x%04lx:0x%04lx, 0x%04lx:0x%04lx\n", bar_index, + "0x%04" PRIx64 ":0x%04zx, 0x%04" PRIx64 ":0x%04zx\n", + bar_index, memreg[0].offset, memreg[0].size, memreg[1].offset, memreg[1].size); } else { @@ -529,8 +531,8 @@ pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res, if (map_addr != MAP_FAILED && memreg[1].offset && memreg[1].size) { void *second_addr = RTE_PTR_ADD(bar_addr, - memreg[1].offset - - (uintptr_t)bar->offset); + (uintptr_t)(memreg[1].offset - + bar->offset)); map_addr = pci_map_resource(second_addr, vfio_dev_fd, memreg[1].offset,