From patchwork Wed Mar 13 23:42:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13591976 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 6D334C54E58 for ; Wed, 13 Mar 2024 23:41:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 039378D0069; Wed, 13 Mar 2024 19:41:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F2C618D0037; Wed, 13 Mar 2024 19:41:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DCC938D0069; Wed, 13 Mar 2024 19:41:35 -0400 (EDT) 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 CDECC8D0037 for ; Wed, 13 Mar 2024 19:41:35 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6D08A1205FD for ; Wed, 13 Mar 2024 23:41:35 +0000 (UTC) X-FDA: 81893640150.26.B15A200 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf27.hostedemail.com (Postfix) with ESMTP id 21B6C4000F for ; Wed, 13 Mar 2024 23:41:32 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="i/SptgVW"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=wc9hKIhb; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="i/SptgVW"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=wc9hKIhb; spf=pass (imf27.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=1710373293; 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=ngQm30aBJqnSASRUqDF9FkkVGLhh+ZhEHI6q7IEdKyE=; b=nb55YyDt1hmkIUJVUxrFExe/nX+Tx+mQSKY5dX1X/4d5F4tFNOWGRtLvIgzqXnqjy5JMy3 Fbp6lJiEYR8grI9t5s5YP3v6V/4h73EX2rM8oefkTRo3Hsb+W8Y0VjGRM64uLoz4+kNwh0 +ozNVlnHzOPIBc8p9PGFhlv0ZH7+LLg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710373293; a=rsa-sha256; cv=none; b=3UOH972tORBX1MYGnBig2JoE+Hynx3VKjnyPLPF7XjM2vOOYQpHTNKfHj07G4dFI36gBAR REyepR1KFG36irK1mgJTKV5JSpypbKb70ufSEhJDwmc+B0YWg/2YxU94CRVEbT0jxdai3U DYSPy42w52GNqKKAidshrqoCIRQlgxE= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="i/SptgVW"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=wc9hKIhb; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="i/SptgVW"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=wc9hKIhb; spf=pass (imf27.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 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 45F391F7FA; Wed, 13 Mar 2024 23:41:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1710373291; 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=ngQm30aBJqnSASRUqDF9FkkVGLhh+ZhEHI6q7IEdKyE=; b=i/SptgVWMAhN1Vq/cMjLGtfHNVL7VjISImpVpbD4etTEpYz7Z4nOXgeuF8PxTR2KArsngN aKdEOEHJ3aYfEh17wonulf9aY84zUEW0u/hrxksJMx50U8QkbnUAAvDJuUIQ5NLt7OT7h4 1ovrYlcHhHFNen6BeIf76wTyK4ga7pY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1710373291; 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=ngQm30aBJqnSASRUqDF9FkkVGLhh+ZhEHI6q7IEdKyE=; b=wc9hKIhbDnPbCzswGaGYi/NVkVDyJ42sQRGJtGP+Mzh9KKLjosWtqiZxvDue8fqnPGnQAR YPu9XnWmgbGPPUBQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1710373291; 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=ngQm30aBJqnSASRUqDF9FkkVGLhh+ZhEHI6q7IEdKyE=; b=i/SptgVWMAhN1Vq/cMjLGtfHNVL7VjISImpVpbD4etTEpYz7Z4nOXgeuF8PxTR2KArsngN aKdEOEHJ3aYfEh17wonulf9aY84zUEW0u/hrxksJMx50U8QkbnUAAvDJuUIQ5NLt7OT7h4 1ovrYlcHhHFNen6BeIf76wTyK4ga7pY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1710373291; 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=ngQm30aBJqnSASRUqDF9FkkVGLhh+ZhEHI6q7IEdKyE=; b=wc9hKIhbDnPbCzswGaGYi/NVkVDyJ42sQRGJtGP+Mzh9KKLjosWtqiZxvDue8fqnPGnQAR YPu9XnWmgbGPPUBQ== 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 B5EB51397F; Wed, 13 Mar 2024 23:41:30 +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 +SNVKao58mVQDAAAD6G6ig (envelope-from ); Wed, 13 Mar 2024 23:41:30 +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] mm,page_owner: Fix recursion Date: Thu, 14 Mar 2024 00:42:45 +0100 Message-ID: <20240313234245.18824-1-osalvador@suse.de> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Spamd-Bar: + X-Stat-Signature: 6g4s151555u8im3hg5wwouydaucr594o X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 21B6C4000F X-Rspam-User: X-HE-Tag: 1710373292-447800 X-HE-Meta: U2FsdGVkX18h0vWG5dw3kCOAK9loc6RIjOMglF2J5R0erAu4v3urvKanY2g5/qWv9Nvgsk54ttWZO4+zq3aDJ93sZSQ9ircLGojlKbDEVcj1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Bf3l+x/RkUxKRjbKVAUAzFRZ1vS0fjON7Ttr39mGqaNdUaU/Bku0IR78x94TSvVRaXJ56Oq/V6GdOfdnpw== 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") --- mm/page_owner.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index e96dd9092658..60663d657f7a 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; } @@ -232,6 +241,7 @@ void __reset_page_owner(struct page *page, unsigned short order) alloc_handle = page_owner->handle; handle = save_stack(GFP_NOWAIT | __GFP_NOWARN); + for (i = 0; i < (1 << order); i++) { __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); page_owner->free_handle = handle; @@ -292,7 +302,9 @@ noinline void __set_page_owner(struct page *page, unsigned short order, return; __set_page_owner_handle(page_ext, handle, order, gfp_mask); page_ext_put(page_ext); + set_current_in_page_owner(); inc_stack_record_count(handle, gfp_mask); + unset_current_in_page_owner(); } void __set_page_owner_migrate_reason(struct page *page, int reason)