Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1323803ybz; Thu, 16 Apr 2020 07:13:32 -0700 (PDT) X-Google-Smtp-Source: APiQypJ8MOzc0P4+OlHR4tGs9QIzZyLxYEUC6t1sUb/CI08Z3tzGMTkHiGx5kOCFcwp3k9J5t2Af X-Received: by 2002:a50:a68a:: with SMTP id e10mr29859716edc.113.1587046412678; Thu, 16 Apr 2020 07:13:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587046412; cv=none; d=google.com; s=arc-20160816; b=A02Cobtkq5SWjCmVBzsfT8xtkjtQoNE8XbX1BFnMGfZ4vrj0hHR5nSfQH2jXPU9ul4 tChFDCjz6sXlhi3xuvB70gGhdnXrmUOsjO4ktC7M+ZsdHyTjCXu27TZG9eTorbiQNg4X MYGxMYSFhG1z300s3TyrnrOWNbksiTN25JjUuy1s2zume+UsAD8faSMus4KNRkL0LnUg fv7bR1AfZinSMj43B+fkYJmN6p/JZMfe6eXQA3GXcvDAKdMMk+DOf5dasidXVdioHDX4 pqJo+4IwlgoMJ9aSJarC/6wJoISqd6aT3oDhDwucQqtUA+vq4vzxP9NdKfXrnzi5UqY2 59ZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Et8bzcjXMcSY9fci2uVKvENG6FWwb/KIh1TYDZi0MQQ=; b=FkyGEXfk6+y1QcGaDgAJM03N325a9LxJErSC98lbbAaKZkrvV5/53BulJJz0aLr9Ya H/mZ58ivSONik3L5JDF9tY1aVvjOxvd2Y5jDtL1JNB731Y7u1H1i3MtTyMDqYUUwH+yM kzQ595KsMUi7NPgJMFONdWxGEfNq2xhDjSgU7hvrFG3udk03dYGE3jbZinlGNto0ll9A HBoXhgs3BepkmR+YtAHk4GvT611z2R5CnV/vshOlHoMQEIkSPnPUdHHfE+vB+kFgMKuJ cy8/NxfOGz8+CoC614eZ4mvWuuU8c3VpHNpHaG74Ne7evWLpF9fghjnPdAEtXtrjHyxq 45Jg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GFGfFd2e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i20si2104952eds.581.2020.04.16.07.13.06; Thu, 16 Apr 2020 07:13:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GFGfFd2e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392165AbgDPOKB (ORCPT + 99 others); Thu, 16 Apr 2020 10:10:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:60538 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2898595AbgDPNqa (ORCPT ); Thu, 16 Apr 2020 09:46:30 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E5BD6208E4; Thu, 16 Apr 2020 13:46:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587044789; bh=ENed83rf4FZN7TR/sxCG3io39qV5+j/aFwGFBuAPpkg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GFGfFd2eDL3kywdIvzQYm34tNqAelO+G6CsXLDyPfWjdJ0hbniWVlPRlVQFgkbFNU INZIvX6gAj4wpsXsfDZx5CjZ1lXxQo251xTM9viLcVB8DoNDo6jhS9H2seFD8zNhXh S6W0A+qVMkmOOrlSN+LUTOSlonDZD1R9zsVrQplA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kishon Vijay Abraham I , Lorenzo Pieralisi Subject: [PATCH 5.4 103/232] PCI: endpoint: Fix for concurrent memory allocation in OB address region Date: Thu, 16 Apr 2020 15:23:17 +0200 Message-Id: <20200416131327.962803146@linuxfoundation.org> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200416131316.640996080@linuxfoundation.org> References: <20200416131316.640996080@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kishon Vijay Abraham I commit 04e046ca57ebed3943422dee10eec9e73aec081e upstream. pci-epc-mem uses a bitmap to manage the Endpoint outbound (OB) 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 an inconsistent state. Use a mutex to protect bitmap updates to prevent the memory allocation API from returning incorrect addresses. Signed-off-by: Kishon Vijay Abraham I Signed-off-by: Lorenzo Pieralisi Cc: stable@vger.kernel.org # v4.14+ Signed-off-by: Greg Kroah-Hartman --- drivers/pci/endpoint/pci-epc-mem.c | 10 ++++++++-- include/linux/pci-epc.h | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) --- 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 *e 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(str 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(str 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 + (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_ep 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); --- a/include/linux/pci-epc.h +++ b/include/linux/pci-epc.h @@ -71,6 +71,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; @@ -78,6 +79,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; }; /**