From patchwork Thu May 31 05:55:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 137320 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp6180085lji; Wed, 30 May 2018 22:56:39 -0700 (PDT) X-Google-Smtp-Source: ADUXVKI8LmyuhlDb1WwchBguBf08y5cC/TVL1mDBKEa3K0mfemq5sNRTfn046VNZiif5KJjf3I7Q X-Received: by 2002:a63:65c2:: with SMTP id z185-v6mr2889238pgb.163.1527746199672; Wed, 30 May 2018 22:56:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527746199; cv=none; d=google.com; s=arc-20160816; b=OwT9xAvdmqaxL2YMn6HbRd1YZP2q2PEsL9jFiRzIeaH5d9gICEUBIY1+TgVh0rONGc UFYCYY1yYvz1nxKNF0gHXPqQtxpnKW4ymx9atYchNZccrTzL7pfKK2nRUCfSBrHD4uoL mg5FVOuqPRX8lZ8G6R4s6J080E5E1hW1+yLeKif2MI1u8i2lrqBqB5OkJnrH9+2WzWmC 8SEGOVK3sJc4BGzpbf9H5D/+SJRYK8h/buO4We0wT+TSyeNm2pTDWC6UdkF6z6yqB0uR 8CQCileqzn8wIelDSopFOLdi88n3fZ+VoiL2ka1X+QMwZd2y70bYUEfoshnfiRG+Jk6p mK1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=LfqECe6/ajDViZPg7Vvp8PjmW1ok+SW0S6pF7ygIRDQ=; b=khwFCO4zdNtNWQ7DrA7bu7rhe50g3yzuJYPFZSWG48kIf13vA3SwewVGXDrQR6vZZ1 +/vlUa6UGyZ/Q8abuhibBHGWQTNGtgWoexiZjY/X9yN6AwgzZKCuK0rRYuibHW3lCywD MkVEQoHkMLDMKCAY0bVD9bWyTR8YeM0NP0IRAObSnDwul1frN4my7RBQU0gkEul1GaFq Mr4iatdWDmIA3Nq1ywjcFS7AeLLWdIBXx+9En4BbyVRkNQ7ODrBgO1RvtWY8taTjPZqP mnQw7lfHRYWzacN9MFkrL3utXxom2Gzmmt06RLfcKnmz3HtYRPYKgCg1qcOdr5mwlNax rMrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AlsOgn0s; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p186-v6si841862pga.2.2018.05.30.22.56.39; Wed, 30 May 2018 22:56:39 -0700 (PDT) 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=@linaro.org header.s=google header.b=AlsOgn0s; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753961AbeEaF4h (ORCPT + 30 others); Thu, 31 May 2018 01:56:37 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:38666 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751037AbeEaF4d (ORCPT ); Thu, 31 May 2018 01:56:33 -0400 Received: by mail-pf0-f196.google.com with SMTP id b74-v6so6764230pfl.5 for ; Wed, 30 May 2018 22:56:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=LfqECe6/ajDViZPg7Vvp8PjmW1ok+SW0S6pF7ygIRDQ=; b=AlsOgn0swjuFHym9QNoArYeBhCngvEATNhOUeL0cVk6ExOJjdj6P1gHmVvkH8o44oD 7kxY3qaQmPLkF6mzikGcr9f7mF+dylThkfLa+FH71AXpGF6QONPN9EAUr3BDoopQiMUh dUGhSNpiWzUxBNHEhK1hnhVXaq4yKXGWPNrgA= 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; bh=LfqECe6/ajDViZPg7Vvp8PjmW1ok+SW0S6pF7ygIRDQ=; b=e/UYVWrQZqZ0uKy7DH7YlIs33r1YH//WENPmSVmm9hB49hsw97MXnZ6zXASWR20BTH fVz6DgGR/9e6664r66acAQFZM3Ne0gkoGvkpTRwlNuWEv9v9T8sWSAC3Ch1jGIZYoRaG N/9FNHhuS3/DopeDybidg+WCotqaSdacpD5MQvvtydJtY/UpnDPaHW1eDfDFhwPSu1Z0 9wAClEw6Kh73LswGHHOujmagqjVrc3Xd8GNukXhJmhugYHSwm5pORd7QIi3ItQ9k5hmY 3nn1NJVUNtIFWgL2/GXhCPAkPaiLANfDoXI1Jx/g17hrvHLSgzmn+HP4GSib5T2D0x9D /apg== X-Gm-Message-State: ALKqPwd57NWNB2UuSC8lK/EMf6dgv5xyppY4aOayirfAfJL/Hwwk1Tlf 0/WDcLhW6do++EppFlFvxY/mkA== X-Received: by 2002:a62:9056:: with SMTP id a83-v6mr5611228pfe.186.1527746193345; Wed, 30 May 2018 22:56:33 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id z15-v6sm54557494pgr.55.2018.05.30.22.56.29 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 30 May 2018 22:56:32 -0700 (PDT) From: Baolin Wang To: hch@lst.de, m.szyprowski@samsung.com Cc: robin.murphy@arm.com, gregkh@linuxfoundation.org, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, arnd@arndb.de, broonie@kernel.org, baolin.wang@linaro.org Subject: [PATCH 1/2] dma-coherent: Add one parameter to save available coherent memory Date: Thu, 31 May 2018 13:55:32 +0800 Message-Id: X-Mailer: git-send-email 1.7.9.5 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It is incorrect to use mem->size to valid if there are enough coherent memory to allocate in __dma_alloc_from_coherent(), since some devices may mark some occupied coherent memory by dma_mark_declared_memory_occupied(). So we can introduce one 'avail' parameter to save the available device coherent memory, to valid if we have enough coherent memory for the device to allocate. Signed-off-by: Baolin Wang --- drivers/base/dma-coherent.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) -- 1.7.9.5 diff --git a/drivers/base/dma-coherent.c b/drivers/base/dma-coherent.c index 597d408..ce19832 100644 --- a/drivers/base/dma-coherent.c +++ b/drivers/base/dma-coherent.c @@ -18,6 +18,7 @@ struct dma_coherent_mem { unsigned long *bitmap; spinlock_t spinlock; bool use_dev_dma_pfn_offset; + int avail; }; static struct dma_coherent_mem *dma_coherent_default_memory __ro_after_init; @@ -73,6 +74,7 @@ static int dma_init_coherent_memory( dma_mem->device_base = device_addr; dma_mem->pfn_base = PFN_DOWN(phys_addr); dma_mem->size = pages; + dma_mem->avail = pages; dma_mem->flags = flags; spin_lock_init(&dma_mem->spinlock); @@ -141,6 +143,7 @@ void *dma_mark_declared_memory_occupied(struct device *dev, dma_addr_t device_addr, size_t size) { struct dma_coherent_mem *mem = dev->dma_mem; + int order = get_order(size); unsigned long flags; int pos, err; @@ -151,7 +154,9 @@ void *dma_mark_declared_memory_occupied(struct device *dev, spin_lock_irqsave(&mem->spinlock, flags); pos = PFN_DOWN(device_addr - dma_get_device_base(dev, mem)); - err = bitmap_allocate_region(mem->bitmap, pos, get_order(size)); + err = bitmap_allocate_region(mem->bitmap, pos, order); + if (!err) + mem->avail -= 1 << order; spin_unlock_irqrestore(&mem->spinlock, flags); if (err != 0) @@ -170,7 +175,7 @@ static void *__dma_alloc_from_coherent(struct dma_coherent_mem *mem, spin_lock_irqsave(&mem->spinlock, flags); - if (unlikely(size > (mem->size << PAGE_SHIFT))) + if (unlikely(size > (mem->avail << PAGE_SHIFT))) goto err; pageno = bitmap_find_free_region(mem->bitmap, mem->size, order); @@ -182,6 +187,7 @@ static void *__dma_alloc_from_coherent(struct dma_coherent_mem *mem, */ *dma_handle = mem->device_base + (pageno << PAGE_SHIFT); ret = mem->virt_base + (pageno << PAGE_SHIFT); + mem->avail -= 1 << order; spin_unlock_irqrestore(&mem->spinlock, flags); memset(ret, 0, size); return ret; @@ -244,6 +250,7 @@ static int __dma_release_from_coherent(struct dma_coherent_mem *mem, spin_lock_irqsave(&mem->spinlock, flags); bitmap_release_region(mem->bitmap, page, order); + mem->avail += 1 << order; spin_unlock_irqrestore(&mem->spinlock, flags); return 1; }