From patchwork Fri Jan 24 21:37:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 13949947 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 CA5E7C0218B for ; Fri, 24 Jan 2025 21:38:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6E36280079; Fri, 24 Jan 2025 16:38:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D2191280077; Fri, 24 Jan 2025 16:38:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A10FF28008E; Fri, 24 Jan 2025 16:38:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7550E280079 for ; Fri, 24 Jan 2025 16:38:21 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2A182A151A for ; Fri, 24 Jan 2025 21:38:21 +0000 (UTC) X-FDA: 83043659202.06.02DBEE8 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf15.hostedemail.com (Postfix) with ESMTP id 620D0A0004 for ; Fri, 24 Jan 2025 21:38:19 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=F+k2J+Cb; spf=pass (imf15.hostedemail.com: domain of luizcap@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=luizcap@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737754699; 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=mpq60malTbL1a0tgUfkQY0dx9GoNhyxzjB2FnEhg2RU=; b=ijiWic9k8iQw3RBuNIqicKxtpWBTp6XRM4CfgI2OgT0I7S409MHlHmJJIKqhWMrh5bTJqY 5POOPPosU5hSJFREOpd1On+toDNX6EW21f2cFVXEF52T1metKOp6O8nVvk+YVgiVoRnXfO OCXRlaLZfqd8ml+wPMB0LhA8nKOdRvM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=F+k2J+Cb; spf=pass (imf15.hostedemail.com: domain of luizcap@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=luizcap@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737754699; a=rsa-sha256; cv=none; b=Q4DKXCaykOYTso/sSdaeYOEuVsdg06Rgj0qDW6XDd4jTPCYX7mXnH89FcItpoViWqL/bn2 t1r927E57r5VINWbm9XPkRJ+lF7hES9Ecq85IdupiAfoBdbI839gfxdjv92QXLKszEb5g+ Y2/pqmErjwXVAGGYhGOegeGFGRo0Lgg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737754698; h=from:from:reply-to:subject:subject: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=mpq60malTbL1a0tgUfkQY0dx9GoNhyxzjB2FnEhg2RU=; b=F+k2J+Cb00yhHHXH3nPQvyxSKNzLNMJ4CtT7MchyrmZwSTjtWJV5cy3fgOuQYVh/Zwxdpk sTE+yAJMNr1k/2LHHmu5U2cH1I9aMBThYij91xcgCpWsA6AhwuJQYp36gkh/WyK6F3sdY8 bRFcoT9imw0VI5UWsstrZNLMEozhIEo= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-658-OcIZJC7zOteXDG1lMPmgNg-1; Fri, 24 Jan 2025 16:38:12 -0500 X-MC-Unique: OcIZJC7zOteXDG1lMPmgNg-1 X-Mimecast-MFC-AGG-ID: OcIZJC7zOteXDG1lMPmgNg Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E74F4195608B; Fri, 24 Jan 2025 21:38:10 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.81.148]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D2B121800348; Fri, 24 Jan 2025 21:38:08 +0000 (UTC) From: Luiz Capitulino To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, david@redhat.com, yuzhao@google.com Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, muchun.song@linux.dev, lcapitulino@gmail.com, luizcap@redhat.com Subject: [RFC 2/4] mm: page_owner: use new iteration API Date: Fri, 24 Jan 2025 16:37:52 -0500 Message-ID: <712c1e058c58ba0d4c407089fbcc45c55ceb8efc.1737754625.git.luizcap@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 620D0A0004 X-Stat-Signature: 5x6bouzw38fcy5xff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OHPYxDn4NXBGogoZbRqheSRDRS5NrbqpItuNz2crOGGuFmJ+OEP8HQO3uwlPV8dz3b9Hw8Xo6VrlMRdkiPJzImy81h4w7tnPixuBAPfCMDnmyGYtIRwGiyq6jA2gKrmxeHeUU9lKo/5fqP7IC494mbiXryyNB6ImrnQgmbpESnpBNZm0UYxw1FJIfPFX/N8mFq9HqOPeJak9BaS4eVVI8RPaajAsftwTgHZy2QRy+q5UQ4aSkStzvk6R1VJeQXpCBv+aJNqREHX9eV15K3XzTcprgLNDOKxZPDmg9B8plw/gn727bWnG6IWWgOuh5/d8YrA== 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: The page_ext_next() function assumes that page extension objects for a page order allocation always reside in the same memory section, which may not be true and could lead to crashes. Use the page_ext_iter API instead. Fixes: e98337d11bbd ("mm/contig_alloc: support __GFP_COMP") Signed-off-by: Luiz Capitulino --- mm/page_owner.c | 68 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index 2d6360eaccbb6..14b4ec8ceb83e 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -229,7 +229,7 @@ static void dec_stack_record_count(depot_stack_handle_t handle, handle); } -static inline void __update_page_owner_handle(struct page_ext *page_ext, +static inline void __update_page_owner_handle(struct page_ext_iter *iter, depot_stack_handle_t handle, unsigned short order, gfp_t gfp_mask, @@ -237,8 +237,11 @@ static inline void __update_page_owner_handle(struct page_ext *page_ext, pid_t pid, pid_t tgid, char *comm) { int i; + struct page_ext *page_ext; struct page_owner *page_owner; + page_ext = page_ext_iter_get(iter); + for (i = 0; i < (1 << order); i++) { page_owner = get_page_owner(page_ext); page_owner->handle = handle; @@ -252,19 +255,22 @@ static inline void __update_page_owner_handle(struct page_ext *page_ext, 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); + page_ext = page_ext_iter_next(iter); } } -static inline void __update_page_owner_free_handle(struct page_ext *page_ext, +static inline void __update_page_owner_free_handle(struct page_ext_iter *iter, depot_stack_handle_t handle, unsigned short order, pid_t pid, pid_t tgid, u64 free_ts_nsec) { int i; + struct page_ext *page_ext; struct page_owner *page_owner; + page_ext = page_ext_iter_get(iter); + for (i = 0; i < (1 << order); i++) { page_owner = get_page_owner(page_ext); /* Only __reset_page_owner() wants to clear the bit */ @@ -275,7 +281,7 @@ static inline void __update_page_owner_free_handle(struct page_ext *page_ext, 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_ext = page_ext_iter_next(iter); } } @@ -286,8 +292,9 @@ void __reset_page_owner(struct page *page, unsigned short order) depot_stack_handle_t alloc_handle; struct page_owner *page_owner; u64 free_ts_nsec = local_clock(); + struct page_ext_iter iter; - page_ext = page_ext_get(page); + page_ext = page_ext_iter_begin(&iter, page); if (unlikely(!page_ext)) return; @@ -295,9 +302,10 @@ void __reset_page_owner(struct page *page, unsigned short order) alloc_handle = page_owner->handle; handle = save_stack(GFP_NOWAIT | __GFP_NOWARN); - __update_page_owner_free_handle(page_ext, handle, order, current->pid, + __update_page_owner_free_handle(&iter, handle, order, current->pid, current->tgid, free_ts_nsec); - page_ext_put(page_ext); + + page_ext_iter_end(&iter); if (alloc_handle != early_handle) /* @@ -314,18 +322,19 @@ noinline void __set_page_owner(struct page *page, unsigned short order, gfp_t gfp_mask) { struct page_ext *page_ext; + struct page_ext_iter iter; u64 ts_nsec = local_clock(); depot_stack_handle_t handle; handle = save_stack(gfp_mask); - page_ext = page_ext_get(page); + page_ext = page_ext_iter_begin(&iter, page); if (unlikely(!page_ext)) return; - __update_page_owner_handle(page_ext, handle, order, gfp_mask, -1, + __update_page_owner_handle(&iter, handle, order, gfp_mask, -1, ts_nsec, current->pid, current->tgid, current->comm); - page_ext_put(page_ext); + page_ext_iter_end(&iter); inc_stack_record_count(handle, gfp_mask, 1 << order); } @@ -345,18 +354,21 @@ void __set_page_owner_migrate_reason(struct page *page, int reason) void __split_page_owner(struct page *page, int old_order, int new_order) { int i; - struct page_ext *page_ext = page_ext_get(page); + struct page_ext *page_ext; + struct page_ext_iter iter; struct page_owner *page_owner; + page_ext = page_ext_iter_begin(&iter, page); if (unlikely(!page_ext)) return; for (i = 0; i < (1 << old_order); i++) { page_owner = get_page_owner(page_ext); page_owner->order = new_order; - page_ext = page_ext_next(page_ext); + page_ext = page_ext_iter_next(&iter); } - page_ext_put(page_ext); + + page_ext_iter_end(&iter); } void __folio_copy_owner(struct folio *newfolio, struct folio *old) @@ -364,24 +376,26 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old) int i; struct page_ext *old_ext; struct page_ext *new_ext; + struct page_ext_iter old_iter; + struct page_ext_iter new_iter; 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); + old_ext = page_ext_iter_begin(&old_iter, &old->page); if (unlikely(!old_ext)) return; - new_ext = page_ext_get(&newfolio->page); + new_ext = page_ext_iter_begin(&new_iter, &newfolio->page); if (unlikely(!new_ext)) { - page_ext_put(old_ext); + page_ext_iter_end(&old_iter); return; } 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, + __update_page_owner_handle(&new_iter, 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, @@ -390,8 +404,13 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old) * 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. + * + * Note that we need to re-grab the page_ext iterator since + * __update_page_owner_handle changed it. */ - __update_page_owner_free_handle(new_ext, 0, old_page_owner->order, + page_ext_iter_end(&new_iter); + page_ext_iter_begin(&new_iter, &newfolio->page); + __update_page_owner_free_handle(&new_iter, 0, old_page_owner->order, old_page_owner->free_pid, old_page_owner->free_tgid, old_page_owner->free_ts_nsec); @@ -402,12 +421,12 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old) */ for (i = 0; i < (1 << new_page_owner->order); i++) { old_page_owner->handle = migrate_handle; - old_ext = page_ext_next(old_ext); + old_ext = page_ext_iter_next(&old_iter); old_page_owner = get_page_owner(old_ext); } - page_ext_put(new_ext); - page_ext_put(old_ext); + page_ext_iter_end(&new_iter); + page_ext_iter_end(&old_iter); } void pagetypeinfo_showmixedcount_print(struct seq_file *m, @@ -782,6 +801,7 @@ static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) for (; pfn < block_end_pfn; pfn++) { struct page *page = pfn_to_page(pfn); struct page_ext *page_ext; + struct page_ext_iter iter; if (page_zone(page) != zone) continue; @@ -804,7 +824,7 @@ static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) if (PageReserved(page)) continue; - page_ext = page_ext_get(page); + page_ext = page_ext_iter_begin(&iter, page); if (unlikely(!page_ext)) continue; @@ -813,12 +833,12 @@ static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) goto ext_put_continue; /* Found early allocated page */ - __update_page_owner_handle(page_ext, early_handle, 0, 0, + __update_page_owner_handle(&iter, early_handle, 0, 0, -1, local_clock(), current->pid, current->tgid, current->comm); count++; ext_put_continue: - page_ext_put(page_ext); + page_ext_iter_end(&iter); } cond_resched(); }