linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [bug report] mm, compaction: capture a page under direct compaction
@ 2020-02-24  6:35 Dan Carpenter
  2020-02-26 17:04 ` Vlastimil Babka
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2020-02-24  6:35 UTC (permalink / raw)
  To: mgorman; +Cc: linux-mm

Hello Mel Gorman,

This is a semi-automatic email about new static checker warnings.

The patch 5e1f0f098b46: "mm, compaction: capture a page under direct 
compaction" from Mar 5, 2019, leads to the following Smatch complaint:

    mm/compaction.c:2321 compact_zone_order()
     error: we previously assumed 'capture' could be null (see line 2313)

mm/compaction.c
  2288  static enum compact_result compact_zone_order(struct zone *zone, int order,
  2289                  gfp_t gfp_mask, enum compact_priority prio,
  2290                  unsigned int alloc_flags, int classzone_idx,
  2291                  struct page **capture)
                                      ^^^^^^^
  2292  {
  2293          enum compact_result ret;
  2294          struct compact_control cc = {
  2295                  .order = order,
  2296                  .search_order = order,
  2297                  .gfp_mask = gfp_mask,
  2298                  .zone = zone,
  2299                  .mode = (prio == COMPACT_PRIO_ASYNC) ?
  2300                                          MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT,
  2301                  .alloc_flags = alloc_flags,
  2302                  .classzone_idx = classzone_idx,
  2303                  .direct_compaction = true,
  2304                  .whole_zone = (prio == MIN_COMPACT_PRIORITY),
  2305                  .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
  2306                  .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
  2307          };
  2308          struct capture_control capc = {
  2309                  .cc = &cc,
  2310                  .page = NULL,
  2311          };
  2312	
  2313		if (capture)
                    ^^^^^^^
Check for NULL

  2314			current->capture_control = &capc;
  2315	
  2316		ret = compact_zone(&cc, &capc);
  2317	
  2318		VM_BUG_ON(!list_empty(&cc.freepages));
  2319		VM_BUG_ON(!list_empty(&cc.migratepages));
  2320	
  2321		*capture = capc.page;
                ^^^^^^^^
Unchecked dereference.

  2322		current->capture_control = NULL;
  2323	

regards,
dan carpenter


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

* Re: [bug report] mm, compaction: capture a page under direct compaction
  2020-02-24  6:35 [bug report] mm, compaction: capture a page under direct compaction Dan Carpenter
@ 2020-02-26 17:04 ` Vlastimil Babka
  2020-02-27  1:56   ` Andrew Morton
  0 siblings, 1 reply; 5+ messages in thread
From: Vlastimil Babka @ 2020-02-26 17:04 UTC (permalink / raw)
  To: Dan Carpenter, Andrew Morton; +Cc: mgorman, linux-mm

On 2/24/20 7:35 AM, Dan Carpenter wrote:
> Hello Mel Gorman,
> 
> This is a semi-automatic email about new static checker warnings.
> 
> The patch 5e1f0f098b46: "mm, compaction: capture a page under direct 
> compaction" from Mar 5, 2019, leads to the following Smatch complaint:
> 
>     mm/compaction.c:2321 compact_zone_order()
>      error: we previously assumed 'capture' could be null (see line 2313)
> 

----8<----
From fb264bb52576a0582e8a92952cf91e8b61d105c3 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <vbabka@suse.cz>
Date: Wed, 26 Feb 2020 17:53:54 +0100
Subject: [PATCH] mm, compaction: fully assume capture can be NULL in
 compact_zone_order()

Dan reports:

The patch 5e1f0f098b46: "mm, compaction: capture a page under direct
compaction" from Mar 5, 2019, leads to the following Smatch complaint:

    mm/compaction.c:2321 compact_zone_order()
     error: we previously assumed 'capture' could be null (see line 2313)

mm/compaction.c
  2288  static enum compact_result compact_zone_order(struct zone *zone, int order,
  2289                  gfp_t gfp_mask, enum compact_priority prio,
  2290                  unsigned int alloc_flags, int classzone_idx,
  2291                  struct page **capture)
                                      ^^^^^^^

  2313		if (capture)
                    ^^^^^^^
Check for NULL

  2314			current->capture_control = &capc;
  2315
  2316		ret = compact_zone(&cc, &capc);
  2317
  2318		VM_BUG_ON(!list_empty(&cc.freepages));
  2319		VM_BUG_ON(!list_empty(&cc.migratepages));
  2320
  2321		*capture = capc.page;
                ^^^^^^^^
Unchecked dereference.

  2322		current->capture_control = NULL;
  2323

In practice this is not an issue, as the only caller path passes non-NULL
capture:

__alloc_pages_direct_compact()
  struct page *page = NULL;
  try_to_compact_pages(capture = &page);
    compact_zone_order(capture = capture);

Hence no stable tag needed, but let's make this more robust and remove the
warning by assuming capture might be NULL in the second dereference.

Fixes: 5e1f0f098b46 ("mm, compaction: capture a page under direct compaction")
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Mel Gorman <mgorman@techsingularity.net>
---
 mm/compaction.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git mm/compaction.c mm/compaction.c
index 672d3c78c6ab..f4bb8091cf4f 100644
--- mm/compaction.c
+++ mm/compaction.c
@@ -2318,8 +2318,10 @@ static enum compact_result compact_zone_order(struct zone *zone, int order,
 	VM_BUG_ON(!list_empty(&cc.freepages));
 	VM_BUG_ON(!list_empty(&cc.migratepages));
 
-	*capture = capc.page;
-	current->capture_control = NULL;
+	if (capture) {
+		*capture = capc.page;
+		current->capture_control = NULL;
+	}
 
 	return ret;
 }
-- 
2.25.1



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

* Re: [bug report] mm, compaction: capture a page under direct compaction
  2020-02-26 17:04 ` Vlastimil Babka
