From patchwork Tue Feb 7 03:51:33 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: 13130970 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 D02D7C636CC for ; Tue, 7 Feb 2023 03:55:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229710AbjBGDzD (ORCPT ); Mon, 6 Feb 2023 22:55:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230163AbjBGDyn (ORCPT ); Mon, 6 Feb 2023 22:54:43 -0500 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1946E30EB2; Mon, 6 Feb 2023 19:54:12 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id mi9so13686179pjb.4; Mon, 06 Feb 2023 19:54:12 -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=9hQ1wmYZNKbh6ubCaoCPj3vqYdMi+xiWWNvAtSowi2o=; b=K3GuS1yr0W6ph4124TteyvhSGbqph5dx0QX3w7vlsuPsCGZd4P2k77PsEPK5l23VHX cnagCyQEot5lD/H6hr+2gBVCvcYfQ0ICQ9bE7IBhyljYwAPPH9yPWzykEWWXSQi/tKOn nCkcQkwOnXlnCWQEGRnFKRjqvmk71mRAtOOapfPlKfPfn1q6YlVNo03Qnb+xloHM6ksC EfuX664vNfN+VFLbGjTsKWXaPEAjC3aBdSuZ9yKkw6OByEoHab18DU31F0Fl+qY0BhYo yL7TRMdYVWaab9Zrd0OXD5QlFZ01/W9XPxuILrCxR2+aBD0NCdQo/ULQ1o4Lc1HGZnnr nrnA== 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=9hQ1wmYZNKbh6ubCaoCPj3vqYdMi+xiWWNvAtSowi2o=; b=PqIDTHWl+zoS0tPtxRZCz52CE6XqG+RQrRvH9b/lOnh6dnTANZHI354Pt7cuqCtRbY Cq1WPhXKtYqRoUcN5eN1xDk8TaI10TemTNNrFTIfkmIlDIUbg0NuPnC2of3Hqa8CqgUh dVLiGufkml7YVJSoLesFMDUafbzoreGNlAzRP8i0HfXPcycfppZbUfrKNWaR2fUDX4fq bJAqCb82gYopdYdvv8qrljgQtEuSYlypmsrQtaX7d68E1V/CjJLXgLgYEeq2FmN9vwhM H3XUdWffVQOBmHxFlQh8esqPyRAkWrYiIEsNkUpUNCSKVe0/XkCfWzOZimP1SdUOSp6S H34g== X-Gm-Message-State: AO0yUKXsMq5M76/gvrTNsxDz9Y3mUjztzcB4ml2/9kF5Mm+PKMi68Xob lJl6HX1yxvkkTw5cZ4DetnI= X-Google-Smtp-Source: AK7set8BweMlzypQiVTtVVURVr8ilTYE/9HKHHuKVpdwfa5VakEqT0QFlksWW1Ae0vdg/3Bbk6hC7g== X-Received: by 2002:a17:902:f0ca:b0:199:9fa:7553 with SMTP id v10-20020a170902f0ca00b0019909fa7553mr1395163pla.17.1675742051331; Mon, 06 Feb 2023 19:54:11 -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.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Feb 2023 19:54:10 -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 08/14] mm/gup: Trigger break COW PTE before calling follow_pfn_pte() Date: Tue, 7 Feb 2023 11:51:33 +0800 Message-Id: <20230207035139.272707-9-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 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 f45a3a5be53a..e702c0800105 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -545,7 +545,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); @@ -588,6 +589,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;