From patchwork Sat Mar 16 01:58:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13594146 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A4A2C54E58 for ; Sat, 16 Mar 2024 01:58:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D0C8980154; Fri, 15 Mar 2024 21:58:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CBCAE800B4; Fri, 15 Mar 2024 21:58:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BAB6080154; Fri, 15 Mar 2024 21:58:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A9C6F800B4 for ; Fri, 15 Mar 2024 21:58:08 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 65F9C1C0880 for ; Sat, 16 Mar 2024 01:58:08 +0000 (UTC) X-FDA: 81901241856.11.F23A0A2 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf01.hostedemail.com (Postfix) with ESMTP id A1FED40005 for ; Sat, 16 Mar 2024 01:58:06 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=SI3JepaC; spf=pass (imf01.hostedemail.com: domain of 3rfz0ZQoKCLAoeihoQXcUTWeeWbU.SecbYdkn-ccalQSa.ehW@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3rfz0ZQoKCLAoeihoQXcUTWeeWbU.SecbYdkn-ccalQSa.ehW@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710554286; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=nz/rmcCjj7pCrgBiVdioT3UkYrnHawa2JdhbUXSYaXE=; b=zwHs8d64J4mXuYTwTxH7aA9h93ewhI0L5eHT0Pw/cMUpTOH+Y28iN/6o1XeWDhbwEP+0dr /xEYq7emkrspxhVJCQyhhQ+WOqv2+tLhreDZDeKc+Sd8zvOQ92UJiJMF1M5G7UJLIXzBTT qM+1BZ5HCxRp20T9g0rmUb0sEYopaJA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710554286; a=rsa-sha256; cv=none; b=eGAPsW0V0nAbSilpXsgIFwfn7JBvjDY+cQU8J1o/rCqJalFsNAlBXu/wi8xPm/5A+m8D+T xS6dVlmoZSVWhiLDTRDRBrkoPooofgzgzYGT9qSdSHY2RqdAp9ZD+bq8Kxbw/XJXpz3nrg Ynusem/C84fYAfAaAnJLeS8QAnUTUW0= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=SI3JepaC; spf=pass (imf01.hostedemail.com: domain of 3rfz0ZQoKCLAoeihoQXcUTWeeWbU.SecbYdkn-ccalQSa.ehW@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3rfz0ZQoKCLAoeihoQXcUTWeeWbU.SecbYdkn-ccalQSa.ehW@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60a2386e932so55279977b3.1 for ; Fri, 15 Mar 2024 18:58:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710554286; x=1711159086; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=nz/rmcCjj7pCrgBiVdioT3UkYrnHawa2JdhbUXSYaXE=; b=SI3JepaClq26nXM8aOEtz8IuL2EjFnAcXf6rsLixroe7rZB+oGnnYEpsCyltMfESgQ Ik2ZY+0vAopQA5Gpkoa/cFGER+U9Qa0/Th9jAhgaVnHpyLhnvLtJiWFfDAU/kpaFwM0w 3n6e7eU9r40JjSNmWrjnQAs5sI4F/+S9OU5JomgHnl8co5V6kMTApIJ13avZy6jiphD6 7osA2ViWZAeKCEIc5sfNRT9VTUtbxQ+v5VxjrHqbx8x6qljh6/vTube9saVSwcnl0mcT PCEwGdmHsil9X8Tz0zJTtYWxYHVIQImJTprNAgHB4srwbgvVPf7lh8q7Al9ozzblxcKw rfug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710554286; x=1711159086; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=nz/rmcCjj7pCrgBiVdioT3UkYrnHawa2JdhbUXSYaXE=; b=JLROeQy22m6dCS5qrCLoIOpeDFx4+KHj2V9WQkryUw8AnvWnBMEulqe0jO+E27ZEM5 cc0lC6nbpYXN8vf3Jq3WXQKb6SEPj4xh88ciNxpWSsUjOpcRs0oOfMfkW5lGB5N7bmnF Av7wvdTsC9/5eEpDuTWqzHVola3SLHasXbpokfPVGopcF6yfo2Qx1Or78Bayqv1ktxo/ 8FSbfNums88C0tH9dC2Ga+RV5w8s5RiRqH5eosyX/xcq1EuyxKtRamlat6VMBIeVvAWv 3XGkG48u/c3FRnMGwP93We01fWeYB5vJLPHJfC2agFBNyTd8Y3tO6sHAFDC2G9PRoecz LlUw== X-Forwarded-Encrypted: i=1; AJvYcCX4yr3Tp34UVQ36k2SM5/qOfGUy7RjgD7tlxZ2Jan+TSNprHhGSDfun/2hOYhDEIG1g47ItAW6kcm4CV29yjyr2rVU= X-Gm-Message-State: AOJu0YyAGjzxyBEL1fdNmyjQ8bb7Egn/BYUul9drEdK3WyJm3GwQ4IqH 26vWCMWM19sHQZw81QSDGike9iQ77LpkYRZVn8oI2JBLorzR703FL2gLN11Bj7t9Enu82wu4E0n JerOR5Q1eB3Nm2ZjkDg== X-Google-Smtp-Source: AGHT+IGmRUBmhZYJ4tRjAVcL7DY6ga9tWCPr/y80MXCGP3YTBlk6hr1HiyCUa9cZ20xmmsx/xGgByCG91hqdia4A X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a0d:e813:0:b0:609:25d1:ea9a with SMTP id r19-20020a0de813000000b0060925d1ea9amr1600283ywe.9.1710554285742; Fri, 15 Mar 2024 18:58:05 -0700 (PDT) Date: Sat, 16 Mar 2024 01:58:03 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240316015803.2777252-1-yosryahmed@google.com> Subject: [PATCH] mm: memcg: add NULL check to obj_cgroup_put() From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Stat-Signature: jk99yraa5ri3uyi1ngj3rud5ubacypaw X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: A1FED40005 X-Rspam-User: X-HE-Tag: 1710554286-271951 X-HE-Meta: U2FsdGVkX19YjnYwcuETiafzCkwO71VBhD8ho0WUL37pAeO4uEjTspJS1g3esDGx3Bwfrqz5v1TN38UNxmRtYAHe2O1pbRqhqQuTftVTIkjM2vlcSvacUbs099F2A4ptN0/HqriJ42FAmD/8RSL2VjfD6QP/j8nc/Le98Q0R/PhYBHeyax9MnpjBnplcL+tflloZcuPjoG/iLtwKh29KkZktSKfu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5McAfngryxnPQl8eUDhgTIy32lknSSmxBc8X/Zrr4/ZUFT0RYHxAXJ+B7ZqwaeWazC0xf9aeJtBmICtCTqa/nYv4Lb6LIRhVVrEGo65mVAU2PHOrH7K/GaQUf7/cLW2uWymNJhvG9N3rWFh1woBGkN5zenuKSWvNCBHV1+yLEAMQOHTq7YTpG2x5NmihnBlas1FBpn5nssIhE2Wh18Dn8UVWb8CueIrStF2y/AWqaDmnsuCMsCrsiN5o/B0hZGorBsZ/0is5Heolngz5/TYUgvPU5SmAMHvyo30Sa/QnP5JQSR9nIBzMG8fEtPro4CyX5zw6Ft21KcE3xvW1gqudpCNwzOQnUxHwEpbJCrfWQDCkzax+ZQnGzl33F88wvdyZ5aA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 9 out of 16 callers perform a NULL check before calling obj_cgroup_put(). Move the NULL check in the function, similar to mem_cgroup_put(). The unlikely() NULL check in current_objcg_update() was left alone to avoid dropping the unlikey() annotation as this a fast path. Signed-off-by: Yosry Ahmed Acked-by: Johannes Weiner Acked-by: Roman Gushchin --- include/linux/memcontrol.h | 3 ++- kernel/bpf/memalloc.c | 6 ++---- mm/memcontrol.c | 18 ++++++------------ mm/zswap.c | 3 +-- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 394fd0a887ae7..b6264796815d4 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -818,7 +818,8 @@ static inline void obj_cgroup_get_many(struct obj_cgroup *objcg, static inline void obj_cgroup_put(struct obj_cgroup *objcg) { - percpu_ref_put(&objcg->refcnt); + if (objcg) + percpu_ref_put(&objcg->refcnt); } static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg) diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index 550f02e2cb132..a546aba46d5da 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -759,8 +759,7 @@ void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma) rcu_in_progress += atomic_read(&c->call_rcu_ttrace_in_progress); rcu_in_progress += atomic_read(&c->call_rcu_in_progress); } - if (ma->objcg) - obj_cgroup_put(ma->objcg); + obj_cgroup_put(ma->objcg); destroy_mem_alloc(ma, rcu_in_progress); } if (ma->caches) { @@ -776,8 +775,7 @@ void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma) rcu_in_progress += atomic_read(&c->call_rcu_in_progress); } } - if (ma->objcg) - obj_cgroup_put(ma->objcg); + obj_cgroup_put(ma->objcg); destroy_mem_alloc(ma, rcu_in_progress); } } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 138bcfa182344..aab3f5473203a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2370,8 +2370,7 @@ static void drain_local_stock(struct work_struct *dummy) clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); local_unlock_irqrestore(&memcg_stock.stock_lock, flags); - if (old) - obj_cgroup_put(old); + obj_cgroup_put(old); } /* @@ -3147,8 +3146,7 @@ static struct obj_cgroup *current_objcg_update(void) if (old) { old = (struct obj_cgroup *) ((unsigned long)old & ~CURRENT_OBJCG_UPDATE_FLAG); - if (old) - obj_cgroup_put(old); + obj_cgroup_put(old); old = NULL; } @@ -3420,8 +3418,7 @@ void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat, mod_objcg_mlstate(objcg, pgdat, idx, nr); local_unlock_irqrestore(&memcg_stock.stock_lock, flags); - if (old) - obj_cgroup_put(old); + obj_cgroup_put(old); } static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes) @@ -3548,8 +3545,7 @@ static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes, } local_unlock_irqrestore(&memcg_stock.stock_lock, flags); - if (old) - obj_cgroup_put(old); + obj_cgroup_put(old); if (nr_pages) obj_cgroup_uncharge_pages(objcg, nr_pages); @@ -5470,8 +5466,7 @@ static void __mem_cgroup_free(struct mem_cgroup *memcg) { int node; - if (memcg->orig_objcg) - obj_cgroup_put(memcg->orig_objcg); + obj_cgroup_put(memcg->orig_objcg); for_each_node(node) free_mem_cgroup_per_node_info(memcg, node); @@ -6622,8 +6617,7 @@ static void mem_cgroup_exit(struct task_struct *task) objcg = (struct obj_cgroup *) ((unsigned long)objcg & ~CURRENT_OBJCG_UPDATE_FLAG); - if (objcg) - obj_cgroup_put(objcg); + obj_cgroup_put(objcg); /* * Some kernel allocations can happen after this point, diff --git a/mm/zswap.c b/mm/zswap.c index e0ec91995f4e3..3c2f960a789ba 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1533,8 +1533,7 @@ bool zswap_store(struct folio *folio) freepage: zswap_entry_cache_free(entry); reject: - if (objcg) - obj_cgroup_put(objcg); + obj_cgroup_put(objcg); check_old: /* * If the zswap store fails or zswap is disabled, we must invalidate the