@ 2020-02-27  1:56   ` Andrew Morton
  2020-02-27  7:45     ` Vlastimil Babka
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2020-02-27  1:56 UTC (permalink / raw)
  To: Vlastimil Babka; +Cc: Dan Carpenter, mgorman, linux-mm

On Wed, 26 Feb 2020 18:04:56 +0100 Vlastimil Babka <vbabka@suse.cz> wrote:

> On 2/24/20 7:35 AM, Dan Carpenter wrote:
> > Hello Mel Gorman,
> > 
> > This is a semi-automatic email about new static checker warnings.
> > 
> > The patch 5e1f0f098b46: "mm, compaction: capture a page under direct 
> > compaction" from Mar 5, 2019, leads to the following Smatch complaint:
> > 
> >     mm/compaction.c:2321 compact_zone_order()
> >      error: we previously assumed 'capture' could be null (see line 2313)
> > 
> 
> ----8<----
> >From fb264bb52576a0582e8a92952cf91e8b61d105c3 Mon Sep 17 00:00:00 2001
> From: Vlastimil Babka <vbabka@suse.cz>
> Date: Wed, 26 Feb 2020 17:53:54 +0100
> Subject: [PATCH] mm, compaction: fully assume capture can be NULL in
>  compact_zone_order()
> 
> Dan reports:
> 
> The patch 5e1f0f098b46: "mm, compaction: capture a page under direct
> compaction" from Mar 5, 2019, leads to the following Smatch complaint:
> 
>     mm/compaction.c:2321 compact_zone_order()
>      error: we previously assumed 'capture' could be null (see line 2313)
> 
> mm/compaction.c
>   2288  static enum compact_result compact_zone_order(struct zone *zone, int order,
>   2289                  gfp_t gfp_mask, enum compact_priority prio,
>   2290                  unsigned int alloc_flags, int classzone_idx,
>   2291                  struct page **capture)
>                                       ^^^^^^^
> 
>   2313		if (capture)
>                     ^^^^^^^
> Check for NULL
> 
>   2314			current->capture_control = &capc;
>   2315
>   2316		ret = compact_zone(&cc, &capc);
>   2317
>   2318		VM_BUG_ON(!list_empty(&cc.freepages));
>   2319		VM_BUG_ON(!list_empty(&cc.migratepages));
>   2320
>   2321		*capture = capc.page;
>                 ^^^^^^^^
> Unchecked dereference.
> 
>   2322		current->capture_control = NULL;
>   2323
> 
> In practice this is not an issue, as the only caller path passes non-NULL
> capture:
> 
> __alloc_pages_direct_compact()
>   struct page *page = NULL;
>   try_to_compact_pages(capture = &page);
>     compact_zone_order(capture = capture);
> 
> Hence no stable tag needed, but let's make this more robust and remove the
> warning by assuming capture might be NULL in the second dereference.
> 
> ...
>
> --- mm/compaction.c
> +++ mm/compaction.c
> @@ -2318,8 +2318,10 @@ static enum compact_result compact_zone_order(struct zone *zone, int order,
>  	VM_BUG_ON(!list_empty(&cc.freepages));
>  	VM_BUG_ON(!list_empty(&cc.migratepages));
>  
> -	*capture = capc.page;
> -	current->capture_control = NULL;
> +	if (capture) {
> +		*capture = capc.page;
> +		current->capture_control = NULL;
> +	}
>  
>  	return ret;
>  }

