From patchwork Fri Jan 24 03:56:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13948882 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 7D06CC0218B for ; Fri, 24 Jan 2025 03:57:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0DED3280033; Thu, 23 Jan 2025 22:57:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 068F828002E; Thu, 23 Jan 2025 22:57:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E233F280033; Thu, 23 Jan 2025 22:57:14 -0500 (EST) 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 BEB5728002E for ; Thu, 23 Jan 2025 22:57:14 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2CA61AECF8 for ; Fri, 24 Jan 2025 03:57:14 +0000 (UTC) X-FDA: 83040985188.15.A26AC1F Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf21.hostedemail.com (Postfix) with ESMTP id 454E51C0004 for ; Fri, 24 Jan 2025 03:57:12 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M4AIm9tF; spf=pass (imf21.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=alexei.starovoitov@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=1737691032; 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=c4la5/cXBbk2HatOeHKkn8ryi3z3XRRBiHexjMZ10FA=; b=K7F6BVu1HKEl8uefae61DPM0idpdXlcbmVBvQI/WRHM3c5mPZsLBwBb7simwIKCfU4sSjL lSia0d+6CvbnTNWviaai16XrFI1ZOihO20MIcO0c0ADdMOG8k1pYQN31B64F2avE9uy4ef 1QnaQgbRD1yM1kADCW6LTHpJZmSUorA= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M4AIm9tF; spf=pass (imf21.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737691032; a=rsa-sha256; cv=none; b=5FiL33mQ7nGKiQ0zkg397NlZ44Ud4A7cH+fRoKzUxTEO7ZmRRxkige94kvMWbSQgQejyCh kB5zaWbJjS30lx8a1YTpUWTu8sMUJmGo+zQOzVV7rzdn5GbTpeJli4uggFXxg+GrRiKXz9 BEFpoaxzS69GLCb0sr5TDM/dEzA4ZqU= Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2ef8c012913so2381016a91.3 for ; Thu, 23 Jan 2025 19:57:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737691031; x=1738295831; 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=c4la5/cXBbk2HatOeHKkn8ryi3z3XRRBiHexjMZ10FA=; b=M4AIm9tFUmqVUnhztVQoMagUFM0FeZZz2hLHEL5x4xir16o/opmSo3qFxqIS0wlYTT kpZVDJZy1ca2Dg0wBSG3t0lWkT9JWF4OQXVpdqWnxJ/DLmGWGmgJcFWeH4e2NVfGyWvU ej9xEmU4SM0VMH3JtflfcNscto6oIQK0/Zvk9Ci92jZLN2emmswKwSftFnmEh+tGL6Q8 XlVEU8jP9rX0coibHsjnz+WPPZ/OitlUG2TlFyoCfQoakl01Wz4/5NQiSHZIfxnafcqB u5K3Gv+nI0KCbGinBtx0DIe+kdho7Ys8l4kmrB1zcN0Zal27/B7yXgUqWY98cUVHOess CCEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737691031; x=1738295831; 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=c4la5/cXBbk2HatOeHKkn8ryi3z3XRRBiHexjMZ10FA=; b=sTGP3ATpVmmfNhgHP1JZIqpVOo5qAYob2G/59RPATiksOm/HohomFnY2lBrUxDYo0s g5ays9xqN6E6UqMQPhhdKX0xWa3LE1AvCAB05vL32iZ7YZdgDAthqAmh/OqS1fUNC7xq 0pzBXuHcJDAnxO3sPGf08w/bkNdGVGm57+pPFNMvfXlGaaFaPRqWsPdfWPbE1Rrj94WP 3btm8IqsvRJM/U5E4mmrMVjVWgGEFtqiV018SWPCHiK243wb31I78jrrl4f1dC5ZvDhu CAqXaTUihiFsa/GEMm+6u0Yb9P6ZD6nnBzo9rBO2UrS3vUgcRpPDS/Zi2RAPBTpRDNdG 2BwA== X-Forwarded-Encrypted: i=1; AJvYcCVzvWUWMZfQuWjXhH2MwIcqZk94o9L8FhfgR9FpSNt+bc9RvP5IKZ9ldhxeW8toB9u5f0asRt3kFw==@kvack.org X-Gm-Message-State: AOJu0Yw5CuBDodal3BVHcZuQJzRlsJ9QlfFUBqWRze65igSxA06Tswlz F/oSa9WsJxxt9PfXvB3lpUmTYXtpS4mtwXj4/B3IHYQbW92H5k47 X-Gm-Gg: ASbGncsFs8mMIFCCmRc1mBaUOTMexhC+d18qRnfM6FTm69P/eVpMQmbIHq7CL1EC3+u 0SuarYFWlNWdML2esVHBGd40KRGwVc5fjFhSe/btaxEezHhwlUrc8yqDYqnhEUT9E8O3YPCyC/K pp9gZrGwv8JeeRHPvx40NuEyy9DUaPF8ovZu+IgaqOucXW+WbUp4XEJO4yoc99gkQFL7/XuDdSM dJBO0JdgJxM4hUwWeUSXo5GNO2TvLeXz8CZlE7MStlNUR43bs6nzJaXWfrxc71DveQFqcZga/4X J4lsyg/wCIiDF4ifUhF2A2/Th0nKBsdxTk2z2Ks= X-Google-Smtp-Source: AGHT+IEwvNwHQhd2JUrRjyZznwdi2ypsyJojHQDiGm/2CjTDfPwIknvaSpVusJ/6NdDxjUU11w5G/A== X-Received: by 2002:a17:90b:4c45:b0:2ee:bc1d:f98b with SMTP id 98e67ed59e1d1-2f782d5d822mr38678528a91.31.1737691031062; Thu, 23 Jan 2025 19:57:11 -0800 (PST) Received: from macbookpro.lan ([2603:3023:16e:5000:8af:ecd2:44cd:8027]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f7ffa6b2bdsm549950a91.25.2025.01.23.19.57.09 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 23 Jan 2025 19:57:10 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@suse.com, willy@infradead.org, tglx@linutronix.de, jannh@google.com, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v6 4/6] memcg: Use trylock to access memcg stock_lock. Date: Thu, 23 Jan 2025 19:56:53 -0800 Message-Id: <20250124035655.78899-5-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250124035655.78899-1-alexei.starovoitov@gmail.com> References: <20250124035655.78899-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 454E51C0004 X-Stat-Signature: 7y7nhpybjcnnaurz979zfiascb3h58zz X-Rspam-User: X-HE-Tag: 1737691032-651641 X-HE-Meta: U2FsdGVkX1+LGPBMamvHaw81Iatxet1WiGtC78ilNPBrnHfz7bCUqwgHTRqUBGdfze2Z9WKs3XJdhtu7EE30hhl+HK3/qBKtlNBZdUipMZSBkkHJ/A1iQkOsB7Y0LocCEiAgAJriIp+QDoBbTGJSViPdovtSArn5ByPwqB1sL647ymEqzf/B2BqbuAZWiM8qrgvU2iwIjxnJckeZs0jioNthjvv4O+IuOZbeKlYbzYIMmJCPbNzbf6VlTBbR5OvV+2OvE2g0lyAzR38g3JDA0wL7lwE3uPYaILP1gAlsBYFuPnoA5gbXBgk+X72JRzWretyZnotHmACqGsoOKB5C8OcqDLYLS35lxtY7ZKEfffbAT6MpGPb/TTg3NNuHMVz1YYqdKNbL3VckMjxZv4+jbPB3Ib5bvUCurWvTfhUE6ZlQ0HxnvjAN2xLkb4o8hQZnIN0TfM2ZJqvbKL/JlJevV5CldJobMetUyHkCXYEZHKL0lxaCLU2evM5szRBjGvLqfOpJYDoVNMddpnWkvenb8fpU3kLc2l5zX6zSyTMsGqR3kbNfgfpEP4CMDcRjSXFsk33hy8wGvdoQbTiAr4OGRQ19zL0Kjd27+smoNiUbJ74JsH/rI7FTEANGeQ+6OUnQcArI3XzjNA5xqDPaMMRnxzK3KMlZF/SpPLH69EQH4lZKdwhvltoOTrFYk1nfYSMY1h4wu/L20O9x3y2V+pPOh8/jdFRvXbtcwQkIExN6ykVxO1WymCy1UEIUMVfsnW+g0BaaHSDr4dg4dGK/tMnYXW79R0QJyPPZW4Jz8Zle0MyeX73gbrW7Qk2ca/kDvSAMMilYzGlBRch/FWuygPoEFrz1vIxnHyqryO94azIUyHstmVg5wbqjAGOQ5ZDyxh/cuCd1pJ+/WRQnWQstsTwLuc3dVdzFXBH+AX0g7lofaubSLrgO6rdkPVNSF1oHBY1jp1yjIAQFxtCOwybTy8A s9tLAeTI KApdQ2gmvZqk79oeR7StXhKXZeWLJa/SKV+Ksr6mwEws05oHDyxHl4DZKjC1LXA1iA/AfOEUXi6NJJ8/mBOgUNNqt5SMifL2uJ7NPI7anPLPXi2ubiCS17Dx4JHbkKujF9lvR5hmLec98OO1Zu/RKrMt8ORtXjd/zG1QJ11MKrZuOG97+6Y8INYRHLullGePRbugj4nEiu1t4BD8iEuINCKlzCAYEX2aDr/OLegvVfP5Rg6bi49XGUbb9IuiuiYa+Fts8Cb90oRPQLalK4xXvKoQc5vKBFEsk/y9SsZuQ/vznOXSlAWebQVUcuRBnxbG/aJRTOHMF7U53uhWK9PZJe0SB7M/rcRVIeHvrGR9Yy4ukfvBhpYknedXiqBjh1mT88fOSJL3pLJBiLlfy9qIlXCH25+9loSJAOfjnntWhq0VwrnRsJVv2o/b6Kv5oCP3WCjj1E1F9dyFZAffFDVZZcWECvnYLqa3+rDDLxN2qHbMtNcvL9ecjhSGub2105c9x08R2XX6ERS931btCjAiyAQr/+vZmeD4Lij0G/5EFmCQU27308t0V7/qnvw== 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: From: Alexei Starovoitov Teach memcg to operate under trylock conditions when spinning locks cannot be used. local_trylock might fail and this would lead to charge cache bypass if the calling context doesn't allow spinning (gfpflags_allow_spinning). In those cases charge the memcg counter directly and fail early if that is not possible. This might cause a pre-mature charge failing but it will allow an opportunistic charging that is safe from try_alloc_pages path. Acked-by: Michal Hocko Acked-by: Vlastimil Babka Acked-by: Shakeel Butt Signed-off-by: Alexei Starovoitov --- mm/memcontrol.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7b3503d12aaf..9caca00cb7de 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1722,7 +1722,7 @@ void mem_cgroup_print_oom_group(struct mem_cgroup *memcg) } struct memcg_stock_pcp { - local_lock_t stock_lock; + local_trylock_t stock_lock; struct mem_cgroup *cached; /* this never be root cgroup */ unsigned int nr_pages; @@ -1756,7 +1756,8 @@ static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, * * returns true if successful, false otherwise. */ -static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) +static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages, + gfp_t gfp_mask) { struct memcg_stock_pcp *stock; unsigned int stock_pages; @@ -1766,7 +1767,11 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) if (nr_pages > MEMCG_CHARGE_BATCH) return ret; - local_lock_irqsave(&memcg_stock.stock_lock, flags); + if (!local_trylock_irqsave(&memcg_stock.stock_lock, flags)) { + if (!gfpflags_allow_spinning(gfp_mask)) + return ret; + local_lock_irqsave(&memcg_stock.stock_lock, flags); + } stock = this_cpu_ptr(&memcg_stock); stock_pages = READ_ONCE(stock->nr_pages); @@ -1851,7 +1856,18 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) { unsigned long flags; - local_lock_irqsave(&memcg_stock.stock_lock, flags); + if (!local_trylock_irqsave(&memcg_stock.stock_lock, flags)) { + /* + * In case of unlikely failure to lock percpu stock_lock + * uncharge memcg directly. + */ + if (mem_cgroup_is_root(memcg)) + return; + page_counter_uncharge(&memcg->memory, nr_pages); + if (do_memsw_account()) + page_counter_uncharge(&memcg->memsw, nr_pages); + return; + } __refill_stock(memcg, nr_pages); local_unlock_irqrestore(&memcg_stock.stock_lock, flags); } @@ -2196,9 +2212,13 @@ int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, unsigned long pflags; retry: - if (consume_stock(memcg, nr_pages)) + if (consume_stock(memcg, nr_pages, gfp_mask)) return 0; + if (!gfpflags_allow_spinning(gfp_mask)) + /* Avoid the refill and flush of the older stock */ + batch = nr_pages; + if (!do_memsw_account() || page_counter_try_charge(&memcg->memsw, batch, &counter)) { if (page_counter_try_charge(&memcg->memory, batch, &counter))