From patchwork Fri Oct 25 02:22:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shawn Wang X-Patchwork-Id: 13849956 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 A8AB2D1038E for ; Fri, 25 Oct 2024 02:22:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3BFFA6B0093; Thu, 24 Oct 2024 22:22:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 370126B0096; Thu, 24 Oct 2024 22:22:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 237986B0099; Thu, 24 Oct 2024 22:22:19 -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 0598C6B0096 for ; Thu, 24 Oct 2024 22:22:18 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 983B58035F for ; Fri, 25 Oct 2024 02:22:01 +0000 (UTC) X-FDA: 82710524232.13.6567F10 Received: from out30-111.freemail.mail.aliyun.com (out30-111.freemail.mail.aliyun.com [115.124.30.111]) by imf24.hostedemail.com (Postfix) with ESMTP id 01098180011 for ; Fri, 25 Oct 2024 02:22:11 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=q8JpH4o1; spf=pass (imf24.hostedemail.com: domain of shawnwang@linux.alibaba.com designates 115.124.30.111 as permitted sender) smtp.mailfrom=shawnwang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729822858; a=rsa-sha256; cv=none; b=2wATh0zWeo53Oc2V/ryBqGgUVuLCQvtM+4WUwIiQhtZXD5zkjhW8cZ5FAUvIcwQpBEH0RF rW2o9dctlyInxx1GUEwKk+wxdMxq0Ha0TRFyLw01LehGO/ZxpcoymNVapxTwYnaiVyOeky 60AJuruL0Tz1rtPog17/Zx3OnBx2zHw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=q8JpH4o1; spf=pass (imf24.hostedemail.com: domain of shawnwang@linux.alibaba.com designates 115.124.30.111 as permitted sender) smtp.mailfrom=shawnwang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729822858; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=dSXBh8mdvx30sYvTAOaMIw3eRXY+OORwBiJ21iouk/U=; b=mNVcBNVlE9VwYtn+YZgzuOUyXhu2T7BMH3YR2VUZtJdawnMzLcZ3IlXm+Wzp0quM165ulZ bV0dfq+tbN1wEwfD3DqrKR1PUzyDgqNuUqJhszof8LaXLhYwMlL2Yrwiw8bB5km5cU77Fm ge3EOl06wmHApxXNbMgQ+MVgM0hnuJ8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1729822930; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=dSXBh8mdvx30sYvTAOaMIw3eRXY+OORwBiJ21iouk/U=; b=q8JpH4o1V4WXLd19i7RJ+56evjv4sOnbemv+sC5QDjZi3PWjbfh1Y6msO2kZHiM1HufXaPXeDt1u+1PqwDcE3M3QlsBJWbsxYVOdt+isV6ROU/7U6nkK2DPZGxF6zpSFxwD0WKxl+YZdEL2ZRwI1h8yEonYT2LkDR2ubyzui/Vc= Received: from localhost(mailfrom:shawnwang@linux.alibaba.com fp:SMTPD_---0WHqqNUL_1729822928 cluster:ay36) by smtp.aliyun-inc.com; Fri, 25 Oct 2024 10:22:08 +0800 From: Shawn Wang To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Liam.Howlett@oracle.com Cc: akpm@linux-foundation.org, baolin.wang@linux.alibaba.com, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, shawnwang@linux.alibaba.com Subject: [PATCH] sched/numa: Fix the potential null pointer dereference in task_numa_work() Date: Fri, 25 Oct 2024 10:22:08 +0800 Message-Id: <20241025022208.125527-1-shawnwang@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.6.gb485710b MIME-Version: 1.0 X-Stat-Signature: gwe3gwyqynpg45t3xi5iuj3d9w9bt4k6 X-Rspamd-Queue-Id: 01098180011 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1729822931-345432 X-HE-Meta: U2FsdGVkX18YlTOqTy89cgt/sWRHu/R5iSfEisF9pYaL+kn8u2vuZBETHJ+DKVeydUvfUc/tRILUEAfvxPLxlvz9n74M0rDbLxFjkD8f3WP4aCX9hFyqHlyD2zWRiHx/o9Y/ChoFgqzC4mAR5PaWMtYHvQ/+l309x8DKWRHt8kLYa5jaY3GAidZlBSlELU9HW51BMIBl+isceUX8JYuD70I9iApoMoTTVoKw6WAqm5DLLoMYk1lqTz7WBO6Dsy0CjvHpvi/TBddl4D2jAiUTAC4rAgHBlgxIPNa1vJa+midD24yLpPWN3043XPzH0r2ZtXzB13G9HDKTi5Pyq7Sdy9BA0Ef7Y6c0p+w94BgyLNeX/Gd7cvrB5k8J7hPDcv7+dWOQaqXrvXkS0/HCxarOx6mf9pL54N5V9GIooRnT6DPrVshLY4RyyvOA0uwzTzhIIWsGPUcAJ13GTuLmCaxgW+mpk/zQLVekbLvTMwN9ROJ0wbHh+rNDpkE4vHXHHP4cbyHb2vZ5dvJiA8krkHqx29ZQRo8umDKGInGDK+qqE2Y9NOSPOVUO9lLCgsfJn2HSiMloPXP6xX88iEEXyXtM5BIqMS3V8KACfZmPc+Bl8FWjJW3wWHVTZgH/IOUJL8Fu52TbRhpH9pK8j41hO9mNPxmcvmV1R33BSinPT6hCKY2XfsPRi07gHqP6C3j8XgEUU0NapU/IKbKVPTiILK6Tkt0q3MX7Ij8DX1NPKz1WzU7p+XQ9afuwHrWwGy7UeF9MOBbxeLGO7kAdX/JrxeKpZQ9rawFeHi/rIOnC2hUQOchRZEXtV29VM1HbPvrWKmbYO8+mnxjWr+EAAZATklF02BpyWmcC0DGT1NAqC7CdjPpzLZPbShq/sHnXkIq3Eo+VbNWYY7/CUpRxH3+j5jY4vcIVGEFQR1Mj5W2zT6IXM9wCnz8CjmFouW/rE/GS48r7twvcwGwW9ZGLjzqIBT0 tu6rNsup hGltb9pqvHaDOR5JedazQzN9z3pmFczoae062Dt7rCMRLD1k3y0h3vkZMb4cu5o+nRjQVceTW7vPEtzzG2q2RJQeFsWYA94uUQ51N4Gxfnzm1APvQbM4fFHIscz1nDliSo5YxXZk7KlO/N0P0NuSgwTPmWpqBBf1g0AZvBRDqmdRccW9pNgg6y37q5hVUFZjxVSRrx+fvDoLGG5EkSPtj9mC/D5inZsHJP5RtbVUXmvz//39sfFsLbBCRtzYBPxe/mXkM3g4oN/eV4EidO0X8BOmdszvWqomM6VV2P8yrs1AUUEs= 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: When running stress-ng-vm-segv test, we found a null pointer dereference error in task_numa_work(). Here is the backtrace: [323676.066985] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000020 ...... [323676.067108] CPU: 35 PID: 2694524 Comm: stress-ng-vm-se ...... [323676.067113] pstate: 23401009 (nzCv daif +PAN -UAO +TCO +DIT +SSBS BTYPE=--) [323676.067115] pc : vma_migratable+0x1c/0xd0 [323676.067122] lr : task_numa_work+0x1ec/0x4e0 [323676.067127] sp : ffff8000ada73d20 [323676.067128] x29: ffff8000ada73d20 x28: 0000000000000000 x27: 000000003e89f010 [323676.067130] x26: 0000000000080000 x25: ffff800081b5c0d8 x24: ffff800081b27000 [323676.067133] x23: 0000000000010000 x22: 0000000104d18cc0 x21: ffff0009f7158000 [323676.067135] x20: 0000000000000000 x19: 0000000000000000 x18: ffff8000ada73db8 [323676.067138] x17: 0001400000000000 x16: ffff800080df40b0 x15: 0000000000000035 [323676.067140] x14: ffff8000ada73cc8 x13: 1fffe0017cc72001 x12: ffff8000ada73cc8 [323676.067142] x11: ffff80008001160c x10: ffff000be639000c x9 : ffff8000800f4ba4 [323676.067145] x8 : ffff000810375000 x7 : ffff8000ada73974 x6 : 0000000000000001 [323676.067147] x5 : 0068000b33e26707 x4 : 0000000000000001 x3 : ffff0009f7158000 [323676.067149] x2 : 0000000000000041 x1 : 0000000000004400 x0 : 0000000000000000 [323676.067152] Call trace: [323676.067153] vma_migratable+0x1c/0xd0 [323676.067155] task_numa_work+0x1ec/0x4e0 [323676.067157] task_work_run+0x78/0xd8 [323676.067161] do_notify_resume+0x1ec/0x290 [323676.067163] el0_svc+0x150/0x160 [323676.067167] el0t_64_sync_handler+0xf8/0x128 [323676.067170] el0t_64_sync+0x17c/0x180 [323676.067173] Code: d2888001 910003fd f9000bf3 aa0003f3 (f9401000) [323676.067177] SMP: stopping secondary CPUs [323676.070184] Starting crashdump kernel... stress-ng-vm-segv in stress-ng is used to stress test the SIGSEGV error handling function of the system, which tries to cause a SIGSEGV error on return from unmapping the whole address space of the child process. Normally this program will not cause kernel crashes. But before the munmap system call returns to user mode, a potential task_numa_work() for numa balancing could be added and executed. In this scenario, since the child process has no vma after munmap, the vma_next() in task_numa_work() will return a null pointer even if the vma iterator restarts from 0. Recheck the vma pointer before dereferencing it in task_numa_work(). Fixes: 214dbc428137 ("sched: convert to vma iterator") Cc: stable@vger.kernel.org # v6.2+ Signed-off-by: Shawn Wang Reviewed-by: Liam R. Howlett --- kernel/sched/fair.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index c157d4860a3b..b4c3277cd563 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -3369,7 +3369,7 @@ static void task_numa_work(struct callback_head *work) vma = vma_next(&vmi); } - do { + for (; vma; vma = vma_next(&vmi)) { if (!vma_migratable(vma) || !vma_policy_mof(vma) || is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_UNSUITABLE); @@ -3491,7 +3491,7 @@ static void task_numa_work(struct callback_head *work) */ if (vma_pids_forced) break; - } for_each_vma(vmi, vma); + } /* * If no VMAs are remaining and VMAs were skipped due to the PID