2015-12-08 10:19:08

by Qais Yousef

[permalink] [raw]
Subject: [PATCH] MIPS: Fix DMA contiguous allocation

Recent changes to how GFP_ATOMIC is defined seems to have broken the condition
to use mips_alloc_from_contiguous() in mips_dma_alloc_coherent().

I couldn't bottom out the exact change but I think it's this one

d0164adc89f6 (mm, page_alloc: distinguish between being unable to sleep,
unwilling to sleep and avoiding waking kswapd)

>From what I see GFP_ATOMIC has multiple bits set and the check for !(gfp
& GFP_ATOMIC) isn't enough. To verify if the flag is atomic we need to make
sure that (gfp & GFP_ATOMIC) == GFP_ATOMIC to verify that all bits rquired to
satisfy GFP_ATOMIC condition are set.

Signed-off-by: Qais Yousef <[email protected]>
---
arch/mips/mm/dma-default.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/mips/mm/dma-default.c b/arch/mips/mm/dma-default.c
index d8117be729a2..d6b8a1445a3a 100644
--- a/arch/mips/mm/dma-default.c
+++ b/arch/mips/mm/dma-default.c
@@ -145,7 +145,7 @@ static void *mips_dma_alloc_coherent(struct device *dev, size_t size,

gfp = massage_gfp_flags(dev, gfp);

- if (IS_ENABLED(CONFIG_DMA_CMA) && !(gfp & GFP_ATOMIC))
+ if (IS_ENABLED(CONFIG_DMA_CMA) && ((gfp & GFP_ATOMIC) != GFP_ATOMIC))
page = dma_alloc_from_contiguous(dev,
count, get_order(size));
if (!page)
--
2.1.0


2015-12-08 12:12:53

by Sergei Shtylyov

[permalink] [raw]
Subject: Re: [PATCH] MIPS: Fix DMA contiguous allocation

On 12/8/2015 1:18 PM, Qais Yousef wrote:

> Recent changes to how GFP_ATOMIC is defined seems to have broken the condition
> to use mips_alloc_from_contiguous() in mips_dma_alloc_coherent().
>
> I couldn't bottom out the exact change but I think it's this one
>
> d0164adc89f6 (mm, page_alloc: distinguish between being unable to sleep,
> unwilling to sleep and avoiding waking kswapd)
>
> From what I see GFP_ATOMIC has multiple bits set and the check for !(gfp
> & GFP_ATOMIC) isn't enough. To verify if the flag is atomic we need to make
> sure that (gfp & GFP_ATOMIC) == GFP_ATOMIC to verify that all bits rquired to

Required.

> satisfy GFP_ATOMIC condition are set.
>
> Signed-off-by: Qais Yousef <[email protected]>
> ---
> arch/mips/mm/dma-default.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/mips/mm/dma-default.c b/arch/mips/mm/dma-default.c
> index d8117be729a2..d6b8a1445a3a 100644
> --- a/arch/mips/mm/dma-default.c
> +++ b/arch/mips/mm/dma-default.c
> @@ -145,7 +145,7 @@ static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
>
> gfp = massage_gfp_flags(dev, gfp);
>
> - if (IS_ENABLED(CONFIG_DMA_CMA) && !(gfp & GFP_ATOMIC))
> + if (IS_ENABLED(CONFIG_DMA_CMA) && ((gfp & GFP_ATOMIC) != GFP_ATOMIC))

() around != not necessary.

[...]

MBR, Sergei

2015-12-08 22:19:41

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] MIPS: Fix DMA contiguous allocation

On Tue, 8 Dec 2015 10:18:50 +0000 Qais Yousef <[email protected]> wrote:

> Recent changes to how GFP_ATOMIC is defined seems to have broken the condition
> to use mips_alloc_from_contiguous() in mips_dma_alloc_coherent().
>
> I couldn't bottom out the exact change but I think it's this one
>
> d0164adc89f6 (mm, page_alloc: distinguish between being unable to sleep,
> unwilling to sleep and avoiding waking kswapd)
>
> >From what I see GFP_ATOMIC has multiple bits set and the check for !(gfp
> & GFP_ATOMIC) isn't enough. To verify if the flag is atomic we need to make
> sure that (gfp & GFP_ATOMIC) == GFP_ATOMIC to verify that all bits rquired to
> satisfy GFP_ATOMIC condition are set.
>
> ...
>
> --- a/arch/mips/mm/dma-default.c
> +++ b/arch/mips/mm/dma-default.c
> @@ -145,7 +145,7 @@ static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
>
> gfp = massage_gfp_flags(dev, gfp);
>
> - if (IS_ENABLED(CONFIG_DMA_CMA) && !(gfp & GFP_ATOMIC))
> + if (IS_ENABLED(CONFIG_DMA_CMA) && ((gfp & GFP_ATOMIC) != GFP_ATOMIC))
> page = dma_alloc_from_contiguous(dev,
> count, get_order(size));
> if (!page)

