From patchwork Tue Dec 31 10:03:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kishon Vijay Abraham I X-Patchwork-Id: 182670 Delivered-To: patch@linaro.org Received: by 2002:a92:815a:0:0:0:0:0 with SMTP id e87csp7388588ild; Tue, 31 Dec 2019 02:02:00 -0800 (PST) X-Google-Smtp-Source: APXvYqyjqVozWniCHTXN4KMhcU+zrq3T8E5C0QndFVyYpRGu7z+TQO2OY9i/d4tQG+K6SrYz9z06 X-Received: by 2002:a9d:7f83:: with SMTP id t3mr58147295otp.115.1577786519972; Tue, 31 Dec 2019 02:01:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577786519; cv=none; d=google.com; s=arc-20160816; b=eVJlme+IindN9D08CLkf5nZKyUaCdfyRGKl9lGFjYVOoSs989+WDEhyxlTPeQnP8QI UD+sDUURUhC1EsVc5hypejaFejWxwVflb4VbVqP+dzXuZSZEN0EwpalwkJ6kMpMqHYbG 5J8QD6yWwErO628PVeK7j4KuSznSaRgGx+LknYz58xJ1Zg2q+YAEtX/BP8QqS6STYWFc 5HIEVvA6NCpUFguSqQhwC9fh+V0QWtNfWFsSwySkR2d6xFAyZ6+CQIIShaBqBxCNSAIY aiisDBzflAOHJjcjYlfaPffXJRyIDnJCH416nrifGkVVoxRtDQx5ZLnVAUUwCYNOnKEv jy3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=b/64JglBpKY6W4ba0VJS0cOO7Ch2+G3ZysdJiabZQfo=; b=qbZPuph95CND+q5KILvL9kNcL+OZWX2GjiH9skKRfQIVA0iWj6GFBxQFK0fPpQD0Re pG1VIpmLhNWspXVyglVccwYBacL7MXEAwARwa9WiluIduYwwGkb0ufzo5djfP2OUQDMJ 1cwIl91z5i2tGsli/NMI+CljsGeARMNGOPt98WbNPeMVO1L5ezkK7JJzAdl0NCl25PKI JzueA9ohcLDcpvSFbAKG23jGQXQ74PErnCWKE5fYVYBC6SfumgwFI8Xbfk3ZbJCE4Iw7 NSjE4aDU0Awqxk7xeiHqYhuBjRhhUGJNQGpI8SC7KS7wXX6RuIjRJvnDr+C6Q7pkHvHg F6Cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=xHkgg8eA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v18si25312949otn.202.2019.12.31.02.01.59; Tue, 31 Dec 2019 02:01:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=xHkgg8eA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727178AbfLaKB5 (ORCPT + 27 others); Tue, 31 Dec 2019 05:01:57 -0500 Received: from lelv0142.ext.ti.com ([198.47.23.249]:52548 "EHLO lelv0142.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726650AbfLaKBx (ORCPT ); Tue, 31 Dec 2019 05:01:53 -0500 Received: from lelv0266.itg.ti.com ([10.180.67.225]) by lelv0142.ext.ti.com (8.15.2/8.15.2) with ESMTP id xBVA1eJQ069033; Tue, 31 Dec 2019 04:01:40 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1577786500; bh=b/64JglBpKY6W4ba0VJS0cOO7Ch2+G3ZysdJiabZQfo=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=xHkgg8eAVctAYV1Zhn+azqu4KozF0iGsaicpEBgDz4t+y+tWiIbUI0ctc1tCTHwqW +lQdlMqAjLtMBV2G09gh7hI3fzVbZYPbXaYw+NYp04+BTcVmjb5Ayk9XqzlCPHYFhw hSOyi+sNSO98z26jocV3MmZ/lWbepQI2TgdMMVuA= Received: from DFLE106.ent.ti.com (dfle106.ent.ti.com [10.64.6.27]) by lelv0266.itg.ti.com (8.15.2/8.15.2) with ESMTPS id xBVA1eFh079660 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 31 Dec 2019 04:01:40 -0600 Received: from DFLE114.ent.ti.com (10.64.6.35) by DFLE106.ent.ti.com (10.64.6.27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1847.3; Tue, 31 Dec 2019 04:01:39 -0600 Received: from lelv0326.itg.ti.com (10.180.67.84) by DFLE114.ent.ti.com (10.64.6.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1847.3 via Frontend Transport; Tue, 31 Dec 2019 04:01:39 -0600 Received: from a0393678ub.india.ti.com (ileax41-snat.itg.ti.com [10.172.224.153]) by lelv0326.itg.ti.com (8.15.2/8.15.2) with ESMTP id xBVA1TZi020876; Tue, 31 Dec 2019 04:01:37 -0600 From: Kishon Vijay Abraham I To: Kishon Vijay Abraham I , Lorenzo Pieralisi , Bjorn Helgaas , Andrew Murray CC: , Subject: [PATCH 3/5] PCI: endpoint: Protect concurrent access to memory allocation with mutex Date: Tue, 31 Dec 2019 15:33:29 +0530 Message-ID: <20191231100331.6316-4-kishon@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191231100331.6316-1-kishon@ti.com> References: <20191231100331.6316-1-kishon@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org pci-epc-mem uses bitmap to manage the PCI address region. This address region will be shared by multiple endpoint functions (in the case of multi function endpoint) and it has to be protected from concurrent access to avoid updating inconsistent state. Use mutex to protect while updating bitmap. Signed-off-by: Kishon Vijay Abraham I --- drivers/pci/endpoint/pci-epc-mem.c | 10 ++++++++-- include/linux/pci-epc.h | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) -- 2.17.1 diff --git a/drivers/pci/endpoint/pci-epc-mem.c b/drivers/pci/endpoint/pci-epc-mem.c index d2b174ce15de..abfac1109a13 100644 --- a/drivers/pci/endpoint/pci-epc-mem.c +++ b/drivers/pci/endpoint/pci-epc-mem.c @@ -79,6 +79,7 @@ int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_base, size_t size, mem->page_size = page_size; mem->pages = pages; mem->size = size; + mutex_init(&mem->lock); epc->mem = mem; @@ -122,7 +123,7 @@ void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc, phys_addr_t *phys_addr, size_t size) { int pageno; - void __iomem *virt_addr; + void __iomem *virt_addr = NULL; struct pci_epc_mem *mem = epc->mem; unsigned int page_shift = ilog2(mem->page_size); int order; @@ -130,15 +131,18 @@ void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc, size = ALIGN(size, mem->page_size); order = pci_epc_mem_get_order(mem, size); + mutex_lock(&mem->lock); pageno = bitmap_find_free_region(mem->bitmap, mem->pages, order); if (pageno < 0) - return NULL; + goto ret; *phys_addr = mem->phys_base + ((phys_addr_t)pageno << page_shift); virt_addr = ioremap(*phys_addr, size); if (!virt_addr) bitmap_release_region(mem->bitmap, pageno, order); +ret: + mutex_unlock(&mem->lock); return virt_addr; } EXPORT_SYMBOL_GPL(pci_epc_mem_alloc_addr); @@ -164,7 +168,9 @@ void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr, pageno = (phys_addr - mem->phys_base) >> page_shift; size = ALIGN(size, mem->page_size); order = pci_epc_mem_get_order(mem, size); + mutex_lock(&mem->lock); bitmap_release_region(mem->bitmap, pageno, order); + mutex_unlock(&mem->lock); } EXPORT_SYMBOL_GPL(pci_epc_mem_free_addr); diff --git a/include/linux/pci-epc.h b/include/linux/pci-epc.h index 97c78464013c..42c2b14ea2b4 100644 --- a/include/linux/pci-epc.h +++ b/include/linux/pci-epc.h @@ -72,6 +72,7 @@ struct pci_epc_ops { * @bitmap: bitmap to manage the PCI address space * @pages: number of bits representing the address region * @page_size: size of each page + * @lock: mutex to protect bitmap */ struct pci_epc_mem { phys_addr_t phys_base; @@ -79,6 +80,8 @@ struct pci_epc_mem { unsigned long *bitmap; size_t page_size; int pages; + /* mutex to protect against concurrent access for memory allocation*/ + struct mutex lock; }; /**