From patchwork Fri Mar 15 22:26:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13594088 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 86C44C54E67 for ; Fri, 15 Mar 2024 22:24:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84A7680151; Fri, 15 Mar 2024 18:24:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7FBE3800B4; Fri, 15 Mar 2024 18:24:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C2D280151; Fri, 15 Mar 2024 18:24:52 -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 5A707800B4 for ; Fri, 15 Mar 2024 18:24:52 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 31E731A03EA for ; Fri, 15 Mar 2024 22:24:52 +0000 (UTC) X-FDA: 81900704424.28.A6C2AAD Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf19.hostedemail.com (Postfix) with ESMTP id F2E381A000A for ; Fri, 15 Mar 2024 22:24:49 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=OlWzbO94; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=ZOiLfmSy; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=dkDMcRMo; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=4JJKeYSa; spf=pass (imf19.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710541490; 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:references:dkim-signature; bh=h+O85a7jNdi3cYiNUyNI4tzZVgdpHXmiNpkwneSX0QM=; b=8bbRl6PzfKmDd4OHLP6FIXehTo8sj+D4dlIyPWd4CGDH8s/kdVwotf1n1oDPHodY/GZ1Pe 7eEpEkVR97bAW6A50VU5ya6FwMIvMkna/oB69YpmwZw0z6ut1yf8gMwZbHaqogbnPRt309 NMFZ2gt08GtoellRDiv9YAcSvy41qzM= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=OlWzbO94; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=ZOiLfmSy; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=dkDMcRMo; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=4JJKeYSa; spf=pass (imf19.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710541490; a=rsa-sha256; cv=none; b=BxzDY8UKnXVWZRk99DMtrftQa/3fB4d99+3wIKC6YGv0NKIWj6kBKgOoe5ooFAPatEFBAG kb1pxLak09gGa2oMF6NZcuv4lWmda498dPyKjwwgNzDb2LYFXSMLh1xBfgR9QMXObOkmih tO2W94JmTjlRDXGXFqfv4IB61AdIFI8= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id F01941FB89; Fri, 15 Mar 2024 22:24:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1710541488; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=h+O85a7jNdi3cYiNUyNI4tzZVgdpHXmiNpkwneSX0QM=; b=OlWzbO94ZrwkTjd69vykRSIT2EtsMBAh0Vh/UGkK9r4MTWTFb1g82GQFr1EofbkiPp7ZI9 jJ4UD0acmOAGj4akicTIR+ukr/m5AlBMOt/rMIsjcgkDUkk1TwL27LaDv8t+NL/xf/zNuh fzJgpOCTW43jNXBOApC0/1wo2d9/bj4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1710541488; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=h+O85a7jNdi3cYiNUyNI4tzZVgdpHXmiNpkwneSX0QM=; b=ZOiLfmSy2bjjMG2wokL+zxJ/0wwisDfGsfivX82jEfZEAi3m4Hlw/8x9NYOSDZJnKeAZHv xp3+PWnr7csdNgBw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1710541487; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=h+O85a7jNdi3cYiNUyNI4tzZVgdpHXmiNpkwneSX0QM=; b=dkDMcRModR5lmkDMl2u65IsWvSdy1eTntdoE++IgZsdgokXM7Ou7b+tjhidDKKn9v9esPp wXwpx4NY3PjRX9sih+umSF/f1zqbAxGNgj3p/d127JdX132ZXKdgFRYB9e/vJoRAezCNnm PNjx/xDl42ACb9Mhg/sTZViEOZD/H3o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1710541487; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=h+O85a7jNdi3cYiNUyNI4tzZVgdpHXmiNpkwneSX0QM=; b=4JJKeYSaa5twd8m9bstWkO1UyJpJSo+aew9hURgNXFrEe7fNMFeZjFmM/w1s8DonaV7uTA f1aFKeP1YlafnpDg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 6EBC013460; Fri, 15 Mar 2024 22:24:47 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 4NwGGK/K9GXkSwAAD6G6ig (envelope-from ); Fri, 15 Mar 2024 22:24:47 +0000 From: Oscar Salvador To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Vlastimil Babka , Marco Elver , Andrey Konovalov , Alexander Potapenko , Oscar Salvador Subject: [PATCH v2] mm,page_owner: Fix recursion Date: Fri, 15 Mar 2024 23:26:10 +0100 Message-ID: <20240315222610.6870-1-osalvador@suse.de> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: F2E381A000A X-Rspam-User: X-Stat-Signature: erarqkkai3twrdhbsgn45ce1jakncu3s X-Rspamd-Server: rspam01 X-HE-Tag: 1710541489-145497 X-HE-Meta: U2FsdGVkX18icWFrTKDKAcFT8BX/lDPH3wW98zDYymORCiq2/FErmgVwHnvthrZqMS0LhEse21d0JDlPxCuoTz3NsXVYh5TUbihJZ0t38gpHPu12HSmxXJHo6K9t9Vb+AY39KErqTea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MnBElob7JTqpgIXOrPz+gYNOvkPFoVagvHrg0G9mS4fVPh8h7A== 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: Prior to 217b2119b9e2 ("mm,page_owner: implement the tracking of the stacks count") the only place where page_owner could potentially go into recursion due to its need of allocating more memory was in save_stack(), which ends up calling into stackdepot code with the possibility of allocating memory. We made sure to guard against that by signaling that the current task was already in page_owner code, so in case a recursion attempt was made, we could catch that and return dummy_handle. After above commit, a new place in page_owner code was introduced where we could allocate memory, meaning we could go into recursion would we take that path. Make sure to signal that we are in page_owner in that codepath as well. Move the guard code into two helpers {un}set_current_in_page_owner() and use them prior to calling in the two functions that might allocate memory. Signed-off-by: Oscar Salvador Fixes: 217b2119b9e2 ("mm,page_owner: implement the tracking of the stacks count") Reviewed-by: Vlastimil Babka --- Changes from v1 -> v2: Wrap {un}set_current_in_page_owner directly around kmalloc call (Vlastimil feedback) --- mm/page_owner.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index e96dd9092658..cde1ee0f9005 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -54,6 +54,22 @@ static depot_stack_handle_t early_handle; static void init_early_allocated_pages(void); +static inline void set_current_in_page_owner(void) +{ + /* + * Avoid recursion. + * + * We might need to allocate more memory from page_owner code, so make + * sure to signal it in order to avoid recursion. + */ + current->in_page_owner = 1; +} + +static inline void unset_current_in_page_owner(void) +{ + current->in_page_owner = 0; +} + static int __init early_page_owner_param(char *buf) { int ret = kstrtobool(buf, &page_owner_enabled); @@ -133,23 +149,16 @@ static noinline depot_stack_handle_t save_stack(gfp_t flags) depot_stack_handle_t handle; unsigned int nr_entries; - /* - * Avoid recursion. - * - * Sometimes page metadata allocation tracking requires more - * memory to be allocated: - * - when new stack trace is saved to stack depot - */ if (current->in_page_owner) return dummy_handle; - current->in_page_owner = 1; + set_current_in_page_owner(); nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2); handle = stack_depot_save(entries, nr_entries, flags); if (!handle) handle = failure_handle; + unset_current_in_page_owner(); - current->in_page_owner = 0; return handle; } @@ -164,9 +173,13 @@ static void add_stack_record_to_list(struct stack_record *stack_record, gfp_mask &= (GFP_ATOMIC | GFP_KERNEL); gfp_mask |= __GFP_NOWARN; + set_current_in_page_owner(); stack = kmalloc(sizeof(*stack), gfp_mask); - if (!stack) + if (!stack) { + unset_current_in_page_owner(); return; + } + unset_current_in_page_owner(); stack->stack_record = stack_record; stack->next = NULL;