Why don't we instead remove the first check for NULL?  Remove some dead
code instead of adding some?


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

* Re: [bug report] mm, compaction: capture a page under direct compaction
  2020-02-27  1:56   ` Andrew Morton
@ 2020-02-27  7:45     ` Vlastimil Babka
  2020-02-28 13:47       ` Mel Gorman
  0 siblings, 1 reply; 5+ messages in thread
From: Vlastimil Babka @ 2020-02-27  7:45 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Dan Carpenter, mgorman, linux-mm

On 2/27/20 2:56 AM, Andrew Morton wrote:
> On Wed, 26 Feb 2020 18:04:56 +0100 Vlastimil Babka <vbabka@suse.cz> wrote:
> 
>> On 2/24/20 7:35 AM, Dan Carpenter wrote:
>> > Hello Mel Gorman,
>> > 
>> > This is a semi-automatic email about new static checker warnings.
>> > 
>> > The patch 5e1f0f098b46: "mm, compaction: capture a page under direct 
>> > compaction" from Mar 5, 2019, leads to the following Smatch complaint:
>> > 
>> >     mm/compaction.c:2321 compact_zone_order()
>> >      error: we previously assumed 'capture' could be null (see line 2313)
>> > 
>> 
>> ----8<----
>> >From fb264bb52576a0582e8a92952cf91e8b61d105c3 Mon Sep 17 00:00:00 2001
>> From: Vlastimil Babka <vbabka@suse.cz>
>> Date: Wed, 26 Feb 2020 17:53:54 +0100
>> Subject: [PATCH] mm, compaction: fully assume capture can be NULL in
>>  compact_zone_order()
>> 
> 
> Why don't we instead remove the first check for NULL?  Remove some dead
> code instead of adding some?
> 

Sure, works too. Anyone creating a new caller and trying to pass NULL will
crash quickly.

----8<----
From 3118d9bea8a0fcef294915220f824674fb039ec3 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <vbabka@suse.cz>
Date: Wed, 26 Feb 2020 17:53:54 +0100
Subject: [PATCH] mm, compaction: fully assume capture is not NULL in
 compact_zone_order()

Dan reports:

The patch 5e1f0f098b46: "mm, compaction: capture a page under direct
compaction" from Mar 5, 2019, leads to the following Smatch complaint:

    mm/compaction.c:2321 compact_zone_order()
     error: we previously assumed 'capture' could be null (see line 2313)

mm/compaction.c
  2288  static enum compact_result compact_zone_order(struct zone *zone, int order,
  2289                  gfp_t gfp_mask, enum compact_priority prio,
  2290                  unsigned int alloc_flags, int classzone_idx,
  2291                  struct page **capture)
                                      ^^^^^^^

  2313		if (capture)
                    ^^^^^^^
Check for NULL

  2314			current->capture_control = &capc;
  2315
  2316		ret = compact_zone(&cc, &capc);
  2317
  2318		VM_BUG_ON(!list_empty(&cc.freepages));
  2319		VM_BUG_ON(!list_empty(&cc.migratepages));
  2320
  2321		*capture = capc.page;
                ^^^^^^^^
Unchecked dereference.

  2322		current->capture_control = NULL;
  2323

In practice this is not an issue, as the only caller path passes non-NULL
capture:

__alloc_pages_direct_compact()
  struct page *page = NULL;
  try_to_compact_pages(capture = &page);
    compact_zone_order(capture = capture);

So let's remove the unnecessary check, which should also make Smatch happy.

Fixes: 5e1f0f098b46 ("mm, compaction: capture a page under direct compaction")
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Mel Gorman <mgorman@techsingularity.net>
---
 mm/compaction.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git mm/compaction.c mm/compaction.c
index 672d3c78c6ab..35c991fbf983 100644
--- mm/compaction.c
+++ mm/compaction.c
@@ -2310,8 +2310,7 @@ static enum compact_result compact_zone_order(struct zone *zone, int order,
 		.page = NULL,
 	};
 
-	if (capture)
-		current->capture_control = &capc;
+	current->capture_control = &capc;
 
 	ret = compact_zone(&cc, &capc);
 
@@ -2333,6 +2332,7 @@ int sysctl_extfrag_threshold = 500;
  * @alloc_flags: The allocation flags of the current allocation
  * @ac: The context of current allocation
  * @prio: Determines how hard direct compaction should try to succeed
+ * @capture: Pointer to free page created by compaction will be stored here
  *
  * This is the main entry point for direct page compaction.
  */
-- 
2.25.1



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

* Re: [bug report] mm, compaction: capture a page under direct compaction
  2020-02-27  7:45     ` Vlastimil Babka
@ 2020-02-28 13:47       ` Mel Gorman
  0 siblings, 0 replies; 5+ messages in thread
From: Mel Gorman @ 2020-02-28 13:47 UTC (permalink / raw)
  To: Vlastimil Babka; +Cc: Andrew Morton, Dan Carpenter, linux-mm

On Thu, Feb 27, 2020 at 08:45:46AM +0100, Vlastimil Babka wrote:

Bit late to the game but;

> ----8<----
> From 3118d9bea8a0fcef294915220f824674fb039ec3 Mon Sep 17 00:00:00 2001
> From: Vlastimil Babka <vbabka@suse.cz>
> Date: Wed, 26 Feb 2020 17:53:54 +0100
> Subject: [PATCH] mm, compaction: fully assume capture is not NULL in
>  compact_zone_order()
> 
> Dan reports:
> 
> The patch 5e1f0f098b46: "mm, compaction: capture a page under direct
> compaction" from Mar 5, 2019, leads to the following Smatch complaint:
> 
>     mm/compaction.c:2321 compact_zone_order()
>      error: we previously assumed 'capture' could be null (see line 2313)
> 
> mm/compaction.c
>   2288  static enum compact_result compact_zone_order(struct zone *zone, int order,
>   2289                  gfp_t gfp_mask, enum compact_priority prio,
>   2290                  unsigned int alloc_flags, int classzone_idx,
>   2291                  struct page **capture)
>                                       ^^^^^^^
> 
>   2313		if (capture)
>                     ^^^^^^^
> Check for NULL
> 
>   2314			current->capture_control = &capc;
>   2315
>   2316		ret = compact_zone(&cc, &capc);
>   2317
>   2318		VM_BUG_ON(!list_empty(&cc.freepages));
>   2319		VM_BUG_ON(!list_empty(&cc.migratepages));
>   2320
>   2321		*capture = capc.page;
>                 ^^^^^^^^
> Unchecked dereference.
> 
>   2322		current->capture_control = NULL;
>   2323
> 
> In practice this is not an issue, as the only caller path passes non-NULL
> capture:
> 
> __alloc_pages_direct_compact()
>   struct page *page = NULL;
>   try_to_compact_pages(capture = &page);
>     compact_zone_order(capture = capture);
> 
> So let's remove the unnecessary check, which should also make Smatch happy.
> 
> Fixes: 5e1f0f098b46 ("mm, compaction: capture a page under direct compaction")
> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> Suggested-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> Cc: Mel Gorman <mgorman@techsingularity.net>

Acked-by: Mel Gorman <mgorman@techsingularity.net>

-- 
Mel Gorman
SUSE Labs


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

end of thread, other threads:[~2020-02-28 13:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-24  6:35 [bug report] mm, compaction: capture a page under direct compaction Dan Carpenter
2020-02-26 17:04 ` Vlastimil Babka
2020-02-27  1:56   ` Andrew Morton
2020-02-27  7:45     ` Vlastimil Babka
2020-02-28 13:47       ` Mel Gorman

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).