Received: by 10.223.185.116 with SMTP id b49csp2615578wrg; Mon, 12 Feb 2018 12:40:39 -0800 (PST) X-Google-Smtp-Source: AH8x22669vRbVsyB/naAgPlndyFpQMaFrW/6pNGSbixSU69U4d3CTrDKBYbwh2vv3sVklNfWKBNM X-Received: by 2002:a17:902:69cf:: with SMTP id m15-v6mr3197641pln.104.1518468039295; Mon, 12 Feb 2018 12:40:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518468039; cv=none; d=google.com; s=arc-20160816; b=a7aegkjylIghKZyfmtP/UU2MgICfUEgc5Etcxfpd/QQi0GGSBXESMxSc8bcqalLKDP G7rNVmbvGAO5KnVDJFTWyKvBAz7yGsaB3QDTzgVeODsdFS55vKnBwwSS/ZJSuPF1YjoX RmjtYH0MxoQsM60iqOp+TW5Oj+GzQ1AiLQ2a7+ea/9G2sGFFRC5nYa7o6OqJEsQKCtKg t21zcEgveV6DF5Ry1+QuWY/Lao/Q30Ni1RBNbVJL0FVBPzRwoWZ6Eox/B703e+OmgkuZ 1l1HbhWNf/W1g8Xgn0xc66VR7FqauAtYeRZgUzsMS0+Kn3IRkqzGA1s1NQ+mPE3qUqGa suTQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=NCXFrja6/RnI32rUHWRhrlG079LGJSdSnSxKqUhZ/+A=; b=vp6TpKYgBlWZABX395Gbz66g2mvi41oa0+gvE+47Rn2lC0zETA5wrFSfgcaeQMzKMp 2/UDZnyUZWVdjnKWtHkgBmnQkT7S4Z9Zve/nGpDh+lCJCQf2OkBi3HKltn4d44KNkXac Yq4gAzwZKEPGzHbDfn/8hxoPpR0hSmWLo0faP0BcsrIo+fSIuXzJsBplAXtw/MjOUIfH Zd74g8hM9t9aHss2hcOLu1xqCEJ4gvHio/kRfHaOR0+IjdZBQFKNiyaBJeVaop9qY3mX +ZBrOWQQUDJ2OtEScmq9KMGMumTnll+6sXcDeqpzZ6bIkqWp6GZMvt5QRR5Nb9ojYqd1 vOCw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c1-v6si368801plk.813.2018.02.12.12.40.24; Mon, 12 Feb 2018 12:40:39 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753916AbeBLUjq (ORCPT + 99 others); Mon, 12 Feb 2018 15:39:46 -0500 Received: from mail-oi0-f67.google.com ([209.85.218.67]:37911 "EHLO mail-oi0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753008AbeBLUjn (ORCPT ); Mon, 12 Feb 2018 15:39:43 -0500 Received: by mail-oi0-f67.google.com with SMTP id j15so12203129oii.5 for ; Mon, 12 Feb 2018 12:39:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=NCXFrja6/RnI32rUHWRhrlG079LGJSdSnSxKqUhZ/+A=; b=YG6wdNRvn4uopjVYh1guPp5C3UbpT9KpJM1kUzNDge/ADIsi+9IOWT78oOPTDWwisf Kjvubq8b248UlHuowmU67PIwlTuSlq0JL9HYWAbqS+98G54vrJUjaxHdjg4mGeBd3bNN VjJhLU+bwOKAuTZGdwisI4YQAAm+Q50PHmrlkQ8v+CCyBqiIClfumF8F2CFFReVrCwsX ZO9rtLYEknygcTMkonuuOHHAlCgg/ThFRtwxVPHED6lPy38YJ1CHd6WUweHcYokyX48i i+ofsbPvFtL7LTqmkqKvQaEEhaNfDoJKn65KHaV8NPdCCGDnG8XfpDz9lVA0BetIh6pA d0MQ== X-Gm-Message-State: APf1xPCfeIkxKcnbwz7G8EpTdIXLso+ecZKdI2VF6qg7dD/srgKRs2CW nd1Hc8YbSqS5RddC8GNjvV17tA== X-Received: by 10.202.72.6 with SMTP id v6mr9298565oia.237.1518467983049; Mon, 12 Feb 2018 12:39:43 -0800 (PST) Received: from ?IPv6:2601:602:9802:a8dc::f21a? ([2601:602:9802:a8dc::f21a]) by smtp.gmail.com with ESMTPSA id p52sm5281274ote.59.2018.02.12.12.39.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Feb 2018 12:39:41 -0800 (PST) Subject: Re: [PATCH] staging: android: ion: Restrict cache maintenance to dma mapped memory To: Liam Mark , Sumit Semwal Cc: Martijn Coenen , Todd Kjos , =?UTF-8?Q?Arve_Hj=c3=b8nnev=c3=a5g?= , linux-kernel@vger.kernel.org, devel@driverdev.osuosl.org, linaro-mm-sig@lists.linaro.org References: From: Laura Abbott Message-ID: <6c61b218-c56d-659a-47f8-2cd5d44d28ca@redhat.com> Date: Mon, 12 Feb 2018 12:39:40 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/09/2018 10:21 PM, Liam Mark wrote: > 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. > I think this looks okay. I was initially concerned about concurrency and setting the dma_mapped flag but I think that should be handled by the caller synchronizing map/unmap/cpu_access calls (we might need to re-evaluate in the future) I would like to hold on queuing this for just a little bit until I finish working on the Ion unit test (doing this in the complete opposite order of course). I'm assuming this passed your internal tests Liam? Thanks, Laura > 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;caller > 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); > >