From patchwork Tue Nov 10 23:44:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 11895833 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D8D58138B for ; Tue, 10 Nov 2020 23:44:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6B2D120825 for ; Tue, 10 Nov 2020 23:44:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nvidia.com header.i=@nvidia.com header.b="CVCrGSMb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6B2D120825 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nvidia.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 70F8A6B0036; Tue, 10 Nov 2020 18:44:20 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6BD8D6B005D; Tue, 10 Nov 2020 18:44:20 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C2216B006C; Tue, 10 Nov 2020 18:44:20 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0036.hostedemail.com [216.40.44.36]) by kanga.kvack.org (Postfix) with ESMTP id 179156B0036 for ; Tue, 10 Nov 2020 18:44:20 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id BC11E181AC9CC for ; Tue, 10 Nov 2020 23:44:19 +0000 (UTC) X-FDA: 77470139838.10.jar15_5e13ec5272f9 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id 996D416A047 for ; Tue, 10 Nov 2020 23:44:19 +0000 (UTC) X-Spam-Summary: 1,0,0,23a92f6beda0bbc3,d41d8cd98f00b204,jgg@nvidia.com,,RULES_HIT:41:69:355:379:800:960:967:973:988:989:1260:1261:1277:1311:1313:1314:1345:1431:1437:1513:1515:1516:1518:1521:1535:1542:1711:1730:1747:1777:1792:2194:2199:2393:2525:2553:2559:2565:2570:2682:2685:2703:2859:2895:2933:2937:2939:2942:2945:2947:2951:2954:3022:3353:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4118:4250:4321:4605:5007:6261:7875:7903:9010:9025:10004:10400:11658,0,RBL:203.18.50.4:@nvidia.com:.lbl8.mailshell.net-64.201.201.201 62.22.107.100;04yfsy7r5rtnokc7ssw8dckbc4radyprafcde41u5xgb8aoe6pkruripjozyn99.i57msjank4815e7jafztt7rso7nk8yi69upwqjp95r8wdeihjreax163xxjpmqu.r-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:69,LUA_SUMMARY:none X-HE-Tag: jar15_5e13ec5272f9 X-Filterd-Recvd-Size: 7184 Received: from nat-hk.nvidia.com (nat-hk.nvidia.com [203.18.50.4]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Nov 2020 23:44:18 +0000 (UTC) Received: from HKMAIL104.nvidia.com (Not Verified[10.18.92.100]) by nat-hk.nvidia.com (using TLS: TLSv1.2, AES256-SHA) id ; Wed, 11 Nov 2020 07:44:15 +0800 Received: from HKMAIL101.nvidia.com (10.18.16.10) by HKMAIL104.nvidia.com (10.18.16.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 10 Nov 2020 23:44:14 +0000 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (104.47.36.54) by HKMAIL101.nvidia.com (10.18.16.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 10 Nov 2020 23:44:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cw37daPpwJvVYTbCJWVeje021A5tMA445JV+Mtia+97DoHN/+n04ke+j24vD1cO0usR73Eq4B0htM7IENl7Do2k1JwnPxhy85ViLZkgzicZitSZ2x8SdTtCI6QmgjuC5SQD5e7LJo2VfHhgTc2EWpq84wAtnmtYew429laOGQzZ/kfS1lr5MM0E219sbTzjKpSJVPFgDEmUC1LAgODUoJGGyRi9OH2PkAI80XReQOKAyu92OFb036HtsKx2DZqur9poudPleuveFmy3E+/5akVudw5Ae+2GrZYeS8lX6G8jq7tSLB0Df0Qxpy1vL1hObDNfgX/ZeUTBTDlur1IbI6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=zE3T3/e7QFMKOiT4QdMggSeF+m2I+3ock9sMuBcNYEU=; b=VV3m9caJ647lXeKvjFdnp2pCO3+59u+1sgBP8Ih4bcoBSclsaWhJqLKX4+5u5eQlacgEwL8vxoAd/6+SSPHosaQnOYvUuYEDeLWZL6ylk9TF3Nx76jOMvDE2K+iclUNhDrEQI2IIWeZXDoUmXYO0joxrjpS6e06+e4mU7zykb/K4UDRvp25gRKtAJ8SEt56f+AZXijajaZJiCCZcWiybBmTbqCu807lmrtWd0bu7bDp3RxLlyx85ki9c3CLybRFS8AhsXZppJcypbaLx/Mwk8TkRZK0YfHhT0GuWvBmqKTwIRNtsmr+jHJQzNgcW8UyhlMTkPtDFoGzZGPHRwskgyg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none Received: from DM6PR12MB3834.namprd12.prod.outlook.com (2603:10b6:5:14a::12) by DM6PR12MB3739.namprd12.prod.outlook.com (2603:10b6:5:1c4::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.24; Tue, 10 Nov 2020 23:44:11 +0000 Received: from DM6PR12MB3834.namprd12.prod.outlook.com ([fe80::cdbe:f274:ad65:9a78]) by DM6PR12MB3834.namprd12.prod.outlook.com ([fe80::cdbe:f274:ad65:9a78%7]) with mapi id 15.20.3499.032; Tue, 10 Nov 2020 23:44:11 +0000 From: Jason Gunthorpe To: , Peter Xu , Linus Torvalds CC: "Ahmed S. Darwish" , Andrea Arcangeli , Andrew Morton , Aneesh Kumar K.V , Christoph Hellwig , Hugh Dickins , Jan Kara , Jann Horn , John Hubbard , Kirill Shutemov , Kirill Tkhai , Leon Romanovsky , Linux-MM , Michal Hocko , Oleg Nesterov Subject: [PATCH v4 0/2] Add a seqcount between gup_fast and copy_page_range() Date: Tue, 10 Nov 2020 19:44:07 -0400 Message-ID: <0-v4-908497cf359a+4782-gup_fork_jgg@nvidia.com> X-ClientProxiedBy: BL1PR13CA0145.namprd13.prod.outlook.com (2603:10b6:208:2bb::30) To DM6PR12MB3834.namprd12.prod.outlook.com (2603:10b6:5:14a::12) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from mlx.ziepe.ca (156.34.48.30) by BL1PR13CA0145.namprd13.prod.outlook.com (2603:10b6:208:2bb::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3564.21 via Frontend Transport; Tue, 10 Nov 2020 23:44:10 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1kcdJ3-0036MF-Bc; Tue, 10 Nov 2020 19:44:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1605051855; bh=sSxdLlqy5pKzAvuhddoSoAooAWo9HIhE52+4XJBO/Qo=; h=ARC-Seal:ARC-Message-Signature:ARC-Authentication-Results:From:To: CC:Subject:Date:Message-ID:Content-Transfer-Encoding:Content-Type: X-ClientProxiedBy:MIME-Version: X-MS-Exchange-MessageSentRepresentingType; b=CVCrGSMbHd2uASxbVJT9KsB/10JxnhIzfL/FC+djQI5ljNXGgr4jciX9nrhEybAsn z+qh5GBt2bRn3PAXsnJ7/ZOzkfNq+G7XUAmm8TQcMD4/BO16TKB0l+EI9fzgs50D9o GHq/spLMk6ixzplB16SCEqrqNeoCIYcCEG+RcfGtDOOerPvgIHcVFbAdzepaYOv9RB nhws5gwniBXkD7v+yWXg0p7YWFpddsPQnkJGQHIR/oWLNQJJnO7jxTm84ToLPQKH+6 Iz8GFVxsd0WOGfdK4uAWLfP+QQa/zXzbqHnM1+0bSQBwfWHEghOlS3Z7IPbgB8DbHT mtVMjpTiCDq2w== 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: As discussed and suggested by Linus use a seqcount to close the small race between gup_fast and copy_page_range(). Ahmed confirms that raw_write_seqcount_begin() is the correct API to use in this case and it doesn't trigger any lockdeps. I was able to test it using two threads, one forking and the other using ibv_reg_mr() to trigger GUP fast. Modifying copy_page_range() to sleep made the window large enough to reliably hit to test the logic. v4: - Use read_seqcount_retry() not read_seqcount_t_retry v3: https://lore.kernel.org/r/0-v3-7358966cab09+14e9-gup_fork_jgg@nvidia.com - Revise comment for write_protect_seq - Revise comment in copy_page_range - Use raw_write_seqcount_begin() not raw_write_seqcount_t_begin() v2: https://lore.kernel.org/r/0-v2-dfe9ecdb6c74+2066-gup_fork_jgg@nvidia.com - Use start not addr in lockless_pages_from_mm - Replace unsigned long casts with using the proper variable type - Update comments - Use raw_write_seqcount_t_begin() instead of open coding - Update commit messages v1: https://lore.kernel.org/r/0-v1-281e425c752f+2df-gup_fork_jgg@nvidia.com To: linux-kernel@vger.kernel.org To: Peter Xu To: Linus Torvalds Cc: Peter Xu Cc: John Hubbard Cc: Linux-MM Cc: Linux Kernel Mailing List Cc: Andrew Morton Cc: Jan Kara Cc: Michal Hocko Cc: Kirill Tkhai Cc: Kirill Shutemov Cc: Hugh Dickins Cc: Christoph Hellwig Cc: Andrea Arcangeli Cc: Oleg Nesterov Cc: Jann Horn Cc: "Ahmed S. Darwish" Jason Gunthorpe (2): mm: reorganize internal_get_user_pages_fast() mm: prevent gup_fast from racing with COW during fork arch/x86/kernel/tboot.c | 1 + drivers/firmware/efi/efi.c | 1 + include/linux/mm_types.h | 8 +++ kernel/fork.c | 1 + mm/gup.c | 117 +++++++++++++++++++++++-------------- mm/init-mm.c | 1 + mm/memory.c | 13 ++++- 7 files changed, 96 insertions(+), 46 deletions(-)