From patchwork Tue Jun 4 02:22:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhaoyang.huang" X-Patchwork-Id: 13684578 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 2E4E0C25B75 for ; Tue, 4 Jun 2024 02:23:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BD3636B008C; Mon, 3 Jun 2024 22:23:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B83726B0095; Mon, 3 Jun 2024 22:23:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A72166B009F; Mon, 3 Jun 2024 22:23:20 -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 88BEB6B008C for ; Mon, 3 Jun 2024 22:23:20 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3104F141081 for ; Tue, 4 Jun 2024 02:23:20 +0000 (UTC) X-FDA: 82191609360.09.A559FEB Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) by imf13.hostedemail.com (Postfix) with ESMTP id 5EF0A2000D for ; Tue, 4 Jun 2024 02:23:16 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf13.hostedemail.com: domain of zhaoyang.huang@unisoc.com designates 222.66.158.135 as permitted sender) smtp.mailfrom=zhaoyang.huang@unisoc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717467797; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=5OzgKsiYClhl2hthTs2jo0H8/UT1fZHyBIgFqykUrxg=; b=EMUufGHQPEZJiBZ7XeREq2qXVKIPedT6t1TcAIbgdoQwIwOUoMJmk0ViGmU4k3TrzfveyA f5C/KONH4VWuLROCtb2iRzA1xHpCz+6QYAvfHPcU+mwAzHAnIUeFVeUxevgC/Q/T48lw3w hEVtmjXkblACeqyMCc9GHSEtKfad7hg= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf13.hostedemail.com: domain of zhaoyang.huang@unisoc.com designates 222.66.158.135 as permitted sender) smtp.mailfrom=zhaoyang.huang@unisoc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717467797; a=rsa-sha256; cv=none; b=IAPCBUVCzyIx4TEQqtUSToHs9lv/7I2JaLIqD1TmnaBSQBdYD3o8bGKN2WWoaq61ulOfqk yD0Xjn0xAq31InQ9MbcZuS0pNuEAgw7EUGz7BQnppjPgYDXFGTUoX+dHTsE4KR6YvLWW15 nsjGOfiJvU1j1cnIIBt+Rvay3lzqGfY= Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 4542Mg2p074077; Tue, 4 Jun 2024 10:22:42 +0800 (+08) (envelope-from zhaoyang.huang@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4VtZ5c3Zc2z2R4pwY; Tue, 4 Jun 2024 10:18:44 +0800 (CST) Received: from bj03382pcu01.spreadtrum.com (10.0.73.40) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Tue, 4 Jun 2024 10:22:40 +0800 From: "zhaoyang.huang" To: Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Baoquan He , Thomas Gleixner , hailong liu , , , Zhaoyang Huang , Subject: [PATCHv4 1/1] mm: fix incorrect vbq reference in purge_fragmented_block Date: Tue, 4 Jun 2024 10:22:32 +0800 Message-ID: <20240604022232.1669983-1-zhaoyang.huang@unisoc.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.0.73.40] X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 4542Mg2p074077 X-Rspamd-Queue-Id: 5EF0A2000D X-Stat-Signature: b6e7uu1558zjyah7omx5aj9omt63g7ai X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1717467796-407858 X-HE-Meta: U2FsdGVkX1/4GQ+aOg/JFhJ5vA5xNiGfODg3X4nZRLk7eewY1X3LxFAYvvja5Xw9faaSumuwOPpDH2+Zh6xe/QkPfBAi5rNsyJ9qhWuVZUbJlBSTXKTNDJQg09SXLsZVXD7HYDPD0RS0kF88smNiQcgsowxEqJ9RMyNyuPuKllvabjsbgRJsT6r8FP4wQ7IJEUf0X29WoSm/VKPmFfsFg/cCk7/gB5Q4X75D7rrZxuHDsL4L/P5gkvrm+eiz6Y6UDkbaZqzAF4zNIjiejjIgcZ+JDBJUBW/cBGINXoGdoji72vQIQDi9WQjsdVl7Zs7fNeNAZMBy49qV83HfPTMnBrvAuU/2+8a8wLnU2/QPIbIHQX2A7wpTto6zp7PCsSydzeLoghcfyEWdoUwCHdxBRyCThLlRTQCXqAVyO6AZyRdOLTxNxUjLIe55Rxx8/wO8ITqAw5YHzgtwNiYhI3Tu8E8s6vRtWXOlBq0nVQagmeZd82COlFnQBsN4+8+4kpdLIyizVRtd8pfCFo1/IiS3BjQ48Oze4oPe//sJopvQQohWeP6DbgWwLd3lKm3SUitzUDb+yvGC/+CgJeMRRI8R1FDugfJjg5W83tedT1DecTJNjlJV3t/9mwR0RF7+z0ebR0G6qo8/SAOFOTnE8ATAfzAdiBYNrKwKV2DT62EjWoorvAZ/TMKlcs+9PTCwXpHaSJh2kS/HT/oK1FP4PkypHIxrX2XY2HDWG4TmovSwGXn1HL17AigFAclbJSMvJpSafUOZ+SuXv6yLtjtqiA9rOPhjQTEUC6oVNSvJs3VjfXLcRO7T9eadQpJXpoE/QqQB641948esdnk5MXAaBLJnLSPWPiNCVd9N0Orya/OiSjavkdHwZhtal05Ynx8umr9OUnE3GfrsmNSlve6iomX3B0Uek+vpRK1K+Q4nKs89jrIDXn+RAxvfq4kam5TqGafLe2pG3D53qb4KqYbz2Qc PlKiFbqm 0dICLB0rT+khS6rLGPs1hygnWTmaSwzTj110YrR7ZGG3EclxI1rLzgRE3PSDEbxi182AhtXAWP53IbobAXpjSRRL9GnZ+y9WTnwUxiOXF8PJ1NLa4D90j2WVgzqj3bZHvcLWx43DhxRnGsmiX0ka6/pwW14WHZkyxEVb1mago5oe8bAVeeCcNA57t/ZuihkpRxSOjgTg5cjJBNuWByhijSX7MRIRBpF9Wr7AKsFH20unvAjOimC7nmGBqpiM0rKY7TKTTH9eNjublR9tHO6w4Udus7QZm/LMq0xVxXIpUWNlW1junxo0amWh86YDoIwAtrEWHpEhkKG4QZ03mgQ5RTMMJAJzFo9BRgkJuDghIg29Nzsc= 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: From: Zhaoyang Huang vmalloc area runs out in our ARM64 system during an erofs test as vm_map_ram failed[1]. By following the debug log, we find that vm_map_ram()->vb_alloc() will allocate new vb->va which corresponding to 4MB vmalloc area as list_for_each_entry_rcu returns immediately when vbq->free->next points to vbq->free. That is to say, 65536 times of page fault after the list's broken will run out of the whole vmalloc area. This should be introduced by one vbq->free->next point to vbq->free which makes list_for_each_entry_rcu can not iterate the list and find the BUG. [1] PID: 1 TASK: ffffff80802b4e00 CPU: 6 COMMAND: "init" #0 [ffffffc08006afe0] __switch_to at ffffffc08111d5cc #1 [ffffffc08006b040] __schedule at ffffffc08111dde0 #2 [ffffffc08006b0a0] schedule at ffffffc08111e294 #3 [ffffffc08006b0d0] schedule_preempt_disabled at ffffffc08111e3f0 #4 [ffffffc08006b140] __mutex_lock at ffffffc08112068c #5 [ffffffc08006b180] __mutex_lock_slowpath at ffffffc08111f8f8 #6 [ffffffc08006b1a0] mutex_lock at ffffffc08111f834 #7 [ffffffc08006b1d0] reclaim_and_purge_vmap_areas at ffffffc0803ebc3c #8 [ffffffc08006b290] alloc_vmap_area at ffffffc0803e83fc #9 [ffffffc08006b300] vm_map_ram at ffffffc0803e78c0 Fixes: fc1e0d980037 ("mm/vmalloc: prevent stale TLBs in fully utilized blocks") For detailed reason of broken list, please refer to below URL https://lore.kernel.org/all/20240531024820.5507-1-hailong.liu@oppo.com/ Suggested-by: Hailong.Liu Signed-off-by: Zhaoyang Huang --- v2: introduce cpu in vmap_block to record the right CPU number v3: use get_cpu/put_cpu to prevent schedule between core v4: replace get_cpu/put_cpu by another API to avoid disabling preemption --- --- mm/vmalloc.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 22aa63f4ef63..89eb034f4ac6 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -2458,6 +2458,7 @@ struct vmap_block { struct list_head free_list; struct rcu_head rcu_head; struct list_head purge; + unsigned int cpu; }; /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ @@ -2585,8 +2586,15 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) free_vmap_area(va); return ERR_PTR(err); } - - vbq = raw_cpu_ptr(&vmap_block_queue); + /* + * list_add_tail_rcu could happened in another core + * rather than vb->cpu due to task migration, which + * is safe as list_add_tail_rcu will ensure the list's + * integrity together with list_for_each_rcu from read + * side. + */ + vb->cpu = raw_smp_processor_id(); + vbq = per_cpu_ptr(&vmap_block_queue, vb->cpu); spin_lock(&vbq->lock); list_add_tail_rcu(&vb->free_list, &vbq->free); spin_unlock(&vbq->lock); @@ -2614,9 +2622,10 @@ static void free_vmap_block(struct vmap_block *vb) } static bool purge_fragmented_block(struct vmap_block *vb, - struct vmap_block_queue *vbq, struct list_head *purge_list, - bool force_purge) + struct list_head *purge_list, bool force_purge) { + struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, vb->cpu); + if (vb->free + vb->dirty != VMAP_BBMAP_BITS || vb->dirty == VMAP_BBMAP_BITS) return false; @@ -2664,7 +2673,7 @@ static void purge_fragmented_blocks(int cpu) continue; spin_lock(&vb->lock); - purge_fragmented_block(vb, vbq, &purge, true); + purge_fragmented_block(vb, &purge, true); spin_unlock(&vb->lock); } rcu_read_unlock(); @@ -2801,7 +2810,7 @@ static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush) * not purgeable, check whether there is dirty * space to be flushed. */ - if (!purge_fragmented_block(vb, vbq, &purge_list, false) && + if (!purge_fragmented_block(vb, &purge_list, false) && vb->dirty_max && vb->dirty != VMAP_BBMAP_BITS) { unsigned long va_start = vb->va->va_start; unsigned long s, e;