hm. It seems that the code is asking "can I do a potentially-sleeping
memory allocation"?

The way to do that under the new regime is

if (IS_ENABLED(CONFIG_DMA_CMA) && gfpflags_allow_blocking(gfp))

Mel, can you please confirm?

2015-12-09 11:36:45

by Mel Gorman

[permalink] [raw]
Subject: Re: [PATCH] MIPS: Fix DMA contiguous allocation

On Tue, Dec 08, 2015 at 02:19:39PM -0800, Andrew Morton wrote:
> On Tue, 8 Dec 2015 10:18:50 +0000 Qais Yousef <[email protected]> wrote:
>
> > Recent changes to how GFP_ATOMIC is defined seems to have broken the condition
> > to use mips_alloc_from_contiguous() in mips_dma_alloc_coherent().
> >
> > I couldn't bottom out the exact change but I think it's this one
> >
> > d0164adc89f6 (mm, page_alloc: distinguish between being unable to sleep,
> > unwilling to sleep and avoiding waking kswapd)
> >
> > >From what I see GFP_ATOMIC has multiple bits set and the check for !(gfp
> > & GFP_ATOMIC) isn't enough. To verify if the flag is atomic we need to make
> > sure that (gfp & GFP_ATOMIC) == GFP_ATOMIC to verify that all bits rquired to
> > satisfy GFP_ATOMIC condition are set.
> >
> > ...
> >
> > --- a/arch/mips/mm/dma-default.c
> > +++ b/arch/mips/mm/dma-default.c
> > @@ -145,7 +145,7 @@ static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
> >
> > gfp = massage_gfp_flags(dev, gfp);
> >
> > - if (IS_ENABLED(CONFIG_DMA_CMA) && !(gfp & GFP_ATOMIC))
> > + if (IS_ENABLED(CONFIG_DMA_CMA) && ((gfp & GFP_ATOMIC) != GFP_ATOMIC))
> > page = dma_alloc_from_contiguous(dev,
> > count, get_order(size));
> > if (!page)
>
> hm. It seems that the code is asking "can I do a potentially-sleeping
> memory allocation"?
>
> The way to do that under the new regime is
>
> if (IS_ENABLED(CONFIG_DMA_CMA) && gfpflags_allow_blocking(gfp))
>
> Mel, can you please confirm?

Yes, this is the correct way it should be checked. The full flags cover
watermark and kswapd treatment which potentially could be altered by
the caller.

--
Mel Gorman
SUSE Labs

2015-12-09 14:32:50

by Qais Yousef

[permalink] [raw]
Subject: Re: [PATCH] MIPS: Fix DMA contiguous allocation

On 12/09/2015 11:36 AM, Mel Gorman wrote:
> On Tue, Dec 08, 2015 at 02:19:39PM -0800, Andrew Morton wrote:
>> On Tue, 8 Dec 2015 10:18:50 +0000 Qais Yousef <[email protected]> wrote:
>>
>>> --- a/arch/mips/mm/dma-default.c
>>> +++ b/arch/mips/mm/dma-default.c
>>> @@ -145,7 +145,7 @@ static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
>>>
>>> gfp = massage_gfp_flags(dev, gfp);
>>>
>>> - if (IS_ENABLED(CONFIG_DMA_CMA) && !(gfp & GFP_ATOMIC))
>>> + if (IS_ENABLED(CONFIG_DMA_CMA) && ((gfp & GFP_ATOMIC) != GFP_ATOMIC))
>>> page = dma_alloc_from_contiguous(dev,
>>> count, get_order(size));
>>> if (!page)
>> hm. It seems that the code is asking "can I do a potentially-sleeping
>> memory allocation"?
>>
>> The way to do that under the new regime is
>>
>> if (IS_ENABLED(CONFIG_DMA_CMA) && gfpflags_allow_blocking(gfp))
>>
>> Mel, can you please confirm?
> Yes, this is the correct way it should be checked. The full flags cover
> watermark and kswapd treatment which potentially could be altered by
> the caller.
>

OK thanks both. I'll send a revised version with this change.

Thanks,
Qais