Received: by 2002:a05:622a:1442:b0:3a5:28ea:c4b9 with SMTP id v2csp750403qtx; Thu, 17 Nov 2022 07:31:57 -0800 (PST) X-Google-Smtp-Source: AA0mqf63l3NWHFj/O1sbRrJOV2/xkk77DbmKoX0eeiJb9ARNdasz4ngU4V7g98TJhPSqOJGUYNUB X-Received: by 2002:a17:902:9f8f:b0:186:738e:5703 with SMTP id g15-20020a1709029f8f00b00186738e5703mr3139754plq.136.1668699117196; Thu, 17 Nov 2022 07:31:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668699117; cv=none; d=google.com; s=arc-20160816; b=JyqTMadhdQsdLc8eTS0NwmnJN9aZF0yGKt/jQzs6O3zthAdF988IKNbKjkt+qrBxOG Bk40kwStnfkEGGEFbQzkkOScrf2rqicdziO4o7QCkklBvmgE4ohg8zl6X2fwYUWIcWey Et7d2qDHythjg5HkWvn3Q8gEkUCSC8tuMNpfBsUq/OiH0yb9pFZ4JfL9S84yDpIUs0WD 4qPV4YtZ/N5gcMEuGtOil8WUiQFA33rDbOkeO5rumEJwdEFU2x/852+qrk4VZSHkPGRF 7zB0wY5bXea3d29i6sxLQmi1g8YwT1EdbeBcw0spsnlQU/11gyNGb8k99kLA0rO/tVFv N1tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=8ILTkK5az/hBLXZXqjYOlgnbsmdjpOMEywp6Rj1fgC0=; b=khG8lCn+ZA1MUAPRryPBRG9cY3DHnbW7yhjdG6NW+EqUcwT09m0iahSPMjEULHE6C3 Fsh5sc0f02h19OwonE45MPY914b35XgJJiu6ad+sThI+OnrPPrRsXPAIJ88ZLTZnA2Tt WMpFSv6du+FjBfMuVP34AEPRTvQ93E6ktPgvWhN8MBkzTftjwrnsP8/mh/K4110OFTHj F0cdST7EaNc1aS7I+/9UQrFkiYK5fjKkbU9ouEUMzj33F8Lx5IXwuAIPYqvyJl3oJh1a VGCJUg3Y3iZM9Sqe6vjwzv5GpeOrzD+KV24NFqagffOSJjeuoPbhGzCDzRbg+1+lV5en 2WIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WBocohBy; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v1-20020a631501000000b00476f87434easi1198880pgl.711.2022.11.17.07.31.44; Thu, 17 Nov 2022 07:31:57 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WBocohBy; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240510AbiKQPTY (ORCPT + 92 others); Thu, 17 Nov 2022 10:19:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234051AbiKQPSx (ORCPT ); Thu, 17 Nov 2022 10:18:53 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F32891C16 for ; Thu, 17 Nov 2022 07:12:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668697931; 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: in-reply-to:in-reply-to:references:references; bh=8ILTkK5az/hBLXZXqjYOlgnbsmdjpOMEywp6Rj1fgC0=; b=WBocohByTwwoFRSwBoeEMxHgwB23Lc4yKpg31b84YS1NJsmtDKYE1qkEqpgVNhWamzOMwf l5dtaazoy+VRBNpZoPJ8REL/+z+0+BhPyv7B632+hajFtOQSvoqDc55Reep67XapR7ahel AigzQbMGMURvk7qS7tDJD8fo8a56Wx0= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-418-alD5l4HdP62v-JMGf-gPjA-1; Thu, 17 Nov 2022 10:12:10 -0500 X-MC-Unique: alD5l4HdP62v-JMGf-gPjA-1 Received: by mail-qv1-f70.google.com with SMTP id do17-20020a056214097100b004c6183e6ce4so1821368qvb.11 for ; Thu, 17 Nov 2022 07:12:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=8ILTkK5az/hBLXZXqjYOlgnbsmdjpOMEywp6Rj1fgC0=; b=NMu6vffEmeJjdNJXuc3HmeLPlcaNNS59gzuKVpvkRPUYSIX/jtKfFRaQd+zPs9bD18 KN+RKqx5sd4QMqdKkb+C2vWbmHKwgzJbPjnfVwu6TLCjMhfPKDUy8pJz5/ml7n8AYQEC uQ0NyCkIqTafOFYS+SscgXTgKLeMvmP0wbq+5U2f+a9OjfiTf1lw0KfffPDUGGYkb0EV myHcqG2YXEhOSvkaZyfpJCQw5Dk2dQnXucNX5JrcyL5JiF+mROzwjuNcrTOJIdwaOwgP AHPP8HTd1f8lxBDcfObmVcGuSFlgMGr9pCrD4mtBP8izPxq8Lnl0XEQEypRlehRhM1i5 5WHA== X-Gm-Message-State: ANoB5pkDkALoUakX1bSG4ZLG7+5+I1thHbB0wZ/SpzDQXsrt0uuFlkQp a591dEXsKmpRJtUCcd08Ol67NdtZpoMJf21EJBr3KAsSAz4z1P6j7St5bJoCK9kf3cNtzCi699J mr18GSHiUW0r0Vk1Vg8p1qLJD X-Received: by 2002:ac8:66c5:0:b0:3a5:4666:a952 with SMTP id m5-20020ac866c5000000b003a54666a952mr2537547qtp.128.1668697929701; Thu, 17 Nov 2022 07:12:09 -0800 (PST) X-Received: by 2002:ac8:66c5:0:b0:3a5:4666:a952 with SMTP id m5-20020ac866c5000000b003a54666a952mr2537516qtp.128.1668697929400; Thu, 17 Nov 2022 07:12:09 -0800 (PST) Received: from x1n (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id c8-20020ac853c8000000b003972790deb9sm471564qtq.84.2022.11.17.07.12.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 07:12:08 -0800 (PST) Date: Thu, 17 Nov 2022 10:12:07 -0500 From: Peter Xu To: Huacai Chen Cc: Huacai Chen , loongarch@lists.linux.dev, Xuefeng Li , Guo Ren , Xuerui Wang , Jiaxun Yang , "David S . Miller" , sparclinux@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH 04/47] LoongArch: Set _PAGE_DIRTY only if _PAGE_WRITE is set in {pmd,pte}_mkdirty() Message-ID: References: <20221117042532.4064448-1-chenhuacai@loongson.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20221117042532.4064448-1-chenhuacai@loongson.cn> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 Hi, Huacai, On Thu, Nov 17, 2022 at 12:25:32PM +0800, Huacai Chen wrote: > 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); Is it safe to drop dirty bit when wr-protect? It means the mm can reclaim the page directly assuming the page contains rubbish. Consider after fork() and memory pressure kicks the kswapd, I don't see anything stops the kswapd from recycling the pages and lose the data in both processes. > 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 > --- > Note: CC sparc maillist because they have similar issues. I also had a look on sparc64, it seems to not do the same as loongarch here (not removing dirty in wr-protect): static inline pmd_t pmd_wrprotect(pmd_t pmd) { pte_t pte = __pte(pmd_val(pmd)); pte = pte_wrprotect(pte); return __pmd(pte_val(pte)); } static inline pte_t pte_wrprotect(pte_t pte) { unsigned long val = pte_val(pte), tmp; __asm__ __volatile__( "\n661: andn %0, %3, %0\n" " nop\n" "\n662: nop\n" " nop\n" " .section .sun4v_2insn_patch, \"ax\"\n" " .word 661b\n" " sethi %%uhi(%4), %1\n" " sllx %1, 32, %1\n" " .word 662b\n" " or %1, %%lo(%4), %1\n" " andn %0, %1, %0\n" " .previous\n" : "=r" (val), "=r" (tmp) : "0" (val), "i" (_PAGE_WRITE_4U | _PAGE_W_4U), "i" (_PAGE_WRITE_4V | _PAGE_W_4V)); return __pte(val); } > > 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; I'm not sure whether mm has rule to always set write bit then set dirty bit, need to be careful here because the outcome may differ when use: pte_mkdirty(pte_mkwrite(pte)) (expected) VS: pte_mkwrite(pte_mkdirty(pte)) (dirty not set) I had a feeling I miss some arch-specific details here on why loongarch needs such implementation, but I can't quickly tell. Thanks, > 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; > } > > -- > 2.31.1 > -- Peter Xu