Received: by 10.223.176.5 with SMTP id f5csp1511740wra; Fri, 9 Feb 2018 22:22:01 -0800 (PST) X-Google-Smtp-Source: AH8x225sZChKMJA3z9Fo5TwMUi82f6vJN4cyFYuy9WRQntHgBJOm9ShFb3v3C775UCCM3JGsB5b4 X-Received: by 2002:a17:902:2bc5:: with SMTP id l63-v6mr4865556plb.108.1518243721665; Fri, 09 Feb 2018 22:22:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518243721; cv=none; d=google.com; s=arc-20160816; b=lDY4jK6Iju+NsptNgO34JOS1qyg6Ha6/ElArkRar9DWhe6dfiOsboIe4Vv3wmLc0Ni 3derCrRmGiJWG+vVOk8SDiC8NgbBoovATJEJqdGpoYhJEa2e0Vzud7mrKQWjlM/WC9Pb RfRjmAC3i8mVElvFOMMK2kbjKWSjonSVUYQ4h0KwZnVpSqnrffF399DegXThE5sB3Fhm reXxfg4lwQJtmstwEiRn6COeU7yC5utEnI2cLoMSLrDHHuqp/K+aqNgZzqUflBkPyuZB a0vq1C9pJbWY7a6RbOsGBHHjQ9oauF8mDTzPs998+shfUbf/SKUZFLs6O+rSAUqknlpq SDvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :subject:cc:to:from:date:dmarc-filter:dkim-signature:dkim-signature :arc-authentication-results; bh=ZgMleiTKeh+4eMqNeUzMd4xt2UA/UwiBOhbWrNem4ZM=; b=RYwN+6S7dMfA3FAtxh6TM4NZozst9nNzD1xhyOQ9f0zAetBETk2/ULUWasWPdfcY+w UfitwroIYV4ItyqDkZE54C5wGiqS7+0dpKomUzzoVMoXfhCOIObV//3SKijchYpPWX7e 0woUv7BVLA+EUoSYh/TxIRthe5nUO/JdZe0AshaYMjsp90uq2cPJ4QW7B5uGH8FDe8O1 duUamBeODhm2bttv5DPMYRl/6xyxJh2PzzQm6HQpDckY9ZSwyRjmBxwuBJ7mCmNowANo ghWORRToGIVIyOAxp2qP7mY0hKUROBaxXUscqdNQ/1Kk138v1Ly2/XNXRhM7iKD0hFDz rWBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=lkjWQN8s; dkim=pass header.i=@codeaurora.org header.s=default header.b=J4wuDx/0; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w19si2911609pfi.313.2018.02.09.22.21.47; Fri, 09 Feb 2018 22:22:01 -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=@codeaurora.org header.s=default header.b=lkjWQN8s; dkim=pass header.i=@codeaurora.org header.s=default header.b=J4wuDx/0; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751070AbeBJGVI (ORCPT + 99 others); Sat, 10 Feb 2018 01:21:08 -0500 Received: from smtp.codeaurora.org ([198.145.29.96]:45952 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750775AbeBJGVH (ORCPT ); Sat, 10 Feb 2018 01:21:07 -0500 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 0D032609EF; Sat, 10 Feb 2018 06:21:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1518243667; bh=m5kayasnjvU1l7CP+69L5/5BiC1vgnqW77qS57VL7Zo=; h=Date:From:To:cc:Subject:From; b=lkjWQN8s75l02rnD30MDP7DHUneFd2xTyo4Fu/2CHVXsgIjtdyDrmzLCK0cx9/vRQ ZTjjW3+obCrg++hpm9POfGxxqwE2e8XqIKkEknQTMGqh3RgT4oHdqAcII5oxw1PPm8 D8TaAk+Zhtlp+ul9qneqrW9NNrcb8cYG63uvczKA= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.0 Received: from lmark-linux.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: lmark@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id E8B5060558; Sat, 10 Feb 2018 06:21:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1518243666; bh=m5kayasnjvU1l7CP+69L5/5BiC1vgnqW77qS57VL7Zo=; h=Date:From:To:cc:Subject:From; b=J4wuDx/038foPMLreOA5KIK+K/4RnEHkIJx4JUJGMkisVR41nZ8bsH9n1D2M7++U9 Y6f+KhOzQy7yld/XKnNm3UKMjrFK3l6iB5GRiRgQpvxE/upZypk9dMBQbPtEnp55Nn MCFiNMrKopKe7beM62evUsjcxwCBpI7GHSALBdlg= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org E8B5060558 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=lmark@codeaurora.org Date: Fri, 9 Feb 2018 22:21:05 -0800 (PST) From: Liam Mark X-X-Sender: lmark@lmark-linux.qualcomm.com To: Laura Abbott , Sumit Semwal cc: Martijn Coenen , Todd Kjos , =?ISO-8859-15?Q?Arve_Hj=F8nnev=E5g?= , linux-kernel@vger.kernel.org, devel@driverdev.osuosl.org, linaro-mm-sig@lists.linaro.org Subject: [PATCH] staging: android: ion: Restrict cache maintenance to dma mapped memory Message-ID: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The ION begin_cpu_access and end_cpu_access functions use the dma_sync_sg_for_cpu and dma_sync_sg_for_device APIs to perform cache maintenance. Currently it is possible to apply cache maintenance, via the begin_cpu_access and end_cpu_access APIs, to ION buffers which are not dma mapped. The dma sync sg APIs should not be called on sg lists which have not been dma mapped as this can result in cache maintenance being applied to the wrong address. If an sg list has not been dma mapped then its dma_address field has not been populated, some dma ops such as the swiotlb_dma_ops ops use the dma_address field to calculate the address onto which to apply cache maintenance. Fix the ION begin_cpu_access and end_cpu_access functions to only apply cache maintenance to buffers which have been dma mapped. Fixes: 2a55e7b5e544 ("staging: android: ion: Call dma_map_sg for syncing and mapping") Signed-off-by: Liam Mark --- drivers/staging/android/ion/ion.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c index f480885e346b..e5df5272823d 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -214,6 +214,7 @@ struct ion_dma_buf_attachment { struct device *dev; struct sg_table *table; struct list_head list; + bool dma_mapped; }; static int ion_dma_buf_attach(struct dma_buf *dmabuf, struct device *dev, @@ -235,6 +236,7 @@ static int ion_dma_buf_attach(struct dma_buf *dmabuf, struct device *dev, a->table = table; a->dev = dev; + a->dma_mapped = false; INIT_LIST_HEAD(&a->list); attachment->priv = a; @@ -272,6 +274,7 @@ static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment, direction)) return ERR_PTR(-ENOMEM); + a->dma_mapped = true; return table; } @@ -279,7 +282,10 @@ static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment, struct sg_table *table, enum dma_data_direction direction) { + struct ion_dma_buf_attachment *a = attachment->priv; + dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction); + a->dma_mapped = false; } static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) @@ -345,8 +351,9 @@ static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, mutex_lock(&buffer->lock); list_for_each_entry(a, &buffer->attachments, list) { - dma_sync_sg_for_cpu(a->dev, a->table->sgl, a->table->nents, - direction); + if (a->dma_mapped) + dma_sync_sg_for_cpu(a->dev, a->table->sgl, + a->table->nents, direction); } mutex_unlock(&buffer->lock); @@ -367,8 +374,9 @@ static int ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, mutex_lock(&buffer->lock); list_for_each_entry(a, &buffer->attachments, list) { - dma_sync_sg_for_device(a->dev, a->table->sgl, a->table->nents, - direction); + if (a->dma_mapped) + dma_sync_sg_for_device(a->dev, a->table->sgl, + a->table->nents, direction); } mutex_unlock(&buffer->lock); -- 1.8.5.2 Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project