From patchwork Tue Feb 7 03:51:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chih-En Lin X-Patchwork-Id: 13130971 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 427ACC636CD for ; Tue, 7 Feb 2023 03:55:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229500AbjBGDzV (ORCPT ); Mon, 6 Feb 2023 22:55:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229630AbjBGDyz (ORCPT ); Mon, 6 Feb 2023 22:54:55 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 145DD36FCD; Mon, 6 Feb 2023 19:54:26 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id c10-20020a17090a1d0a00b0022e63a94799so17291825pjd.2; Mon, 06 Feb 2023 19:54:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7Fn2XJ6BeCuZXUbdo2K6qkX2BSn2D/fR6IYZR7N3Gdw=; b=CCUTutBoo2pbeZ3p1iGL8KlrwP8i3sjy+zx/AKmz1K9JrJ6oIfbvMlkFe++UpZ/o9c jLR0f59TMqFDvtBg/ZmOnybfErE0kys1xmXLilQ4FNTfi+LYKDLdIAWZW2V+uXG84A8G rJ0AdKYEfh/6NMITd7LBVlt8V4YMumtaI/ciIbWz7MN+mzdB5Mx+rEKHHtpThg8oRG3L At7ypimq/NDP8NtbuqqygEIWJ/zadD+1PVG6vwc4ahN51pv4KYtCTrdPMnGhQa4H4iEe V0DTtr2SOV4h13OOuEpVbRXS1EibeJGHdSfw/VsQXeEnMfZsXT51uoS1YherAt6a013L XX+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7Fn2XJ6BeCuZXUbdo2K6qkX2BSn2D/fR6IYZR7N3Gdw=; b=VTuC/meU9lRqBuk5m5EwezpGhGMssruvVgQbPLskhLyZt8JexiItjl22JmfmYgzl9Y Pmd2FHVqNJFeYUaWUvpPTPmPpXfR29kUnL9x6NNkIlH0zHV/HF1zOgaD6ey+brMd31hT P+A8zcagzSKDXh48oPDkLEpKj8v3cysGxLjvJWSiGSPF+twQPqDOfsIV3s2FwpopnaV0 kJxPZL8g5cDTQhP1LRuwcgdyxMm1SeBBOzJ+sxLQdJnY3ysHGqLgKIbvXw1SOESm6WT1 8hxtSswDZSWdSYKstABMwbehFRC3VSfoZe4v2Xkk5SLot945QT5lQP56GID1wd1gkkKA wIqA== X-Gm-Message-State: AO0yUKWLv9g+CDV2G7s21rTfbj7JumHKVxDNew+NCmhUq8/bVE8ZVczc M6Nx3F86Aol7OXlwXzuEUBk= X-Google-Smtp-Source: AK7set8H/+HhL0xGcMIRPsUXfCYGK7THtXNOLIHlQ4vaHRULRWfe88k0qBDEJHUrIvleBegMqDXp6Q== X-Received: by 2002:a17:902:c404:b0:198:c27f:8954 with SMTP id k4-20020a170902c40400b00198c27f8954mr1407914plk.54.1675742062426; Mon, 06 Feb 2023 19:54:22 -0800 (PST) Received: from strix-laptop.hitronhub.home ([123.110.9.95]) by smtp.googlemail.com with ESMTPSA id q4-20020a170902b10400b0019682e27995sm7647655plr.223.2023.02.06.19.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Feb 2023 19:54:21 -0800 (PST) From: Chih-En Lin To: Andrew Morton , Qi Zheng , David Hildenbrand , "Matthew Wilcox (Oracle)" , Christophe Leroy , John Hubbard , Nadav Amit , Barry Song Cc: Steven Rostedt , Masami Hiramatsu , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Yang Shi , Peter Xu , Vlastimil Babka , "Zach O'Keefe" , Yun Zhou , Hugh Dickins , Suren Baghdasaryan , Pasha Tatashin , Yu Zhao , Juergen Gross , Tong Tiangen , Liu Shixin , Anshuman Khandual , Li kunyu , Minchan Kim , Miaohe Lin , Gautam Menghani , Catalin Marinas , Mark Brown , Will Deacon , Vincenzo Frascino , Thomas Gleixner , "Eric W. Biederman" , Andy Lutomirski , Sebastian Andrzej Siewior , "Liam R. Howlett" , Fenghua Yu , Andrei Vagin , Barret Rhoden , Michal Hocko , "Jason A. Donenfeld" , Alexey Gladkov , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Dinglan Peng , Pedro Fonseca , Jim Huang , Huichun Feng , Chih-En Lin Subject: [PATCH v4 09/14] mm/mprotect: Break COW PTE before changing protection Date: Tue, 7 Feb 2023 11:51:34 +0800 Message-Id: <20230207035139.272707-10-shiyn.lin@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230207035139.272707-1-shiyn.lin@gmail.com> References: <20230207035139.272707-1-shiyn.lin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org If the PTE table is COW-ed, break it before changing the protection. Signed-off-by: Chih-En Lin --- mm/mprotect.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mm/mprotect.c b/mm/mprotect.c index 61cf60015a8b..8b18cd0e5c5e 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -103,6 +103,9 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, if (pmd_trans_unstable(pmd)) return 0; + if (break_cow_pte(vma, pmd, addr)) + return 0; + /* * The pmd points to a regular pte so the pmd can't change * from under us even if the mmap_lock is only hold for @@ -314,6 +317,12 @@ static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd) return 1; if (pmd_trans_huge(pmdval)) return 0; + /* + * If the entry point to COW-ed PTE, it's write protection bit + * will cause pmd_bad(). + */ + if (!pmd_write(pmdval)) + return 0; if (unlikely(pmd_bad(pmdval))) { pmd_clear_bad(pmd); return 1;