From patchwork Wed Feb 14 17:01:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13556792 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 098ABC48BC1 for ; Wed, 14 Feb 2024 17:01:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 468A56B00AB; Wed, 14 Feb 2024 12:01:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C9726B00AC; Wed, 14 Feb 2024 12:01:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2427B6B00AD; Wed, 14 Feb 2024 12:01:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 0434A6B00AC for ; Wed, 14 Feb 2024 12:01:03 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 30C32C0E06 for ; Wed, 14 Feb 2024 17:01:02 +0000 (UTC) X-FDA: 81791024364.26.B511502 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf23.hostedemail.com (Postfix) with ESMTP id 7C02114003A for ; Wed, 14 Feb 2024 17:00:59 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=EpHuBP6g; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=fCmFCy05; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=EpHuBP6g; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=fCmFCy05; spf=pass (imf23.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=1707930059; 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=UE3eKfl5kkI5fbvZQeYL5WioU57G5GzrnlK8ssFNQw4=; b=cNtGzwbJEwgPcHXkgeFGKo5izUGQZpxAbMXxRsewPHtxv1o+d0bTFD9QtWEyIF9YlYnbh2 5Luf7X+8lDUGxo447lY5UplNtKXTx8zBqK1wVJ0oG63gjJVf1wIJLC8u4juagYyLv59FpS sI5ef+Qc+ZUkGcJ/nMD0WejcX/iBUiA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707930059; a=rsa-sha256; cv=none; b=Zx27ZvywttaKe9uDR84Wb97pBpOTJsOyFfGCbkIdc+Hv4sW71jfHzOzwgK/Q4Aejtv5nrc +QOTBjZUbJ+dMPdOXGrq9Q7XcsEqB3Cx4lwCeMUzeyazi6ASAD01KEjhvKCx18VW1dZvkT 56h0s22oO/uTtTFzMYl1+kfIlO30cPM= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=EpHuBP6g; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=fCmFCy05; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=EpHuBP6g; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=fCmFCy05; spf=pass (imf23.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 imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (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 017141FD18; Wed, 14 Feb 2024 17:00:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707930058; 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: in-reply-to:in-reply-to:references:references; bh=UE3eKfl5kkI5fbvZQeYL5WioU57G5GzrnlK8ssFNQw4=; b=EpHuBP6glUCfx3TEAZSizbmIf9MU6xfQbSgNaV2UwXijSOS5Fy1blUiRDza1eJ8xpVSHWh BiZep5YNbbJTsqIb2LNty3m3UVqH5Mm+AVyQFFLv9tsNNzNO4l3ppN0E47RDJxKsCr9zQz sBYq+LYB7AOCPmw5RW3vjSDTlt7M5/Y= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707930058; 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: in-reply-to:in-reply-to:references:references; bh=UE3eKfl5kkI5fbvZQeYL5WioU57G5GzrnlK8ssFNQw4=; b=fCmFCy055XA4DUwbrXzHieNWBb5QQ3ODxy9PZ+pFHq7/1ZcJULtxR7NF16xAmFkf4tCUdt pDnnoetfjziN4XBw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707930058; 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: in-reply-to:in-reply-to:references:references; bh=UE3eKfl5kkI5fbvZQeYL5WioU57G5GzrnlK8ssFNQw4=; b=EpHuBP6glUCfx3TEAZSizbmIf9MU6xfQbSgNaV2UwXijSOS5Fy1blUiRDza1eJ8xpVSHWh BiZep5YNbbJTsqIb2LNty3m3UVqH5Mm+AVyQFFLv9tsNNzNO4l3ppN0E47RDJxKsCr9zQz sBYq+LYB7AOCPmw5RW3vjSDTlt7M5/Y= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707930058; 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: in-reply-to:in-reply-to:references:references; bh=UE3eKfl5kkI5fbvZQeYL5WioU57G5GzrnlK8ssFNQw4=; b=fCmFCy055XA4DUwbrXzHieNWBb5QQ3ODxy9PZ+pFHq7/1ZcJULtxR7NF16xAmFkf4tCUdt pDnnoetfjziN4XBw== Received: from imap2.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 imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 72D7F13A35; Wed, 14 Feb 2024 17:00:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id sIpWGcnxzGUfMwAAn2gu4w (envelope-from ); Wed, 14 Feb 2024 17:00:57 +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 v9 4/7] mm,page_owner: Implement the tracking of the stacks count Date: Wed, 14 Feb 2024 18:01:54 +0100 Message-ID: <20240214170157.17530-5-osalvador@suse.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214170157.17530-1-osalvador@suse.de> References: <20240214170157.17530-1-osalvador@suse.de> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7C02114003A X-Rspam-User: X-Stat-Signature: ng9ycqfy9pok6nuxeeqtmyycwtyuyfad X-Rspamd-Server: rspam03 X-HE-Tag: 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pAyazrhHNCJuBhDAVr6AI3QgJ/WEzPNbUy5Di9rZy0oTgGMA0CYFOCn8c69X/FDdAKXXZf/JdocngcDHJIAxKXIQ7SAHs8eyAaTKX9dogBkktoae8HKh2MKR92HjQiD6bmZD02nPJ+aN1WxZ9XRgVbFcrgvurPIcrGyul08VE+Oiow0EUstz02xzgurSA== 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: Implement {inc,dec}_stack_record_count() which increments or decrements on respective allocation and free operations, via __reset_page_owner() (free operation) and __set_page_owner() (alloc operation). Newly allocated stack_record structs will be added to the list stack_list via add_stack_record_to_list(). Modifications on the list are protected via a spinlock with irqs disabled, since this code can also be reached from IRQ context. Signed-off-by: Oscar Salvador Reviewed-by: Marco Elver Reviewed-by: Vlastimil Babka --- mm/page_owner.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index 33e342b15d9b..df6a923af5de 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -43,6 +43,7 @@ struct stack { static struct stack dummy_stack; static struct stack failure_stack; static struct stack *stack_list; +static DEFINE_SPINLOCK(stack_list_lock); static bool page_owner_enabled __initdata; DEFINE_STATIC_KEY_FALSE(page_owner_inited); @@ -150,11 +151,68 @@ static noinline depot_stack_handle_t save_stack(gfp_t flags) return handle; } +static void add_stack_record_to_list(struct stack_record *stack_record, + gfp_t gfp_mask) +{ + unsigned long flags; + struct stack *stack; + + /* Filter gfp_mask the same way stackdepot does, for consistency */ + gfp_mask &= ~GFP_ZONEMASK; + gfp_mask &= (GFP_ATOMIC | GFP_KERNEL); + gfp_mask |= __GFP_NOWARN; + + stack = kmalloc(sizeof(*stack), gfp_mask); + if (!stack) + return; + + stack->stack_record = stack_record; + stack->next = NULL; + + spin_lock_irqsave(&stack_list_lock, flags); + stack->next = stack_list; + stack_list = stack; + spin_unlock_irqrestore(&stack_list_lock, flags); +} + +static void inc_stack_record_count(depot_stack_handle_t handle, gfp_t gfp_mask) +{ + struct stack_record *stack_record = __stack_depot_get_stack_record(handle); + + if (!stack_record) + return; + + /* + * New stack_record's that do not use STACK_DEPOT_FLAG_GET start + * with REFCOUNT_SATURATED to catch spurious increments of their + * refcount. + * Since we do not use STACK_DEPOT_FLAG_GET API, let us + * set a refcount of 1 ourselves. + */ + if (refcount_read(&stack_record->count) == REFCOUNT_SATURATED) { + int old = REFCOUNT_SATURATED; + + if (atomic_try_cmpxchg_relaxed(&stack_record->count.refs, &old, 1)) + /* Add the new stack_record to our list */ + add_stack_record_to_list(stack_record, gfp_mask); + } + refcount_inc(&stack_record->count); +} + +static void dec_stack_record_count(depot_stack_handle_t handle) +{ + struct stack_record *stack_record = __stack_depot_get_stack_record(handle); + + if (stack_record) + refcount_dec(&stack_record->count); +} + void __reset_page_owner(struct page *page, unsigned short order) { int i; struct page_ext *page_ext; depot_stack_handle_t handle; + depot_stack_handle_t alloc_handle; struct page_owner *page_owner; u64 free_ts_nsec = local_clock(); @@ -162,17 +220,29 @@ void __reset_page_owner(struct page *page, unsigned short order) if (unlikely(!page_ext)) return; + page_owner = get_page_owner(page_ext); + 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 = get_page_owner(page_ext); page_owner->free_handle = handle; page_owner->free_ts_nsec = free_ts_nsec; page_owner->free_pid = current->pid; page_owner->free_tgid = current->tgid; page_ext = page_ext_next(page_ext); + page_owner = get_page_owner(page_ext); } page_ext_put(page_ext); + if (alloc_handle != early_handle) + /* + * early_handle is being set as a handle for all those + * early allocated pages. See init_pages_in_zone(). + * Since their refcount is not being incremented because + * the machinery is not ready yet, we cannot decrement + * their refcount either. + */ + dec_stack_record_count(alloc_handle); } static inline void __set_page_owner_handle(struct page_ext *page_ext, @@ -214,6 +284,7 @@ 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); + inc_stack_record_count(handle, gfp_mask); } void __set_page_owner_migrate_reason(struct page *page, int reason)