From patchwork Wed Apr 13 12:29:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: patrick wang X-Patchwork-Id: 12811985 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 BCB2CC433F5 for ; Wed, 13 Apr 2022 12:29:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 01E8E6B0072; Wed, 13 Apr 2022 08:29:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EE7C46B0073; Wed, 13 Apr 2022 08:29:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D625A6B0074; Wed, 13 Apr 2022 08:29:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0092.hostedemail.com [216.40.44.92]) by kanga.kvack.org (Postfix) with ESMTP id C3DD26B0072 for ; Wed, 13 Apr 2022 08:29:38 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 7C34618357DAC for ; Wed, 13 Apr 2022 12:29:38 +0000 (UTC) X-FDA: 79351786836.28.77E8D68 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf22.hostedemail.com (Postfix) with ESMTP id 0066FC000A for ; Wed, 13 Apr 2022 12:29:37 +0000 (UTC) Received: by mail-pg1-f174.google.com with SMTP id i184so1184289pgc.2 for ; Wed, 13 Apr 2022 05:29:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=H7RNiZg/8gOIIES7OOcwf04Do6rZvxL0SbEAQMOefrA=; b=ol1Cr0DK4iBnjeMMAZ0tbtH8YzJRU++cW96aYH/4gd+XbhPNsev+CBDM2Br7CKQ1iL Wigc/enFmo7ZRqfwiBOOsYSAMpsj1/vVswGtX5PkeWUHkV+r/DDgE6lg8UCJhJuyStR4 DzG03G4TEfhG8RPFm3MbkTG7woMA1/F19Ty+t3UpxkO2WT5McQdMNJUc8RxEXSa9EN8p w+BhXGur0FNoDB9myvJsKqNjd/lzLXD5AwlpHG2HKnzA6s2bT24y1EA+Bu3cXQhg/Xqp E6G7naSVOBWZScMOJIc8P/7HahJm8dLj+vrW3SGmZQRryYeCKMMiL03DW+eS2+EyKCy6 TLQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=H7RNiZg/8gOIIES7OOcwf04Do6rZvxL0SbEAQMOefrA=; b=153amrN2NKmBi/uQwjo8B/y69ca2xZN/acqUy4jwx9yEEW2Hi+QAljddDS7ZHbDPxW 4vlu6VPhWZBq4gGbuXnkFVQTgJJZBg3G/d/2cUqUF9EXHgjHpjt61prRbb3fVbVTaKCs faDrpXxBpyCPV9GRDoJPcnHEgUoPGg2LNV/VFwoHLsZNN0NCSuIOneVBOJQC/rMwPDem MwJfnaNfgWfBBIiJo4OYsXwT3ZaaPu08yOFyfSO/sQCJW6n+zxiYwNnvSlI7n8w/41Go DoaeJouFdLD0c9d9jQqckLOZdu/O4jc5PO0Mo/We+CMK9mS1LlUAbsAd9/Beo9Es78ze z5lw== X-Gm-Message-State: AOAM5327ZxT2LuuLbgh8ceuH0DmuwtoEU+ZgVXhNsdB1VyvUnHBqJeQO 7gc6iSJcbb6iX9HxitIH4kg= X-Google-Smtp-Source: ABdhPJzAcODm/tZUkV37d3UPMuBYgn6AcKkV/ksm7+BT14CAeCE47JigBvPp1zMkgLchZFLCYrt08w== X-Received: by 2002:a65:4503:0:b0:382:aad5:ad7d with SMTP id n3-20020a654503000000b00382aad5ad7dmr35057977pgq.488.1649852976951; Wed, 13 Apr 2022 05:29:36 -0700 (PDT) Received: from localhost.localdomain ([101.86.206.159]) by smtp.gmail.com with ESMTPSA id t31-20020a056a00139f00b00505b8a8ac08sm12403372pfg.160.2022.04.13.05.29.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 05:29:36 -0700 (PDT) From: Patrick Wang To: catalin.marinas@arm.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, patrick.wang.shcn@gmail.com Subject: [PATCH] mm: kmemleak: take a full lowmem check in kmemleak_*_phys() Date: Wed, 13 Apr 2022 20:29:25 +0800 Message-Id: <20220413122925.33856-1-patrick.wang.shcn@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Stat-Signature: 4xrti8gqcqbrepqn86mbpr98c75n4iua Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ol1Cr0DK; spf=pass (imf22.hostedemail.com: domain of patrick.wang.shcn@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=patrick.wang.shcn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 0066FC000A X-HE-Tag: 1649852977-306841 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: The kmemleak_*_phys() apis do not check the address for lowmem's min boundary, while the caller may pass an address below lowmem, which will trigger an oops: # echo scan > /sys/kernel/debug/kmemleak [ 54.888353] Unable to handle kernel paging request at virtual address ff5fffffffe00000 [ 54.888932] Oops [#1] [ 54.889102] Modules linked in: [ 54.889326] CPU: 2 PID: 134 Comm: bash Not tainted 5.18.0-rc1-next-20220407 #33 [ 54.889620] Hardware name: riscv-virtio,qemu (DT) [ 54.889901] epc : scan_block+0x74/0x15c [ 54.890215] ra : scan_block+0x72/0x15c [ 54.890390] epc : ffffffff801e5806 ra : ffffffff801e5804 sp : ff200000104abc30 [ 54.890607] gp : ffffffff815cd4e8 tp : ff60000004cfa340 t0 : 0000000000000200 [ 54.890835] t1 : 00aaaaaac23954cc t2 : 00000000000003ff s0 : ff200000104abc90 [ 54.891024] s1 : ffffffff81b0ff28 a0 : 0000000000000000 a1 : ff5fffffffe01000 [ 54.891201] a2 : ffffffff81b0ff28 a3 : 0000000000000002 a4 : 0000000000000001 [ 54.891377] a5 : 0000000000000000 a6 : ff200000104abd7c a7 : 0000000000000005 [ 54.891552] s2 : ff5fffffffe00ff9 s3 : ffffffff815cd998 s4 : ffffffff815d0e90 [ 54.891727] s5 : ffffffff81b0ff28 s6 : 0000000000000020 s7 : ffffffff815d0eb0 [ 54.891903] s8 : ffffffffffffffff s9 : ff5fffffffe00000 s10: ff5fffffffe01000 [ 54.892078] s11: 0000000000000022 t3 : 00ffffffaa17db4c t4 : 000000000000000f [ 54.892271] t5 : 0000000000000001 t6 : 0000000000000000 [ 54.892408] status: 0000000000000100 badaddr: ff5fffffffe00000 cause: 000000000000000d [ 54.892643] [] scan_gray_list+0x12e/0x1a6 [ 54.892824] [] kmemleak_scan+0x2aa/0x57e [ 54.892961] [] kmemleak_write+0x32a/0x40c [ 54.893096] [] full_proxy_write+0x56/0x82 [ 54.893235] [] vfs_write+0xa6/0x2a6 [ 54.893362] [] ksys_write+0x6c/0xe2 [ 54.893487] [] sys_write+0x22/0x2a [ 54.893609] [] ret_from_syscall+0x0/0x2 [ 54.894183] ---[ end trace 0000000000000000 ]--- So check the address for lowmem's min boundary. Signed-off-by: Patrick Wang Acked-by: Catalin Marinas --- mm/kmemleak.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index acd7cbb82..a182f5dda 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1132,7 +1132,7 @@ EXPORT_SYMBOL(kmemleak_no_scan); void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, int min_count, gfp_t gfp) { - if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn) + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) kmemleak_alloc(__va(phys), size, min_count, gfp); } EXPORT_SYMBOL(kmemleak_alloc_phys); @@ -1146,7 +1146,7 @@ EXPORT_SYMBOL(kmemleak_alloc_phys); */ void __ref kmemleak_free_part_phys(phys_addr_t phys, size_t size) { - if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn) + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) kmemleak_free_part(__va(phys), size); } EXPORT_SYMBOL(kmemleak_free_part_phys); @@ -1158,7 +1158,7 @@ EXPORT_SYMBOL(kmemleak_free_part_phys); */ void __ref kmemleak_not_leak_phys(phys_addr_t phys) { - if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn) + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) kmemleak_not_leak(__va(phys)); } EXPORT_SYMBOL(kmemleak_not_leak_phys); @@ -1170,7 +1170,7 @@ EXPORT_SYMBOL(kmemleak_not_leak_phys); */ void __ref kmemleak_ignore_phys(phys_addr_t phys) { - if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn) + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) kmemleak_ignore(__va(phys)); } EXPORT_SYMBOL(kmemleak_ignore_phys);