linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/cma.c: remove redundant cma_mutex lock
@ 2020-10-20 10:22 Lecopzer Chen
  2020-10-20 11:27 ` David Hildenbrand
  0 siblings, 1 reply; 3+ messages in thread
From: Lecopzer Chen @ 2020-10-20 10:22 UTC (permalink / raw)
  To: linux-kernel, linux-mm; +Cc: matthias.bgg, akpm, yj.chiang, Lecopzer Chen

The cma_mutex which protects alloc_contig_range() was first appeared in
commit 7ee793a62fa8c ("cma: Remove potential deadlock situation"),
at that time, there is no guarantee the behavior of concurrency inside
alloc_contig_range().

After the commit 2c7452a075d4db2dc
("mm/page_isolation.c: make start_isolate_page_range() fail if already isolated")
  > However, two subsystems (CMA and gigantic
  > huge pages for example) could attempt operations on the same range.  If
  > this happens, one thread may 'undo' the work another thread is doing.
  > This can result in pageblocks being incorrectly left marked as
  > MIGRATE_ISOLATE and therefore not available for page allocation.
The concurrency inside alloc_contig_range() was clarified.

Now we can find that hugepage and virtio call alloc_contig_range() without
any lock, thus cma_mutex is "redundant" in cma_alloc() now.

Signed-off-by: Lecopzer Chen <lecopzer.chen@mediatek.com>
---
 mm/cma.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/mm/cma.c b/mm/cma.c
index 7f415d7cda9f..3692a34e2353 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -38,7 +38,6 @@
 
 struct cma cma_areas[MAX_CMA_AREAS];
 unsigned cma_area_count;
-static DEFINE_MUTEX(cma_mutex);
 
 phys_addr_t cma_get_base(const struct cma *cma)
 {
@@ -454,10 +453,9 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
 		mutex_unlock(&cma->lock);
 
 		pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
-		mutex_lock(&cma_mutex);
 		ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA,
 				     GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0));
-		mutex_unlock(&cma_mutex);
+
 		if (ret == 0) {
 			page = pfn_to_page(pfn);
 			break;
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/cma.c: remove redundant cma_mutex lock
  2020-10-20 10:22 [PATCH] mm/cma.c: remove redundant cma_mutex lock Lecopzer Chen
@ 2020-10-20 11:27 ` David Hildenbrand
  2020-10-23 11:29   ` Vlastimil Babka
  0 siblings, 1 reply; 3+ messages in thread
From: David Hildenbrand @ 2020-10-20 11:27 UTC (permalink / raw)
  To: Lecopzer Chen, linux-kernel, linux-mm; +Cc: matthias.bgg, akpm, yj.chiang

On 20.10.20 12:22, Lecopzer Chen wrote:
> The cma_mutex which protects alloc_contig_range() was first appeared in
> commit 7ee793a62fa8c ("cma: Remove potential deadlock situation"),
> at that time, there is no guarantee the behavior of concurrency inside
> alloc_contig_range().
> 
> After the commit 2c7452a075d4db2dc
> ("mm/page_isolation.c: make start_isolate_page_range() fail if already isolated")
>   > However, two subsystems (CMA and gigantic
>   > huge pages for example) could attempt operations on the same range.  If
>   > this happens, one thread may 'undo' the work another thread is doing.
>   > This can result in pageblocks being incorrectly left marked as
>   > MIGRATE_ISOLATE and therefore not available for page allocation.
> The concurrency inside alloc_contig_range() was clarified.
> 
> Now we can find that hugepage and virtio call alloc_contig_range() without
> any lock, thus cma_mutex is "redundant" in cma_alloc() now.
> 
> Signed-off-by: Lecopzer Chen <lecopzer.chen@mediatek.com>
> ---
>  mm/cma.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/mm/cma.c b/mm/cma.c
> index 7f415d7cda9f..3692a34e2353 100644
> --- a/mm/cma.c
> +++ b/mm/cma.c
> @@ -38,7 +38,6 @@
>  
>  struct cma cma_areas[MAX_CMA_AREAS];
>  unsigned cma_area_count;
> -static DEFINE_MUTEX(cma_mutex);
>  
>  phys_addr_t cma_get_base(const struct cma *cma)
>  {
> @@ -454,10 +453,9 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
>  		mutex_unlock(&cma->lock);
>  
>  		pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
> -		mutex_lock(&cma_mutex);
>  		ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA,
>  				     GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0));
> -		mutex_unlock(&cma_mutex);
> +
>  		if (ret == 0) {
>  			page = pfn_to_page(pfn);
>  			break;
> 

I guess this is fine. In case there is a race we return with -EBUSY -
which is suboptimal (as it could just be a temporary issue if the other
user backs off), but should be good enough for now.

Acked-by: David Hildenbrand <david@redhat.com>

-- 
Thanks,

David / dhildenb



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/cma.c: remove redundant cma_mutex lock
  2020-10-20 11:27 ` David Hildenbrand
