From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f174.google.com (mail-yw1-f174.google.com [209.85.128.174]) (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 7129E33EB17 for ; Fri, 17 Jul 2026 16:21:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784305266; cv=none; b=tKi7P7zDu7GUrDu9wW6FN0+9JBwNOTpFOJNbL22P3bOQ3K1Z/VieMpIw84ABT9S77mGUNyG/v56x/eQdWAzJJgXe0iCqg6zSIMLjKhb3e9l+dbNtLMfiC1WhzQJwgEG7Eb3L6pfRIiSt0iT6yVlFpINCuCKi56k50XrU4hInbkc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784305266; c=relaxed/simple; bh=7JoL+vPbY3zxvHoMhroiRNPg/3gPaMNGOSXOMEJZc2g=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=aGKeG3cWNnEqcHL1nBgmLtIIBipeuXKLIvHf0CjSSS023LllcZLRxaiP9H61mH/Z8LWYitUN35SA3pu8AbeqPoKnesXNmZM6Zck70X2+zIi7Ik2T3ksGwtVyrhouCDZkwOicemqqToWMValiMRZqCJ/zONnVoKUTWpphPe2MDX8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=OQIgmCjb; arc=none smtp.client-ip=209.85.128.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="OQIgmCjb" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-81e6f2ee60bso15545207b3.1 for ; Fri, 17 Jul 2026 09:21:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1784305264; x=1784910064; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=MEBrAHUFnk2FiioO0VpLsJTiSrnUVA33Eunb1QkbnAs=; b=OQIgmCjbEHxhFe/FpYWJFoNas9+DjrPdlyCcSD8PUHASOJWNjzv38uafxFFbtTXilH sYLsf8OYmVaFCGlPN7fqAg3KjmlgaZVnvjCDmF0H13THWBSt2XuWfhN8agvVmVpriBIB s3og79XjmE+8dcRi8dGY46oLRujF9txKhW+hAYLL2M5hsBceXZQckfoRR3578D3fJdjX GmMulUp8+5F/zaiu7rLfp/JKC6MfsMHySSKn8bbSrunYivBP5Xhc+pWyrfyMraLizRJk JfyXU3UmPg0raRY9vQxOjV1F4Rg0J8SmR44mW9hjft9qIrqYyp8LucfJvHjDlNIKeZfj 1MNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784305264; x=1784910064; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=MEBrAHUFnk2FiioO0VpLsJTiSrnUVA33Eunb1QkbnAs=; b=cK4tB/s9Ta0Bbzgnj59Icou+nTgpIUXOBqvkJOrmsLfQlnpOUrxSqBmxoLaXfvMizN EpSROgz41CPE3rBw46wA8sjDESd2jem/2zBOdx+3J9VGba366a9B0/uyoh0DRPPZg5+1 EWXP6sfTuL9DyWuZPR74rDFxtqtYuHCPGIGHOIymNljSRCzO9otTmAEgmimZxKdI41qK 9iO239iMtmXp7/RHBieebrlCoimKWQa8d7nr7x+7vLPKWqTjpV/HcDYv7jj6wPiXDtJp Olc4mKItD+DcB+Cu2qSWqCJLbLgGwVB51MaUdqVr8fFgBXJ0cPL4ByXfm310jdM+GY6H 40Kg== X-Forwarded-Encrypted: i=1; AHgh+RoUuxhjC2dkfw8GExU9wfmu7drtCn2I5MReV2stKgBkN+L8JdTtPb/Q4JniAgk3gM8IynKcxFMvOC9uqFM=@vger.kernel.org X-Gm-Message-State: AOJu0YyLTNMsNLTNyqumPOmATDUJ5RlmrNpjHmaLP0xe3QfFA6bnjiZs qGlE3Ev48TthON4zKmtuZMajnMgZ9YSR72JZE31Nxy5qdIwTPQY82ePtWXVYM89u1hc= X-Gm-Gg: AfdE7cm1Tx+O3eS17UNDPnH7KxmiqONJAy8cpkbwYCmdxjIO5sVRMs50KeleL0sUKJZ I42wHESso53nnnDljPGZitoYEYmYTemTvcWW+09oIVjnGrGAgDxNJYmDFHx6EyPFqpmlBat2Aez 1zHBMqcHtmwn5PJJhFOpRzygbKUuIZNEuY4QQCxtCQqqeuK922KaYuDoaO4z/CNZBEZAqY71WHW 8d+hDwXJWD5MUPNR7g/o/MNqoRFqPMHirrQT87FQSwM8D7MZCyVlONlUkhBM3vbXJEGfKgitEsJ uDKVlhHgQb5CGM01N9jjEwEqg8OxzmeMRnOH4JW8Q8FqRuCS4itDxLlSTqJTQ3j9gwdT2cu7aQW N2AOyjz9l9vxm7aAQssxW0oLcg3gm/T0+ldT2c6siPrvR7vx2hFC4M2Y3PGf74aTCG+qjhMBJm8 6FNSivZIaoS7Iv/LTO4BJg091JUGnulPVZAMI9Shninl9qHDMoZo82wGkhUReTN2+b55V69e/Sp t0LweQtMa7u X-Received: by 2002:a05:690c:c4e7:b0:80c:88b8:2dd9 with SMTP id 00721157ae682-81ef15ea910mr12966957b3.35.1784305264127; Fri, 17 Jul 2026 09:21:04 -0700 (PDT) Received: from google.com (138.200.150.34.bc.googleusercontent.com. [34.150.200.138]) by smtp.gmail.com with ESMTPSA id 00721157ae682-81ef42e89e8sm13834677b3.44.2026.07.17.09.21.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2026 09:21:03 -0700 (PDT) Date: Fri, 17 Jul 2026 12:21:03 -0400 From: Pasha Tatashin To: Michal Clapinski Cc: Mike Rapoport , Pasha Tatashin , Pratyush Yadav , Alexander Graf , Evangelos Petrongonas , kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] kho: align kho_scratch to MAX_ORDER_NR_PAGES pages Message-ID: References: <20260717134028.2880508-1-mclapinski@google.com> 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: <20260717134028.2880508-1-mclapinski@google.com> On 07-17 15:40, Michal Clapinski wrote: > While booting with KHO, the following crash was observed: > > BUG: unable to handle page fault for address: ff19164fffff8328 > RIP: 0010:__free_one_page+0x1a1/0x6b0 > Call Trace: > > [] free_one_page+0xaf/0x240 > [] deferred_free_pages+0xa8/0xd0 > [] deferred_init_memmap_chunk+0x10f/0x1b0 > [] padata_mt_helper+0x65/0xa0 > [] process_scheduled_works+0x202/0x410 > [] worker_thread+0x1f9/0x2d0 > [] kthread+0x27d/0x2f0 > [] ? __pfx_worker_thread+0x10/0x10 > [] ? __pfx_kthread+0x10/0x10 > [] ret_from_fork+0x145/0x280 > [] ? __pfx_kthread+0x10/0x10 > [] ret_from_fork_asm+0x1a/0x30 > > > deferred_init_memmap_chunk() interleaves initialization of struct pages > with freeing them. This works fine without KHO because free regions > will never be neighbors. However, with KHO, free memory will be split > into (free && scratch) and (free && !scratch), which can be neighbors. > > KHO scratch is aligned to CMA_MIN_ALIGNMENT_PAGES pages but buddy looks > at the neighborhood of MAX_ORDER_NR_PAGES pages. These values are > configurable but CMA_MIN_ALIGNMENT_PAGES is always less or equal to > MAX_ORDER_NR_PAGES. In the crashing configuration they were set as > follows: > CMA_MIN_ALIGNMENT_PAGES = 1 << 9 > MAX_ORDER_NR_PAGES = 1 << 10 > > So while freeing one chunk, buddy accessed uninitialized struct pages > from another chunk, tried to merge the blocks and crashed. > > To fix this, let's just align KHO scratch to MAX_ORDER_NR_PAGES pages. > > Fixes: c6073743d0c7 ("kho: make preserved pages compatible with deferred struct page init") > Signed-off-by: Michal Clapinski > --- > Unfortunately, this is very hard to catch, so I don't have a good > reproducer. But I run the code with the fix through extensive testing > and it seems fine. > I think I've never caught it before because enabling HUGETLB is what > causes CMA_MIN_ALIGNMENT_PAGES to be 1 << 9, instead of 1 << 10. > > If this passes review, please cherry-pick it to 7.2 so we don't release > a broken kernel. A changelog would be super helful here. Pasha > --- > kernel/liveupdate/kexec_handover.c | 22 ++++++++++++++++------ > 1 file changed, 16 insertions(+), 6 deletions(-) > > diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c > index 4834a809985a..175c08a6e41e 100644 > --- a/kernel/liveupdate/kexec_handover.c > +++ b/kernel/liveupdate/kexec_handover.c > @@ -38,6 +38,16 @@ > #include "../kexec_internal.h" > #include "kexec_handover_internal.h" > > +/* > + * This is the minimal alignment required by deferred struct page init. > + * deferred_init_memmap_chunk frees memory to the buddy allocator, which looks > + * at the neighboring pages (up to MAX_PAGE_ORDER) to merge them. > + * If KHO scratch is not aligned to that value, buddy can access uninitialized > + * struct pages, which can cause a crash. > + */ > +#define SCRATCH_ALIGNMENT_BYTES (PAGE_SIZE * MAX_ORDER_NR_PAGES) > +static_assert(SCRATCH_ALIGNMENT_BYTES >= CMA_MIN_ALIGNMENT_BYTES); > + > /* The magic token for preserved pages */ > #define KHO_PAGE_MAGIC 0x4b484f50U /* ASCII for 'KHOP' */ > > @@ -640,8 +650,8 @@ static void __init scratch_size_update(void) > * Scratch areas are released as MIGRATE_CMA. Round them up to the right > * size. > */ > - scratch_size_lowmem = round_up(scratch_size_lowmem, CMA_MIN_ALIGNMENT_BYTES); > - scratch_size_global = round_up(scratch_size_global, CMA_MIN_ALIGNMENT_BYTES); > + scratch_size_lowmem = round_up(scratch_size_lowmem, SCRATCH_ALIGNMENT_BYTES); > + scratch_size_global = round_up(scratch_size_global, SCRATCH_ALIGNMENT_BYTES); > } > > static phys_addr_t __init scratch_size_node(int nid) > @@ -656,7 +666,7 @@ static phys_addr_t __init scratch_size_node(int nid) > size = scratch_size_pernode; > } > > - return round_up(size, CMA_MIN_ALIGNMENT_BYTES); > + return round_up(size, SCRATCH_ALIGNMENT_BYTES); > } > > /** > @@ -692,7 +702,7 @@ static void __init kho_reserve_scratch(void) > * next kernel > */ > size = scratch_size_lowmem; > - addr = memblock_phys_alloc_range(size, CMA_MIN_ALIGNMENT_BYTES, 0, > + addr = memblock_phys_alloc_range(size, SCRATCH_ALIGNMENT_BYTES, 0, > ARCH_LOW_ADDRESS_LIMIT); > if (!addr) { > pr_err("Failed to reserve lowmem scratch buffer\n"); > @@ -705,7 +715,7 @@ static void __init kho_reserve_scratch(void) > > /* reserve large contiguous area for allocations without nid */ > size = scratch_size_global; > - addr = memblock_phys_alloc(size, CMA_MIN_ALIGNMENT_BYTES); > + addr = memblock_phys_alloc(size, SCRATCH_ALIGNMENT_BYTES); > if (!addr) { > pr_err("Failed to reserve global scratch buffer\n"); > goto err_free_scratch_areas; > @@ -721,7 +731,7 @@ static void __init kho_reserve_scratch(void) > */ > for_each_node_state(nid, N_MEMORY) { > size = scratch_size_node(nid); > - addr = memblock_alloc_range_nid(size, CMA_MIN_ALIGNMENT_BYTES, > + addr = memblock_alloc_range_nid(size, SCRATCH_ALIGNMENT_BYTES, > 0, MEMBLOCK_ALLOC_ACCESSIBLE, > nid, true); > if (!addr) { > -- > 2.55.0.229.g6434b31f56-goog >