Subject: Re: [PATCH kernel v10 05/34] powerpc/iommu: Always release iommu_table in iommu_free_table()

From: Alex Williamson <[email-masked]>
Date: Wed, 13 May 2015 20:53:04 -0600

On Thu, 2015-05-14 at 12:34 +1000, Alexey Kardashevskiy wrote:
> On 05/14/2015 09:27 AM, Gavin Shan wrote:
> > On Wed, May 13, 2015 at 02:51:36PM +0200, Thomas Huth wrote:
> >> On Wed, 13 May 2015 16:30:16 +1000
> >> Alexey Kardashevskiy <[email-masked]> wrote:
> >>
> >>> On 05/13/2015 03:33 PM, Gavin Shan wrote:
> >>>> On Tue, May 12, 2015 at 01:38:54AM +1000, Alexey Kardashevskiy wrote:
> >>>>> At the moment iommu_free_table() only releases memory if
> >>>>> the table was initialized for the platform code use, i.e. it had
> >>>>> it_map initialized (which purpose is to track DMA memory space use).
> >>>>>
> >>>>> With dynamic DMA windows, we will need to be able to release
> >>>>> iommu_table even if it was used for VFIO in which case it_map is NULL
> >>>>> so does the patch.
> >>>>>
> >>>>> Signed-off-by: Alexey Kardashevskiy <[email-masked]>
> >>>>
> >>>> Reviewed-by: Gavin Shan <[email-masked]>
> >>>>
> >>>>> ---
> >>>>> arch/powerpc/kernel/iommu.c | 3 +--
> >>>>> 1 file changed, 1 insertion(+), 2 deletions(-)
> >>>>>
> >>>>> diff --git a/arch/powerpc/kernel/iommu.c b/arch/powerpc/kernel/iommu.c
> >>>>> index 3d47eb3..2c02d4c 100644
> >>>>> --- a/arch/powerpc/kernel/iommu.c
> >>>>> +++ b/arch/powerpc/kernel/iommu.c
> >>>>> @@ -714,8 +714,7 @@ void iommu_free_table(struct iommu_table *tbl, const char *node_name)
> >>>>> 	unsigned int order;
> >>>>>
> >>>>> 	if (!tbl || !tbl->it_map) {
> >>>>> -		printk(KERN_ERR "%s: expected TCE map for %s\n", __func__,
> >>>>> -				node_name);
> >>>>> +		kfree(tbl);
> >>>>
> >>>> I'm not sure if the "tbl" needs to be checked against NULL as kfree() already
> >>>> has the check. But it looks a bit strange to free NULL "tbl" from the code
> >>>> itself.
> >>>
> >>> Yeah, looks a bit weird, agree, I'll change but in general kfree/vfree/...
> >>> - they all check the passed pointer for NULL.
> >>
> >> But if tbl is NULL, the tbl->it_map check will fail, won't it? So in
> >> this case, I think you have to keep it.
> >>
> >
> > If I understood your question correctly, "tbl->it_map" won't be checked
> > when "tbl" is NULL because the connection ("||") for the two conditions.
> > The code can be changed to something like below if Alexey want:
> >
> > 	if (!tbl)
> > 		return;
> > 	if (!tbl->itmap)
> > 		kfree(tbl);
> 
> To be precise ;)
> 
> if (!tbl->itmap) {
> 	kfree(tbl);
> 	return;
> }

I hope that's not your solution, it clearly segfaults with a null
pointer de-ref if !tbl, which is apparently a concern down this path.