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 81DF6C5B543 for ; Tue, 10 Jun 2025 10:15:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2A226B007B; Tue, 10 Jun 2025 06:15:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BDB1F6B0089; Tue, 10 Jun 2025 06:15:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF0FA6B008A; Tue, 10 Jun 2025 06:15:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 935A56B007B for ; Tue, 10 Jun 2025 06:15:39 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2F5EEC1A0D for ; Tue, 10 Jun 2025 10:15:39 +0000 (UTC) X-FDA: 83539084398.14.94202E7 Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by imf30.hostedemail.com (Postfix) with ESMTP id 5A3CC80006 for ; Tue, 10 Jun 2025 10:15:37 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=neon.tech header.s=google header.b=Maua0w0V; spf=pass (imf30.hostedemail.com: domain of sharnoff@neon.tech designates 209.85.208.53 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=1749550537; 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=nvfG20MQLYlKkF+o+WgnnDhDnQDbafUQgPnApR8FRuI=; b=8LJVSREwMxGv/m0vKW9mT5IzdBTAD0RNnR0Xn2dQXIDWnb5u9qhxroWXY/puQ6MXzqjFlL S+vcq1JWDLefbbRU49Urrg5A2frPD52x53CMwvW/TfGenJ/ZzNyWb3eoU3Vu1M9gHMV3KM 7/27D6+ysFEBOBP1mP6qyK7NEE7Z0Vg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=neon.tech header.s=google header.b=Maua0w0V; spf=pass (imf30.hostedemail.com: domain of sharnoff@neon.tech designates 209.85.208.53 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=1749550537; a=rsa-sha256; cv=none; b=dyY5gOTlR2d+C8QGCmfnO8iA2Yl3Vidjd3qNLrS8gHIXmyDaSYMjwgRgQgptpxaUpKzMYG /F55nLei62CO3XlRdsXb8+I6+v5QnzQ94J88o6cJ0XGChvDGqwgrBeuEuhU+pyhuJraLnj VhNrposWkH3vcn+ofKLSxYHIU6HXZIk= Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-6069d764980so11490566a12.0 for ; Tue, 10 Jun 2025 03:15:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=neon.tech; s=google; t=1749550536; x=1750155336; 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=nvfG20MQLYlKkF+o+WgnnDhDnQDbafUQgPnApR8FRuI=; b=Maua0w0VOIgZBUhXvChmiec7a7UN9PoqdMilv3ypLipXy74OxzOY8mVjxnO2G1vc57 l9Wmw6ltWyRdBPpwobn67a/QnoL3nfvTwB9nfhjvLvkXnzOjxFj9vAqoAfa2RuZ/9oRL HCfdaSdkbmUk8lT+/zgTlJ86e/nrHM5rQYYTI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749550536; x=1750155336; 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=nvfG20MQLYlKkF+o+WgnnDhDnQDbafUQgPnApR8FRuI=; b=cYtCjTBJKhqDdVNAsYm4hXCU97+wlIvpbzrqlQnwrvbVflCWqq6VVujhwMf8VF6D5I 66fMeNiuXj/4zMQZN0LqQF/oXMCkcPKkcnCEJ/Xoo568elsl6ykOkzSBSTHiUWR+Be5z F5NZHTAJHcbic7RqPdhAcfAuY1MQ0XV1RyJKHuKwXPb/+3c9jCwxOaGdaIfz14IL+7bN 533yDMdY3AakAaJnjjkW4P8+JlB4F5hfWSpfP1qmaLwFHPYwahqVadj2n4MBhpUm4oco 97dfRnpkGxuO7GuQVSF3L3yLsAHv9fkmkQsYy7FzC0hHDgrzFmYPiQM60a2GjKxa94IH taTg== X-Forwarded-Encrypted: i=1; AJvYcCWgteUzIcgQzvbi1ge76xHZqbbjyhbRFsBikCCMhhtbxD6rVzEL7WnYNTy0XlF62iOnPqnsDnESGw==@kvack.org X-Gm-Message-State: AOJu0YxBSPclJkRVXrAi92Gru7abxyOp5LySWJLy0G8hBhc92jhTqSrI nHF8JsjqKKqTGG3/o3ZTV/Q/uJ1TtZ1blrOrumJI0/csSgoYwjp+/p7cedx0mNiQpS8= X-Gm-Gg: ASbGncvKs156k4HySwX77dypho5MPaW2eEwy9/nkvhqhpqdmFOC8HTq1L6LPkyP7LQj ot5qdxkdZ0iQECwvbojpgTSQ+s5rpzFt/u/AIeozVXjvu3PdddbfYdyI/9xVNaylNzaTbKvJeUL qbo0LFcVnLVQeyLBFUWVKS4fIKJjTaZTeMLyEgVNs5oYwz9nEnjqdpRkuPubFxihi9/smZEq3Fi mA0lk0sY0NIlTj7Rrgx4I5cnuUEmjr7p364AWKCp+9Kb3TPN9lVzxoLdYCG5MKug4HcBun6Wnos rl8GemD3HH9P+w7DnZHWNdd9WCHtHn4/pFheY6Evn0wCEpDWuQqK4CjsjPmt89ZzFQ== X-Google-Smtp-Source: AGHT+IF9jX0Gs6s3uI6ySvWxXbzgdwbHAVjqfgfgUtHPqTkNctsYqnHPKZewjwqixROxeuDD4J40ow== X-Received: by 2002:a17:906:6a16:b0:ad5:7649:b3f5 with SMTP id a640c23a62f3a-ade770fba75mr300445066b.3.1749550535545; Tue, 10 Jun 2025 03:15:35 -0700 (PDT) Received: from [192.168.86.142] ([84.65.228.220]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ade3c427cafsm547042766b.75.2025.06.10.03.15.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Jun 2025 03:15:35 -0700 (PDT) Message-ID: Date: Tue, 10 Jun 2025 11:15:33 +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 v3 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: 5A3CC80006 X-Stat-Signature: 1664a95qco6bpuajdyzbeaddtx8acomy X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1749550537-909977 X-HE-Meta: U2FsdGVkX1+44BzNdo9wOKRs1wB1YuXIvKS/pzBTXLGo+WsiBl4duOZ3HM4cN/HzK94BGfLRTYmvMtvR2nRrqpjMTJoT+OzK4mudkKRaxkH54z+iWch+tp/dPt4QB20vyKJ+1fuYyRYhc/9VcCRXOqjfHfO4NvkNGePeXG6UA+j+FrC3QuuHMn1AVfaDFKdOuh55xilNP91LH128jOK8Vih7TUKgTITKyr8eIy2Ic1mQ41AtOPSLRqMA4s3qWY7US7fdRR0poy8/vpwifxmngCLgCu9HJ5QH+G0xZKjzBDFIN/oCuBydqKmfbZUDXuyPpBqH7sY/oYP7T+ehaH5SLtlB/MF7IkNkhspUWzMA8ClS4UqFr/fHf5wSLH0si4VKFhRWZpxx+0eHeXAaWmFZhgRPJMQ76nZS02aa5YeagDgWshlxJ7f1eU6DvQPO0g0ub4w5ZS5vHAOeu/sfL1+6uyRleF6rxvK9XjrFsNFa3lYYagfJSBriw4tCmKdOImw2eSqO0Q+hl5Z9dGVb1UWxGf0u6K24B0frJSNW4rCnlawvQZ91akOJrop2Uaq/otwRts9DYjb6P0xUbQ7NIPSl4oyrXfuxjEZMCMdXOVCrGv2ihdv0voDeQneO4d0c21O3Pfr/0DX5CGg0S04X9S2zUVXYhGWS73CqN+2zXjqmEjweTwDMvpejTFOn7lKYJF4Vamp+MSULCAd3Y+0LXkFxAH1fF3BwqNqtnENmqYCtftUsh8pIqmGlc6rbnrmAaJdfN1U5I2MgEsAXQikaCZ78l/WnI1QSJNNyzPYEV33L5Zi5MAAJl68BxL5sVSVqM6ApUoQOpj5XD1piSF5qAC4IPNY3SB1mmDQP4BwcyUEn26mHNZDLrCsLW7hVtEFaEhmrkGGSGz5BdFhKuEOn89gf9TpCW8A5hcyFSxyYkU5VmDvEHnvyaSBd9E0wy0xna+icFowFIyyJ4ELnb1ny4B6 njy45lMA AGdYOqk58xm1TLnNJDDI2cDEy/dpJvwHSb+Pcy3NRNhxZz+CHslz77mVCMNTws4qP51Ku 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 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: https://lore.kernel.org/all/0ce5e150-19e0-457f-bec3-ee031c0be7e7@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 v3: - Fix -Wint-conversion issues === 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-10, 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