From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 137441917FB for ; Fri, 3 Jul 2026 02:08:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783044524; cv=none; b=LhUAGevKkQYcNkhrKINRE4bsvbSAsy5fJDy+u9k1S0/JGkZXAV+6x6hhNQmC4F3eQboUbUxNGjGvK2NK+c+HLRnH0rKa9OlvLTMs/j5x29ZsP00ZijcqwZaGNPj9y2HY+fBBnN/l8egtpIyQqqamVrTUksj4ofmixa73dfLDNB8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783044524; c=relaxed/simple; bh=U86BcEMy6Dg9BlHa4pwus9u7mEAR5wkVBm6ryNLLqf0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=apwkxqfnclP8IKdBw6tLUj/Xrrvf7p1AI0UcHvR6gxV8aRsZRBLgkYTOJRSQuEA1CEmtaHduG3u2cnPXPEMhLGFZtTJSXjO5IRzaiHAoN694d3tNhuiTsH8WQoqdI2VgPX/56ulIleCRX2PjXzZ7MzWtp5M8rE7x6pWTriwxPYA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--praan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cgSTIO/i; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--praan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cgSTIO/i" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-8478423e020so136219b3a.0 for ; Thu, 02 Jul 2026 19:08:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1783044522; x=1783649322; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=twAjwEQPJQoMPii2nLtsAn0UlOpk5gfqaTEPYw4GO2E=; b=cgSTIO/iIr+ETgUwbGtR+mJHyhID2e3ghzWJUCIMv4ggDmncebdQAfGuTu7oMA8+Tg 2uYA1MalcuItfjRpjU9BG7fKbnPNty9aq/knaRPFF8Ypr9f4TWYn+dCIi3LxFmmtPiDF 54AKQzOpI24rPAtHX4r67G3brrNmTqKUthSMwYNJGGqzpAlCnzudNpYCfLTxa82t06wL WhyBlWMa2SAaHBQKXRWP6IkMgz3F0IU5fGfAVdjXgGMv6+1Z0ld8akAP1PIhZytQFWih 1+lbdtyssojh5xijm2i/rzk8NCq4mgOIB9Y5w1G3EHH/inHsfE0qBOiFXBoFmbgmXueq VSGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783044522; x=1783649322; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=twAjwEQPJQoMPii2nLtsAn0UlOpk5gfqaTEPYw4GO2E=; b=OoGYvRfRu8VO4fQIxzgEkGzCUxDPf9ivGDp3bwHr+Ja2X2nTGAgLEFFuvwFSZt5SUy QaZpRVajrzmEdIE+5AxCJYVKHMNwes5bd7ryTCAmXjOdJuCCMyMt9RlzmRSqTWLoic+Z GB7Oi9avmwjL16pi7Gomn7wAlm9PZRlgVcOciinyH/8Nciw7jH0XVXLV7+6A2S/tpsFo /uD6x040d0Wk2jdXjZSMZ4wj1ECAqozKoUdPrvI8DMvG7XCzZfXLexFaz9JmsZSazIhV YO6ZxOY6WASXiZMSbIHu7hSwWgC4TnPbr0Z697b6jmBa7ri16Po/eENZJPzxuTaNJUit 0FmA== X-Forwarded-Encrypted: i=1; AFNElJ89cNwservS4b3uZ0TM8NOCzvzli5g6ximazCC8H1ZzVag4X+RJWt5ycCZOFMJCypCaezwiP8rHJjFO4IE=@vger.kernel.org X-Gm-Message-State: AOJu0YxAZy7eb+rYXmAlyR79l3aPDMywM2FwAIyismcUYUW4fXemC3wV Q7J7m5B+SaDcHYQkfW0oaZ624uPb5aZwtutZ2+eF4Bm+B2pfHt3PlYgOIDA2VtUVJPCTOgEUZ7q 0QA== X-Received: from pfbhx24.prod.google.com ([2002:a05:6a00:8998:b0:847:8ef2:f06b]) (user=praan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:c8d:b0:847:81e2:2468 with SMTP id d2e1a72fcca58-847c0674f36mr8855060b3a.4.1783044522128; Thu, 02 Jul 2026 19:08:42 -0700 (PDT) Date: Fri, 3 Jul 2026 02:08:31 +0000 In-Reply-To: <20260703020832.1731864-1-praan@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260703020832.1731864-1-praan@google.com> X-Mailer: git-send-email 2.55.0.rc0.799.gd6f94ed593-goog Message-ID: <20260703020832.1731864-4-praan@google.com> Subject: [RFC PATCH 3/4] kho: Implement page-aware refcount restoration From: Pranjal Shrivastava To: Mike Rapoport , Pasha Tatashin , Pratyush Yadav Cc: Alexander Graf , Samiullah Khawaja , David Matlack , kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Pranjal Shrivastava Content-Type: text/plain; charset="UTF-8" The KHO restoration logic currently forces a refcount of 1 on every page of a multi-page block. While that is correct for split pages, it violates the expectations of the buddy allocator for high-order non-compound pages allocated by kernel user (like the DMA allocator), where tail pages are expected to have a refcount of 0. Update the restoration path to respect the preserved page type stored in the page->private metadata. For KHO_PAGE_CONTIG blocks, only the head page is given a reference count of 1. For KHO_PAGE_SPLIT blocks, every page is given a reference count of 1. Signed-off-by: Pranjal Shrivastava --- kernel/liveupdate/kexec_handover.c | 35 +++++++++++++++++------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c index d6e81f72fe5d..f6ca5e24c740 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -375,11 +375,18 @@ int kho_radix_walk_tree(struct kho_radix_tree *tree, } EXPORT_SYMBOL_GPL(kho_radix_walk_tree); -/* For physically contiguous 0-order pages. */ -static void kho_init_pages(struct page *page, unsigned long nr_pages) +/* For physically contiguous pages. */ +static void kho_restore_refcounts(struct page *page, unsigned long nr_pages, + enum kho_page_type type) { - for (unsigned long i = 0; i < nr_pages; i++) { - set_page_count(page + i, 1); + /* Head page always gets refcount of 1. */ + set_page_count(page, 1); + clear_page_tag_ref(page); + + for (unsigned long i = 1; i < nr_pages; i++) { + unsigned int count = (type == KHO_PAGE_SPLIT) ? 1 : 0; + + set_page_count(page + i, count); /* Clear each page's codetag to avoid accounting mismatch. */ clear_page_tag_ref(page + i); } @@ -387,16 +394,7 @@ static void kho_init_pages(struct page *page, unsigned long nr_pages) static void kho_init_folio(struct page *page, unsigned int order) { - unsigned long nr_pages = (1 << order); - - /* Head page gets refcount of 1. */ - set_page_count(page, 1); - /* Clear head page's codetag to avoid accounting mismatch. */ - clear_page_tag_ref(page); - - /* For higher order folios, tail pages get a page count of zero. */ - for (unsigned long i = 1; i < nr_pages; i++) - set_page_count(page + i, 0); + kho_restore_refcounts(page, 1 << order, KHO_PAGE_CONTIG); if (order > 0) prep_compound_page(page, order); @@ -421,13 +419,20 @@ static struct page *kho_restore_page(phys_addr_t phys, bool is_folio) return NULL; nr_pages = (1 << info.order); + /* + * If we want to restore a folio, but the memory was split in the + * previous kernel, something is wrong. + */ + if (WARN_ON_ONCE(is_folio && info.type == KHO_PAGE_SPLIT)) + return NULL; + /* Clear private to make sure later restores on this page error out. */ page->private = 0; if (is_folio) kho_init_folio(page, info.order); else - kho_init_pages(page, nr_pages); + kho_restore_refcounts(page, nr_pages, info.type); adjust_managed_page_count(page, nr_pages); return page; -- 2.55.0.rc0.799.gd6f94ed593-goog