From patchwork Wed Jan 15 02:17:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13939761 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 3E09BE77188 for ; Wed, 15 Jan 2025 02:18:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CBE03280002; Tue, 14 Jan 2025 21:18:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C6E60280001; Tue, 14 Jan 2025 21:18:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE7F6280002; Tue, 14 Jan 2025 21:18:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8C033280001 for ; Tue, 14 Jan 2025 21:18:15 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1C198160E7F for ; Wed, 15 Jan 2025 02:18:15 +0000 (UTC) X-FDA: 83008076550.19.7285320 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf26.hostedemail.com (Postfix) with ESMTP id 333FD140007 for ; Wed, 15 Jan 2025 02:18:12 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JvweOne2; spf=pass (imf26.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.181 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=1736907493; 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=mxa1zavVGcrL3mhmNj3zaW3g0F2sJ8bBr01E6sisj+Y=; b=qbL35h7euf/2F+A5qBrZiwHDXTDef0WPeVKry8IoJ0mekpSz0RSXahyjzML9K9GIm+9tdV ERtCQT10DKWnxnzfwxs8TO2DHayOwPav+4vP1O9NVlVZIbnqtqzyDwl50NfBWfIopHzfpH jm+WvdkU31BW3/hAHknzKemMMYxhjo8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736907493; a=rsa-sha256; cv=none; b=zyDdL+4KMt01UKW1kW15W+OYD5BmfCHceJ9mq891usJFux8u6DabjnRsmr5RsEirCXxa8w GeXGcH+7J3zGWI6qPqiDjyO2ZQpQJP15leRwHABnjvgECqK6z7+oKO18OG7kdLOtSYchTH E6lXGR1MCg1Mit3UgWOyhaEzZEuM4aI= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JvweOne2; spf=pass (imf26.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-215770613dbso75286635ad.2 for ; Tue, 14 Jan 2025 18:18:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736907492; x=1737512292; 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=mxa1zavVGcrL3mhmNj3zaW3g0F2sJ8bBr01E6sisj+Y=; b=JvweOne23A2wra/5hAhMKf3hAi9v2Df8eSSxMX/wIFNLbqHG+2VRhvkw2A4tw37t7z a9mYRi6eOfTnNmY/851+EZD3LwX93AXpjWlBihkFHw2NkMCBG3MBlAYJAgs07Rn0D8iw E7ttipiI5oP+M3nQhMTPuFBngiXP4xaRNqT43JfrTbhTXs2OCI9MNB9vckTAbERZL8jk MNDs3Dj9ahwsPF8Kf0MJHpLr8+bHDC/LUlja3uAhsD4GsSyYxzUKFsj9hwpLkuF2iFL4 nxasYWohnbycCR+9FOYRknVxFiF3gXcz5x8djchtP/myWdCJLs6hFA0UjTwOV/QL74rK 8Uow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736907492; x=1737512292; 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=mxa1zavVGcrL3mhmNj3zaW3g0F2sJ8bBr01E6sisj+Y=; b=rWzAVhkjkINGUnfRFW+TfztukVdBx0wHxT2UWvK5wZ+YFmfdtmv0WXwHYw6gXpWM39 LuB1pKGL33+riT6Q7eN7JEvExPXvlMZoKg/E5Ex6tzmHhPKUCCBnyvo0ESpoKkiWRa4r K+2WuRVufSzsvJsfMzxg5JN2NmFif71HPmDbFPRtqxbYh+OmLqtThpNnjAU6Xp34nwcG QNQJpGKL2xLjDd1QWP+jREAbvVsVgdpsUkDRq7GdOPRFMe2r5L9qWnd2SIPbEV9aWiNL fYHy6VVUBWmwHnNVBJYGbaohy+LTd5xyWnpKzcasGY7e43ufkHRZzUrbU32SPnVk/R1v uklw== X-Forwarded-Encrypted: i=1; AJvYcCUMR4Osmw6/E5x4Ki00kZszFvNluaNbs8NOkAaVn4SDFG3fyckIDRGii1QYT+r4cohXxjFb5DVsZg==@kvack.org X-Gm-Message-State: AOJu0YyDJ3VBi7hugFRn47Dn/ZcB/T2E4bJsB1Pvyg04b7Z20XNrJL9f n0c54evXr8L74R87KGF3MjyTgyYsYzrZOkZ94si8ZkL/EV2LcKy/ X-Gm-Gg: ASbGncvCQAFckO4A2WCLxkloZMgSl+giW2Po+5ey7H9xcWAd+dUCavHphag5vTCq+2C yG3P1cKtV2ODp1RR/RM06lxxOZhlJyfMmeOIBUEP20SeOZEtl3uUfBXYcC+axRJOCFnyHYiS0RR B6ZtvSIpqI2Dgow6Ucts0uS8D8/YvHkSSF8TuHZzuL5PkujBNZuNFAnnMU7ZANZRDfzrGA4lNOO 749rpusglPI7WBmW/dA2UczzGJ1+yp2Z/NRBzy5f8cpOIakYnNjER7Z+o4P5AFxMhL1nK6RY/I/ WypWxVV7 X-Google-Smtp-Source: AGHT+IES28E6/UzBuAXcKHyytweWh1DbAVGKG/FlHuIJMf6aRzxb6IkpsZav+M1wSoIo/J2A0mfOZg== X-Received: by 2002:a17:903:41c5:b0:215:19ae:77bf with SMTP id d9443c01a7336-21a83f4ea67mr419970035ad.19.1736907491906; Tue, 14 Jan 2025 18:18:11 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::5:4043]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f12f919sm73361445ad.69.2025.01.14.18.18.09 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 14 Jan 2025 18:18:11 -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 v5 4/7] memcg: Use trylock to access memcg stock_lock. Date: Tue, 14 Jan 2025 18:17:43 -0800 Message-Id: <20250115021746.34691-5-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250115021746.34691-1-alexei.starovoitov@gmail.com> References: <20250115021746.34691-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 333FD140007 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: oencnryexobtz7k8p4we19eqfx65nxtd X-HE-Tag: 1736907492-690670 X-HE-Meta: U2FsdGVkX18wwm4rfgenU6BAotmVFKeW6kAPZab9wP5wD1HR/jVX5vffI+/xK7pShxKNBp3sDmjls+ottYAVme0GLez1Dr5KSSigKExbqVGFRvRA8ktnpe9u0ffDPCz38mV6bucZpN1vQ/S6Al6ICwu8BoQ94bffWKeWUmi7+INPe/mmlNTYE3Tb8GJPDIfx3+Zxu1t7SUed8XAoX+eBd0GLV9NhMZjwKLzkbiDWVC8rM76YBw5ZzRJYiY1aQ4KEXl/zJ7s+dyPU8UB2V7nPJAav60aUfqs2AddlNJk4XwdjT5ywQZJso8UEKaJwJTFR0ypKtZe4XOCbOt5hLwNJjW9FGlapke9OydAzFMriCvO9S9vxTbwko5lvoBa8VADX74hF0Ujoh/a5VF2UojS8IeYMSq8wbCxXl6wujMa/ka6UA0NZplVpE0FGlScHQjkmWk3eclg3kiBnVBym9C/q0zvhTGJEQEPviWsBiPHEDDkiS6EnH/RGaanwAW5OzuoT7pQZDSxmsWcN5U4xfIxhk9/2FDwfz8XD/9JOn6TvX6vZV04UglVtX+y9itxGO9AOOBMTBYeo6F6aPj/jfjj/51+4S9W3Ej2r5UcgnxHG05RoplmsVxW2UPbRT4U6N861AGyB2nh904TpLTEpODTEkULOWniA2JQeqpfkaIs5Mxudw9SLLByTgMMkYlQAnYtKl9/P84+HAPF/9d+OeLIpGG/2IdsA7ZfULGFAP1ly7U0gpXXf6/ey9EDP75xOOsJjbLnwERPdN9RdU6T7wOAIT+Gj2JxlopBiaQTNJWeOrm/usI6sPZQPVFQw0WHj+9PUyLc9M/NzZmN5Lfq7AwCroYHRQtl/gkcbm8TUlV/1GgyKHgnifCl2718wU2U24Qsb2h3ULxUUB+JXmjLJp7BGl2K71SY0WKfsxQyS05KTgIyqSCualT/kzFFzU8JDOzl363N3BtK1cit/zKX7/S6 riTa1skj UZ322dgWhH6eod97Lp+8Nqz3rYN8psm1IMGR9Gt7JEF7tXIMVnyE+hgMw9Au6rA4B10wQ9FQSPSHBCyHSHOx3MnfZuhkPp/QwZoGUonvLWTHPNkrosYFI1/QmYXVpr0NeitYY1qoinmyxl3eQ/z2Z69lKFt2daL5lJemyjixHkLIHzrjDv3tC4osZbBknJ7MAwray1MqTfoLDPaxXwF8nYXYg2Gn9Z6kCxcL+gmkDiCGKi0IJG+bulz2xUEV7PqnqW9p7ebwzdqh7FZ6OblslNiRUVgOwD3X3/BC24ra/oHZ6jIWs8ZJ2/zJKnOKt7wIyBx3B63LffNQAewy7epG453grian0WpyljDIKGqpYFRmfTGG7l43NGHeQmUTslWEVmt/Er0Z9t0fEDlMJYdpsw9vunO7+5X/D9Zy8gfoguo4hwBXGuN20uYsNR8XX4aafYoAJ2JLm2I3RMOwe16q9olMTGmuccDpHAqY1OmrSSRp6uZbKMDdy21NUrBmFKJ9efmNqzpF3HbrYNZSAI+RNrUxi8A8Nbst1edRHPbBw/pLnJ1JaxMU0+zacKg== 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 Signed-off-by: Alexei Starovoitov --- mm/memcontrol.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7b3503d12aaf..e4c7049465e0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -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,14 @@ 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. + */ + mem_cgroup_cancel_charge(memcg, nr_pages); + return; + } __refill_stock(memcg, nr_pages); local_unlock_irqrestore(&memcg_stock.stock_lock, flags); } @@ -2196,9 +2208,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))