Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp728249pxb; Tue, 5 Apr 2022 20:38:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4yeIqceW3E6k2itMzziO0LdGp5rI+9Pk6GZYcZm93jmz3OCtgEgqeNwoIv/Xw0U0js/cL X-Received: by 2002:a05:6a00:890:b0:4f6:686e:a8a9 with SMTP id q16-20020a056a00089000b004f6686ea8a9mr6905625pfj.83.1649216280160; Tue, 05 Apr 2022 20:38:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649216280; cv=none; d=google.com; s=arc-20160816; b=GtK9Cd0UeAnsipcYqUSyIYVS7eHH79SjBkDV5ZQLlVC+kLLwiw9WNGxDYqeXmVhoGf tGuTxkiRy72TIBPOdRw3cBal2YKbEbYknXnNVZKaZ6bDPhIKZoINzq9UhhaafBm9ilNE U4c4b0haQm8jyU+PjPUD3djrQfm5AYXWXLRPftvxBjczKdvTL5LZzU4bDyFyc0vQCu/C +jJScI9sBn8qpDqxz5KqejCQ2qGJA8SqEcp1FdyQ9ugznkN+60oJlzTFAQIXLbVttjrs 7aF2wDKlIidonws86v2D8hZtKzQ7j+D0LI9DDyakz8rYlO/DSTAkMcxNIImeU5RTUKCd Y5QQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=E5sDRh6HevcWQo3jlwEevgjnnCbgCV0/OzaluCSbnzc=; b=siropvm7PrYCJb3oIpJf7wd+sodSrek154zj5CTfd3OeSR3Inz6MnCr7GVHMA/rUHT kG/kaJ4ciGkjAiRLbnkXjJDnAQC+recMcMNx8zcBFSwHIqIn/JVbKBhxpHdJPTZ5CJzw pcfGHx/NHy4I+0scQhJc6DD5r7MDoQVU78mIKk2RxnP4HqkSYrI7IFKhlo24i6nrkvpq dtWKKpiAq+vLtpICai0Xdign4Ohaop05hUStPYD4XCR9LbcNBGoxWAO+i6ezj2iW5DIP SmssYB4neAM1exP2lGhAUEGE53+GgqC6eBGsoX7PgJ9XLAKmU0iHk5XvfX2fytIeAFG1 tSOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jKdu1ir8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id w7-20020a6556c7000000b003985b5d10dbsi14875418pgs.384.2022.04.05.20.37.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 20:38:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jKdu1ir8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 620A6326008; Tue, 5 Apr 2022 20:05:52 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352142AbiDEMIb (ORCPT + 99 others); Tue, 5 Apr 2022 08:08:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244466AbiDEIwI (ORCPT ); Tue, 5 Apr 2022 04:52:08 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10BB8D64C2; Tue, 5 Apr 2022 01:41:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 18838B81B92; Tue, 5 Apr 2022 08:40:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 843D7C385A0; Tue, 5 Apr 2022 08:40:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649148049; bh=nK806L/HXQjI0JbXIYqotYrEiT9JChqTmGRmHJKNS4I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jKdu1ir8SB/e0ciR+/XAH/4DD8U+kMiG3SOA6nj+dGD/EuWtUtCn3AY0fW99y+21/ qqk6IDiuuqgEVLvsPg+dEFE7Bo3bAW9p6fNjcmILOZGDwEVwXGsPsQ4Is8ROBAPUjr qu3NnEFvJt4nKPvrKIktYHO0YY/wGL9QoDU1cZwk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jianyong Wu , Catalin Marinas , Will Deacon , Sasha Levin Subject: [PATCH 5.16 0215/1017] arm64/mm: avoid fixmap race condition when create pud mapping Date: Tue, 5 Apr 2022 09:18:48 +0200 Message-Id: <20220405070400.633299036@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jianyong Wu [ Upstream commit ee017ee353506fcec58e481673e4331ff198a80e ] The 'fixmap' is a global resource and is used recursively by create pud mapping(), leading to a potential race condition in the presence of a concurrent call to alloc_init_pud(): kernel_init thread virtio-mem workqueue thread ================== =========================== alloc_init_pud(...) alloc_init_pud(...) pudp = pud_set_fixmap_offset(...) pudp = pud_set_fixmap_offset(...) READ_ONCE(*pudp) pud_clear_fixmap(...) READ_ONCE(*pudp) // CRASH! As kernel may sleep during creating pud mapping, introduce a mutex lock to serialise use of the fixmap entries by alloc_init_pud(). However, there is no need for locking in early boot stage and it doesn't work well with KASLR enabled when early boot. So, enable lock when system_state doesn't equal to "SYSTEM_BOOTING". Signed-off-by: Jianyong Wu Reviewed-by: Catalin Marinas Fixes: f4710445458c ("arm64: mm: use fixmap when creating page tables") Link: https://lore.kernel.org/r/20220201114400.56885-1-jianyong.wu@arm.com Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- arch/arm64/mm/mmu.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 044d2021c20c..37b8230cda6a 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -63,6 +63,7 @@ static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused; static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused; static DEFINE_SPINLOCK(swapper_pgdir_lock); +static DEFINE_MUTEX(fixmap_lock); void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd) { @@ -329,6 +330,12 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, } BUG_ON(p4d_bad(p4d)); + /* + * No need for locking during early boot. And it doesn't work as + * expected with KASLR enabled. + */ + if (system_state != SYSTEM_BOOTING) + mutex_lock(&fixmap_lock); pudp = pud_set_fixmap_offset(p4dp, addr); do { pud_t old_pud = READ_ONCE(*pudp); @@ -359,6 +366,8 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, } while (pudp++, addr = next, addr != end); pud_clear_fixmap(); + if (system_state != SYSTEM_BOOTING) + mutex_unlock(&fixmap_lock); } static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, -- 2.34.1