From patchwork Wed Jun 19 09:55:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gowans, James" X-Patchwork-Id: 13703589 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 56999C2BA15 for ; Wed, 19 Jun 2024 09:56:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ABB326B0391; Wed, 19 Jun 2024 05:56:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A6A0F6B0392; Wed, 19 Jun 2024 05:56:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 959186B0393; Wed, 19 Jun 2024 05:56:20 -0400 (EDT) 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 7949A6B0391 for ; Wed, 19 Jun 2024 05:56:20 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E5BDB161041 for ; Wed, 19 Jun 2024 09:56:19 +0000 (UTC) X-FDA: 82247182878.21.6AAC969 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by imf25.hostedemail.com (Postfix) with ESMTP id E4820A0006 for ; Wed, 19 Jun 2024 09:56:17 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=aijsnspO; spf=pass (imf25.hostedemail.com: domain of "prvs=893c3a89a=jgowans@amazon.com" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=893c3a89a=jgowans@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718790972; 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=raIEZpdgkW+RHfmC8vCCejhzjRvyZaw+50MAD0K0AN4=; b=Qt0ahl0ZTK5OC2mt6O8YJhQfIH6HD/4UcVcUzf75dSSIvLcQlSrlsZ0kUD/6GIai6W8VcL FRXG4CKLI4ZqrUHQImRKharjSaYTI8BK/+Ow3XbL6NLXC3UspjscCy6BUmZRwfe7WnqFFo mmv4e5CP5EAeNQif5euOazs7CyUs/AY= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=aijsnspO; spf=pass (imf25.hostedemail.com: domain of "prvs=893c3a89a=jgowans@amazon.com" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=893c3a89a=jgowans@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718790972; a=rsa-sha256; cv=none; b=wFczNpFdoe1ZEli8qWbFTwNoZY9nIsUA89yYLoxNkgxIHYumb/6qES7zDS99DHfMUgPLxO DcAY6Tr3SXJ0xiiSryQK+dyBHI37UCBpApEIka+hHz6Ry2KQXOt/iwUvZ3P0SjZoCPMY+W eLZ6ZIYQbNYBJ+rVG0wU/DblasAFuIw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1718790977; x=1750326977; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=raIEZpdgkW+RHfmC8vCCejhzjRvyZaw+50MAD0K0AN4=; b=aijsnspOcj9IQ+XZZzFAh1sQjDaoXUv3mrOXDaZAVWoVe6ORs80Las3R mIGNStPrC+GJ9daPtVoKBMdTKlLpiAxt/IfNFj4mmCmkpo3psBq8DLOHA SfXGMhKX5Dx8cEpeJ5q5hi+0H9OaP2vapxDMIjHKToXUcoR1L6SPJscaZ s=; X-IronPort-AV: E=Sophos;i="6.08,250,1712620800"; d="scan'208";a="97813385" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jun 2024 09:56:14 +0000 Received: from EX19MTAEUA002.ant.amazon.com [10.0.43.254:12585] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.9.211:2525] with esmtp (Farcaster) id 65371dce-1daa-44d6-9e02-e86036a7bf71; Wed, 19 Jun 2024 09:56:14 +0000 (UTC) X-Farcaster-Flow-ID: 65371dce-1daa-44d6-9e02-e86036a7bf71 Received: from EX19D014EUC004.ant.amazon.com (10.252.51.182) by EX19MTAEUA002.ant.amazon.com (10.252.50.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34; Wed, 19 Jun 2024 09:56:13 +0000 Received: from u5d18b891348c5b.ant.amazon.com (10.146.13.221) by EX19D014EUC004.ant.amazon.com (10.252.51.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34; Wed, 19 Jun 2024 09:56:10 +0000 From: James Gowans To: Mike Rapoport , Andrew Morton CC: James Gowans , , , Alex Graf Subject: [PATCH v2] memblock: Move late alloc warning down to phys alloc Date: Wed, 19 Jun 2024 11:55:55 +0200 Message-ID: <20240619095555.85980-1-jgowans@amazon.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.146.13.221] X-ClientProxiedBy: EX19D041UWB004.ant.amazon.com (10.13.139.143) To EX19D014EUC004.ant.amazon.com (10.252.51.182) X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MeDOl0fzBR26qSPVfEnAq+x FNtirDla fMtzbJE0m29lpbYUy40dC8q3ShfL103dDVtMovwpeHN4XaD48M19oe0M3OC3Ff0XHL7YvdqtLaCUsM67GhHPXjH5aeWTxXZZggKms2wKaCv4mUsyJpNMHvXLm/JnLn543sxukUXnpRPyEkMVApsBZpSQAJwB4HycYmSRRq9lF0K/4e6XFxfmJsVO3D/dbhsuND/oBwD7uPIwBmpuBH0HmBHpVIAByI5alnBfRaIxDvtzwpijYKXNxbEXUa/BXQgcHb3mh8fC4WHFxSP13JTZ3Y2O9p/Zg1JhB6t9BJyaSnmkNUhEZLevN98fKEjGFSzmljlCxUJCZ1twlzv2OTKlM37wJE0n6iJuAKgpuv9ig/ehpBElJZA8R0QCSS2axAHM2hXAWk31CwJvlMPcufPMRpjIj+sNfDV25tZ3eHS6hoA+V7+LJwQZViKPC7ABHvNvP7I49sNuW3aG7+CZfcl54JOBlb2CcWoxxHMFY5lvLCwjk0mGNGKziq/IuuWoOGYiIkOITPkXEGNdu3aM6pYoi5XBSy4FoNGf2BnmJKFb9dXrjs9N+Xkwr9DypY5up+guGxHsCWScxZfxoSddBxk+UIvFpv9W9Qr+YaRu4RNHXrd2AWDMLRDJAGuLhkbqGxX+ptqN3rumJgYjGg498Pjt5X543mcwymd0lnqpcyWZHL3vna/WeCWf0myiuwQ== 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: If a driver/subsystem tries to do an allocation after the memblock allocations have been freed and the memory handed to the buddy allocator, it will not actually be legal to use that allocation: the buddy allocator owns the memory. Currently this mis-use is handled by the memblock function which does allocations and returns virtual addresses by printing a warning and doing a kmalloc instead. However the physical allocation function does not to do this check - callers of the physical alloc function are unprotected against mis-use. Improve the error catching here by moving the check into the physical allocation function which is used by the virtual addr allocation function. Signed-off-by: James Gowans Cc: Mike Rapoport Cc: Andrew Morton Cc: Alex Graf Reviewed-by: Wei Yang --- Notes: Changes since v1: https://lore.kernel.org/all/20240614133016.134150-1-jgowans@amazon.com/ - Move this late usage check before alignment check - Replace memblocks with memblock mm/memblock.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/mm/memblock.c b/mm/memblock.c index d09136e040d3..dbb3d700247e 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -1451,6 +1451,17 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) nid = NUMA_NO_NODE; + /* + * Detect any accidental use of these APIs after slab is ready, as at + * this moment memblock may be deinitialized already and its + * internal data may be destroyed (after execution of memblock_free_all) + */ + if (WARN_ON_ONCE(slab_is_available())) { + void *vaddr = kzalloc_node(size, GFP_NOWAIT, nid); + + return vaddr ? virt_to_phys(vaddr) : 0; + } + if (!align) { /* Can't use WARNs this early in boot on powerpc */ dump_stack(); @@ -1576,13 +1587,6 @@ static void * __init memblock_alloc_internal( { phys_addr_t alloc; - /* - * Detect any accidental use of these APIs after slab is ready, as at - * this moment memblock may be deinitialized already and its - * internal data may be destroyed (after execution of memblock_free_all) - */ - if (WARN_ON_ONCE(slab_is_available())) - return kzalloc_node(size, GFP_NOWAIT, nid); if (max_addr > memblock.current_limit) max_addr = memblock.current_limit;