Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1206255rdb; Fri, 9 Feb 2024 13:51:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXKFD7aleH64xgP979otJXxpfJPRiJDglruixHirmJwoG5vxtEr77Rmp6/osVmu8kF9AaiEtd/efB8HXvySa1aDzuZAEsDndxC3QMV+Jw== X-Google-Smtp-Source: AGHT+IE0O3q1S2pcGpxDMNLphU+7mzoRDvYx+Fsqxs/D25s1N//1V0sU1DQfBWTa33W5a8e2NR6T X-Received: by 2002:a81:49c1:0:b0:604:9b28:920a with SMTP id w184-20020a8149c1000000b006049b28920amr617109ywa.0.1707515510533; Fri, 09 Feb 2024 13:51:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707515510; cv=pass; d=google.com; s=arc-20160816; b=MynrVG6nsb8ep5ugxSBvO/yj3TeWzVyOrOmclN3LPJaQQ837lnH0Zq2fEENlmTiAsQ evqI63eBoVYB22A+f8XXxodhLQWsihRsHq4brJF4dXVZIsezAY9eAWWqGZa20ayefxOd /vfNzsIiqh1OmAr05RnBNN6T4z8q01mLimKTm3QMzB9FpiEep3WyRIjSUlfcgPPXRxkQ Iwn9OR7U7/ahn7JlpbFjr5mCoL4bFL2YskjnTsJoRAqWP7iCBSt+UTCgedvWpmDkxa6i t1jV2btl/R9jg2asV/+FCH6YVzK8jhf3/qq5Dx8+zJ/Ukv8KyN56Lwez3GYWKGBjYeji VFtg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-signature:dkim-signature :dkim-signature; bh=DefqWIsK/Bt8MttPzkKY/ykhXHgBYzfo86LWXkmEtbQ=; fh=m6K8CmpiQy8WJFp1Is2CY66cImNzt1uj9UmEgoM23kE=; b=jM7TBTjLlqTHWr8CtXlcjL8/Pbi4W4ttPSElcF4wzu/+pog4GT7/SBiucT9NI9KfAf taYShoUMMRaLy5ZpDr++muHLGWkg7W8O4tgxfUx/OJqcXcowuyDszpATuE5yP/yIg9ey pRcuT+m3WRjdP1p8eAuad3/7WP2kCK4Hshd1pbm0tpWeetwDODQjsrzoA1v6ZOhamOwH Fd+ZZApwF83VgdFSaOkp/3AdaXTIKBsgD0dYuUWOHX57wjmwmSrpzlnkxmRlTpr8EFDJ zPOVpD8asnwFG1InCmpJhbPXSmw2PupODOYZTSe8KNVuzZ6rLFSM/zkORVQRQx8pfLsF 4jPg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=o3Aqh1my; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=o3Aqh1my; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-kernel+bounces-60052-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60052-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de X-Forwarded-Encrypted: i=2; AJvYcCXSLCXlL+7srjbQS87trfNmRyu1dxoIwvYcifib1C1CJ/1BG9NxMWdDLRn5xKgBq2P4sg2SXz+d4KVk3dtiOCApl87cJ1xkgSsOJSL0Kg== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id bm21-20020a05620a199500b0078562f9f819si463263qkb.23.2024.02.09.13.51.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 13:51:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60052-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=o3Aqh1my; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=o3Aqh1my; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-kernel+bounces-60052-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60052-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3D56A1C21C3D for ; Fri, 9 Feb 2024 21:51:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0CF8C20DE3; Fri, 9 Feb 2024 21:51:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="o3Aqh1my"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="IGctspPZ"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="o3Aqh1my"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="IGctspPZ" Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F98363B8 for ; Fri, 9 Feb 2024 21:51:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707515506; cv=none; b=VASdcRhuJEbSLGOIvyL6hkaKBuBQPxZyj/RVl2B2AB2+t1XlMnafALSky9ol7kABTQ76yI1iPEmOT/66pu6avv7BllqA+42yHb7BBjlxYWnbrricAW7h6HipaJDVnMQ9JmrSLB0t263mfuYblNN2X56xyFN7r2+9pcaa65eao48= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707515506; c=relaxed/simple; bh=Pr9D+fptCBeC23igqt5i7qdv9JbM3rvuP78jW1t7QzM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=FzNYMdlJ/xyKfQPDQJI4T8qcGfp9zJkWATZzZBOY52JFiyFg8UXLMFFG2mVQSF9oetcnNrYkqu6ehW+U5j+VywXLPlZq3zKK+DJqtjk7TtvWj+ZdXGOzpoteXeeeJUI4bfePb3meaNd7mJSgCSsUcKIS4i3wU8uRgvX+XawNr44= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=o3Aqh1my; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=IGctspPZ; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=o3Aqh1my; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=IGctspPZ; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [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 6C96D1F834; Fri, 9 Feb 2024 21:51:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707515502; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=DefqWIsK/Bt8MttPzkKY/ykhXHgBYzfo86LWXkmEtbQ=; b=o3Aqh1myufMoF8LFhNC8pbhmUZ6BoHteOhIaZvTfnOnDx9Kwc6i59X07xJvJbCpx180mhD jPhlNvAnXzGUB80ZDX1kQpxt8NvORzZxihZPxzpbxaExyJCUELY3aH1dedqOXUsGTqMrNC BZ/WCd4IhmWoP7olQl5TP6Iz7A4SPFE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707515502; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=DefqWIsK/Bt8MttPzkKY/ykhXHgBYzfo86LWXkmEtbQ=; b=IGctspPZ9dIM6Jy3X942CzbhvGx+EjT+YsV2l4EVuvA5BlcYiuOMGvEKfVItBOJ4TtYRTc +9+hawEQW3nboXBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707515502; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=DefqWIsK/Bt8MttPzkKY/ykhXHgBYzfo86LWXkmEtbQ=; b=o3Aqh1myufMoF8LFhNC8pbhmUZ6BoHteOhIaZvTfnOnDx9Kwc6i59X07xJvJbCpx180mhD jPhlNvAnXzGUB80ZDX1kQpxt8NvORzZxihZPxzpbxaExyJCUELY3aH1dedqOXUsGTqMrNC BZ/WCd4IhmWoP7olQl5TP6Iz7A4SPFE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707515502; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=DefqWIsK/Bt8MttPzkKY/ykhXHgBYzfo86LWXkmEtbQ=; b=IGctspPZ9dIM6Jy3X942CzbhvGx+EjT+YsV2l4EVuvA5BlcYiuOMGvEKfVItBOJ4TtYRTc +9+hawEQW3nboXBA== 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 E6024139E7; Fri, 9 Feb 2024 21:51:41 +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 CfAHNG2exmWybAAAD6G6ig (envelope-from ); Fri, 09 Feb 2024 21:51:41 +0000 Date: Fri, 9 Feb 2024 22:52:48 +0100 From: Oscar Salvador To: Marco Elver Cc: Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Vlastimil Babka , Andrey Konovalov , Alexander Potapenko Subject: Re: [PATCH v7 3/4] mm,page_owner: Display all stacks and their count Message-ID: References: <20240208234539.19113-1-osalvador@suse.de> <20240208234539.19113-4-osalvador@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -4.30 X-Spamd-Result: default: False [-4.30 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_LONG(-1.00)[-1.000]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; RCPT_COUNT_SEVEN(0.00)[8]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_CC(0.00)[linux-foundation.org,vger.kernel.org,kvack.org,suse.com,suse.cz,gmail.com,google.com]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Flag: NO On Fri, Feb 09, 2024 at 09:00:40AM +0100, Marco Elver wrote: > > +/** > > + * stack_depot_get_next_stack - Returns all stacks, one at a time > > "Returns all stack_records" to be clear that this is returning the struct. Fixed. > > > + * > > + * @table: Current table we are checking > > + * @bucket: Current bucket we are checking > > + * @last_found: Last stack that was found > > + * > > + * This function finds first a non-empty bucket and returns the first stack > > + * stored in it. On consequent calls, it walks the bucket to see whether > > + * it contains more stacks. > > + * Once we have walked all the stacks in a bucket, we check > > + * the next one, and we repeat the same steps until we have checked all of them > > I think for this function it's important to say that no entry returned > from this function can be evicted. > > I.e. the easiest way to ensure this is that the caller makes sure the > entries returned are never passed to stack_depot_put() - which is > certainly the case for your usecase because you do not use > stack_depot_put(). > > > + * Return: A pointer a to stack_record struct, or NULL when we have walked all > > + * buckets. > > + */ > > +struct stack_record *stack_depot_get_next_stack(unsigned long *table, > > To keep consistent, I'd also call this > __stack_depot_get_next_stack_record(), so that we're clear this is > more of an internal function not for general usage. > > > + struct list_head **bucket, > > + struct stack_record **last_found); > > + > > /** > > * stack_depot_fetch - Fetch a stack trace from stack depot > > * > > diff --git a/lib/stackdepot.c b/lib/stackdepot.c > > index 197c355601f9..107bd0174cd6 100644 > > --- a/lib/stackdepot.c > > +++ b/lib/stackdepot.c > > @@ -782,6 +782,52 @@ unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle) > > } > > EXPORT_SYMBOL(stack_depot_get_extra_bits); > > > > +struct stack_record *stack_depot_get_next_stack(unsigned long *table, > > + struct list_head **curr_bucket, > > + struct stack_record **last_found) > > +{ > > + struct list_head *bucket = *curr_bucket; > > + unsigned long nr_table = *table; > > + struct stack_record *found = NULL; > > + unsigned long stack_table_entries = stack_hash_mask + 1; > > + > > + rcu_read_lock_sched_notrace(); > > We are returning pointers to stack_records out of the RCU-read > critical section, which are then later used to continue the iteration. > list_for_each_entry_continue_rcu() says this is fine if "... you held > some sort of non-RCU reference (such as a reference count) ...". > Updating the function's documentation to say none of these entries can > be evicted via a stack_depot_put() is required. Thinking about it some more, I think I made a mistake: I am walking all buckets, and within those buckets there are not only page_owner stack_records, which means that I could return a stack_record from e.g: KASAN (which I think can evict stack_records) and then everything goes off the rails. Which means I cannot walk the buckets like that. Actually, I think that having something like the following struct list_stack_records { struct stack_record *stack; struct list_stack_records *next; } in page_owner would make sense. Then the only thing I would have to do is to add a new record on every new stack_record, and then I could just walk the list like a linked list. Which means that the function stack_depot_get_next_stack() could be killed because everything would happen in page_owner code. e.g: static void inc_stack_record_count(depot_stack_handle_t handle) { struct stack_record *stack = __stack_depot_get_stack_record(handle); if (stack) { /* * 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,PUT} API, let us * set a refcount of 1 ourselves. */ if (refcount_read(&stack->count) == REFCOUNT_SATURATED) { refcount_set(&stack->count, 1); add_new_stack_record_into_the_list(stack) } refcount_inc(&stack->count); } } and then just walk the list_stack_records list whenever we want to show the stacktraces and their counting. I think that overall this approach is cleaner and safer. -- Oscar Salvador SUSE Labs