From patchwork Thu Nov 17 04:25:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huacai Chen X-Patchwork-Id: 21428 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp212903wrr; Wed, 16 Nov 2022 20:47:24 -0800 (PST) X-Google-Smtp-Source: AA0mqf5vdMdeKZHpZNrS1MhLmx5jv3P3oAYkkNUMpIQWXub8ynIG6wTUyAcyaatxNZzkMJvVEFHg X-Received: by 2002:a05:6402:1002:b0:467:7c62:64 with SMTP id c2-20020a056402100200b004677c620064mr712854edu.82.1668660444243; Wed, 16 Nov 2022 20:47:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668660444; cv=none; d=google.com; s=arc-20160816; b=rSrpooJkJkCxvB22w5PX0Am/9IvX4sQnhTp7T7Kk+ULIzJWigfsdJQkQWuQZb96HfE JvKt78wP25tT+RQPUEfTwkhfbm9TjwkLV9YIZuejOPZuk2n/U6NZdS/5t/RUwxRqv++4 2RQ8Kw3nOTfJATDSQHQQmIadV0FwywuC/Ewksjpo/DwoUKHHcSMO4YEYCBPcawnh4Ujv sjCs4rKKXu0cbfuU61BdHirZKxKQKyCHFOaMqnwYPz5/zwmQ5rR4zA8AVxxndCrroAZB a6/j5SmmXjtsIeCxGzJ46azCuHLzG7rBFNarp1w+/E4rVRAVn18wYtbXgJDbHqsp1FgV 5h6g== 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 :message-id:date:subject:cc:to:from; bh=/eND6FtSpvrIOFTjT9NEU1TEX8WD4mAvR/VhDNdt+UA=; b=jNQWZiYwAlTxNmmCkNw7ww0LfknuGueT3T5tP2JHoQRqSvIt1Bs+CGpVPenU2xar+c czdJzcEI+/Nf5Dpi9VfiYTKtOcSwRI2AgfOQ2zlSrnCBgzWwDvq8p47KlYAyfPaytCzC 02/tvd1KM4R2J42OAOmBTCrhtZXrJr+2tZ6zfsWElWDhEqt/n9wEnoFRGBid1Hk+wNlw wOH+VpxuI+stxRVhRI0Uuu9o7weIa5SFMdLD6CeRnaeLzl1SahFWu5X6sli2A7YckkiY 7q8Wf1zKj8n5ukPUq6/5vY4pPJuG+XSK8qGs8w/NE5fDWnuzmBjPB48/Wb92OAIeUw4a IytA== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f12-20020a056402354c00b004608c0b9a8asi52315edd.201.2022.11.16.20.46.58; Wed, 16 Nov 2022 20:47:24 -0800 (PST) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238944AbiKQE2f (ORCPT + 99 others); Wed, 16 Nov 2022 23:28:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233477AbiKQE2b (ORCPT ); Wed, 16 Nov 2022 23:28:31 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4041D6464; Wed, 16 Nov 2022 20:28:28 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 9D46D61FF8; Thu, 17 Nov 2022 04:28:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 369D6C433D6; Thu, 17 Nov 2022 04:28:23 +0000 (UTC) From: Huacai Chen To: Huacai Chen Cc: loongarch@lists.linux.dev, Xuefeng Li , Guo Ren , Xuerui Wang , Jiaxun Yang , "David S . Miller" , sparclinux@vger.kernel.org, linux-kernel@vger.kernel.org, Huacai Chen , stable@vger.kernel.org, Peter Xu Subject: [PATCH 04/47] LoongArch: Set _PAGE_DIRTY only if _PAGE_WRITE is set in {pmd,pte}_mkdirty() Date: Thu, 17 Nov 2022 12:25:32 +0800 Message-Id: <20221117042532.4064448-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749717294237909274?= X-GMAIL-MSGID: =?utf-8?q?1749717294237909274?= Now {pmd,pte}_mkdirty() set _PAGE_DIRTY bit unconditionally, this causes random segmentation fault after commit 0ccf7f168e17bb7e ("mm/thp: carry over dirty bit when thp splits on pmd"). The reason is: when fork(), parent process use pmd_wrprotect() to clear huge page's _PAGE_WRITE and _PAGE_DIRTY (for COW); then pte_mkdirty() set _PAGE_DIRTY as well as _PAGE_MODIFIED while splitting dirty huge pages; once _PAGE_DIRTY is set, there will be no tlb modify exception so the COW machanism fails; and at last memory corruption occurred between parent and child processes. So, we should set _PAGE_DIRTY only when _PAGE_WRITE is set in {pmd,pte}_ mkdirty(). Cc: stable@vger.kernel.org Cc: Peter Xu Signed-off-by: Huacai Chen Tested-by: Xi Ruoyao --- Note: CC sparc maillist because they have similar issues. arch/loongarch/include/asm/pgtable.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h index 946704bee599..debbe116f105 100644 --- a/arch/loongarch/include/asm/pgtable.h +++ b/arch/loongarch/include/asm/pgtable.h @@ -349,7 +349,9 @@ static inline pte_t pte_mkclean(pte_t pte) static inline pte_t pte_mkdirty(pte_t pte) { - pte_val(pte) |= (_PAGE_DIRTY | _PAGE_MODIFIED); + pte_val(pte) |= _PAGE_MODIFIED; + if (pte_val(pte) & _PAGE_WRITE) + pte_val(pte) |= _PAGE_DIRTY; return pte; } @@ -478,7 +480,9 @@ static inline pmd_t pmd_mkclean(pmd_t pmd) static inline pmd_t pmd_mkdirty(pmd_t pmd) { - pmd_val(pmd) |= (_PAGE_DIRTY | _PAGE_MODIFIED); + pmd_val(pmd) |= _PAGE_MODIFIED; + if (pmd_val(pmd) & _PAGE_WRITE) + pmd_val(pmd) |= _PAGE_DIRTY; return pmd; }