From patchwork Fri Nov 25 18:58:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13056227 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5B3CC4332F for ; Fri, 25 Nov 2022 18:59:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 00D256B0078; Fri, 25 Nov 2022 13:59:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EFFED6B007B; Fri, 25 Nov 2022 13:59:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC8896B007D; Fri, 25 Nov 2022 13:59:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CB1A06B0078 for ; Fri, 25 Nov 2022 13:59:03 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A028E1C646B for ; Fri, 25 Nov 2022 18:59:03 +0000 (UTC) X-FDA: 80172876966.01.E1E9791 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 33EE1180009 for ; Fri, 25 Nov 2022 18:59:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669402741; 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; bh=RV0U9RdGRZiYinfHu3agKBivQwemACMiM9oHLAnMCi4=; b=YGGLQ7vHJaug7c+Ng5OPL3dIaWYx4lEzNl2fMLYDIKkVSLqhAu4MdUHou3GwMhQW0eKmjO lDAec9tWuOWM6QDjEq8kgzqQ+2SqTgasMJGkvxyn4yxdeW8NTywpf3GzrP6khBsK27bBl7 Is8jIDMU6CYdx60cpRLgT8vLbxpRM10= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-62-E-Dk3fgWP1iEjClqxxlWHQ-1; Fri, 25 Nov 2022 13:59:00 -0500 X-MC-Unique: E-Dk3fgWP1iEjClqxxlWHQ-1 Received: by mail-qk1-f200.google.com with SMTP id v7-20020a05620a0f0700b006faffce43b2so6243939qkl.9 for ; Fri, 25 Nov 2022 10:59:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=RV0U9RdGRZiYinfHu3agKBivQwemACMiM9oHLAnMCi4=; b=C5dbvBCsJVr+biINqwBXFiyQX5EZewatltOUf90IeGhc6xdV2jsVB3D7p2XTY9UJ00 3BF7jNtSVotF+KMILZrwQwbVF/1X1Zn+HGdAaqOZW0UZCf4WjpPqxBcRIJbGV4Qt+5Zl Gjua5yz6UatUwC5J5EMOdnypWthC6Fmf8qASQv455GhI9pYgN+R67Q00Nru5TEYGg7d8 OpoJUEBY9HvQQJVwdreAcHTdoC3fU6RZZHmkEVaqfq+A/Wn+Fj6ScS0y8ASXP0F6mmn1 XT75m0T8M+ejysnOJ43tRCjXcuaEWC5YvUKxfkDxdePFe+FBE4W3BUQbwYqC6SRmQ3As ETag== X-Gm-Message-State: ANoB5pkhAZLvWU0c+eKT6GH6DiN1vmaRhhOeRL/lvNxk72/yZuviyi42 seGBhfjOX9AwbFb9MhxXOE3DXoEaKED6T61aRVs9ioq3lIBs3tq1hYkebfCdBuSiKZY7KDUGOLM AQnIaOIUE6IJkOVojdQ1HkzBPHhEquRER+EUxhHTptG/jNlxqXqa1y0FiF5KN X-Received: by 2002:ac8:6899:0:b0:3a5:122:fbe4 with SMTP id m25-20020ac86899000000b003a50122fbe4mr37645227qtq.493.1669402739736; Fri, 25 Nov 2022 10:58:59 -0800 (PST) X-Google-Smtp-Source: AA0mqf4KEAfKu/z8lqhOzgWKnhgNda71vMnpJjEyBHAW1nvQf+e3GI5Il1jaIu6a2nqtJB01FG5a8g== X-Received: by 2002:ac8:6899:0:b0:3a5:122:fbe4 with SMTP id m25-20020ac86899000000b003a50122fbe4mr37645194qtq.493.1669402739381; Fri, 25 Nov 2022 10:58:59 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id bq38-20020a05620a46a600b006fc40dafaa2sm3212729qkb.8.2022.11.25.10.58.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Nov 2022 10:58:58 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Andrew Morton , Anatoly Pugachev , Raghavendra K T , Thorsten Leemhuis , Hev Subject: [PATCH] mm/thp: Re-apply mkdirty for small pages after split Date: Fri, 25 Nov 2022 13:58:57 -0500 Message-Id: <20221125185857.3110155-1-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669402742; a=rsa-sha256; cv=none; b=5kv5ERYQQ9BudmH7KMpfp1JaqXLlKDs7zO2FsdL8Ozeuwh7zxREy4Es+vbNtFlt35bZCBP IYkZBZadq16K1HN0Bp0h3jP/fJuOqWccGyFPRSk9NuPfq+QfzOsoBpZ934UkE+nGLx5qHb gZdx8XdoUMcvTMFCNNF58MKLy/vHHRA= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YGGLQ7vH; spf=pass (imf06.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669402742; h=from:from:sender: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: references:dkim-signature; bh=RV0U9RdGRZiYinfHu3agKBivQwemACMiM9oHLAnMCi4=; b=A+D9suV6Z2fNXkJ3NJ9+eA1oQzL0lSOL8bFB01kl85sp7FKVbyQrrPN9X0rsTqjxPhiIjr INOtvil7SLd1Ysv0cb529vy9fQXnVH99ja96m4myfriyL14r+lEw86sTGXWcJOHfvzo83N K1sSgQU45s1cLOufMBCrwA3qKjRAmH4= X-Stat-Signature: dy4cdsfjes6t7qi4wdq8wodftba8a8ar X-Rspamd-Queue-Id: 33EE1180009 X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YGGLQ7vH; spf=pass (imf06.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam10 X-HE-Tag: 1669402741-499512 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: We used to have 624a2c94f5b7 fixing the regression reported here by Anatoly Pugachev on sparc64: https://lore.kernel.org/r/20221021160603.GA23307@u164.east.ru Where we temporarily ignored the dirty bit for small pages. Then, Hev also reported similar issue on loongarch: (the original mail was private, but Anatoly copied the list here) https://lore.kernel.org/r/CADxRZqxqb7f_WhMh=jweZP+ynf_JwGd-0VwbYgp4P+T0-AXosw@mail.gmail.com Hev pointed out that the issue is having HW write bit set within the pte_mkdirty() so the split pte can be written after split even if e.g. they were shared by more than one processes, causing data corrupt. Hev also tried to explain why loongarch set HW write bit in mkdirty: https://lore.kernel.org/r/CAHirt9itKO_K_HPboXh5AyJtt16Zf0cD73PtHvM=na39u_ztxA@mail.gmail.com One way to fix it is as what Huacai proposed here for loongarch (then we can re-apply the dirty bit in thp split): https://lore.kernel.org/r/20221117042532.4064448-1-chenhuacai@loongson.cnn We may need similar thing for sparc64, though. For now since we've found the root cause of the dirty bit issue the simpler solution (which won't lose the dirty bit for small) that will work for both is we wr-protect after pte_mkdirty(), so the HW write bit can be persistent after thp split. Add a comment for wrprotect, so we will not mess up the ordering later. With 624a2c94f5b7 this is not a fix anymore, but just brings back the dirty bit for thp split safely, so we re-apply the optimization but in safe way. Provide a T-B credit to Hev too (not the exact same patch but the same outcome) for loongarch. Cc: Anatoly Pugachev Cc: Andrew Morton Cc: Raghavendra K T Cc: Thorsten Leemhuis Tested-by: Hev # loongarch Signed-off-by: Peter Xu --- PS: Anatoly, feel free to try this too on sparc64. Applicable to either branch (Linus's, or akpm's) as long as latest. Signed-off-by: Peter Xu --- mm/huge_memory.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index ae1e2d80bb6e..5f93ba064323 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2192,16 +2192,18 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, entry = maybe_mkwrite(entry, vma); if (anon_exclusive) SetPageAnonExclusive(page + i); - if (!write) - entry = pte_wrprotect(entry); if (!young) entry = pte_mkold(entry); + /* NOTE: this may set soft-dirty too on some archs */ + if (dirty) + entry = pte_mkdirty(entry); /* - * NOTE: we don't do pte_mkdirty when dirty==true - * because it breaks sparc64 which can sigsegv - * random process. Need to revisit when we figure - * out what is special with sparc64. + * NOTE: this needs to happen after pte_mkdirty, + * because some archs (sparc64, loongarch) could + * set hw write bit when mkdirty. */ + if (!write) + entry = pte_wrprotect(entry); if (soft_dirty) entry = pte_mksoft_dirty(entry); if (uffd_wp)