From mboxrd@z Thu Jan 1 00:00:00 1970 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 E9029C5B552 for ; Mon, 9 Jun 2025 10:32:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 855446B0098; Mon, 9 Jun 2025 06:32:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 82CE06B0099; Mon, 9 Jun 2025 06:32:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 743226B009A; Mon, 9 Jun 2025 06:32:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 58DFB6B0098 for ; Mon, 9 Jun 2025 06:32:10 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C3B56141799 for ; Mon, 9 Jun 2025 10:32:09 +0000 (UTC) X-FDA: 83535497178.07.C7D6F8B Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by imf29.hostedemail.com (Postfix) with ESMTP id ADD12120014 for ; Mon, 9 Jun 2025 10:32:07 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=neon.tech header.s=google header.b=TaEx0PR2; spf=pass (imf29.hostedemail.com: domain of sharnoff@neon.tech designates 209.85.208.50 as permitted sender) smtp.mailfrom=sharnoff@neon.tech; dmarc=pass (policy=reject) header.from=neon.tech ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1749465127; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=vOh1kmN1BpJUB3fkO2pyW4zC5QemAdxkKxwT4QLR+CM=; b=nkYdU48TL/G9oGW5Po1jSLB+Gdmb7Dui+YQaPqD5H3VTnFpoPyPL+A5d22/IW3XppBvi7B xWlzxGy8Cv57KaGOM4EF0FkJZimR0IdxmeYRoyWmO62SylVougUBMbZ9UI1KScSt7W/uaL igfjlM+YnDSpyEXer5NYA+6o3z4eB2U= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=neon.tech header.s=google header.b=TaEx0PR2; spf=pass (imf29.hostedemail.com: domain of sharnoff@neon.tech designates 209.85.208.50 as permitted sender) smtp.mailfrom=sharnoff@neon.tech; dmarc=pass (policy=reject) header.from=neon.tech ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1749465127; a=rsa-sha256; cv=none; b=6tfRYx8t8r0rIkLsDEXXZC/CW64Myu+z2WaXN2OxNGcbynpnmJeLqYzlT/e6I9K3iFw9DB 4GaQNyx+4m5GAFM7WTQt/jTHoeKrAHOqshNDk9U7I6646ucEGRKHgpfZFBe7VcYK0JUEEa PV6y3ixVaYhXy+l50I6PriWHYwCga7k= Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-60497d07279so8264808a12.3 for ; Mon, 09 Jun 2025 03:32:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=neon.tech; s=google; t=1749465126; x=1750069926; darn=kvack.org; h=content-transfer-encoding:subject:from:cc:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=vOh1kmN1BpJUB3fkO2pyW4zC5QemAdxkKxwT4QLR+CM=; b=TaEx0PR26+dTamohq6YsVwWSNBEtfSFGYsoK3RlQ8RFZQHDnTXt36y1Ot9jI0SQvfd B2qQ0n2GaJ2Th9XxPh/TGWm9iS8+VIEBbUVxW0S4caM/PQAenHSRvsfwL2qpnXEGohu4 gkqrwX0keP9ZpdTbUhmQXB0RYsQKrZKzfqZJU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749465126; x=1750069926; h=content-transfer-encoding:subject:from:cc:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=vOh1kmN1BpJUB3fkO2pyW4zC5QemAdxkKxwT4QLR+CM=; b=YUh/PgpmHzaoti+T8VnQi1Z5IW7kiYVz/5ivAL98lt8lvEjDxDiuCbo+8atJ/4KZg7 pzQVUkHSdQKseDCwAtlwIDZVY+U5xUVzxXF3ucWUlFSGL9h3gcvvBqHGv+QLCIHQh6nx d5rcRSNOeyt1ZjjqT4SvGuuaoBZ3IETW5IjIcJ7VKTjmi51jdFGNq0fKdY0SxZAWfsW7 sK0PMMYy2wecfAQ9v/Wx/jmc1esY07TpiRsh6O+abR2yA2PxjYGNCOudr1Efnjl5tFbW EvMVW6hXgh/BpVPCb0BGs7B7SYhQzr6lgOwIRP3a68WwzbUv+YGyhEwYhtkL7Q3aiDad tYzQ== X-Forwarded-Encrypted: i=1; AJvYcCXjB75Wj+QBgNOY31joIa8xBp0tqQ07RWAHIO/DEJcKys+ST1eLDfQmS5wXLMt6AtxikuXGHsqpjg==@kvack.org X-Gm-Message-State: AOJu0Yw31hGuwkOEG+FDB0iN8ZpUDCm+uTST05KJ/SFZuFpI+CRaIRiR 7snmbMZRChnJoQl1DlwPOLtwphtRy+sUnRulV+G296V4DtaNXGRmbDnXmX/4O1BaO+A= X-Gm-Gg: ASbGnct4mAkyKISEBhGPjse/BebclE22fF3El+he3chQ19zx0c0RweBBDmTrm2/apCH o8kq/kd/lesE/+T5rIw5fZrsuNlkzhYL7FAOykZl9FLQ082wi3Nr7zoeQg6yYGzrtaZvM/CX0ek hW5B8R1pCK8pgAo9PI7EXm0ZWkuvJzYVeZyRffRWG9xqmLj8bepN8v8TXV53+H6+VG8e9nhHEkk GXhtYa/82xuP5xxHOt8k9DZwfWJg3DC5KETf6XPJWq6S3Hkl8H+HAD5IRUKUlHGEQzq5nqqUbss hyUCM0bdStnLlUuCXyalV2wdcCMwNRMOidDPlm2O2tNZZ9j0gqVNQws= X-Google-Smtp-Source: AGHT+IETLIUhGfaSESn8EKtmg8Vs25bOw1qtrO9ao2zSDcQl0Ezv0B68l58mWF6bgDn4au63vE3mbA== X-Received: by 2002:a05:6402:2694:b0:607:1ee1:1ea2 with SMTP id 4fb4d7f45d1cf-6077489733amr11327963a12.30.1749465125944; Mon, 09 Jun 2025 03:32:05 -0700 (PDT) Received: from [192.168.86.142] ([84.65.228.220]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-607783c0620sm4592766a12.49.2025.06.09.03.32.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Jun 2025 03:32:04 -0700 (PDT) Message-ID: <0ce5e150-19e0-457f-bec3-ee031c0be7e7@neon.tech> Date: Mon, 9 Jun 2025 11:32:02 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: linux-kernel@vger.kernel.org, x86@kernel.org, linux-mm@kvack.org Cc: Ingo Molnar , "H. Peter Anvin" , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Borislav Petkov , "Edgecombe, Rick P" , Oleg Vasilev , Arthur Petukhovsky , Stefan Radig , Misha Sakhnov From: Em Sharnoff Subject: [PATCH v2 0/2] x86/mm: Improve alloc handling of phys_*_init() Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: ADD12120014 X-Stat-Signature: sszg4g55xhaguxhmiixd6z33ircmfwrq X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1749465127-65695 X-HE-Meta: U2FsdGVkX1/JTRQOIM389MDRfZBnzj2kj2f6MmW2cwb9R9/t0i14E6X6fnwnQkgw9Wr0OZw7PuSmS+wjLJa9i3/25Kqj5OZ/N5Q8aLR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XhozGG+h7wdDz2H+NwmM+1MHuyXHzuQd29BqtfgCauLiS4WvTZGVHWfPQ5gDnwqoXeuOOhc8QN567MFDW4LQpf0gamu8LjozG3bWZ5/BJoP/xE54p/BnC52wea/TPkgBw0iT49awKnYugqqinvmUNEVtHh1IoWtGA4gmKlqO7eGosQC9rnzs4pY9PA/7bt5EfOCVZtkCdAE6ZBgtccGhOkX0L0RDnUVZDSaQ/nL9OMEzR0+cgV1UQyVmlfQph6N5IRUCOs98N9WWLdYm1d+AtuhCcqKkTecx2u47RogYjjsC0j/IOBdHCqH3F/QTDOBpyIu62hsGjjan3f8qcOspn5u1HWqmZ/NLEr2ouU9i9 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: Hi (again) folks, See changelog + more context below. tl;dr: * Currently alloc_low_page() uses GFP_ATOMIC after boot, which may fail * Those failures aren't currently handled by phys_pud_init() and similar functions. * Those failures can happen during memory hotplug So: 1. Add handling for those allocation failures 2. Use GFP_KERNEL instead of GFP_ATOMIC Previous version here, if you missed it: https://lore.kernel.org/all/9f4c0972-a123-4cc3-89f2-ed3490371e65@neon.tech/ === Changelog === v2: - Switch from special-casing zero values to ERR_PTR() - Add patch to move from GFP_ATOMIC -> GFP_KERNEL - Move commentary out of the patch message and into this cover letter === Background === We recently started observing these null pointer dereferences happening in practice (albeit quite rarely), triggered by allocation failures during virtio-mem hotplug. We use virtio-mem quite heavily - adding/removing memory based on resource usage of customer workloads across a fleet of VMs - so it's somewhat expected that we have occasional allocation failures here, if we run out of memory before hotplug takes place. We started seeing this bug after upgrading from 6.6.64 to 6.12.26, but there didn't appear to be relevant changes in the codepaths involved, so we figured the upgrade was triggering a latent issue. The possibility for this issue was also pointed out a while back: > For alloc_low_pages(), I noticed the callers don’t check for allocation > failure. I'm a little surprised that there haven't been reports of the > allocation failing, because these operations could result in a lot more > pages getting allocated way past boot, and failure causes a NULL > pointer dereference. https://lore.kernel.org/all/5aee7bcdf49b1c6b8ee902dd2abd9220169c694b.camel@intel.com/ For completeness, here's an example stack trace we saw (on 6.12.26): BUG: kernel NULL pointer dereference, address: 0000000000000000 .... Call Trace: phys_pud_init+0xa0/0x390 phys_p4d_init+0x93/0x330 __kernel_physical_mapping_init+0xa1/0x370 kernel_physical_mapping_init+0xf/0x20 init_memory_mapping+0x1fa/0x430 arch_add_memory+0x2b/0x50 add_memory_resource+0xe6/0x260 add_memory_driver_managed+0x78/0xc0 virtio_mem_add_memory+0x46/0xc0 virtio_mem_sbm_plug_and_add_mb+0xa3/0x160 virtio_mem_run_wq+0x1035/0x16c0 process_one_work+0x17a/0x3c0 worker_thread+0x2c5/0x3f0 ? _raw_spin_unlock_irqrestore+0x9/0x30 ? __pfx_worker_thread+0x10/0x10 kthread+0xdc/0x110 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x35/0x60 ? __pfx_kthread+0x10/0x10 ret_from_fork_asm+0x1a/0x30 and the allocation failure preceding it: kworker/0:2: page allocation failure: order:0, mode:0x920(GFP_ATOMIC|__GFP_ZERO), nodemask=(null),cpuset=/,mems_allowed=0 ... Call Trace: dump_stack_lvl+0x5b/0x70 dump_stack+0x10/0x20 warn_alloc+0x103/0x180 __alloc_pages_slowpath.constprop.0+0x738/0xf30 __alloc_pages_noprof+0x1e9/0x340 alloc_pages_mpol_noprof+0x47/0x100 alloc_pages_noprof+0x4b/0x80 get_free_pages_noprof+0xc/0x40 alloc_low_pages+0xc2/0x150 phys_pud_init+0x82/0x390 ... (everything from phys_pud_init and below was the same) There's some additional context in a github issue we opened on our side: https://github.com/neondatabase/autoscaling/issues/1391 === Reproducing / Testing === I was able to partially reproduce the original issue we saw by modifying phys_pud_init() to simulate alloc_low_page() returning null after boot, and then doing memory hotplug to trigger the "failure". Something roughly like: - pmd = alloc_low_page(); + if (!after_bootmem) + pmd = alloc_low_page(); + else + pmd = 0; To test recovery, I also tried simulating just one alloc_low_page() failure after boot. This change seemed to handle it at a basic level (virito-mem hotplug succeeded with the right amount, after retrying), but I didn't dig further. We also plan to test this in our production environment (where we should see the difference after a few days); as of 2025-06-09, we haven't yet rolled that out. Em Sharnoff (2): x86/mm: Handle alloc failure in phys_*_init() x86/mm: Use GFP_KERNEL for alloc_low_pages() after boot arch/x86/mm/init.c | 8 +++++-- arch/x86/mm/init_64.c | 54 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 6 deletions(-) base-commit: 82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3 -- 2.39.5