From patchwork Fri Apr 14 14:23: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: 13211636 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 90293C77B6E for ; Fri, 14 Apr 2023 14:26:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230403AbjDNO0y (ORCPT ); Fri, 14 Apr 2023 10:26:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231182AbjDNO01 (ORCPT ); Fri, 14 Apr 2023 10:26:27 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45E88C144; Fri, 14 Apr 2023 07:25:54 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id kh6so16856994plb.0; Fri, 14 Apr 2023 07:25:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681482353; x=1684074353; 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=/tVFETdIXCOfqp++FbGXI8ZTRFNtRU4kz7mKWpQ0TKQ=; b=V/L9mGM5vRCilsgFuug2qDo+tf+mz8X3NMe4ExWTkIzgp5PoriAleb9ABHiqBVuzoB llo+kuyF6EvctKAWOk7/AgctLIF5nzoIZG8o22MemV1Trqr4GMU4SWQjs7lk4AFfxWZs fEFh+J5+1k4h11tgMkVGCwcojSc3VhOk+jlEQkXDF+LG7ADIDJS5DzduuXdI5BA3XzGA o5xxBjrwCkRjOkZx09CVQJsh7Y3tKhqCo7zRr9YQVNfXI9o2wXUwtf+xHKgwJyn/eUFx DbBL8+08eqWZGvXOjPhYZPEEOFvmlvTmXp2I+hQAV+uqiFqt7yhXiTpblR0eG92ZfVyg zMBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681482353; x=1684074353; 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=/tVFETdIXCOfqp++FbGXI8ZTRFNtRU4kz7mKWpQ0TKQ=; b=eAPdAV+Kwf65yINl2J7xlqvEhtlEmrmfgl7zcXjoRNq2ut85AxNxMm/B/qpkDvURqm n70T0uvgHehhjQlbGgd2nc6usPdIBPLDOV9eDSPQEcHCxzYPMpeSyb462HDPLjw55tiB VfBz7Bpl29QVEqOFgKivBwIXpIDP2rP1ZrZ6ref+UTVbZfXu6c2r026gBa8mXYDn4FSM farVAKNMoiyvdclGlusE/lb2fi9HxisBp85pcMWutEb/R3ST/3yoLfISEeuLm3GifoPy iaW4XTT6bMm/5F+N+ZEl/i8KUkHDD/pCEyrqUVBZ1YFJeHOWGRo272+Pa8Y8q7IvMCoq +Ceg== X-Gm-Message-State: AAQBX9c+2cqRwF/sZoOKMFhddEReArpFrc2jGHH1sqGiSi5VW4vwSqu+ AMB0DmU8oSQYNN259RbfwXA= X-Google-Smtp-Source: AKy350bY4APGkMEn5jnT3hrPcgD0IFpWnxPux4TPQ74b4zdNFtZk3bMgcaFkmd5oasXe2ggPZ2CL0Q== X-Received: by 2002:a17:903:22ce:b0:1a5:f:a7c7 with SMTP id y14-20020a17090322ce00b001a5000fa7c7mr3342685plg.0.1681482353510; Fri, 14 Apr 2023 07:25:53 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 07:25:53 -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 10/17] mm/gup: Trigger break COW PTE before calling follow_pfn_pte() Date: Fri, 14 Apr 2023 22:23:34 +0800 Message-Id: <20230414142341.354556-11-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 In most of cases, GUP will not modify the page table, excluding follow_pfn_pte(). To deal with COW PTE, Trigger the break COW PTE fault before calling follow_pfn_pte(). Signed-off-by: Chih-En Lin --- mm/gup.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/gup.c b/mm/gup.c index eab18ba045db..325424c02ca6 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -544,7 +544,8 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) == (FOLL_PIN | FOLL_GET))) return ERR_PTR(-EINVAL); - if (unlikely(pmd_bad(*pmd))) + /* COW-ed PTE has write protection which can trigger pmd_bad(). */ + if (unlikely(pmd_write(*pmd) && pmd_bad(*pmd))) return no_page_table(vma, flags); ptep = pte_offset_map_lock(mm, pmd, address, &ptl); @@ -587,6 +588,11 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, if (is_zero_pfn(pte_pfn(pte))) { page = pte_page(pte); } else { + if (test_bit(MMF_COW_PTE, &mm->flags) && + !pmd_write(*pmd)) { + page = ERR_PTR(-EMLINK); + goto out; + } ret = follow_pfn_pte(vma, address, ptep, flags); page = ERR_PTR(ret); goto out;