From patchwork Wed Dec 11 20:39:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joshua Hahn X-Patchwork-Id: 13904130 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 9D4F1E7717D for ; Wed, 11 Dec 2024 20:40:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BC1316B0085; Wed, 11 Dec 2024 15:39:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4CDF6B0088; Wed, 11 Dec 2024 15:39:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99B2C6B0089; Wed, 11 Dec 2024 15:39:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 721FE6B0085 for ; Wed, 11 Dec 2024 15:39:58 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D580D141318 for ; Wed, 11 Dec 2024 20:39:57 +0000 (UTC) X-FDA: 82883844288.15.D11EFAB Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) by imf19.hostedemail.com (Postfix) with ESMTP id 0F0221A0011 for ; Wed, 11 Dec 2024 20:39:30 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=U2zSTmK6; spf=pass (imf19.hostedemail.com: domain of joshua.hahnjy@gmail.com designates 209.85.128.175 as permitted sender) smtp.mailfrom=joshua.hahnjy@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733949585; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=etyn39dYkYYqxl1MOdPR2a9G+AL/gyYbItp4iRGm6Gk=; b=hpstalAgqhkP4gXuuVGEBi+wobWz6ibfITAWPbCdHWdHXB8MrU/4bi1PyAopEKb39wRvhE j0w8YFp+k451mxZ3RRi4DMWHMRXN6HPGgJmh3v5qitztx15vtKxln34K82ZbXpZxCyjc4c 8GtOJF3LESVtNCE+D5iDrzIjEvOGXaY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=U2zSTmK6; spf=pass (imf19.hostedemail.com: domain of joshua.hahnjy@gmail.com designates 209.85.128.175 as permitted sender) smtp.mailfrom=joshua.hahnjy@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733949585; a=rsa-sha256; cv=none; b=k/ADAjLgOweaRGdRCXs/AZCjPdJox7TplB/PZihehMA8SkszsLFbqldGJPuUQD6hw0+sIx gApmlbpBhnQUSp4ZXS+KNeMRvcK4fknhvHd/peQvfmN9j74tqJkaxfpj8owI0hdRBaCmjZ e5ELcn8edMfoi4BDBWguioyEN0IO0ec= Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-6eff5f99de4so49854687b3.1 for ; Wed, 11 Dec 2024 12:39:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733949595; x=1734554395; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=etyn39dYkYYqxl1MOdPR2a9G+AL/gyYbItp4iRGm6Gk=; b=U2zSTmK69sOMqaYdNa0OSFwyW4zyV/X3p4/DrVfOk1Z0q9kmrkQ8KtBdac0S918CXp 4Rhat6sWJhAwy+dc+pFqKbo/b1q0DcDk5YQu/VTIWD9vywNCfa61HM39H70zkZzZz4vP 2w1cGq7f5UYsuPjpqH/drs1uESwr7jwC2l5ZFj2CqLnwbm8Mb5RTnOxCc+uW90057iwz IgdotrBOcM86vFYZg7n+/cW6sMvdyI06yZygV08m/UQ1i5QRFeG3JjgocBPZY+txHLZL 8aKf92Ewu5zpccxy1AjbUiPgvxdHQzyKVhxDYNmndzHF7lUQL4zwAcKYeAHtePxriUkv P5Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733949595; x=1734554395; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=etyn39dYkYYqxl1MOdPR2a9G+AL/gyYbItp4iRGm6Gk=; b=tXqQaUEh9xdPS00GlX4m6ZjCu9q3OQLG1qGW367XQcO3WrygZ2d7R7oNcpweZhPxh7 qiuPwj91vTzMMfFzAG+w9a3V8L8j07iTtDLTuAzNumXasiJAV2thzwf0mblNKx/pdR2E zHvsR6+S9X5yzZZORrTTDM7RW+aHHUQcLt7CmhvjmV6Lu+JC77EcvzhiK6jJ2awtVzSD 02rpZNARCgLKFgdkw0+96VCs6NFi0O4FwR2QBF8riASUfXlLPocmUmyG1KeMV5oWjO6c 8h+VWnTspiw9uO3h2eO5pN4sHTt0l8BDKZx7kZGYSsd4pUOMHH/quw+ggDb2lmrnOueW mDMw== X-Forwarded-Encrypted: i=1; AJvYcCUAhRd5zg4Tjnqe7bqQJvj/qwCaxpZYyO5sw/MAQTJfU5Wqe4vnbOKtL5v2jF0l4ePx8R2550Uu8g==@kvack.org X-Gm-Message-State: AOJu0YwOK3O6pH8JabMrAHFMlqdE1cOQHIGvB7GdbnyR4G3U3BOoxZQH tq1o0ozeXYPEK/AjFx+DJcwTgQW1cGk0fLHRCrcvnpN9rbosQrjg X-Gm-Gg: ASbGncvOh1qETWZTSZELVmeD0M/CgqmlPFcsK3ml5kz68DMh1Tpz5CxibYb61klTSrs uWSx9MdrTk7aQSLol1JKSurkva42tbgt/WeIxtaxfKvaisfed6z3+gwq8eiXyadqwOPZa9BG/l0 dA+UIvatNE3G7S9w3dlFccfdrEDs5fjgbMPNfbxWykLpJo7eOX8T6q6ROz59Y1k5/p24nr1f1Ms F0r/BWcPbOQoLXJZoA9M319tbEjBMG1yknTOwPZEAmJ3ryems+tMVwE/5Xf3HQaFU6VpsZPKc+D iIyKkMdlTim4aw== X-Google-Smtp-Source: AGHT+IHiKNGpDGljLutgpDps2QixRf6dKOvZDYWteW4cFSGFlQb0IBB4jIkNpQKhnKnsth+eROjXLA== X-Received: by 2002:a05:690c:9a01:b0:6ef:7312:d05c with SMTP id 00721157ae682-6f1a4f68714mr11743177b3.2.1733949595078; Wed, 11 Dec 2024 12:39:55 -0800 (PST) Received: from localhost (fwdproxy-nha-010.fbsv.net. [2a03:2880:25ff:a::face:b00c]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6f14cebffeesm4012667b3.126.2024.12.11.12.39.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Dec 2024 12:39:54 -0800 (PST) From: Joshua Hahn To: shakeel.butt@linux.dev Cc: hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, muchun.song@linux.dev, akpm@linux-foundation.org, sj@kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [v3 PATCH 2/3] memcg/hugetlb: Introduce mem_cgroup_charge_hugetlb Date: Wed, 11 Dec 2024 12:39:50 -0800 Message-ID: <20241211203951.764733-3-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241211203951.764733-1-joshua.hahnjy@gmail.com> References: <20241211203951.764733-1-joshua.hahnjy@gmail.com> MIME-Version: 1.0 X-Rsp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+hBqCNLGIeHEiGWFGa7O IxH03oEL KFvXYAMTLIVqKp3SSw07WD0FazvyeglZTPugt/lTtQfQAjWBjwokxx+Gl9vtrFDM4zA5+IoypVfPZx3Gm1bBj0sbLY5kwHsCFQVVoqY2/vtglokqbfHEwyWWMQveVNi4WGpea73idrifaB2p+4APSbAlKawb1/jtsvvnpg+4PqL+UfHMumDC+yNR7Bsf6N2oo8PaXbE7zTSbLAbviTUWu7DU2XX2OJKNpUi1SrnZyGOZjwuCzMXH2rE571wZDPGitTLH5R+EIULVWR+xDiPxG1PGWfsS/q5eAt+IdCOFaNqcIyBFavmcPl4/4+bDJKZ4uK+jVBoKsSgqtxRBrrqfNVBkfnbNmGb94WQbViLre5pnF4jMZRgi2bxJAtzJAWbKIWLL6Tt4S9+ubffhEEbfx5PHx3pK5rzXgd38fJoolCWoELnsxyofJZ12iZw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.004448, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch introduces mem_cgroup_charge_hugetlb which combines the logic of mem_cgroup_hugetlb_try_charge / mem_cgroup_hugetlb_commit_charge and removes the need for mem_cgroup_hugetlb_cancel_charge. It also reduces the footprint of memcg in hugetlb code and consolidates all memcg related error paths into one. Signed-off-by: Joshua Hahn Acked-by: Shakeel Butt --- include/linux/memcontrol.h | 7 +++++++ mm/hugetlb.c | 35 ++++++++++++++--------------------- mm/memcontrol.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 5502aa8e138e..f4271cfdba92 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -649,6 +649,8 @@ static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp, long nr_pages); +int mem_cgroup_charge_hugetlb(struct folio* folio, gfp_t gfp); + int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, gfp_t gfp, swp_entry_t entry); @@ -1152,6 +1154,11 @@ static inline int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, return 0; } +static inline int mem_cgroup_charge_hugetlb(struct folio* folio, gfp_t gfp) +{ + return 0; +} + static inline int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, gfp_t gfp, swp_entry_t entry) { diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ea2ed8e301ef..f0f0ffae30a3 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2967,21 +2967,13 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, struct hugepage_subpool *spool = subpool_vma(vma); struct hstate *h = hstate_vma(vma); struct folio *folio; - long map_chg, map_commit, nr_pages = pages_per_huge_page(h); + long map_chg, map_commit; long gbl_chg; - int memcg_charge_ret, ret, idx; + int ret, idx; struct hugetlb_cgroup *h_cg = NULL; - struct mem_cgroup *memcg; bool deferred_reserve; gfp_t gfp = htlb_alloc_mask(h) | __GFP_RETRY_MAYFAIL; - memcg = get_mem_cgroup_from_current(); - memcg_charge_ret = mem_cgroup_hugetlb_try_charge(memcg, gfp, nr_pages); - if (memcg_charge_ret == -ENOMEM) { - mem_cgroup_put(memcg); - return ERR_PTR(-ENOMEM); - } - idx = hstate_index(h); /* * Examine the region/reserve map to determine if the process @@ -2989,12 +2981,8 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, * code of zero indicates a reservation exists (no change). */ map_chg = gbl_chg = vma_needs_reservation(h, vma, addr); - if (map_chg < 0) { - if (!memcg_charge_ret) - mem_cgroup_cancel_charge(memcg, nr_pages); - mem_cgroup_put(memcg); + if (map_chg < 0) return ERR_PTR(-ENOMEM); - } /* * Processes that did not create the mapping will have no @@ -3092,10 +3080,18 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, } } - if (!memcg_charge_ret) - mem_cgroup_commit_charge(folio, memcg); + ret = mem_cgroup_charge_hugetlb(folio, gfp); + /* + * Unconditionally increment NR_HUGETLB here. If it turns out that + * mem_cgroup_charge_hugetlb failed, then immediately free the page and + * decrement NR_HUGETLB. + */ lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h)); - mem_cgroup_put(memcg); + + if (ret == -ENOMEM) { + free_huge_folio(folio); + return ERR_PTR(-ENOMEM); + } return folio; @@ -3110,9 +3106,6 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, hugepage_subpool_put_pages(spool, 1); out_end_reservation: vma_end_reservation(h, vma, addr); - if (!memcg_charge_ret) - mem_cgroup_cancel_charge(memcg, nr_pages); - mem_cgroup_put(memcg); return ERR_PTR(-ENOSPC); } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b25eab9c933e..c903e260a830 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4561,6 +4561,40 @@ int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp, return 0; } +/** + * mem_cgroup_charge_hugetlb - charge the memcg for a hugetlb folio + * @folio: folio being charged + * @gfp: reclaim mode + * + * This function is called when allocating a huge page folio, after the page has + * already been obtained and charged to the appropriate hugetlb cgroup + * controller (if it is enabled). + * + * Returns ENOMEM if the memcg is already full. + * Returns 0 if either the charge was successful, or if we skip the charging. + */ +int mem_cgroup_charge_hugetlb(struct folio *folio, gfp_t gfp) +{ + struct mem_cgroup *memcg = get_mem_cgroup_from_current(); + int ret = 0; + + /* + * Even memcg does not account for hugetlb, we still want to update + * system-level stats via lruvec_stat_mod_folio. Return 0, and skip + * charging the memcg. + */ + if (mem_cgroup_disabled() || !memcg_accounts_hugetlb() || + !memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys)) + goto out; + + if (charge_memcg(folio, memcg, gfp)) + ret = -ENOMEM; + +out: + mem_cgroup_put(memcg); + return ret; +} + /** * mem_cgroup_swapin_charge_folio - Charge a newly allocated folio for swapin. * @folio: folio to charge.