From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5497BC28CF5 for ; Wed, 26 Jan 2022 10:19:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To: Subject:MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=QSLBarIg4zXJSCI3/RXpN/cH6FUhRBveoJmmRa4fhSU=; b=r8dRoYN1QvKPaq XJ60yAVfANYE+ScgLkj0dNavH0sLUVdTKdflkZlvppzjp8QgUu/Sp4hoiW/rImWWCXEYXIjsrQeyy qT3fG4qun1Erle45eJH/GwOJXW+ywRiMH+yBZMjwp7iFYKmrLaQrq9XQ3GW56EAUUfbtGsLgOhzPE XoJm09RgaNIeIsWSQ1ardenjwp4zyxZB0mfmPMDNhtKc7a+PSufCWNtyRlLye17CBG/4lej8O5qwS Ke8MzrnCNIpQy6LhuwrxKflXd8GFrWEz9fjoDt23FtOmZhkpV0A1h5MCpcTjS383cY4oBCl0hXOA2 zdwqRR3Kq94ELPNbAcCQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nCfNB-00BBW8-RU; Wed, 26 Jan 2022 10:17:54 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nCfN5-00BBUo-QW for linux-arm-kernel@lists.infradead.org; Wed, 26 Jan 2022 10:17:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643192266; h=from:from: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:in-reply-to:references:references; bh=SILyOQqKrzuALl9m/eqyPTkpkJunL5lLtYUAWYstulM=; b=VO3aIX8GRTLh08n4VhFPBLlGsop8SwD5KqWG0W7bDaUr0J1P3PWubkDqkqDFPpQF9yhVj1 AVBxUVfNtYD06LcvNFoTEo9ZjH/3CT1G1GK7E25cBi8pjqX0xn6eqwah7cGOLulOUOFDch z6NXYckntVS4pqjrupNi46yyeTJiHy4= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-301--fDiR9WRO1ST7fGfX31k1w-1; Wed, 26 Jan 2022 05:17:45 -0500 X-MC-Unique: -fDiR9WRO1ST7fGfX31k1w-1 Received: by mail-wr1-f70.google.com with SMTP id a11-20020adffb8b000000b001a0b0f4afe9so4094914wrr.13 for ; Wed, 26 Jan 2022 02:17:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=SILyOQqKrzuALl9m/eqyPTkpkJunL5lLtYUAWYstulM=; b=ymvjG6kZuSMALqnjf/AyS7mFj6pJyO3y610+L27TvVrSu1WzZKY+vjns6IMkhzrKzM ItT4XpVPBW7hbV76ajA8HHVaCz+5rXZ3ljqoU6EhcjaxkOgSo0ZCCokHy3IRXzVaWOOq FzvnTAfQiLFQbSvjsAA5vqfAYYSpsimPfKG+IN8A4HMChyOpPH2zxdxW9iHBGHnt3qFD QKap4aINXzalli5fyUaAoIuSgontPoGuL2M+AkHU1WEk2DE+2WPEuv4nvZx4OLWPLaqt pVP5zzj0vZsIt+qy99wK5ZGPdBGpzk0dgI/VTcE5F94DBtPsozhYFRzxD4OurrGQR1v+ hQ1A== X-Gm-Message-State: AOAM5302qwE+XjgNVMm4JrrGsJMsIsPyxmOhPlqqaaP7ygORrs5elo9u W3KA8oLZY8wOf5CwkL3nw6sHdv1pJWczUKrZ7qodeMn6C9J7h6zuudANZylybs4ZzNMDm0pMSqU 1uRkiNcFBLugk8GNImMJ4e7ejVCgC1Q/12K8= X-Received: by 2002:a5d:6190:: with SMTP id j16mr22342939wru.471.1643192264241; Wed, 26 Jan 2022 02:17:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJwPITbtNxwMRyOvup3XQ/BmJaIQPNmfEryURdj55eCY/UP2rFdKRH2JcVV8T8UOHULQZ29/gQ== X-Received: by 2002:a5d:6190:: with SMTP id j16mr22342928wru.471.1643192264026; Wed, 26 Jan 2022 02:17:44 -0800 (PST) Received: from ?IPV6:2003:cb:c709:2700:cdd8:dcb0:2a69:8783? (p200300cbc7092700cdd8dcb02a698783.dip0.t-ipconnect.de. [2003:cb:c709:2700:cdd8:dcb0:2a69:8783]) by smtp.gmail.com with ESMTPSA id 8sm23765087wrz.57.2022.01.26.02.17.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 26 Jan 2022 02:17:43 -0800 (PST) Message-ID: <3e6513f9-77ca-79e5-d185-7e9a11ec7689@redhat.com> Date: Wed, 26 Jan 2022 11:17:42 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [PATCH v3] arm64/mm: avoid fixmap race condition when create pud mapping To: Ard Biesheuvel , Jianyong Wu Cc: Justin He , Catalin Marinas , "will@kernel.org" , Anshuman Khandual , "akpm@linux-foundation.org" , "quic_qiancai@quicinc.com" , "linux-kernel@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "gshan@redhat.com" , nd References: <20211216082812.165387-1-jianyong.wu@arm.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220126_021747_992556_53358B07 X-CRM114-Status: GOOD ( 25.57 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 26.01.22 11:12, Ard Biesheuvel wrote: > On Wed, 26 Jan 2022 at 11:09, Jianyong Wu wrote: >> >> Hi Ard, >> >>> -----Original Message----- >>> From: Ard Biesheuvel >>> Sent: Wednesday, January 26, 2022 4:37 PM >>> To: Justin He >>> Cc: Catalin Marinas ; Jianyong Wu >>> ; will@kernel.org; Anshuman Khandual >>> ; akpm@linux-foundation.org; >>> david@redhat.com; quic_qiancai@quicinc.com; linux- >>> kernel@vger.kernel.org; linux-arm-kernel@lists.infradead.org; >>> gshan@redhat.com; nd >>> Subject: Re: [PATCH v3] arm64/mm: avoid fixmap race condition when create >>> pud mapping >>> >>> On Wed, 26 Jan 2022 at 05:21, Justin He wrote: >>>> >>>> Hi Catalin >>>> >>>>> -----Original Message----- >>>>> From: Catalin Marinas >>>>> Sent: Friday, January 7, 2022 6:43 PM >>>>> To: Jianyong Wu >>>>> Cc: will@kernel.org; Anshuman Khandual >>> ; >>>>> akpm@linux-foundation.org; david@redhat.com; >>>>> quic_qiancai@quicinc.com; ardb@kernel.org; >>>>> linux-kernel@vger.kernel.org; linux-arm- kernel@lists.infradead.org; >>>>> gshan@redhat.com; Justin He ; nd >>>>> Subject: Re: [PATCH v3] arm64/mm: avoid fixmap race condition when >>>>> create pud mapping >>>>> >>>>> On Fri, Jan 07, 2022 at 09:10:57AM +0000, Jianyong Wu wrote: >>>>>> Hi Catalin, >>>>>> >>>>>> I roughly find the root cause. >>>>>> alloc_init_pud will be called at the very beginning of kernel >>>>>> boot in >>>>> create_mapping_noalloc where no memory allocator is initialized. But >>>>> lockdep check may need allocate memory. So, kernel take exception >>>>> when acquire lock.(I have not found the exact code that cause this >>>>> issue) that's say we may not be able to use a lock so early. >>>>>> >>>>>> I come up with 2 methods to address it. >>>>>> 1) skip dead lock check at the very beginning of kernel boot in >>>>>> lockdep >>>>> code. >>>>>> 2) provided 2 two versions of __create_pgd_mapping, one with lock >>>>>> in it and the other without. There may be no possible of race for >>>>>> memory mapping at the very beginning time of kernel boot, thus we >>>>>> can use the no lock version of __create_pgd_mapping safely. >>>>>> In my test, this issue is gone if there is no lock held in >>>>>> create_mapping_noalloc. I think create_mapping_noalloc is called >>>>>> early enough to avoid the race conditions of memory mapping, >>>>>> however, I have not proved it. >>>>> >>>>> I think method 2 would work better but rather than implementing new >>>>> nolock functions I'd add a NO_LOCK flag and check it in >>>>> alloc_init_pud() before mutex_lock/unlock. Also add a comment when >>>>> passing the NO_LOCK flag on why it's needed and why there wouldn't >>>>> be any races at that stage (early boot etc.) >>>>> >>>> The problematic code path is: >>>> __primary_switched >>>> early_fdt_map->fixmap_remap_fdt >>>> create_mapping_noalloc->alloc_init_pud >>>> mutex_lock (with Jianyong's patch) >>>> >>>> The problem seems to be that we will clear BSS segment twice if kaslr >>>> is enabled. Hence, some of the static variables in lockdep init >>>> process were messed up. That is to said, with kaslr enabled we might >>>> initialize lockdep twice if we add mutex_lock/unlock in alloc_init_pud(). >>>> >>> >>> Thanks for tracking that down. >>> >>> Note that clearing the BSS twice is not the root problem here. The root >>> problem is that we set global state while the kernel runs at the default link >>> time address, and then refer to it again after the entire kernel has been >>> shifted in the kernel VA space. Such global state could consist of mutable >>> pointers to statically allocated data (which would be reset to their default >>> values after the relocation code runs again), or global pointer variables in BSS. >>> In either case, relying on such a global variable after the second relocation >>> performed by KASLR would be risky, and so we should avoid manipulating >>> global state at all if it might involve pointer to statically allocated data >>> structures. >>> >>>> In other ways, if we invoke mutex_lock/unlock in such a early booting stage. >>>> It might be unsafe because lockdep inserts lock_acquire/release as the >>>> complex hooks. >>>> >>>> In summary, would it better if Jianyong splits these early boot and >>>> late boot case? e.g. introduce a nolock version for >>> create_mapping_noalloc(). >>>> >>>> What do you think of it? >>>> >>> >>> The pre-KASLR case definitely doesn't need a lock. But given that >>> create_mapping_noalloc() is only used to map the FDT, which happens very >>> early one way or the other, wouldn't it be better to move the lock/unlock >>> into other callers of __create_pgd_mapping()? (and make sure no other >>> users of the fixmap slots exist) >> >> There are server callers of __create_pgd_mapping. I think some of them need no fixmap lock as they are called so early. I figure out all of them here: >> create_mapping_noalloc: no lock >> create_pgd_mapping: no lock >> __map_memblock: no lock >> map_kernel_segment: no lock >> map_entry_trampoline: no lock >> update_mapping_prot: need lock >> arch_add_memory: need lock >> >> WDYT? >> > > That seems reasonable, but it needs to be documented clearly in the code. > Just a random thought, could we rely on system_state to do the locking conditionally? -- Thanks, David / dhildenb _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel