From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752531AbcCGKsU (ORCPT ); Mon, 7 Mar 2016 05:48:20 -0500 Received: from szxga02-in.huawei.com ([119.145.14.65]:46393 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752169AbcCGKsP (ORCPT ); Mon, 7 Mar 2016 05:48:15 -0500 Subject: Re: [PATCH 1/1] dma-mapping: to avoid exception when cpu_addr is NULL To: Andrew Morton , Marek Szyprowski , Will Deacon , linux-kernel References: <1457342485-16628-1-git-send-email-thunder.leizhen@huawei.com> CC: Zefan Li , Xinwei Hu , "Tianhong Ding" , Hanjun Guo From: "Leizhen (ThunderTown)" Message-ID: <56DD5B63.9050609@huawei.com> Date: Mon, 7 Mar 2016 18:43:47 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: <1457342485-16628-1-git-send-email-thunder.leizhen@huawei.com> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.23.164] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.56DD5B79.00DF,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 1ef151569c40d2d2bdaa7f351d8f2bfe Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Suppose: CONFIG_SPARSEMEM is opened. CONFIG_DMA_API_DEBUG or CONFIG_CMA is opened. Then virt_to_page or phys_to_page will be called. Finally, in __pfn_to_page, __sec = __pfn_to_section(__pfn) is NULL. So access section->section_mem_map will trigger exception. --------- #define __pfn_to_page(pfn) \ ({ unsigned long __pfn = (pfn); \ struct mem_section *__sec = __pfn_to_section(__pfn); \ __section_mem_map_addr(__sec) + __pfn; \ }) static inline struct page *__section_mem_map_addr(struct mem_section *section) { unsigned long map = section->section_mem_map; map &= SECTION_MAP_MASK; return (struct page *)map; } On 2016/3/7 17:21, Zhen Lei wrote: > Do this to keep consistent with kfree, which tolerate ptr is NULL. > > Signed-off-by: Zhen Lei > --- > include/linux/dma-mapping.h | 5 ++++- > 1 file changed, 4 insertions(+), 1 deletion(-) > > diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h > index 75857cd..fdd4294 100644 > --- a/include/linux/dma-mapping.h > +++ b/include/linux/dma-mapping.h > @@ -402,7 +402,10 @@ static inline void *dma_alloc_coherent(struct device *dev, size_t size, > static inline void dma_free_coherent(struct device *dev, size_t size, > void *cpu_addr, dma_addr_t dma_handle) > { > - return dma_free_attrs(dev, size, cpu_addr, dma_handle, NULL); > + if (unlikely(!cpu_addr)) > + return; > + > + dma_free_attrs(dev, size, cpu_addr, dma_handle, NULL); > } > > static inline void *dma_alloc_noncoherent(struct device *dev, size_t size, > -- > 2.5.0 > > > > . >