Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp4046638pxb; Tue, 19 Apr 2022 15:47:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywnPQ4xYDRtLWpGgeBnCacqUXXxMTUYIXHS0nsxSyUwvI+ZxnfJPexTOBB+vJt0OOHhboN X-Received: by 2002:aa7:df92:0:b0:41d:84fd:5d92 with SMTP id b18-20020aa7df92000000b0041d84fd5d92mr19919967edy.337.1650408430867; Tue, 19 Apr 2022 15:47:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650408430; cv=none; d=google.com; s=arc-20160816; b=U4rNZxZlfebnXFLxCnW6lztFrv+wjNeQkRcuRb5ZYSCIvSGcO46/+7MYdWcvpmNGCh K3kH9sLwS2PvtJj9oJoUM28UvrIs3bfHaOVHqRGn5vvSXvhqLr6DDJZ4vB78H9pz25bY RIuJ64B4EFpryfV52CLNTntGMNPPtNTcKJwANvPzOr1NNaadHzcrOXg+G2ruw3NMQVbb J0joLsbeVg7XVayFS7gH5scHNEzEDYqoBNN2r9bHBzPS2do1MuAJmo0RlcafcahHrMzN levUaWgSutTR/KM3yKOQ1AiKEMcikc7wKPYcee9PejzfSYRGWPpoLFmUGzPpYgcGYYiY 35Qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=RYT3Hx+TNnGTeRDj6FUmZB/kjjskdNELNBa3eS2BbII=; b=a79vCGytdyJ7fCpKI/1MvUS++pXB6sMGIhhVdhrI8hPVm9GR0WaqcbKQ4PlFDajlKu MCjO9zyeEuBX+8yH57XeJij+zSMOJSJ2EWbV/OAFYX1iF/cRL2tmEY0PVvHrLRvRgJZD 2A1fCinpeRufcx4isPl4uIoH1v3y3FFs72FkKCIqRVDJkYhodTSZkq7K9Yo+80f32Tf1 o6//Hk9/hqeQjAX4g7SbGCcl2p51x6/12O5fJv8jtvfMjTcbmfy9E7+6GJlwKF8c1jz5 s4IrxNpbuIg1rvUjSIzi1o4fsHfFjP3aA4Rv12R3DjE4SKXMukgAUBvmQPh+vAu0UlKQ XgNA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g14-20020a056402090e00b00421b5d86019si227962edz.45.2022.04.19.15.46.47; Tue, 19 Apr 2022 15:47:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350167AbiDSIzb (ORCPT + 99 others); Tue, 19 Apr 2022 04:55:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235265AbiDSIz3 (ORCPT ); Tue, 19 Apr 2022 04:55:29 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1EC44193EF for ; Tue, 19 Apr 2022 01:52:46 -0700 (PDT) Received: from kwepemi500024.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4KjHc014LhzfZDR; Tue, 19 Apr 2022 16:52:00 +0800 (CST) Received: from kwepemm600017.china.huawei.com (7.193.23.234) by kwepemi500024.china.huawei.com (7.221.188.100) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 19 Apr 2022 16:52:43 +0800 Received: from [10.174.179.234] (10.174.179.234) by kwepemm600017.china.huawei.com (7.193.23.234) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 19 Apr 2022 16:52:42 +0800 Message-ID: Date: Tue, 19 Apr 2022 16:52:41 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: [PATCH -next v4 3/4] arm64: mm: add support for page table check To: Anshuman Khandual , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , , "H. Peter Anvin" , Pasha Tatashin , Andrew Morton , Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou CC: , , , , Kefeng Wang , Guohanjun References: <20220418034444.520928-1-tongtiangen@huawei.com> <20220418034444.520928-4-tongtiangen@huawei.com> <1c314feb-cd78-2bb3-462e-4ea3cefe122e@arm.com> <88a70fa7-b1c8-6f5c-1018-df673949785c@arm.com> From: Tong Tiangen In-Reply-To: <88a70fa7-b1c8-6f5c-1018-df673949785c@arm.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.179.234] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm600017.china.huawei.com (7.193.23.234) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 在 2022/4/19 15:10, Anshuman Khandual 写道: > > > On 4/18/22 21:17, Tong Tiangen wrote: >> >> >> 在 2022/4/18 17:28, Anshuman Khandual 写道: >>> On 4/18/22 09:14, Tong Tiangen wrote: >>>> From: Kefeng Wang [...] >>> >>> Could you explain what was expected during pmdp_collapse_flush() which when >>> failed, triggered this BUG_ON() ? This counter seems to be page table check >>> specific, could it just go wrong ? I have not looked into the details about >>> page table check mechanism. >>> >>> - Anshuman >>> . >> >> Hi Anshuman: >> >> Thanks for your job. >> >> Let me briefly explain the principle of page table check(PTC). >> >> PTC introduces the following struct for page mapping type count: >> struct page_table_check { >>         atomic_t anon_map_count; >>         atomic_t file_map_count; >> }; >> This structure can be obtained by "lookup_page_ext(page)" > > > Right. > >> >> When page table entries are set(pud/pmd/pte), page_table_check_set()  is called to increase the page mapping count, Also check for errors (eg:if a page is used for anonymous mapping, then the page cannot be used for file mapping at the same time). >> >> When page table entries are clear(pud/pmd/pte), page_table_check_clear()  is called to decrease the page mapping count, Also check for errors. >> >> The error check rules are described in the following documents: Documentation/vm/page_table_check.rst > > Snippet from that document. > > +-------------------+-------------------+-------------------+------------------+ > | Current Mapping | New mapping | Permissions | Rule | > +===================+===================+===================+==================+ > | Anonymous | Anonymous | Read | Allow | > +-------------------+-------------------+-------------------+------------------+ > | Anonymous | Anonymous | Read / Write | Prohibit | > +-------------------+-------------------+-------------------+------------------+ > | Anonymous | Named | Any | Prohibit | > +-------------------+-------------------+-------------------+------------------+ > | Named | Anonymous | Any | Prohibit | > +-------------------+-------------------+-------------------+------------------+ > | Named | Named | Any | Allow | > +-------------------+-------------------+-------------------+------------------+ > > Does 'Named' refer to file mapping ? Also what does 'Prohibit' imply here ? The > check will call out a BUG_ON() in such cases ? Right, Named means file mapping, Prohibit here trigger BUG_ON. > > page_table_check_clear() > { > > if (anon) { > BUG_ON(atomic_read(&ptc->file_map_count)); > BUG_ON(atomic_dec_return(&ptc->anon_map_count) < 0); > } else { > BUG_ON(atomic_read(&ptc->anon_map_count)); > BUG_ON(atomic_dec_return(&ptc->file_map_count) < 0); > } > } > > So in the clear path, there are two checks > > - If the current mapping is Anon, file_map_count cannot be positive and other way > - Decrement the applicable counter ensuring that it does not turn negative > > page_table_check_set() > { > if (anon) { > BUG_ON(atomic_read(&ptc->file_map_count)); > BUG_ON(atomic_inc_return(&ptc->anon_map_count) > 1 && rw); > } else { > BUG_ON(atomic_read(&ptc->anon_map_count)); > BUG_ON(atomic_inc_return(&ptc->file_map_count) < 0); > } > } > > So in the set path, there are two checks > > - If the current mapping is anon, file_map_count cannot be positive and other way > - Anon mapping cannot be RW if the page has been mapped more than once > - But then why check for negative values for file_map_count after increment ? Check for negative after increment is logically OK and <=0 should be more reasonable. > > Is there any other checks, which this test ensures, that I might be missing ? The following checks are performed when page table entry are allocated/released: __page_table_check_zero() { BUG_ON(atomic_read(&ptc->anon_map_count)); BUG_ON(atomic_read(&ptc->file_map_count)); } > >> >> The setting and clearing of page table entries are symmetrical. > > This assumption should be true for any user accessible mapping, for this test to work ? Right, if not, here is BUG_ON. However, as Pasha said: "this being new on ARM64, it is possible that the bug is in PTC/khugepaged itself." > > Also why PUD_PAGE_SIZE/PMD_PAGE_SIZE are being used here instead of directly using > generic macros such as PUD_SIZE/PMD_SIZE ? Is there a specific reason ? I did code optimization for this, in patch 1/4 of this patchset: +#ifndef PMD_PAGE_SIZE +#define PMD_PAGE_SIZE PMD_SIZE +#endif + +#ifndef PUD_PAGE_SIZE +#define PUD_PAGE_SIZE PUD_SIZE +#endif Thank you. Tong. > >> >> Here __page_table_check_pmd_clear() trigger BUGON which indicates that the pmd entry file mapping count has become negative. >> >> I guess if PTC didn't detect this exception, would there have been any problems? > > I am looking into this, not sure for now. > .