diff mbox series

[PATCH-next] mm/memcontrol.c: Fix potential uninitialized variable warning

Message ID 20210526193602.8742-1-longman@redhat.com (mailing list archive)
State New, archived
Headers show
Series [PATCH-next] mm/memcontrol.c: Fix potential uninitialized variable warning | expand

Commit Message

Waiman Long May 26, 2021, 7:36 p.m. UTC
If the -Wno-maybe-uninitialized gcc option is not specified, compilation
of memcontrol.c may generate the following warnings:

mm/memcontrol.c: In function ‘refill_obj_stock’:
./arch/x86/include/asm/irqflags.h:127:17: warning: ‘flags’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  return !(flags & X86_EFLAGS_IF);
          ~~~~~~~^~~~~~~~~~~~~~~~
mm/memcontrol.c:3216:16: note: ‘flags’ was declared here
  unsigned long flags;
                ^~~~~
In file included from mm/memcontrol.c:29:
mm/memcontrol.c: In function ‘uncharge_page’:
./include/linux/memcontrol.h:797:2: warning: ‘objcg’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  percpu_ref_put(&objcg->refcnt);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix that by properly initializing *pflags in get_obj_stock() and
introducing a use_objcg bool variable in uncharge_page() to avoid
potentially accessing the struct page data twice.

Signed-off-by: Waiman Long <longman@redhat.com>
---
 mm/memcontrol.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Andrew Morton May 26, 2021, 8:43 p.m. UTC | #1
On Wed, 26 May 2021 15:36:02 -0400 Waiman Long <longman@redhat.com> wrote:

> If the -Wno-maybe-uninitialized gcc option is not specified, compilation
> of memcontrol.c may generate the following warnings:
> 
> mm/memcontrol.c: In function ‘refill_obj_stock’:
> ./arch/x86/include/asm/irqflags.h:127:17: warning: ‘flags’ may be used uninitialized in this function [-Wmaybe-uninitialized]
>   return !(flags & X86_EFLAGS_IF);
>           ~~~~~~~^~~~~~~~~~~~~~~~
> mm/memcontrol.c:3216:16: note: ‘flags’ was declared here
>   unsigned long flags;
>                 ^~~~~
> In file included from mm/memcontrol.c:29:
> mm/memcontrol.c: In function ‘uncharge_page’:
> ./include/linux/memcontrol.h:797:2: warning: ‘objcg’ may be used uninitialized in this function [-Wmaybe-uninitialized]
>   percpu_ref_put(&objcg->refcnt);
>   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Fix that by properly initializing *pflags in get_obj_stock() and
> introducing a use_objcg bool variable in uncharge_page() to avoid
> potentially accessing the struct page data twice.
> 

Thanks.  I'll queue this as a fix against your "mm/memcg: optimize user
context object stock access".
Waiman Long May 26, 2021, 10:29 p.m. UTC | #2
On 5/26/21 4:43 PM, Andrew Morton wrote:
> On Wed, 26 May 2021 15:36:02 -0400 Waiman Long <longman@redhat.com> wrote:
>
>> If the -Wno-maybe-uninitialized gcc option is not specified, compilation
>> of memcontrol.c may generate the following warnings:
>>
>> mm/memcontrol.c: In function ‘refill_obj_stock’:
>> ./arch/x86/include/asm/irqflags.h:127:17: warning: ‘flags’ may be used uninitialized in this function [-Wmaybe-uninitialized]
>>    return !(flags & X86_EFLAGS_IF);
>>            ~~~~~~~^~~~~~~~~~~~~~~~
>> mm/memcontrol.c:3216:16: note: ‘flags’ was declared here
>>    unsigned long flags;
>>                  ^~~~~
>> In file included from mm/memcontrol.c:29:
>> mm/memcontrol.c: In function ‘uncharge_page’:
>> ./include/linux/memcontrol.h:797:2: warning: ‘objcg’ may be used uninitialized in this function [-Wmaybe-uninitialized]
>>    percpu_ref_put(&objcg->refcnt);
>>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> Fix that by properly initializing *pflags in get_obj_stock() and
>> introducing a use_objcg bool variable in uncharge_page() to avoid
>> potentially accessing the struct page data twice.
>>
> Thanks.  I'll queue this as a fix against your "mm/memcg: optimize user
> context object stock access".
>
Thanks for that.

Cheers,
Longman
diff mbox series

Patch

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index cb864f87b01d..b9a6db6a7d4f 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2108,6 +2108,7 @@  static inline struct obj_stock *get_obj_stock(unsigned long *pflags)
 	struct memcg_stock_pcp *stock;
 
 	if (likely(in_task())) {
+		*pflags = 0UL;
 		preempt_disable();
 		stock = this_cpu_ptr(&memcg_stock);
 		return &stock->task_obj;
@@ -6840,6 +6841,7 @@  static void uncharge_page(struct page *page, struct uncharge_gather *ug)
 	unsigned long nr_pages;
 	struct mem_cgroup *memcg;
 	struct obj_cgroup *objcg;
+	bool use_objcg = PageMemcgKmem(page);
 
 	VM_BUG_ON_PAGE(PageLRU(page), page);
 
@@ -6848,7 +6850,7 @@  static void uncharge_page(struct page *page, struct uncharge_gather *ug)
 	 * page memcg or objcg at this point, we have fully
 	 * exclusive access to the page.
 	 */
-	if (PageMemcgKmem(page)) {
+	if (use_objcg) {
 		objcg = __page_objcg(page);
 		/*
 		 * This get matches the put at the end of the function and
@@ -6876,7 +6878,7 @@  static void uncharge_page(struct page *page, struct uncharge_gather *ug)
 
 	nr_pages = compound_nr(page);
 
-	if (PageMemcgKmem(page)) {
+	if (use_objcg) {
 		ug->nr_memory += nr_pages;
 		ug->nr_kmem += nr_pages;