From patchwork Wed Oct 10 23:33:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 148586 Delivered-To: patches@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp1457452lji; Wed, 10 Oct 2018 16:33:41 -0700 (PDT) X-Received: by 2002:a62:4301:: with SMTP id q1-v6mr35274676pfa.163.1539214421494; Wed, 10 Oct 2018 16:33:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539214421; cv=none; d=google.com; s=arc-20160816; b=uqD79bZjsehgRjHH7yd118Eir4MzuCjU5WoJvYElaer325lylnjNt+axAKoUZ8fI2j i6fGifTHjkkq6fVr3umUGXHbnC83Ou3y7Ge3+QstYWzZy69syRQxDn/+MN3Tx8fI15ar 07qZvfSkxyd3eHi7crmxgoWtop/Fa1NtZHS5PQTTPbem+tekfIWrsgtoXmeDhKmpVmTq MBfQ4gv9Dp13HEtwJ1PrHbxdoiGpNPST2NRYfEUeBQbDU91H0TZ5BxMib++mMYyTPX/2 tgjIM3DBbeUkzSTSCoF/6SWHusW4FfEPmDnDrXTbcK0zCykBgv59BpYkWKPMDeU0p51f dvtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature; bh=oq/1X2eWSUAz4jFjB/D59QGpN992y5YOc8MZYE9CqZQ=; b=AoLTbd4WEUqjXiEsrqlqOJs7TVsE5kIGSeNikU6f8hIOLwdc1m1mnkDcrzL1tDbwlt oni0Ta1XxqGPJzJHP3YKQ8glbj2NJxBr615S4bMtupJ6UdZVxkT2CihR8Vl+JHShXHAd nylpkIZEpryEDAkipNkn/qCiAJBezJ1SegXSu4Wr8Xk8oUpvs4yx6KV5QqA35ykPMLvy SK+FDi11lcz4M0Wd6PD8ovOpgcvk8zj0ZWJAM3TRY+VQleyaO1vQrsZKGvXlZEtiNqZo JNBc3TXOkE/e5my4/zgtzMZSQYcGaHMiPaEhftMmCeGHh7SXdr/lcYGr2cKdT51mWIb/ 9dQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GWn9Ky9I; 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 q4-v6sor11112280pli.34.2018.10.10.16.33.41 for (Google Transport Security); Wed, 10 Oct 2018 16:33:41 -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=GWn9Ky9I; 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; bh=oq/1X2eWSUAz4jFjB/D59QGpN992y5YOc8MZYE9CqZQ=; b=GWn9Ky9IucSpJsGk7CsZsX30oSpZRgynTQlcnX7q9DfhWv7MvLJnhj54tPeCsgmAE6 KSoWepqwO0mNLVW8mLg3yUuurEJYT4g2UGbwQTkTcHgK0C8SRQsAUjEMDY9YBBb68LOl uC/+EH7gST+vfYyerzahVzCnNnEfdSHUXvyj8= 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=oq/1X2eWSUAz4jFjB/D59QGpN992y5YOc8MZYE9CqZQ=; b=advPgZ6Yo/ddx0OtmbOR5WhI8iGxU6MwqMQjHetVdyUFJHsI6v3qYje0RjUT3KtpGF 43SSV0Z+ag5YaWBVuK7jIN0fOiSLdxtjZBtLf63X3txaK+/+rzDzQdc2azGHZo1WCqtB WcLuK3WEpW1hCZX5/ITTHIFeGs+5bm6OsQwzQh6w2VPx3CG+pCDAfugf/gPV8zip+/QA SJALGsy84vQlCYsOuOcaobJPlCw0/ALWuzWNYyt9z8Gun8mfLlRxkgNRIp6j9e+wE7Dw BV5bLA0BNmb1kf4UgA06eYSZmxtrQ/VJ9Ic6MGggmMrkrmwIvkCVGA7izyIT8gFgSUdp fE2Q== X-Gm-Message-State: ABuFfog0WNULQVmOUWYLbbtATB7liW5505xP6/lN6Wom0rLiYgIWZVR3 Tt3c3J2djTOFOas+pRwqiPoNDnLk X-Google-Smtp-Source: ACcGV60JxTJ0RziMCXqSDS12eA4/uWBZXhfDl7BX/z7MFmSkvHZ6KsLfMi9Qwomeq3xvu1V1Q6HYlA== X-Received: by 2002:a17:902:7c8b:: with SMTP id y11-v6mr1377404pll.321.1539214420377; Wed, 10 Oct 2018 16:33:40 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id q127-v6sm31115235pgq.19.2018.10.10.16.33.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 10 Oct 2018 16:33:38 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Beata Michalska , Matt Szczesiak , Anders Pedersen , John Reitan , Liam Mark , Laura Abbott , Sumit Semwal , Greg Kroah-Hartman , Todd Kjos , Martijn Coenen , dri-devel@lists.freedesktop.org Subject: [PATCH] staging: ion: Rework ion_map_dma_buf() to minimize re-mapping Date: Wed, 10 Oct 2018 16:33:33 -0700 Message-Id: <1539214413-26173-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 Since 4.12, much later narrowed down to commit 2a55e7b5e544 ("staging: android: ion: Call dma_map_sg for syncing and mapping"), we have seen graphics performance issues on the HiKey960. This was initially confounded by the fact that the out-of-tree DRM driver was using HiSi custom ION heap which broke with the 4.12 ION abi changes, so there was lots of suspicion that the performance problems were due to switching to a somewhat simple cma based DRM driver for HiKey960. Additionally, as no performance regression was seen w/ the original HiKey board (which is SMP, not big.LITTLE as w/ HiKey960), there was some thought that the out-of-tree EAS code wasn't quite optimized. But after chasing a number of other leads, I found that reverting the ION code to 4.11-era got the majority of the graphics performance back (there may yet be further EAS tweaks needed), which lead me to the dma_map_sg change. In talking w/ Laura and Liam, it was suspected that the extra cache operations were causing the trouble. Additionally, I found that part of the reason we didn't see this w/ the original HiKey board is that its (proprietary blob) GL code uses ion_mmap and ion_map_dma_buf is called very rarely, where as with HiKey960, the (also proprietary blob) GL code calls ion_map_dma_buf much more frequently via the kernel driver. Anyway, with the cause of the performance regression isolated, I've tried to find a way to improve the performance of the current code. This approach, which I've mostly copied from the drm_prime implementation is to try to track the direction we're mapping the buffers so we can avoid calling dma_map/unmap_sg on every ion_map_dma_buf/ion_unmap_dma_buf call, and instead try to do the work in attach/detach paths. I'm not 100% sure of the correctness here, so close review would be good, but it gets the performance back to being similar to reverting the ION code to the 4.11-era. Feedback would be greatly appreciated! Cc: Beata Michalska Cc: Matt Szczesiak Cc: Anders Pedersen Cc: John Reitan Cc: Liam Mark Cc: Laura Abbott Cc: Sumit Semwal Cc: Greg Kroah-Hartman Cc: Todd Kjos Cc: Martijn Coenen Cc: dri-devel@lists.freedesktop.org Signed-off-by: John Stultz --- drivers/staging/android/ion/ion.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) -- 2.7.4 diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c index 9907332..a4d7fca 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -199,6 +199,7 @@ struct ion_dma_buf_attachment { struct device *dev; struct sg_table *table; struct list_head list; + enum dma_data_direction dir; }; static int ion_dma_buf_attach(struct dma_buf *dmabuf, @@ -220,6 +221,7 @@ static int ion_dma_buf_attach(struct dma_buf *dmabuf, a->table = table; a->dev = attachment->dev; + a->dir = DMA_NONE; INIT_LIST_HEAD(&a->list); attachment->priv = a; @@ -236,6 +238,18 @@ static void ion_dma_buf_detatch(struct dma_buf *dmabuf, { struct ion_dma_buf_attachment *a = attachment->priv; struct ion_buffer *buffer = dmabuf->priv; + struct sg_table *table; + + if (!a) + return; + + table = a->table; + if (table) { + if (a->dir != DMA_NONE) + dma_unmap_sg(attachment->dev, table->sgl, table->nents, + a->dir); + sg_free_table(table); + } free_duped_table(a->table); mutex_lock(&buffer->lock); @@ -243,6 +257,7 @@ static void ion_dma_buf_detatch(struct dma_buf *dmabuf, mutex_unlock(&buffer->lock); kfree(a); + attachment->priv = NULL; } static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment, @@ -251,12 +266,24 @@ static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment, struct ion_dma_buf_attachment *a = attachment->priv; struct sg_table *table; - table = a->table; + if (WARN_ON(direction == DMA_NONE || !a)) + return ERR_PTR(-EINVAL); - if (!dma_map_sg(attachment->dev, table->sgl, table->nents, - direction)) - return ERR_PTR(-ENOMEM); + if (a->dir == direction) + return a->table; + if (WARN_ON(a->dir != DMA_NONE)) + return ERR_PTR(-EBUSY); + + table = a->table; + if (!IS_ERR(table)) { + if (!dma_map_sg(attachment->dev, table->sgl, table->nents, + direction)) { + table = ERR_PTR(-ENOMEM); + } else { + a->dir = direction; + } + } return table; } @@ -264,7 +291,6 @@ static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment, struct sg_table *table, enum dma_data_direction direction) { - dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction); } static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)