From patchwork Sat Oct 3 04:02:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 314022 Delivered-To: patches@linaro.org Received: by 2002:a05:6e02:1081:0:0:0:0 with SMTP id r1csp2002162ilj; Fri, 2 Oct 2020 21:03:12 -0700 (PDT) X-Received: by 2002:a17:90b:1496:: with SMTP id js22mr5955570pjb.20.1601697792119; Fri, 02 Oct 2020 21:03:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601697792; cv=none; d=google.com; s=arc-20160816; b=qMxGLXG4eb8llso82OnmjQPJoZTX10pZUMgBOnFoKdWuSkzHWrv27Hfkr537SAozJJ lgmAf8LIxK1yXva3OopwLUVGuFsmzBnG1GdJGYf98zRcy4d6x7EjtIdDWadezFWtMlDM WDLsxEUs64kDjGkvK4Gm51XKZqtk3+OmNeUgKxhQP9smFzBHFpsFSpsX3xUlnyXc5umW E5ThCYxFzJBG2NexFQyuFyfxJBFST0ZSZhQZHsKvGinpAx52Ls9I0Q8dZ9NGRKBRgykW Aa+i+8Vt9SnqC1JlXoJqD5unH3K2O4JnFbQS7IKIpzCJD4E2SWmTrVjvvmEx03QyybxD pqhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=/3T6DtyMdgIaqBLXnI/IP8eViK4GR+w936JVTX0oqqo=; b=vlnk718eYvPVr8rz2R2+TTmx/1yMOx70BLUhdoV3HFCqoWg6X/2/e7RO+ygT8H+C7/ cNrg1rHCUhGeqOjp9xdFF0tU+RfV4uVonUTJPUI9C+mqqrmPAfn5OE/iNREKxTUtTC77 DhZWd5gF8IM3ZoC9HuKCu0KiP4Z2bYsgLDFppznMVyisRLQm1PqfmxSWaqKiNXQQpVgF gWQXFCqwSquqMTK4RVyyuVq59SoHWgsnPex+2NvC9qKsp4RSm6GU5ZdfrgNH+q2iL00K vz6wG+1Om0gmTKOilXMdi0DotxbsbDXub8+mn83EhQP9vLbHDk+HNFbuQo7mO0Iiji4/ CfqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pumPlKkp; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id m3sor1722543pgt.5.2020.10.02.21.03.12 for (Google Transport Security); Fri, 02 Oct 2020 21:03:12 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pumPlKkp; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/3T6DtyMdgIaqBLXnI/IP8eViK4GR+w936JVTX0oqqo=; b=pumPlKkppWyU+6tvZrtcIGLQrPzfL7DZXw2xIEL7N1BD4bqnhs4eXFAUn64o4Nn+nX HrWqWdezJ7Iv0l61f5U1GYVetpPGokCd+2dreOx84XOxe9YfzLy6R20suB4EDD27mkjg uYJrTDRBihA/lhdcA91c68fBm2ljxvaovtymqyWtTs7sHeTgdwClFfSoXHReGV+pltU3 a2MU9TcOJDFuAPkf19EV/qr5VqEhwwpu+gtDWnIfFQTNyfbFjlrlBnSfd/qWeJMH99bz /B5ab7LGawktkvIzNmXHWHHvnogmlLssMAe/wpRtGOe16eviByyjRE4XlUqcHZocd4X6 JQ1g== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=/3T6DtyMdgIaqBLXnI/IP8eViK4GR+w936JVTX0oqqo=; b=oX2APV02I/RwmyydcdSvJx5RfCwOab1JaS252f51AHXqUVv6xtRB46hsTAqDZPzRwd 01TqeJQqpmsNvOPPC7t5nS05/23O6jfpuEM/Fi6xg2UW6As94PX2oCsvBlZW2ijUazTC AJTK8dSCX96u4X3rlcX6wvk+H//O0Wh1pmXRldq5KFDWoatnFbPD0j5w2fp6xFtrqgjZ 1pbgmqALT8jdcWbgqjgpoRcpeyZ0B1VfRjxzBMrCtaVHujjKO+SYdI8V9efoRdS7BTwW qmKXVyimfjSajDImTt0Q5dA9CNONsgV0+BY6Y5nCDYhhbeaP7mF3N4wEuXouY5imqW1P 7gsQ== X-Gm-Message-State: AOAM530XPr9OK51XaORcjrZHFtckjuHySaSZ9+Aa/C7dyxzTaicPvl3s etaoTtk06RkfQCwMWic8xm7rpJSm X-Google-Smtp-Source: ABdhPJyC/0wtN9vlme0BdjUqwt1Gpcijj0nApYBm+ZJ5owlf/V7bA8Y3GedcktbkKhwMga2K22EpjQ== X-Received: by 2002:a63:a70d:: with SMTP id d13mr5033122pgf.65.1601697791699; Fri, 02 Oct 2020 21:03:11 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id 190sm3909290pfy.22.2020.10.02.21.03.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2020 21:03:11 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Sumit Semwal , Liam Mark , Laura Abbott , Brian Starkey , Hridya Valsaraju , Suren Baghdasaryan , Sandeep Patil , Daniel Mentz , Chris Goldsworthy , =?utf-8?q?=C3=98rjan_Eide?= , Robin Murphy , Ezequiel Garcia , Simon Ser , James Jones , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [PATCH v3 4/7] dma-buf: heaps: Skip sync if not mapped Date: Sat, 3 Oct 2020 04:02:54 +0000 Message-Id: <20201003040257.62768-5-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201003040257.62768-1-john.stultz@linaro.org> References: <20201003040257.62768-1-john.stultz@linaro.org> MIME-Version: 1.0 This patch is basically a port of Ørjan Eide's similar patch for ION https://lore.kernel.org/lkml/20200414134629.54567-1-orjan.eide@arm.com/ Only sync the sg-list of dma-buf heap attachment when the attachment is actually mapped on the device. dma-bufs may be synced at any time. It can be reached from user space via DMA_BUF_IOCTL_SYNC, so there are no guarantees from callers on when syncs may be attempted, and dma_buf_end_cpu_access() and dma_buf_begin_cpu_access() may not be paired. Since the sg_list's dma_address isn't set up until the buffer is used on the device, and dma_map_sg() is called on it, the dma_address will be NULL if sync is attempted on the dma-buf before it's mapped on a device. Before v5.0 (commit 55897af63091 ("dma-direct: merge swiotlb_dma_ops into the dma_direct code")) this was a problem as the dma-api (at least the swiotlb_dma_ops on arm64) would use the potentially invalid dma_address. How that failed depended on how the device handled physical address 0. If 0 was a valid address to physical ram, that page would get flushed a lot, while the actual pages in the buffer would not get synced correctly. While if 0 is an invalid physical address it may cause a fault and trigger a crash. In v5.0 this was incidentally fixed by commit 55897af63091 ("dma-direct: merge swiotlb_dma_ops into the dma_direct code"), as this moved the dma-api to use the page pointer in the sg_list, and (for Ion buffers at least) this will always be valid if the sg_list exists at all. But, this issue is re-introduced in v5.3 with commit 449fa54d6815 ("dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device") moves the dma-api back to the old behaviour and picks the dma_address that may be invalid. dma-buf core doesn't ensure that the buffer is mapped on the device, and thus have a valid sg_list, before calling the exporter's begin_cpu_access. Logic and commit message originally by: Ørjan Eide Cc: Sumit Semwal Cc: Liam Mark Cc: Laura Abbott Cc: Brian Starkey Cc: Hridya Valsaraju Cc: Suren Baghdasaryan Cc: Sandeep Patil Cc: Daniel Mentz Cc: Chris Goldsworthy Cc: Ørjan Eide Cc: Robin Murphy Cc: Ezequiel Garcia Cc: Simon Ser Cc: James Jones Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Signed-off-by: John Stultz --- drivers/dma-buf/heaps/cma_heap.c | 10 ++++++++++ drivers/dma-buf/heaps/system_heap.c | 10 ++++++++++ 2 files changed, 20 insertions(+) -- 2.17.1 diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c index 366963b94c72..fece9d7739ae 100644 --- a/drivers/dma-buf/heaps/cma_heap.c +++ b/drivers/dma-buf/heaps/cma_heap.c @@ -44,6 +44,7 @@ struct dma_heap_attachment { struct device *dev; struct sg_table table; struct list_head list; + bool mapped; }; static int cma_heap_attach(struct dma_buf *dmabuf, @@ -68,6 +69,7 @@ static int cma_heap_attach(struct dma_buf *dmabuf, a->dev = attachment->dev; INIT_LIST_HEAD(&a->list); + a->mapped = false; attachment->priv = a; @@ -102,6 +104,7 @@ static struct sg_table *cma_heap_map_dma_buf(struct dma_buf_attachment *attachme ret = dma_map_sgtable(attachment->dev, table, direction, 0); if (ret) return ERR_PTR(-ENOMEM); + a->mapped = true; return table; } @@ -109,6 +112,9 @@ static void cma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, struct sg_table *table, enum dma_data_direction direction) { + struct dma_heap_attachment *a = attachment->priv; + + a->mapped = false; dma_unmap_sgtable(attachment->dev, table, direction, 0); } @@ -123,6 +129,8 @@ static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, mutex_lock(&buffer->lock); list_for_each_entry(a, &buffer->attachments, list) { + if (!a->mapped) + continue; dma_sync_sgtable_for_cpu(a->dev, &a->table, direction); } mutex_unlock(&buffer->lock); @@ -141,6 +149,8 @@ static int cma_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf, mutex_lock(&buffer->lock); list_for_each_entry(a, &buffer->attachments, list) { + if (!a->mapped) + continue; dma_sync_sgtable_for_device(a->dev, &a->table, direction); } mutex_unlock(&buffer->lock); diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 00ed107b3b76..ef8d47e5a7ff 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -37,6 +37,7 @@ struct dma_heap_attachment { struct device *dev; struct sg_table *table; struct list_head list; + bool mapped; }; static struct sg_table *dup_sg_table(struct sg_table *table) @@ -84,6 +85,7 @@ static int system_heap_attach(struct dma_buf *dmabuf, a->table = table; a->dev = attachment->dev; INIT_LIST_HEAD(&a->list); + a->mapped = false; attachment->priv = a; @@ -120,6 +122,7 @@ static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attac if (ret) return ERR_PTR(ret); + a->mapped = true; return table; } @@ -127,6 +130,9 @@ static void system_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, struct sg_table *table, enum dma_data_direction direction) { + struct dma_heap_attachment *a = attachment->priv; + + a->mapped = false; dma_unmap_sgtable(attachment->dev, table, direction, 0); } @@ -142,6 +148,8 @@ static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, invalidate_kernel_vmap_range(buffer->vaddr, buffer->len); list_for_each_entry(a, &buffer->attachments, list) { + if (!a->mapped) + continue; dma_sync_sgtable_for_cpu(a->dev, a->table, direction); } mutex_unlock(&buffer->lock); @@ -161,6 +169,8 @@ static int system_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf, flush_kernel_vmap_range(buffer->vaddr, buffer->len); list_for_each_entry(a, &buffer->attachments, list) { + if (!a->mapped) + continue; dma_sync_sgtable_for_device(a->dev, a->table, direction); } mutex_unlock(&buffer->lock);