From nobody Tue Nov 26 12:28:24 2024 Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9A941DED44 for ; Thu, 17 Oct 2024 18:14:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729188851; cv=none; b=uBXKUjV2XFBnsmc4DALODapAKfu/rHKZYiruiIkEl28dj8syjsehQ4lTO0pkslwmr9InJmEL5H2kQNOW6e8FgCFJSr+YFAZBuZ1nu6ahl2w+Jc9RDQcLEFy8h02hU+CXuys9WSIR6bo7dyuUSnKrM/dJUULe1XgU2d+EYGhJjsg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729188851; c=relaxed/simple; bh=XO+oLqPhK2FCwtl+k5xHn8MLn0qkKcZlSKIG+z+asss=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uHFXjzTfBIgioonk3Uwr+0N0vMWd0E2dp0TwV2FS7MAga2NskviM2G//5ABSGYE0uBuVvL2WWudXwg9pS95q+M0N+SHfQprkWq9kzk0O9Y//AkCd5ExoAwBHqwDmP7DXSESc1ZvFuoFimAe26KpXZbwSFHqyub1OoNfqXbbT2Ks= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=DmzVUPhL; arc=none smtp.client-ip=91.218.175.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="DmzVUPhL" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1729188843; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tzHmyuSvQgBZ0rMpQ3JAK1It+QHSTZx2/NVdS2eTqbg=; b=DmzVUPhLoqfcjzLAgI1+LMhg5q1KFfE1s5gk20uMsJjxwUS9QufgUU8YwIfE9NPRib7BJW UJ+aHLeiADWbmV0bMTB+11DZ001h8fPxyAMepyI8CLpwKxqPJQ8hBQqjj4Y1Lga2qLMlJ6 daoMIfJc+4VhkPX0MUOPqiB7jT+tUwY= From: Sean Anderson To: Christoph Hellwig , iommu@lists.linux.dev Cc: linux-trace-kernel@vger.kernel.org, Masami Hiramatsu , linux-kernel@vger.kernel.org, Robin Murphy , Mathieu Desnoyers , Steven Rostedt , Marek Szyprowski , Sean Anderson Subject: [PATCH 1/3] dma-mapping: Trace dma_alloc/free direction Date: Thu, 17 Oct 2024 14:13:52 -0400 Message-Id: <20241017181354.2834674-2-sean.anderson@linux.dev> In-Reply-To: <20241017181354.2834674-1-sean.anderson@linux.dev> References: <20241017181354.2834674-1-sean.anderson@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" In preparation for using these tracepoints in a few more places, trace the DMA direction as well. For coherent allocations this is always bidirectional. Signed-off-by: Sean Anderson --- include/trace/events/dma.h | 18 ++++++++++++------ kernel/dma/mapping.c | 6 ++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/trace/events/dma.h b/include/trace/events/dma.h index b0f41265191c..012729cc178f 100644 --- a/include/trace/events/dma.h +++ b/include/trace/events/dma.h @@ -116,8 +116,9 @@ DEFINE_EVENT(dma_unmap, dma_unmap_resource, =20 TRACE_EVENT(dma_alloc, TP_PROTO(struct device *dev, void *virt_addr, dma_addr_t dma_addr, - size_t size, gfp_t flags, unsigned long attrs), - TP_ARGS(dev, virt_addr, dma_addr, size, flags, attrs), + size_t size, enum dma_data_direction dir, gfp_t flags, + unsigned long attrs), + TP_ARGS(dev, virt_addr, dma_addr, size, dir, flags, attrs), =20 TP_STRUCT__entry( __string(device, dev_name(dev)) @@ -125,6 +126,7 @@ TRACE_EVENT(dma_alloc, __field(u64, dma_addr) __field(size_t, size) __field(gfp_t, flags) + __field(enum dma_data_direction, dir) __field(unsigned long, attrs) ), =20 @@ -137,8 +139,9 @@ TRACE_EVENT(dma_alloc, __entry->attrs =3D attrs; ), =20 - TP_printk("%s dma_addr=3D%llx size=3D%zu virt_addr=3D%p flags=3D%s attrs= =3D%s", + TP_printk("%s dir=3D%s dma_addr=3D%llx size=3D%zu virt_addr=3D%p flags=3D= %s attrs=3D%s", __get_str(device), + decode_dma_data_direction(__entry->dir), __entry->dma_addr, __entry->size, __entry->virt_addr, @@ -148,14 +151,15 @@ TRACE_EVENT(dma_alloc, =20 TRACE_EVENT(dma_free, TP_PROTO(struct device *dev, void *virt_addr, dma_addr_t dma_addr, - size_t size, unsigned long attrs), - TP_ARGS(dev, virt_addr, dma_addr, size, attrs), + size_t size, enum dma_data_direction dir, unsigned long attrs), + TP_ARGS(dev, virt_addr, dma_addr, size, dir, attrs), =20 TP_STRUCT__entry( __string(device, dev_name(dev)) __field(void *, virt_addr) __field(u64, dma_addr) __field(size_t, size) + __field(enum dma_data_direction, dir) __field(unsigned long, attrs) ), =20 @@ -164,11 +168,13 @@ TRACE_EVENT(dma_free, __entry->virt_addr =3D virt_addr; __entry->dma_addr =3D dma_addr; __entry->size =3D size; + __entry->dir =3D dir; __entry->attrs =3D attrs; ), =20 - TP_printk("%s dma_addr=3D%llx size=3D%zu virt_addr=3D%p attrs=3D%s", + TP_printk("%s dir=3D%s dma_addr=3D%llx size=3D%zu virt_addr=3D%p attrs=3D= %s", __get_str(device), + decode_dma_data_direction(__entry->dir), __entry->dma_addr, __entry->size, __entry->virt_addr, diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 864a1121bf08..944ac835030a 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -619,7 +619,8 @@ void *dma_alloc_attrs(struct device *dev, size_t size, = dma_addr_t *dma_handle, else return NULL; =20 - trace_dma_alloc(dev, cpu_addr, *dma_handle, size, flag, attrs); + trace_dma_alloc(dev, cpu_addr, *dma_handle, size, DMA_BIDIRECTIONAL, + flag, attrs); debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr, attrs); return cpu_addr; } @@ -644,7 +645,8 @@ void dma_free_attrs(struct device *dev, size_t size, vo= id *cpu_addr, if (!cpu_addr) return; =20 - trace_dma_free(dev, cpu_addr, dma_handle, size, attrs); + trace_dma_free(dev, cpu_addr, dma_handle, size, DMA_BIDIRECTIONAL, + attrs); debug_dma_free_coherent(dev, size, cpu_addr, dma_handle); if (dma_alloc_direct(dev, ops)) dma_direct_free(dev, size, cpu_addr, dma_handle, attrs); --=20 2.35.1.1320.gc452695387.dirty From nobody Tue Nov 26 12:28:24 2024 Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89E991DFE2A for ; Thu, 17 Oct 2024 18:14:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.185 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729188852; cv=none; b=pDvLfNc1Bay2ivMUR0OKuWh2rdr84S/1j00WFIpBJ1W97yd9s5StSDAfKjEroSwaxmmTAb1ykZ5PV9GPJXeaPMBEUs2E+AlolT8QSXqGDx4qLX7cX7KMw/TJfi6xF0akd/GU6bDsoaEaZ4gZBqvZ8ywoaiNMTjvsFP1qdllGW/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729188852; c=relaxed/simple; bh=uA8a1ikplMyqr+uLsKVt9H+ZhJSzfwtxijknmp+of3Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=paneo1lpHhno7rNbZ8x4TDraA8OSRZai311XfdXvXStqsmsaPioCUFygaePM3eMY4ZzXvIXZi64RCVNnmm2zwiQVwYP6oIripCLkcps5eJm+YlqmuIKZh6Xx29kKteAcr6DXeiNkx+X8uP+zuUhzCSgtoTYldR3GnQIsONXOr/s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=fiVr8PLn; arc=none smtp.client-ip=91.218.175.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="fiVr8PLn" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1729188845; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5VE/LN5ytV5ixrWdnI2IQyWvpMxqaOvoKt4lNHXW/fY=; b=fiVr8PLnqCX5MuBdmy8lNmqvxcjDHmQFvQO9Z5Wd3u6BO5fSLqXTKDVKZ5Z1CLXLIsd1qa SyBHm+FIXkQBlDHA2xYWB3+/YrwfTb+yd6Na/LW3lCQ0t+srQ4Nql5khArhJHH6W1fXlmI geFVLXRSr8eNXrvRXCh0c3///1wd5UY= From: Sean Anderson To: Christoph Hellwig , iommu@lists.linux.dev Cc: linux-trace-kernel@vger.kernel.org, Masami Hiramatsu , linux-kernel@vger.kernel.org, Robin Murphy , Mathieu Desnoyers , Steven Rostedt , Marek Szyprowski , Sean Anderson Subject: [PATCH 2/3] dma-mapping: Use trace_dma_alloc for dma_alloc* instead of using trace_dma_map Date: Thu, 17 Oct 2024 14:13:53 -0400 Message-Id: <20241017181354.2834674-3-sean.anderson@linux.dev> In-Reply-To: <20241017181354.2834674-1-sean.anderson@linux.dev> References: <20241017181354.2834674-1-sean.anderson@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" In some cases, we use trace_dma_map to trace dma_alloc* functions. This generally follows dma_debug. However, this does not record all of the relevant information for allocations, such as GFP flags. Create new dma_alloc tracepoints for these functions. Note that while dma_alloc_noncontiguous may allocate discontiguous pages (from the CPU's point of view), the device will only see one contiguous mapping. Therefore, we just need to trace dma_addr and size. Signed-off-by: Sean Anderson --- include/trace/events/dma.h | 102 ++++++++++++++++++++++++++++++++++++- kernel/dma/mapping.c | 10 ++-- 2 files changed, 105 insertions(+), 7 deletions(-) diff --git a/include/trace/events/dma.h b/include/trace/events/dma.h index 012729cc178f..9bc647f9ad4d 100644 --- a/include/trace/events/dma.h +++ b/include/trace/events/dma.h @@ -114,7 +114,7 @@ DEFINE_EVENT(dma_unmap, dma_unmap_resource, enum dma_data_direction dir, unsigned long attrs), TP_ARGS(dev, addr, size, dir, attrs)); =20 -TRACE_EVENT(dma_alloc, +DECLARE_EVENT_CLASS(_dma_alloc, TP_PROTO(struct device *dev, void *virt_addr, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir, gfp_t flags, unsigned long attrs), @@ -149,7 +149,60 @@ TRACE_EVENT(dma_alloc, decode_dma_attrs(__entry->attrs)) ); =20 -TRACE_EVENT(dma_free, +DEFINE_EVENT(_dma_alloc, dma_alloc, + TP_PROTO(struct device *dev, void *virt_addr, dma_addr_t dma_addr, + size_t size, enum dma_data_direction dir, gfp_t flags, + unsigned long attrs), + TP_ARGS(dev, virt_addr, dma_addr, size, dir, flags, attrs)); + +DEFINE_EVENT(_dma_alloc, dma_alloc_pages, + TP_PROTO(struct device *dev, void *virt_addr, dma_addr_t dma_addr, + size_t size, enum dma_data_direction dir, gfp_t flags, + unsigned long attrs), + TP_ARGS(dev, virt_addr, dma_addr, size, dir, flags, attrs)); + +TRACE_EVENT(dma_alloc_sgt, + TP_PROTO(struct device *dev, struct sg_table *sgt, size_t size, + enum dma_data_direction dir, gfp_t flags, unsigned long attrs), + TP_ARGS(dev, sgt, size, dir, flags, attrs), + + TP_STRUCT__entry( + __string(device, dev_name(dev)) + __dynamic_array(u64, phys_addrs, sgt->orig_nents) + __field(u64, dma_addr) + __field(size_t, size) + __field(enum dma_data_direction, dir) + __field(gfp_t, flags) + __field(unsigned long, attrs) + ), + + TP_fast_assign( + struct scatterlist *sg; + int i; + + __assign_str(device); + for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) + ((u64 *)__get_dynamic_array(phys_addrs))[i] =3D sg_phys(sg); + __entry->dma_addr =3D sg_dma_address(sgt->sgl); + __entry->size =3D size; + __entry->dir =3D dir; + __entry->flags =3D flags; + __entry->attrs =3D attrs; + ), + + TP_printk("%s dir=3D%s dma_addr=3D%llx size=3D%zu phys_addrs=3D%s flags= =3D%s attrs=3D%s", + __get_str(device), + decode_dma_data_direction(__entry->dir), + __entry->dma_addr, + __entry->size, + __print_array(__get_dynamic_array(phys_addrs), + __get_dynamic_array_len(phys_addrs) / + sizeof(u64), sizeof(u64)), + show_gfp_flags(__entry->flags), + decode_dma_attrs(__entry->attrs)) +); + +DECLARE_EVENT_CLASS(_dma_free, TP_PROTO(struct device *dev, void *virt_addr, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir, unsigned long attrs), TP_ARGS(dev, virt_addr, dma_addr, size, dir, attrs), @@ -181,6 +234,51 @@ TRACE_EVENT(dma_free, decode_dma_attrs(__entry->attrs)) ); =20 +DEFINE_EVENT(_dma_free, dma_free, + TP_PROTO(struct device *dev, void *virt_addr, dma_addr_t dma_addr, + size_t size, enum dma_data_direction dir, unsigned long attrs), + TP_ARGS(dev, virt_addr, dma_addr, size, dir, attrs)); + +DEFINE_EVENT(_dma_free, dma_free_pages, + TP_PROTO(struct device *dev, void *virt_addr, dma_addr_t dma_addr, + size_t size, enum dma_data_direction dir, unsigned long attrs), + TP_ARGS(dev, virt_addr, dma_addr, size, dir, attrs)); + +TRACE_EVENT(dma_free_sgt, + TP_PROTO(struct device *dev, struct sg_table *sgt, size_t size, + enum dma_data_direction dir), + TP_ARGS(dev, sgt, size, dir), + + TP_STRUCT__entry( + __string(device, dev_name(dev)) + __dynamic_array(u64, phys_addrs, sgt->orig_nents) + __field(u64, dma_addr) + __field(size_t, size) + __field(enum dma_data_direction, dir) + ), + + TP_fast_assign( + struct scatterlist *sg; + int i; + + __assign_str(device); + for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) + ((u64 *)__get_dynamic_array(phys_addrs))[i] =3D sg_phys(sg); + __entry->dma_addr =3D sg_dma_address(sgt->sgl); + __entry->size =3D size; + __entry->dir =3D dir; + ), + + TP_printk("%s dir=3D%s dma_addr=3D%llx size=3D%zu phys_addrs=3D%s", + __get_str(device), + decode_dma_data_direction(__entry->dir), + __entry->dma_addr, + __entry->size, + __print_array(__get_dynamic_array(phys_addrs), + __get_dynamic_array_len(phys_addrs) / + sizeof(u64), sizeof(u64))) +); + TRACE_EVENT(dma_map_sg, TP_PROTO(struct device *dev, struct scatterlist *sgl, int nents, int ents, enum dma_data_direction dir, unsigned long attrs), diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 944ac835030a..b8a6bc492fae 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -685,8 +685,8 @@ struct page *dma_alloc_pages(struct device *dev, size_t= size, struct page *page =3D __dma_alloc_pages(dev, size, dma_handle, dir, gfp); =20 if (page) { - trace_dma_map_page(dev, page_to_phys(page), *dma_handle, size, - dir, 0); + trace_dma_alloc_pages(dev, page_to_virt(page), *dma_handle, + size, dir, gfp, 0); debug_dma_map_page(dev, page, 0, size, dir, *dma_handle, 0); } return page; @@ -710,7 +710,7 @@ static void __dma_free_pages(struct device *dev, size_t= size, struct page *page, void dma_free_pages(struct device *dev, size_t size, struct page *page, dma_addr_t dma_handle, enum dma_data_direction dir) { - trace_dma_unmap_page(dev, dma_handle, size, dir, 0); + trace_dma_free_pages(dev, page_to_virt(page), dma_handle, size, dir, 0); debug_dma_unmap_page(dev, dma_handle, size, dir); __dma_free_pages(dev, size, page, dma_handle, dir); } @@ -770,7 +770,7 @@ struct sg_table *dma_alloc_noncontiguous(struct device = *dev, size_t size, =20 if (sgt) { sgt->nents =3D 1; - trace_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir, attrs); + trace_dma_alloc_sgt(dev, sgt, size, dir, gfp, attrs); debug_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir, attrs); } return sgt; @@ -789,7 +789,7 @@ static void free_single_sgt(struct device *dev, size_t = size, void dma_free_noncontiguous(struct device *dev, size_t size, struct sg_table *sgt, enum dma_data_direction dir) { - trace_dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir, 0); + trace_dma_free_sgt(dev, sgt, size, dir); debug_dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); =20 if (use_dma_iommu(dev)) --=20 2.35.1.1320.gc452695387.dirty From nobody Tue Nov 26 12:28:24 2024 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36E421E00BC for ; Thu, 17 Oct 2024 18:14:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729188852; cv=none; b=rcT7vzZMQRirI2vI/jkEVF6O43chucaSge7sX9n8/jzrqfvkeV/nEcESYGGfdupJCJpmsCnGKwF/bP5puV8XQInOVghVYqDge9SmJ1/RtpfrLeZKriwyd9dLWeAgCfQZzU56Y4fQ7dpzU+18z/jT6K3p89gpWzIL6AtUilm/Wwk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729188852; c=relaxed/simple; bh=Bkxf/s2KVugIM6ypQC9OHaVP5BGsDoJqzVxswBQsqLQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Uo2eYYqeBvxo8F640CMoCq2p7WoEAXqvqS+90an5nv0a/dGsZBmSeYhLiY+wdzWPFIuOzAVWzL/QK4MO0k+B0mjAA/nYzgFmT2JzOPmt/Hjs4Ip62wpREtZnqiVBC9f93NV10b361xeeyH/6j6+kzMh28RXg5DIlt1Q0b6Jf3QY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=cuRuTJaQ; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="cuRuTJaQ" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1729188847; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e4+EelHK0Kh0nAsHxkJLrbq+zC392Zdrc6//8o5rDy0=; b=cuRuTJaQlcgxCM1mjYSkLgff+DNPD8vIUGZcFslEp5RVh5KPhMY2tTOItMrUBAGlS8xlAR QCl7EnQF0FNUksuZCqj6bxjZ7CNwTFI5lbBjhi9q6MzbyTaYAnFgXcLzqG+iHTCqzAdQG8 4T/rHKIw8PRWp93GT9HZhWfE31nC+Zc= From: Sean Anderson To: Christoph Hellwig , iommu@lists.linux.dev Cc: linux-trace-kernel@vger.kernel.org, Masami Hiramatsu , linux-kernel@vger.kernel.org, Robin Murphy , Mathieu Desnoyers , Steven Rostedt , Marek Szyprowski , Sean Anderson Subject: [PATCH 3/3] dma-mapping: Trace more error paths Date: Thu, 17 Oct 2024 14:13:54 -0400 Message-Id: <20241017181354.2834674-4-sean.anderson@linux.dev> In-Reply-To: <20241017181354.2834674-1-sean.anderson@linux.dev> References: <20241017181354.2834674-1-sean.anderson@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" It can be surprising to the user if DMA functions are only traced on success. On failure, it can be unclear what the source of the problem is. Fix this by tracing all functions even when they fail. Cases where we BUG/WARN are skipped, since those should be sufficiently noisy already. Signed-off-by: Sean Anderson --- include/trace/events/dma.h | 41 ++++++++++++++++++++++++++++++++++++++ kernel/dma/mapping.c | 27 +++++++++++++++++-------- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/include/trace/events/dma.h b/include/trace/events/dma.h index 9bc647f9ad4d..321cce327404 100644 --- a/include/trace/events/dma.h +++ b/include/trace/events/dma.h @@ -161,6 +161,12 @@ DEFINE_EVENT(_dma_alloc, dma_alloc_pages, unsigned long attrs), TP_ARGS(dev, virt_addr, dma_addr, size, dir, flags, attrs)); =20 +DEFINE_EVENT(_dma_alloc, dma_alloc_sgt_err, + TP_PROTO(struct device *dev, void *virt_addr, dma_addr_t dma_addr, + size_t size, enum dma_data_direction dir, gfp_t flags, + unsigned long attrs), + TP_ARGS(dev, virt_addr, dma_addr, size, dir, flags, attrs)); + TRACE_EVENT(dma_alloc_sgt, TP_PROTO(struct device *dev, struct sg_table *sgt, size_t size, enum dma_data_direction dir, gfp_t flags, unsigned long attrs), @@ -325,6 +331,41 @@ TRACE_EVENT(dma_map_sg, decode_dma_attrs(__entry->attrs)) ); =20 +TRACE_EVENT(dma_map_sg_err, + TP_PROTO(struct device *dev, struct scatterlist *sgl, int nents, + int err, enum dma_data_direction dir, unsigned long attrs), + TP_ARGS(dev, sgl, nents, err, dir, attrs), + + TP_STRUCT__entry( + __string(device, dev_name(dev)) + __dynamic_array(u64, phys_addrs, nents) + __field(int, err) + __field(enum dma_data_direction, dir) + __field(unsigned long, attrs) + ), + + TP_fast_assign( + struct scatterlist *sg; + int i; + + __assign_str(device); + for_each_sg(sgl, sg, nents, i) + ((u64 *)__get_dynamic_array(phys_addrs))[i] =3D sg_phys(sg); + __entry->err =3D err; + __entry->dir =3D dir; + __entry->attrs =3D attrs; + ), + + TP_printk("%s dir=3D%s dma_addrs=3D%s err=3D%d attrs=3D%s", + __get_str(device), + decode_dma_data_direction(__entry->dir), + __print_array(__get_dynamic_array(phys_addrs), + __get_dynamic_array_len(phys_addrs) / + sizeof(u64), sizeof(u64)), + __entry->err, + decode_dma_attrs(__entry->attrs)) +); + TRACE_EVENT(dma_unmap_sg, TP_PROTO(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir, unsigned long attrs), diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index b8a6bc492fae..636dbb0629a4 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -223,6 +223,7 @@ static int __dma_map_sg_attrs(struct device *dev, struc= t scatterlist *sg, debug_dma_map_sg(dev, sg, nents, ents, dir, attrs); } else if (WARN_ON_ONCE(ents !=3D -EINVAL && ents !=3D -ENOMEM && ents !=3D -EIO && ents !=3D -EREMOTEIO)) { + trace_dma_map_sg_err(dev, sg, nents, ents, dir, attrs); return -EIO; } =20 @@ -604,20 +605,26 @@ void *dma_alloc_attrs(struct device *dev, size_t size= , dma_addr_t *dma_handle, if (WARN_ON_ONCE(flag & __GFP_COMP)) return NULL; =20 - if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr)) + if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr)) { + trace_dma_alloc(dev, cpu_addr, *dma_handle, size, + DMA_BIDIRECTIONAL, flag, attrs); return cpu_addr; + } =20 /* let the implementation decide on the zone to allocate from: */ flag &=3D ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM); =20 - if (dma_alloc_direct(dev, ops)) + if (dma_alloc_direct(dev, ops)) { cpu_addr =3D dma_direct_alloc(dev, size, dma_handle, flag, attrs); - else if (use_dma_iommu(dev)) + } else if (use_dma_iommu(dev)) { cpu_addr =3D iommu_dma_alloc(dev, size, dma_handle, flag, attrs); - else if (ops->alloc) + } else if (ops->alloc) { cpu_addr =3D ops->alloc(dev, size, dma_handle, flag, attrs); - else + } else { + trace_dma_alloc(dev, NULL, 0, size, DMA_BIDIRECTIONAL, flag, + attrs); return NULL; + } =20 trace_dma_alloc(dev, cpu_addr, *dma_handle, size, DMA_BIDIRECTIONAL, flag, attrs); @@ -642,11 +649,11 @@ void dma_free_attrs(struct device *dev, size_t size, = void *cpu_addr, */ WARN_ON(irqs_disabled()); =20 - if (!cpu_addr) - return; - trace_dma_free(dev, cpu_addr, dma_handle, size, DMA_BIDIRECTIONAL, attrs); + if (!cpu_addr) + return; + debug_dma_free_coherent(dev, size, cpu_addr, dma_handle); if (dma_alloc_direct(dev, ops)) dma_direct_free(dev, size, cpu_addr, dma_handle, attrs); @@ -688,6 +695,8 @@ struct page *dma_alloc_pages(struct device *dev, size_t= size, trace_dma_alloc_pages(dev, page_to_virt(page), *dma_handle, size, dir, gfp, 0); debug_dma_map_page(dev, page, 0, size, dir, *dma_handle, 0); + } else { + trace_dma_alloc_pages(dev, NULL, 0, size, dir, gfp, 0); } return page; } @@ -772,6 +781,8 @@ struct sg_table *dma_alloc_noncontiguous(struct device = *dev, size_t size, sgt->nents =3D 1; trace_dma_alloc_sgt(dev, sgt, size, dir, gfp, attrs); debug_dma_map_sg(dev, sgt->sgl, sgt->orig_nents, 1, dir, attrs); + } else { + trace_dma_alloc_sgt_err(dev, NULL, 0, size, gfp, dir, attrs); } return sgt; } --=20 2.35.1.1320.gc452695387.dirty