@ 2020-10-23 11:29   ` Vlastimil Babka
  0 siblings, 0 replies; 3+ messages in thread
From: Vlastimil Babka @ 2020-10-23 11:29 UTC (permalink / raw)
  To: David Hildenbrand, Lecopzer Chen, linux-kernel, linux-mm
  Cc: matthias.bgg, akpm, yj.chiang

On 10/20/20 1:27 PM, David Hildenbrand wrote:
> On 20.10.20 12:22, Lecopzer Chen wrote:
>> The cma_mutex which protects alloc_contig_range() was first appeared in
>> commit 7ee793a62fa8c ("cma: Remove potential deadlock situation"),
>> at that time, there is no guarantee the behavior of concurrency inside
>> alloc_contig_range().
>> 
>> After the commit 2c7452a075d4db2dc
>> ("mm/page_isolation.c: make start_isolate_page_range() fail if already isolated")
>>   > However, two subsystems (CMA and gigantic
>>   > huge pages for example) could attempt operations on the same range.  If
>>   > this happens, one thread may 'undo' the work another thread is doing.
>>   > This can result in pageblocks being incorrectly left marked as
>>   > MIGRATE_ISOLATE and therefore not available for page allocation.
>> The concurrency inside alloc_contig_range() was clarified.
>> 
>> Now we can find that hugepage and virtio call alloc_contig_range() without
>> any lock, thus cma_mutex is "redundant" in cma_alloc() now.
>> 
>> Signed-off-by: Lecopzer Chen <lecopzer.chen@mediatek.com>
>> ---
>>  mm/cma.c | 4 +---
>>  1 file changed, 1 insertion(+), 3 deletions(-)
>> 
>> diff --git a/mm/cma.c b/mm/cma.c
>> index 7f415d7cda9f..3692a34e2353 100644
>> --- a/mm/cma.c
>> +++ b/mm/cma.c
>> @@ -38,7 +38,6 @@
>>  
>>  struct cma cma_areas[MAX_CMA_AREAS];
>>  unsigned cma_area_count;
>> -static DEFINE_MUTEX(cma_mutex);
>>  
>>  phys_addr_t cma_get_base(const struct cma *cma)
>>  {
>> @@ -454,10 +453,9 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
>>  		mutex_unlock(&cma->lock);
>>  
>>  		pfn = cma->base_pfn + (bitmap_no << cma->order_per_bit);
>> -		mutex_lock(&cma_mutex);
>>  		ret = alloc_contig_range(pfn, pfn + count, MIGRATE_CMA,
>>  				     GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0));
>> -		mutex_unlock(&cma_mutex);
>> +
>>  		if (ret == 0) {
>>  			page = pfn_to_page(pfn);
>>  			break;
>> 
> 
> I guess this is fine. In case there is a race we return with -EBUSY -
> which is suboptimal (as it could just be a temporary issue if the other
> user backs off), but should be good enough for now.

Agreed.

> Acked-by: David Hildenbrand <david@redhat.com>

Acked-by: Vlastimil Babka <vbabka@suse.cz>



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-10-23 11:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-20 10:22 [PATCH] mm/cma.c: remove redundant cma_mutex lock Lecopzer Chen
2020-10-20 11:27 ` David Hildenbrand
2020-10-23 11:29   ` Vlastimil Babka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).