From patchwork Thu Apr 4 07:06:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13617328 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 4E8D6CD1284 for ; Thu, 4 Apr 2024 07:05:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACF2D6B0087; Thu, 4 Apr 2024 03:05:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A7F366B0089; Thu, 4 Apr 2024 03:05:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92B3D6B0087; Thu, 4 Apr 2024 03:05:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 71B616B0087 for ; Thu, 4 Apr 2024 03:05:53 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E790240F9F for ; Thu, 4 Apr 2024 07:05:52 +0000 (UTC) X-FDA: 81970964544.19.BAAD372 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf11.hostedemail.com (Postfix) with ESMTP id 0638640005 for ; Thu, 4 Apr 2024 07:05:50 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=b+JDHNpN; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=l19wa1e0; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf11.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712214351; 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=L9csULMalRTJ5bwNt2srfmcGplNPvj1BEqpu1yrZ8PM=; b=Q8mKySc2AX+gbgmGrdVoP7W1eQprp1Hsc9dOYm5eeu+b+l0TcDssycKl9DRaP/HTRZClP+ NRA1bs3rc3Xv4J/o/pffDm4lBRPxfWeHzveGPp9sRGQ8rhXC6pZxhuk7ZXSNBwzMyxWdLY 8jfI0bI4G2W2MKoXPNU1exZ8vzvmBgA= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=b+JDHNpN; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=l19wa1e0; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf11.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712214351; a=rsa-sha256; cv=none; b=Wf1POLCJTh12rwqDaCZ+cf8UdYXBwbEycwpL7j2R1U/gewUeONYJZbyHM2zceojp5xzUOk zjQyuvShXuDpIJMexU+BJMUJEysX4o1YxOggap8HmZ7kRQUsCk9vaAhp70L6umIThJ5gW/ plWzWC5M6PIMNiEi5kc+C6+JTnm9kxc= 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-out1.suse.de (Postfix) with ESMTPS id 7E193378EC; Thu, 4 Apr 2024 07:05:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1712214348; 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=L9csULMalRTJ5bwNt2srfmcGplNPvj1BEqpu1yrZ8PM=; b=b+JDHNpNj2CVttstyNqm814TITnISk4F7GULwqe+fIIP1mtyQ47GBToNvgBAC7H8/oGTfi Toh+8PzgcYrloVYdeg3thh3Tk5TlB1cInGEN2hML821UWErMTCw3kddxvluc+w0fX6Ra/B WPWbyqmxIhELcQ6nWa/BYZzbpHJUyeA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1712214348; 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=L9csULMalRTJ5bwNt2srfmcGplNPvj1BEqpu1yrZ8PM=; b=l19wa1e0HyXrXtipe5A2g71Pqed7khVCl1p+XMHjKo0UB/mwOIA+Q5UR0iRhXBjIjgaZXD VZRaK1iWwefbCQAQ== 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 E0607139E8; Thu, 4 Apr 2024 07:05:47 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap2.dmz-prg2.suse.org with ESMTPSA id SAvZMktRDmY6cgAAn2gu4w (envelope-from ); Thu, 04 Apr 2024 07:05: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 , Alexandre Ghiti , Oscar Salvador Subject: [PATCH v4 1/4] mm,page_owner: Update metadata for tail pages Date: Thu, 4 Apr 2024 09:06:59 +0200 Message-ID: <20240404070702.2744-2-osalvador@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240404070702.2744-1-osalvador@suse.de> References: <20240404070702.2744-1-osalvador@suse.de> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0638640005 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: gpf6cyexmafq64t4r7jzmbrrbzhgzzww X-HE-Tag: 1712214350-172461 X-HE-Meta: U2FsdGVkX1/z96bYW3o134C1xK4oLDT0ec5aMh+I40vvXHBXkO5JsIJrAJDWXOodjMZE+guHXDI1odxEphhRmElTtKD9cdtmrdO2BurG/y3lEygxMLrpmkNJC530XufmYE0QxW6eedJipAyo079OProbE5EfKGIKhFt6IFV8RifaX3anyoA0d5RXARY2gyr+JIja7CXtcdG4Szd9y5qBnt+ja2B8S/fed/yuOlhhrfK9STE6gSfw41fyJW4uoz1SafiEc+xaCQdhLu/VplDDOMWq/LBXE804rhrje7WF8BVTr6n/1pmSpmSdCvNfs+576dkCzyb/ETG/12jyALY/uQ+ZdVVGFf32hvjY1wErr4F9UD6icaJY5aM603pmEFSNFxwzSX9k1/vERLZTL6hqJB73gKIP2WnYaVJThvk99FtPVu0eSB5DeRywbs8wgRGNvpl723SlNFURZlxu1HfilqUDGymWpNp7T0AgZFkiy6ER53KD3UzcloWzkL3q718dcnRi4Q+f+d8LXU0m57NtXFeElJ70MNV69ksTpvJFjfG2kvHeQCjpge12lpQPbQUTkqCmXM8lF24CFSl67BC6RLg/WRxPz5MhYD7+X8J2OPSOesKF9/ACUYB0gt/AFUBMCcfFOeDOxti6hAVnVAIjs3D7LF/ubfg7CLR6dwU9ZPc00+Mvp+4BeRWg3yn545+01h7PqNJCAIK7RxbhnbQHkTWN+m3XIce7e7MxtE36icR8cQ9lIwiNJq81VSIX31vdNRxErGPjgmx+Ak8VS2vp7HvdWlqTiId+JBdae6F3MY+mZFxg5R47fW1FEYsDEquFSBxJd850NffIuEqcdeWbicrD1d28T4snBHqRFZOqhR89iwDUB2kjkFCM0jeOvWydg0gCERNUSqToZMHabuCb6k1eA1M6nXLIJJHkdd8fNDO2hLR1cRlPmS6XQkxswEGHdDSsrojF8hLEPN8DAKV GxumwFzG g4RFRS1nZwNKLl5WYVueZ7RVP3Q/58JXIZTEk 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: __set_page_owner_handle() and __reset_page_owner() update the metadata of all pages when the page is of a higher-order, but we miss to do the same when the pages are migrated. __folio_copy_owner() only updates the metadata of the head page, meaning that the information stored in the first page and the tail pages will not match. Strictly speaking that is not a big problem because 1) we do not print tail pages and 2) upon splitting all tail pages will inherit the metadata of the head page, but it is better to have all metadata in check should there be any problem, so it can ease debugging. For that purpose, a couple of helpers are created __update_page_owner_handle() which updates the metadata on allocation, and __update_page_owner_free_handle() which does the same when the page is freed. __folio_copy_owner() will make use of both as it needs to entirely replace the page_owner metadata for the new page. Signed-off-by: Oscar Salvador Reviewed-by: Vlastimil Babka --- mm/page_owner.c | 137 ++++++++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 63 deletions(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index d17d1351ec84..52d1ced0b57f 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -228,9 +228,58 @@ static void dec_stack_record_count(depot_stack_handle_t handle) refcount_dec(&stack_record->count); } -void __reset_page_owner(struct page *page, unsigned short order) +static inline void __update_page_owner_handle(struct page_ext *page_ext, + depot_stack_handle_t handle, + unsigned short order, + gfp_t gfp_mask, + short last_migrate_reason, u64 ts_nsec, + pid_t pid, pid_t tgid, char *comm) { int i; + struct page_owner *page_owner; + + for (i = 0; i < (1 << order); i++) { + page_owner = get_page_owner(page_ext); + page_owner->handle = handle; + page_owner->order = order; + page_owner->gfp_mask = gfp_mask; + page_owner->last_migrate_reason = last_migrate_reason; + page_owner->pid = pid; + page_owner->tgid = tgid; + page_owner->ts_nsec = ts_nsec; + strscpy(page_owner->comm, comm, + sizeof(page_owner->comm)); + __set_bit(PAGE_EXT_OWNER, &page_ext->flags); + __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); + page_ext = page_ext_next(page_ext); + } +} + +static inline void __update_page_owner_free_handle(struct page_ext *page_ext, + depot_stack_handle_t handle, + unsigned short order, + pid_t pid, pid_t tgid, + u64 free_ts_nsec) +{ + int i; + struct page_owner *page_owner; + + for (i = 0; i < (1 << order); i++) { + page_owner = get_page_owner(page_ext); + /* Only __reset_page_owner() wants to clear the bit */ + if (handle) { + __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); + 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); + } +} + +void __reset_page_owner(struct page *page, unsigned short order) +{ struct page_ext *page_ext; depot_stack_handle_t handle; depot_stack_handle_t alloc_handle; @@ -245,16 +294,10 @@ 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; - 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); - } + __update_page_owner_free_handle(page_ext, handle, order, current->pid, + current->tgid, free_ts_nsec); page_ext_put(page_ext); + if (alloc_handle != early_handle) /* * early_handle is being set as a handle for all those @@ -266,36 +309,11 @@ void __reset_page_owner(struct page *page, unsigned short order) dec_stack_record_count(alloc_handle); } -static inline void __set_page_owner_handle(struct page_ext *page_ext, - depot_stack_handle_t handle, - unsigned short order, gfp_t gfp_mask) -{ - struct page_owner *page_owner; - int i; - u64 ts_nsec = local_clock(); - - for (i = 0; i < (1 << order); i++) { - page_owner = get_page_owner(page_ext); - page_owner->handle = handle; - page_owner->order = order; - page_owner->gfp_mask = gfp_mask; - page_owner->last_migrate_reason = -1; - page_owner->pid = current->pid; - page_owner->tgid = current->tgid; - page_owner->ts_nsec = ts_nsec; - strscpy(page_owner->comm, current->comm, - sizeof(page_owner->comm)); - __set_bit(PAGE_EXT_OWNER, &page_ext->flags); - __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); - - page_ext = page_ext_next(page_ext); - } -} - noinline void __set_page_owner(struct page *page, unsigned short order, gfp_t gfp_mask) { struct page_ext *page_ext; + u64 ts_nsec = local_clock(); depot_stack_handle_t handle; handle = save_stack(gfp_mask); @@ -303,7 +321,9 @@ noinline void __set_page_owner(struct page *page, unsigned short order, page_ext = page_ext_get(page); if (unlikely(!page_ext)) return; - __set_page_owner_handle(page_ext, handle, order, gfp_mask); + __update_page_owner_handle(page_ext, handle, order, gfp_mask, -1, + current->pid, current->tgid, ts_nsec, + current->comm); page_ext_put(page_ext); inc_stack_record_count(handle, gfp_mask); } @@ -342,7 +362,7 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old) { struct page_ext *old_ext; struct page_ext *new_ext; - struct page_owner *old_page_owner, *new_page_owner; + struct page_owner *old_page_owner; old_ext = page_ext_get(&old->page); if (unlikely(!old_ext)) @@ -355,31 +375,21 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old) } old_page_owner = get_page_owner(old_ext); - new_page_owner = get_page_owner(new_ext); - new_page_owner->order = old_page_owner->order; - new_page_owner->gfp_mask = old_page_owner->gfp_mask; - new_page_owner->last_migrate_reason = - old_page_owner->last_migrate_reason; - new_page_owner->handle = old_page_owner->handle; - new_page_owner->pid = old_page_owner->pid; - new_page_owner->tgid = old_page_owner->tgid; - new_page_owner->free_pid = old_page_owner->free_pid; - new_page_owner->free_tgid = old_page_owner->free_tgid; - new_page_owner->ts_nsec = old_page_owner->ts_nsec; - new_page_owner->free_ts_nsec = old_page_owner->ts_nsec; - strcpy(new_page_owner->comm, old_page_owner->comm); - + __update_page_owner_handle(new_ext, old_page_owner->handle, + old_page_owner->order, old_page_owner->gfp_mask, + old_page_owner->last_migrate_reason, + old_page_owner->ts_nsec, old_page_owner->pid, + old_page_owner->tgid, old_page_owner->comm); /* - * We don't clear the bit on the old folio as it's going to be freed - * after migration. Until then, the info can be useful in case of - * a bug, and the overall stats will be off a bit only temporarily. - * Also, migrate_misplaced_transhuge_page() can still fail the - * migration and then we want the old folio to retain the info. But - * in that case we also don't need to explicitly clear the info from - * the new page, which will be freed. + * Do not proactively clear PAGE_EXT_OWNER{_ALLOCATED} bits as the folio + * will be freed after migration. Keep them until then as they may be + * useful. */ - __set_bit(PAGE_EXT_OWNER, &new_ext->flags); - __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags); + __update_page_owner_free_handle(new_ext, 0, old_page_owner->order, + old_page_owner->free_pid, + old_page_owner->free_tgid, + old_page_owner->free_ts_nsec); + page_ext_put(new_ext); page_ext_put(old_ext); } @@ -787,8 +797,9 @@ static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) goto ext_put_continue; /* Found early allocated page */ - __set_page_owner_handle(page_ext, early_handle, - 0, 0); + __update_page_owner_handle(page_ext, early_handle, 0, 0, + -1, local_clock(), current->pid, + current->tgid, current->comm); count++; ext_put_continue: page_ext_put(page_ext); From patchwork Thu Apr 4 07:07:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13617329 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 B8E05CD1292 for ; Thu, 4 Apr 2024 07:05:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E5666B0088; Thu, 4 Apr 2024 03:05:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 094EA6B0089; Thu, 4 Apr 2024 03:05:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E298E6B008A; Thu, 4 Apr 2024 03:05:53 -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 C1B116B0088 for ; Thu, 4 Apr 2024 03:05:53 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7BD07140152 for ; Thu, 4 Apr 2024 07:05:53 +0000 (UTC) X-FDA: 81970964586.07.7F4181E Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf03.hostedemail.com (Postfix) with ESMTP id 6730820010 for ; Thu, 4 Apr 2024 07:05:51 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=BHMMgYkU; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=DMflKcu2; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf03.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712214351; 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=ghkXx40nWEPSM9bclOWwsS092PohOPXWVqRF6XEBYQE=; b=liWmXOhXwaog9ZkllomsOgy3rsM/lYDGTuA14lPVan5gaizjNhuIigntLKTEt5gBKibQbq cmi0exznF1A3P3L2Ofq4ELCnnsmVYskh+Uh7GAFWk5V+3kDhy8Vm2OBS+Kw/3anqpAkBvt RZlLRMN1bPHFTIrvYNp6RNGUMP0QhEI= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=BHMMgYkU; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=DMflKcu2; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf03.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712214351; a=rsa-sha256; cv=none; b=4LjoyoQUVnbir7WskOqASuLib877bM9pNIJ79aXhEosnvwmQGvuJQALDXkAtrxT7PuhWnX 4rmLJW+WZXTMZQzWz4h+X8LyAP8JRVGnv2hO7siD1MvDtkTe5xszhXMSYoZRi96mjR/2Lj lasSJLzPXtJd9vS6SOT2u6KTnL6VXpU= 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 CF8805D673; Thu, 4 Apr 2024 07:05:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1712214349; 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=ghkXx40nWEPSM9bclOWwsS092PohOPXWVqRF6XEBYQE=; b=BHMMgYkUiju095r33Rm4aFYQVJAwhFlIDN4uo666GZf1EZGTkXYRxMz5A3BQTzfRhJ8oNb CtTBgFODKCu7/cFXbTZUQheOsQIhLtf4bHs5qlDS6ulVH5UdWyu5SiRrmOxqe0qk6e8ihl +wYgMi7rurqELUnAhNYcjr67THsJgts= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1712214349; 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=ghkXx40nWEPSM9bclOWwsS092PohOPXWVqRF6XEBYQE=; b=DMflKcu20nVPELyns9/pBRYiLwB8A3fIMLkoYrZfn85iYZ6nYiNRUcVI529KzKH7zGBig0 jJO3FfEYbmXXKABg== 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 370B413A91; Thu, 4 Apr 2024 07:05:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap2.dmz-prg2.suse.org with ESMTPSA id wAuiCk1RDmY6cgAAn2gu4w (envelope-from ); Thu, 04 Apr 2024 07:05:49 +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 , Alexandre Ghiti , Oscar Salvador , syzbot+41bbfdb8d41003d12c0f@syzkaller.appspotmail.com Subject: [PATCH v4 2/4] mm,page_owner: Fix refcount imbalance Date: Thu, 4 Apr 2024 09:07:00 +0200 Message-ID: <20240404070702.2744-3-osalvador@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240404070702.2744-1-osalvador@suse.de> References: <20240404070702.2744-1-osalvador@suse.de> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 6730820010 X-Stat-Signature: fenn8zhcj3rim7udy9psgk45d9xhrruq X-HE-Tag: 1712214351-906268 X-HE-Meta: U2FsdGVkX18UVF2nZlQXIRvnGaHEo8Z0+wK7n+yrjAmAIqTu/OUCTQt3nHT9a0ZJJa8AnbNq3JEYHUBzPOa9eOcbzm7qFvlZMDHmjm3nJlI7zakkCBW8RPSgSKEaBw86aReir2qCD2TrhwpVsIJTPnA5ss8RvSI1oSRxOO31DbKv51g5hJ3meyz2FG2sHdhFVDcugR6EXqGAfOu1kbncKZka6ZtV4rLY3Gz6KtWMRekJJ76+L2C1zNUbKo00P5nf3CmyKPB0CKe7nYmfBJbmSyq/f/5XVDD6iNhP8zePhJQJOEXFrykIjA9ODGjIJPLsp4DqDmo0gtWzOPa0Z0O23oLJy8ET3nt4CBMbd1jXLWAVEVL1iQGd7kalxfnTFy/wYgELUp8eK2TlN3tckJ7Zo30FCfrEvBbXcIBJbhQrJp+gnNDiNfe2FyyiUevnJRGtiWPNcJ9cM9QqQiESogTov/fhSgvox1E8HWER8sRxdwe6xgTz5brGoRhDtNAlVvfyfDGnBFuKlNpJEeK3YVzNqtpdR1XYT2rAOgih7WViVDKgJk+v2qEHyNuF08DKpQ5hKxf5UTRJ5C0R8L3orJMbFjlH1gqLx/jABZGLTfmfEe/vXxV2eSOX3APddCMrm1tMp4wd0F8vs6BUArA0AISj3FACqmmbOtbr3mnfJU/g3lty+XOlOZPF/J2Wfxe4RtI/9C2NAnVnrA6Gxqdt00uovdgrZy5loElYRZWjXWwtcFHXLNlqiG52/H+bdqNGD7hBwGrKRoomvZlR0Dk1ASi7TGrAfiaSVYBrdKVeZwRyjBsvjvut7VYAiFboM7WHWSbKam4WFCzyWUfcXFV5Odog+maZcSCTnOLuZXUHSQs/EqILHOzdix/drz2pX0oBYh/ymHntK7JvbP6Nc4hP2Dnli5AHxkePeJNoC9mlUcb5K9rCoC5JvEO4Ld0c8EbIN5c1MVF74vFHSNSnPymkPOE 0DzDVsMX X7uOge468nt3xg7ZmRuyl1wVDFX/HUa0Vq3ai2zi4du8w3tpFR6kuUDxjg2ZZy8ZNPvrruVWX4ewRhThSdKJQiMO1cvVV9vm1SpMlCfkIUqcKF9hQ4HcFC8MXFxxCeEHMSX8tClXlAByRLV436vmB1h4RLMg7gVCRPALoYz5GeVPuCq/PiWxAzx5Ow/ymH62bw6Y694Zr99Kah9JTz87QAUc/+Ow0NE43D+ha0sz1gsjQRS4F6lLMCcs/6u9CWMwjZPwWv0JEzTezzdddk8mPpfCPyiU8q7GMixbdIcIcUfPZpiiVI+h4JtwOdwI277p4ss1qG9epIEnTFXpFiTwOhlL1fa0Ecez3or8a99FImxBYZ/w+CjKKDxXu81V5rG6crOD56aRrfINwe304AOnk9G+2mPraLHwN/JKyR45DPdB/J9eGmoZ5/8Uor3X5y4ovJSk5AWL06ye3Hl/DfJsLlnm3AIjbxgWAt3aY8jSI41i6AAe4Jz69dnJcjQDE+VECKhuFYXJhWU/zpCL1k2Xeu+l6Jdqh1B4bWSsJ91NaNHDygRyMXuMLVcOLpQ9Ua89f69HB3oDR0kE25qc= 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: Current code does not contemplate scenarios were an allocation and free operation on the same pages do not handle it in the same amount at once. To give an example, page_alloc_exact(), where we will allocate a page of enough order to stafisfy the size request, but we will free the remainings right away. In the above example, we will increment the stack_record refcount only once, but we will decrease it the same number of times as number of unused pages we have to free. This will lead to a warning because of refcount imbalance. Fix this by recording the number of base pages in the refcount field. Reported-by: syzbot+41bbfdb8d41003d12c0f@syzkaller.appspotmail.com Closes: https://lore.kernel.org/linux-mm/00000000000090e8ff0613eda0e5@google.com Fixes: 217b2119b9e2 ("mm,page_owner: implement the tracking of the stacks count") Signed-off-by: Oscar Salvador Reviewed-by: Vlastimil Babka Tested-by: Alexandre Ghiti --- Documentation/mm/page_owner.rst | 73 +++++++++++++++++---------------- mm/page_owner.c | 34 ++++++++------- 2 files changed, 58 insertions(+), 49 deletions(-) diff --git a/Documentation/mm/page_owner.rst b/Documentation/mm/page_owner.rst index 0d0334cd5179..3a45a20fc05a 100644 --- a/Documentation/mm/page_owner.rst +++ b/Documentation/mm/page_owner.rst @@ -24,10 +24,10 @@ fragmentation statistics can be obtained through gfp flag information of each page. It is already implemented and activated if page owner is enabled. Other usages are more than welcome. -It can also be used to show all the stacks and their outstanding -allocations, which gives us a quick overview of where the memory is going -without the need to screen through all the pages and match the allocation -and free operation. +It can also be used to show all the stacks and their current number of +allocated base pages, which gives us a quick overview of where the memory +is going without the need to screen through all the pages and match the +allocation and free operation. page owner is disabled by default. So, if you'd like to use it, you need to add "page_owner=on" to your boot cmdline. If the kernel is built @@ -75,42 +75,45 @@ Usage cat /sys/kernel/debug/page_owner_stacks/show_stacks > stacks.txt cat stacks.txt - prep_new_page+0xa9/0x120 - get_page_from_freelist+0x7e6/0x2140 - __alloc_pages+0x18a/0x370 - new_slab+0xc8/0x580 - ___slab_alloc+0x1f2/0xaf0 - __slab_alloc.isra.86+0x22/0x40 - kmem_cache_alloc+0x31b/0x350 - __khugepaged_enter+0x39/0x100 - dup_mmap+0x1c7/0x5ce - copy_process+0x1afe/0x1c90 - kernel_clone+0x9a/0x3c0 - __do_sys_clone+0x66/0x90 - do_syscall_64+0x7f/0x160 - entry_SYSCALL_64_after_hwframe+0x6c/0x74 - stack_count: 234 + post_alloc_hook+0x177/0x1a0 + get_page_from_freelist+0xd01/0xd80 + __alloc_pages+0x39e/0x7e0 + allocate_slab+0xbc/0x3f0 + ___slab_alloc+0x528/0x8a0 + kmem_cache_alloc+0x224/0x3b0 + sk_prot_alloc+0x58/0x1a0 + sk_alloc+0x32/0x4f0 + inet_create+0x427/0xb50 + __sock_create+0x2e4/0x650 + inet_ctl_sock_create+0x30/0x180 + igmp_net_init+0xc1/0x130 + ops_init+0x167/0x410 + setup_net+0x304/0xa60 + copy_net_ns+0x29b/0x4a0 + create_new_namespaces+0x4a1/0x820 + nr_base_pages: 16 ... ... echo 7000 > /sys/kernel/debug/page_owner_stacks/count_threshold cat /sys/kernel/debug/page_owner_stacks/show_stacks> stacks_7000.txt cat stacks_7000.txt - prep_new_page+0xa9/0x120 - get_page_from_freelist+0x7e6/0x2140 - __alloc_pages+0x18a/0x370 - alloc_pages_mpol+0xdf/0x1e0 - folio_alloc+0x14/0x50 - filemap_alloc_folio+0xb0/0x100 - page_cache_ra_unbounded+0x97/0x180 - filemap_fault+0x4b4/0x1200 - __do_fault+0x2d/0x110 - do_pte_missing+0x4b0/0xa30 - __handle_mm_fault+0x7fa/0xb70 - handle_mm_fault+0x125/0x300 - do_user_addr_fault+0x3c9/0x840 - exc_page_fault+0x68/0x150 - asm_exc_page_fault+0x22/0x30 - stack_count: 8248 + post_alloc_hook+0x177/0x1a0 + get_page_from_freelist+0xd01/0xd80 + __alloc_pages+0x39e/0x7e0 + alloc_pages_mpol+0x22e/0x490 + folio_alloc+0xd5/0x110 + filemap_alloc_folio+0x78/0x230 + page_cache_ra_order+0x287/0x6f0 + filemap_get_pages+0x517/0x1160 + filemap_read+0x304/0x9f0 + xfs_file_buffered_read+0xe6/0x1d0 [xfs] + xfs_file_read_iter+0x1f0/0x380 [xfs] + __kernel_read+0x3b9/0x730 + kernel_read_file+0x309/0x4d0 + __do_sys_finit_module+0x381/0x730 + do_syscall_64+0x8d/0x150 + entry_SYSCALL_64_after_hwframe+0x62/0x6a + nr_base_pages: 20824 ... cat /sys/kernel/debug/page_owner > page_owner_full.txt diff --git a/mm/page_owner.c b/mm/page_owner.c index 52d1ced0b57f..5df0d6892bdc 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -196,7 +196,8 @@ static void add_stack_record_to_list(struct stack_record *stack_record, spin_unlock_irqrestore(&stack_list_lock, flags); } -static void inc_stack_record_count(depot_stack_handle_t handle, gfp_t gfp_mask) +static void inc_stack_record_count(depot_stack_handle_t handle, gfp_t gfp_mask, + int nr_base_pages) { struct stack_record *stack_record = __stack_depot_get_stack_record(handle); @@ -217,15 +218,20 @@ static void inc_stack_record_count(depot_stack_handle_t handle, gfp_t gfp_mask) /* Add the new stack_record to our list */ add_stack_record_to_list(stack_record, gfp_mask); } - refcount_inc(&stack_record->count); + refcount_add(nr_base_pages, &stack_record->count); } -static void dec_stack_record_count(depot_stack_handle_t handle) +static void dec_stack_record_count(depot_stack_handle_t handle, + int nr_base_pages) { struct stack_record *stack_record = __stack_depot_get_stack_record(handle); - if (stack_record) - refcount_dec(&stack_record->count); + if (!stack_record) + return; + + if (refcount_sub_and_test(nr_base_pages, &stack_record->count)) + pr_warn("%s: refcount went to 0 for %u handle\n", __func__, + handle); } static inline void __update_page_owner_handle(struct page_ext *page_ext, @@ -306,7 +312,7 @@ void __reset_page_owner(struct page *page, unsigned short order) * the machinery is not ready yet, we cannot decrement * their refcount either. */ - dec_stack_record_count(alloc_handle); + dec_stack_record_count(alloc_handle, 1 << order); } noinline void __set_page_owner(struct page *page, unsigned short order, @@ -325,7 +331,7 @@ noinline void __set_page_owner(struct page *page, unsigned short order, current->pid, current->tgid, ts_nsec, current->comm); page_ext_put(page_ext); - inc_stack_record_count(handle, gfp_mask); + inc_stack_record_count(handle, gfp_mask, 1 << order); } void __set_page_owner_migrate_reason(struct page *page, int reason) @@ -872,11 +878,11 @@ static void *stack_next(struct seq_file *m, void *v, loff_t *ppos) return stack; } -static unsigned long page_owner_stack_threshold; +static unsigned long page_owner_pages_threshold; static int stack_print(struct seq_file *m, void *v) { - int i, stack_count; + int i, nr_base_pages; struct stack *stack = v; unsigned long *entries; unsigned long nr_entries; @@ -887,14 +893,14 @@ static int stack_print(struct seq_file *m, void *v) nr_entries = stack_record->size; entries = stack_record->entries; - stack_count = refcount_read(&stack_record->count) - 1; + nr_base_pages = refcount_read(&stack_record->count) - 1; - if (stack_count < 1 || stack_count < page_owner_stack_threshold) + if (nr_base_pages < 1 || nr_base_pages < page_owner_pages_threshold) return 0; for (i = 0; i < nr_entries; i++) seq_printf(m, " %pS\n", (void *)entries[i]); - seq_printf(m, "stack_count: %d\n\n", stack_count); + seq_printf(m, "nr_base_pages: %d\n\n", nr_base_pages); return 0; } @@ -924,13 +930,13 @@ static const struct file_operations page_owner_stack_operations = { static int page_owner_threshold_get(void *data, u64 *val) { - *val = READ_ONCE(page_owner_stack_threshold); + *val = READ_ONCE(page_owner_pages_threshold); return 0; } static int page_owner_threshold_set(void *data, u64 val) { - WRITE_ONCE(page_owner_stack_threshold, val); + WRITE_ONCE(page_owner_pages_threshold, val); return 0; } From patchwork Thu Apr 4 07:07:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13617330 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 A6284CD1284 for ; Thu, 4 Apr 2024 07:06:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A37F6B0089; Thu, 4 Apr 2024 03:05:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 354156B008A; Thu, 4 Apr 2024 03:05:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F46E6B008C; Thu, 4 Apr 2024 03:05:55 -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 E75E36B0089 for ; Thu, 4 Apr 2024 03:05:54 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7EBC4140ECA for ; Thu, 4 Apr 2024 07:05:54 +0000 (UTC) X-FDA: 81970964628.20.6B45F71 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf27.hostedemail.com (Postfix) with ESMTP id 714BF4000E for ; Thu, 4 Apr 2024 07:05:52 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=vqzIj68i; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=EdzULppk; spf=pass (imf27.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 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=1712214352; 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=u8WcJoZIAuccn6bS5H52hIFrTVidppTfRzzAEdTNAso=; b=NTrxShijoj+ce6fi+1BO9ejMYtH0HXPvHJZ3/4knXEjxdEesFh1AE0XwG2BMwYBRfJl9rC LzxXqXtXotuTKhPbIE6W0BAdc4zNyQx2emMSndQoWi1X253t/+z4RlNkeXzemglEgkGILM oKI/hw9Uex0GMbxRnBPbABJfSgZ1zhU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712214352; a=rsa-sha256; cv=none; b=idKVo1njZ0S0xPBss+fC6FW6DXi3WUHn1GW1RWs+3xgOf7I345h8jIO9PFR+YfEuTg5rBC Cbc9P0qy/mnifRy6XutaaDN/zsRzejGp8wVG0PugCgX87qAMj9QLjGw2n5KfJ4Zsct8oJb eFymCC3AIFZbUIECGLu/4LPw9j+oEyI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=vqzIj68i; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=EdzULppk; spf=pass (imf27.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 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-out1.suse.de (Postfix) with ESMTPS id 2766F378EA; Thu, 4 Apr 2024 07:05:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1712214351; 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=u8WcJoZIAuccn6bS5H52hIFrTVidppTfRzzAEdTNAso=; b=vqzIj68iArnn+jsBa8iE51o8g82HwP3tf58Jqo9Y1QMYTKq2ADZ9BtLOfdwVq3H1Zj/rOk ABYB4xBZ18nMrmC9evEUEUGLraGaY2LAegAdqp83pZzId2i8HXmie44k0kg4ChyXiegCN3 ULyIg4hpges6VTrSCOUJTS/KUaeDNLE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1712214351; 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=u8WcJoZIAuccn6bS5H52hIFrTVidppTfRzzAEdTNAso=; b=EdzULppkr+ztcsNQp/E7F9+/5X5wb36VwFC+Yv41g7GfIyiAb7myRXX/2cisk7VhtnjoI6 qzePAph+8U8gttCQ== 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 919BA139E8; Thu, 4 Apr 2024 07:05:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap2.dmz-prg2.suse.org with ESMTPSA id yCTjIE5RDmY6cgAAn2gu4w (envelope-from ); Thu, 04 Apr 2024 07:05:50 +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 , Alexandre Ghiti , Oscar Salvador Subject: [PATCH v4 3/4] mm,page_owner: Fix accounting of pages when migrating Date: Thu, 4 Apr 2024 09:07:01 +0200 Message-ID: <20240404070702.2744-4-osalvador@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240404070702.2744-1-osalvador@suse.de> References: <20240404070702.2744-1-osalvador@suse.de> MIME-Version: 1.0 X-Stat-Signature: p63xxezm5fhq5gjitkynfwbpwyhwygpk X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 714BF4000E X-Rspam-User: X-HE-Tag: 1712214352-676438 X-HE-Meta: U2FsdGVkX1+dQiZE6u4FrjxZ7EQXlRvG/ldzLiz9RYYne7mkKT8I103wJ3QhLA+ATLn6u/7RFp80/fas3piu720U/aBqYXOVB6iNCcR4HAZcRzykeRWOj7TfRQErST6AftI3aulQhxU33uHcDK6TIjs930LweNzGneMx3OiIZabEMTJNvdCwYtFd7Tz3W6uKQgpxPlENvgXtoQePEWBZMixz6Ofm8T4gl8V/6DbJhuXfrTphyEH8922RtpLZZ4QrlAFL4W3m8AxETc4ftKPADp2+Xh7tuU88JxD59lPRyaOU6OujPVSksnGjWvRYq2T/6/IkRLDpKXP0SXbB718n9sHI+kdNCACo+mVgOl5bAw75H421xzKZeuq58Kt/LfL11raHFUfkJmNU2+ZViwp+ZkTUszP83gDbPMufBPJ/w+33RqIWmTfstV9VYtySIBEFTvSNosYAdM/ym4aU3RZz/ZKZ+yR/Ii8M6sYES9twTJwthNzIZvuWMJgJoTVn1RgeLLzeE9mq1aLD8GMBaqsp0vl945EB5Exq/ID4w6PU7hhRCC6DbpPOcbvTfdaVT/NpeMTe4p4Eprapj7xNWinxeFu+rIm6qofGlt4dVUo1WDyYiDZtml8ZVsAwLpga5UURGizIfBGYJeNGBPNTCI8/QBf0arx7HPgizoa5CXTOSHXbVtm2zWY4liEOa2+DpcZWs4Wz4Yn4ZlBGAxHC5CKLeeNzjBricsN6ke2E9EpmGqEG+WtRfhW1unGDjsJzZVjP0ZIsS9edHZZXRcD4ToHNjMq/RXXTQhs4b8KvThPykknEshqac3/58FqlqMFOz6qNi+uTDA8JR5gmy8dABR0OacdcMyz4fs/oXex7HoufxL2uuJjphJMEZ7W0SMMS1otQ/mU8WHCXQabz2I1DcEVqpeJhTM6F0TljwMG7WcBQHRnWlm2uHB6eqj1dZSxCOAzQf/AX/DsXPtMYeuPJm/L /gWlywtU /bPNfqB2u6fIZPcN6+X03zKm6xvxZj5fR80tygyn/I1/DAzsI74ZAZaR+R3LV2CYVBIDdXb448D+UX03vdQC43BOYH3EWBhuELPxLDhSiXAarnxz5viiHQjFoyqKGfdPklhBkYl9Bh9Kknb9zRqu9Mob5TR8fMTpGssrz+PeF0UnEy68uksfc/sptzUkKW/jd6QZfFGC2mSiCfvFUiVmHoO6kPTgwLyh7aW4823yxfvZzpUDYK+lwVgX2Cyb36rmw1RjWutvRbD8BZg9VfPXWUS/GlQ== 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: Upon migration, new allocated pages are being given the handle of the old pages. This is problematic because it means that for the stack which allocated the old page, we will be substracting the old page + the new one when that page is freed, creating an accounting imbalance. There is an interest in keeping it that way, as otherwise the output will biased towards migration stacks should those operations occur often, but that is not really helpful. The link from the new page to the old stack is being performed by calling __update_page_owner_handle() in __folio_copy_owner(). The only thing that is left is to link the migrate stack to the old page, so the old page will be subtracted from the migrate stack, avoiding by doing so any possible imbalance. Fixes: 217b2119b9e2 ("mm,page_owner: implement the tracking of the stacks count") Signed-off-by: Oscar Salvador Reviewed-by: Vlastimil Babka --- mm/page_owner.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mm/page_owner.c b/mm/page_owner.c index 5df0d6892bdc..b4476f45b376 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -366,9 +366,12 @@ void __split_page_owner(struct page *page, int old_order, int new_order) void __folio_copy_owner(struct folio *newfolio, struct folio *old) { + int i; struct page_ext *old_ext; struct page_ext *new_ext; struct page_owner *old_page_owner; + struct page_owner *new_page_owner; + depot_stack_handle_t migrate_handle; old_ext = page_ext_get(&old->page); if (unlikely(!old_ext)) @@ -381,6 +384,8 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old) } old_page_owner = get_page_owner(old_ext); + new_page_owner = get_page_owner(new_ext); + migrate_handle = new_page_owner->handle; __update_page_owner_handle(new_ext, old_page_owner->handle, old_page_owner->order, old_page_owner->gfp_mask, old_page_owner->last_migrate_reason, @@ -395,6 +400,16 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old) old_page_owner->free_pid, old_page_owner->free_tgid, old_page_owner->free_ts_nsec); + /* + * We linked the original stack to the new folio, we need to do the same + * for the new one and the old folio otherwise there will be an imbalance + * when subtracting those pages from the stack. + */ + for (i = 0; i < (1 << new_page_owner->order); i++) { + old_page_owner->handle = migrate_handle; + old_ext = page_ext_next(old_ext); + old_page_owner = get_page_owner(old_ext); + } page_ext_put(new_ext); page_ext_put(old_ext); From patchwork Thu Apr 4 07:07:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13617331 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 CA4BECD1292 for ; Thu, 4 Apr 2024 07:06:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2A836B008A; Thu, 4 Apr 2024 03:05:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DDBDB6B008C; Thu, 4 Apr 2024 03:05:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C54686B0092; Thu, 4 Apr 2024 03:05:56 -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 A124F6B008A for ; Thu, 4 Apr 2024 03:05:56 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 68C96120F67 for ; Thu, 4 Apr 2024 07:05:56 +0000 (UTC) X-FDA: 81970964712.03.87F34B9 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf13.hostedemail.com (Postfix) with ESMTP id 5EF2220015 for ; Thu, 4 Apr 2024 07:05:54 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=DTeAE4Cs; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=HFc2R9tS; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf13.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712214354; 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=J4wwG5jpQSOVuMqfScgmDBg4jiw7p8cJsBUm20LXaGw=; b=Xnzx4XJ1qU5QH/sFVl2ySguzc2zjtr3N2WaJ+9P/k0QINlEJMFjIZlsPDTOQlnyDrk5RHw MgTD5ioJwGuLMX9Jq7s4h1s8m08h7WdUiEZ9MVigl2V7fTkISevbDkC0hBrKSe4uJO+Usn TILIo/jq9L3V5BT1ZzAszPAOlbWqLkw= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=DTeAE4Cs; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=HFc2R9tS; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf13.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712214354; a=rsa-sha256; cv=none; b=bRYGlm/rgzE9Cp//PR+gb8vmmxdVmDF3meXKxJk7+3bLS93eGDG3vwOH5EJOkeJWU4jIGh Xzk/VPFWsdv/j8BGVaGmlSZB9VPQHoiW/dyQ/N9B+FDzqj5O2jAq23g/J2d8hrsJG18fNr /hTi8FEDWi+Zxp7IFE+6hhM79UFWiE4= 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-out1.suse.de (Postfix) with ESMTPS id 19D81378EF; Thu, 4 Apr 2024 07:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1712214353; 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=J4wwG5jpQSOVuMqfScgmDBg4jiw7p8cJsBUm20LXaGw=; b=DTeAE4CsKqN3QlIzPAFEGEu264vov2pEAU74p3IMXEN0UUxbIwMY7WjLmpbOZI/Yo9i0f/ QtgaatcX3RYl4TDQSmZunCCmnz4X6M34Ce28kpEhZl16noUIxaotEvf4ooVyaBYguu7C4F +dGOXk6kbkbeJrXe/sfi0bzMWkxarAU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1712214353; 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=J4wwG5jpQSOVuMqfScgmDBg4jiw7p8cJsBUm20LXaGw=; b=HFc2R9tSN3nHIpQ56020vRM6DlTy536CD43M5GHP2BJMk5Y7fnoGEi7KWMz5cJE1VSC8tc AulWxQIGEB4CsAAQ== 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 838D0139E8; Thu, 4 Apr 2024 07:05:52 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap2.dmz-prg2.suse.org with ESMTPSA id kIhbHVBRDmY6cgAAn2gu4w (envelope-from ); Thu, 04 Apr 2024 07:05:52 +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 , Alexandre Ghiti , Oscar Salvador Subject: [PATCH v4 4/4] mm,page_owner: Fix printing of stack records Date: Thu, 4 Apr 2024 09:07:02 +0200 Message-ID: <20240404070702.2744-5-osalvador@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240404070702.2744-1-osalvador@suse.de> References: <20240404070702.2744-1-osalvador@suse.de> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 5EF2220015 X-Stat-Signature: x68opfbjkatxc97oejw1e9dn1e8drdzi X-Rspam-User: X-HE-Tag: 1712214354-235042 X-HE-Meta: U2FsdGVkX186evglK3qF7ff6h/RQGyZV0OWSE8RIni+NEE4izConwPFIwpEBwGAV8E6Dpos1c5MvvlgKaXC6n3ACBcFoCiRttl8uEOj1RMmOPuJx00gA1DoJfwAYFWwqRJlB4tuBguDaawNI7S8mmY4Y0i3CymkbvPijhxwZ4/AbapkhVTUGtZJLXrbXPNxc//1jzesYYFY8uMdIUWJ7zBgvNZR7IxEQNhGgxQpfcQ+iPYsSfsv5//6KZvFez1Ske57s1l/UNbSNawLoQ/qd8U6R801wFUea9K9hyRQc15JyTGJBE0k3pTAQhu2pB6uvSke94pLG95GtW6PVpX/yPMohoE6oIoZs1xRdy/1p95n7ZF/vbgFcDRCLQlUA38A4k0qKT5xJPaYDBwwEjYext7f9Jhy+8PnKfyVlCQdBYcCR2ytr+OcB85VTHK+pPRTqsEoNPRdW2E2IinhjqHok4rImvGSuXgRxY39BrsZI+h2sPiu3Atd88LrUFNqOhHJ0fMrrYQvsdhtkPthRmXzUPtQyppuJU6seHIaFZL58PuvN1DMnbNpejxb/aDW+6jUhW9UdDUOo8QOokOKtrdnAmcppwOJ16mEeXnEqoBqffce1QWxG7ZjoqZtMHbkuunsX2OHXNDTAiE3lQuqblEYuatURtX9QR2PVD5JUS2gtdvvHPYwYFvyDuH2QIkekPTcaSYVKTkOXxs/srRlvlSsYJ1Uxvli2oie6NAxBr8nbDObG2aU2LQzut3vPRF139RwP3ckKWa07tZFXKlIPObgeNpX6z0mJwIS7tZ2KAlrOREWLc3ptGqB0HMFFaor6/M7ztkV4r4brm8vDWi1Eu8ukH5dTGCQkaDLcz8URoBzn1QDwbKQU+FfM6p5mSfeVpnVX4kY7GheztV6tK0JArJW5w8AhjVP+IwkMtJIEvejAOJSAViB6GPRjYEZgsG7VTK7wfA4D9gkHHnfi5WntoTe U4x/ueQI F1VkGsxrHHwOSUKhd2g+QzT5ofZscA+TVCwMkSq3DW3PBmDquOxOxClgOts581Svg2BAOEM7qKsbdhWudbpgvQuIxABn0A3bVAZ/W+zAxVhVDKw5QUaNjlkoVUXwC3YKQ5Va5RdkI/B2qNat1/PFusFrV+vtZzfePSwqvI1IsTf/qiMduo5CpRF8nN8kxumM86W08UCQbARLjwXrO56W7WZjMB1Zdm1hZz4Z07PIII/vdg/sIDZld233ZHlRaGBGuk4Citoxfo+nOofRzR1vAmsaLSA== 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: When seq_* code sees that its buffer overflowed, it re-allocates a bigger onecand calls seq_operations->start() callback again. stack_start() naively though that if it got called again, it meant that the old record got already printed so it returned the next object, but that is not true. The consequence of that is that every time stack_stop() -> stack_start() get called because we needed a bigger buffer, stack_start() will skip entries, and those will not be printed. Fix it by not advancing to the next object in stack_start(). Fixes: 765973a09803 ("mm,page_owner: display all stacks and their count") Signed-off-by: Oscar Salvador Reviewed-by: Vlastimil Babka --- mm/page_owner.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index b4476f45b376..9bef0b442863 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -872,13 +872,11 @@ static void *stack_start(struct seq_file *m, loff_t *ppos) * value of stack_list. */ stack = smp_load_acquire(&stack_list); + m->private = stack; } else { stack = m->private; - stack = stack->next; } - m->private = stack; - return stack; }