From patchwork Fri Apr 14 14:23:35 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: 13211637 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 6E3CEC77B6E for ; Fri, 14 Apr 2023 14:27:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231231AbjDNO1M (ORCPT ); Fri, 14 Apr 2023 10:27:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231209AbjDNO0j (ORCPT ); Fri, 14 Apr 2023 10:26:39 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29C79A5DE; Fri, 14 Apr 2023 07:26:04 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id v9so23781319pjk.0; Fri, 14 Apr 2023 07:26:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681482363; x=1684074363; 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=JhJhY1i1X+rQfu4Bn4P5O3hneE5Ahqf44H1V3MlMZSE=; b=WVSBGvQEWeeZCzJHUSLw6TdU7X7htMEhqLVUOYjuOm5exiNC5ijdRkufnNFt84Bcvp nBa+HXyOE083VwztPKaRnxXoBufXzhDqGmx5EU3nQdOfFARZyba4elQgihG66ZdIP+us AWQNBgqB3ZgRwKcjr/anLW7Yt0XHKvMOhoHHSJ+HOxL3sObVLefIP9AIAVNCeVEtEv1h XIqwvMreBHtnZXG4BLKofcPieosXJ8aJJOZrrwvezai6c/gmMsNouFT0iBOD95fZd4ZF KiC837Ko9b7VgCfyYEEiAshz0fl07KLeKm1Og9HKhxYYplg/Ram9QSDXV5nmw7PrXhcj UxEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681482363; x=1684074363; 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=JhJhY1i1X+rQfu4Bn4P5O3hneE5Ahqf44H1V3MlMZSE=; b=I3yWmk9/SnuWH4NgGe/+okpq54cUPsfNpqIcFmHifKA1FfwZZ7KAwF2ybTXSpjvd/N GG3BtDGjhDKwGiXFS3bVKAdycnUK/yYnBDp0xB9JXlpJVWwgwuKgrfvBTMYsetvlFpEV gIlTsNa9mnDW8YgjN4e3zKcuEtWxEpsrI9b6o9x+sW2vlqrJVf2Ew0LFW6IhvKoCF+d4 F/a3vGHUWNbh3XDRaGBYBVEFBpHiMoSVOc8/LvKIhb7JOP0ZLVrI1aeBZadllmjN8w2X IRTEfCLAEEIYdGSSrybFatLVqsdCMO+yU+oiz5NZsRs//BVklMxfFrgf1ztxR3k6YXPC bjjA== X-Gm-Message-State: AAQBX9esOUpz2Nj3PX+L3kYvJCBkcBpLxU8H133ucw/GKr8+KndtpioT +dwIOBxcWSWSRl0qlDwHN2s= X-Google-Smtp-Source: AKy350Y3WXBAfHkfEIZhG0ei3Tp0PNc3fK1Ni3ksxBjZThrmtWAJdx1hd2dMChF2jmP20depYO7fIQ== X-Received: by 2002:a17:90a:5b12:b0:244:9385:807f with SMTP id o18-20020a17090a5b1200b002449385807fmr5616908pji.44.1681482362745; Fri, 14 Apr 2023 07:26:02 -0700 (PDT) Received: from strix-laptop.. (2001-b011-20e0-1499-8303-7502-d3d7-e13b.dynamic-ip6.hinet.net. [2001:b011:20e0:1499:8303:7502:d3d7:e13b]) by smtp.googlemail.com with ESMTPSA id h7-20020a17090ac38700b0022335f1dae2sm2952386pjt.22.2023.04.14.07.25.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 07:26:02 -0700 (PDT) From: Chih-En Lin To: Andrew Morton , Qi Zheng , David Hildenbrand , "Matthew Wilcox (Oracle)" , Christophe Leroy , John Hubbard , Nadav Amit , Barry Song , Pasha Tatashin Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Peter Zijlstra , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Yu Zhao , Steven Barrett , Juergen Gross , Peter Xu , Kefeng Wang , Tong Tiangen , Christoph Hellwig , "Liam R. Howlett" , Yang Shi , Vlastimil Babka , Alex Sierra , Vincent Whitchurch , Anshuman Khandual , Li kunyu , Liu Shixin , Hugh Dickins , Minchan Kim , Joey Gouly , Chih-En Lin , Michal Hocko , Suren Baghdasaryan , "Zach O'Keefe" , Gautam Menghani , Catalin Marinas , Mark Brown , "Eric W. Biederman" , Andrei Vagin , Shakeel Butt , Daniel Bristot de Oliveira , "Jason A. Donenfeld" , Greg Kroah-Hartman , Alexey Gladkov , x86@kernel.org, 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 Subject: [PATCH v5 11/17] mm/mprotect: Break COW PTE before changing protection Date: Fri, 14 Apr 2023 22:23:35 +0800 Message-Id: <20230414142341.354556-12-shiyn.lin@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230414142341.354556-1-shiyn.lin@gmail.com> References: <20230414142341.354556-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 13e84d8c0797..a33f23a73fa5 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -103,6 +103,9 @@ static 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 @@ -312,6 +315,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;