From patchwork Fri Nov 6 15:55:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 11887313 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 D911F15E6 for ; Fri, 6 Nov 2020 15:55:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 84CD522203 for ; Fri, 6 Nov 2020 15:55:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nvidia.com header.i=@nvidia.com header.b="bsRXBzg4" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 84CD522203 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 0B1B56B006C; Fri, 6 Nov 2020 10:55:27 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 071166B0070; Fri, 6 Nov 2020 10:55:27 -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 E92F86B006E; Fri, 6 Nov 2020 10:55:26 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0042.hostedemail.com [216.40.44.42]) by kanga.kvack.org (Postfix) with ESMTP id AC57A6B0068 for ; Fri, 6 Nov 2020 10:55:26 -0500 (EST) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 5B3698249980 for ; Fri, 6 Nov 2020 15:55:26 +0000 (UTC) X-FDA: 77454443052.01.milk72_2f14898272d3 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin01.hostedemail.com (Postfix) with ESMTP id 377E010047854 for ; Fri, 6 Nov 2020 15:55:26 +0000 (UTC) X-Spam-Summary: 1,0,0,e9ead17c1cc50c62,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:2564:2682:2685:2859:2895:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4118:4250:4605:5007:6261:6653:6742:7550:7875:7903:9010:9025:10004:10226:10400:11026:11658:11914:12043:12291:12296:12297:12438:12519:12521:12679:12683:12895:13846:14096:14097:14181:14394:14721:21080:21433:21444:21451:21627:21939:30054:30060:30064:30070:30090,0,RBL:203.18.50.4:@nvidia.com:.lbl8.mailshell.net-64.201.201.201 62.22.107.100;04yfdbu9854hnwntrdzbyftbwgpubyprafcde41u5xgb8aoe6pk1t6fxrxgmias.c31ntoenk4815e7jafztt7rso3judj1mcbxqfoquh4im5hzbn6cwc1zqh86r6er.w-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck: none,Dom X-HE-Tag: milk72_2f14898272d3 X-Filterd-Recvd-Size: 7037 Received: from nat-hk.nvidia.com (nat-hk.nvidia.com [203.18.50.4]) by imf45.hostedemail.com (Postfix) with ESMTP for ; Fri, 6 Nov 2020 15:55:24 +0000 (UTC) Received: from HKMAIL103.nvidia.com (Not Verified[10.18.92.100]) by nat-hk.nvidia.com (using TLS: TLSv1.2, AES256-SHA) id ; Fri, 06 Nov 2020 23:55:19 +0800 Received: from HKMAIL104.nvidia.com (10.18.16.13) by HKMAIL103.nvidia.com (10.18.16.12) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Fri, 6 Nov 2020 15:55:18 +0000 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (104.47.36.55) by HKMAIL104.nvidia.com (10.18.16.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Fri, 6 Nov 2020 15:55:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Izw69P4sFmyQTENqCV0SaD7ej7OdXTB3q1zR0nEt3Cw+NzchOKuYcZOmUNDA6Oj8MxlJNGPrv87H6P+3gzs/kHjhHgxrhVUSYRyI5CJcgF9GHgMDwoFIvvjut7UzKGGFrRpEbp3fIZImPdLe8SIOzQKBszAGe0ZJpByDMIEz1LiZIiU0m2mb7ajJVhb6deoxi7jZ+Nw+eucRpYTAeBM96lHyedSkHb3DOB1WzNbsdqZwzPC3dn8DoDs1ETElhjEvaRfj4/jiAFy8ihVo+RgYj1Wm2m+HKWAkEnX87lazw/CQRNmmTKKkkPlwL0AgDNIZTbDQonrrjptFruVlpc6v/w== 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=Y4Qy+oLPmuyVMsQaxEZxutdixiNMMD8gkxyVmg4tMGQ=; b=W+cXmJjf00FUO08wP53bfeBJj5nx31PoYPgWQ9eBBPyAuBo9GtsE4k1jYvbsLOND5ppc8PfPhZtsDTi33CNt4BhrDZ/m7wwpBtwKdYuFMA6OfehqNj0rqm513lLXKL0QLhJmY+QJkbqnCNcaWiYIaTEumaMmia4/ny722FDJY4wUXkTEe7VGl8DhdIVt3i2zuQ9UQmimzr2SxjNRwTlilYQlbYv6TAa1oaitR9KVxUWseV+sKx8Muzx27I+pcuHvZ6v5j/PuKzf5obLazYAU+L6TxzAH/PJ2NAnF6TSDWvY6m3/xanB7778gB+Ngz21hU0LHtwG0erUWCLrM/8Jilw== 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 DM6PR12MB4617.namprd12.prod.outlook.com (2603:10b6:5:35::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.18; Fri, 6 Nov 2020 15:55:16 +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; Fri, 6 Nov 2020 15:55:16 +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 v3 0/2] Add a seqcount between gup_fast and copy_page_range() Date: Fri, 6 Nov 2020 11:55:12 -0400 Message-ID: <0-v3-7358966cab09+14e9-gup_fork_jgg@nvidia.com> X-ClientProxiedBy: MN2PR04CA0020.namprd04.prod.outlook.com (2603:10b6:208:d4::33) 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 MN2PR04CA0020.namprd04.prod.outlook.com (2603:10b6:208:d4::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3541.21 via Frontend Transport; Fri, 6 Nov 2020 15:55:15 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1kb454-000vSk-B1; Fri, 06 Nov 2020 11:55:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1604678119; bh=j+kIuZ0lK5KiIT4Ebi/8Z2RmX4RefiP5M2kW/JXd4Qc=; 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=bsRXBzg4+vg0H0MbUnpcC3WwUriy8pnw+ErvhicNr8Avsk9nNOjdrCRJn0iEiswWx u7zjFi8Ac1SzhfmIxMiKZJwSfKVGfKOsYO15OUAiXiy5BKXTjvA4StK7ukZHQLaBdY g8lOfVJp1BY+UdmbkHhbrTRmh/Rk+hDRzsVJHGtX0ZVQeVHl2MbEu6d8KIJ+B9NjYz Fo0tkLZPSQeBglCpMBbrY2czkb1TcyuC4J8qzS0uNRvy8BUMEhGptjJi5Mu+FhOQph AKWQ337zEcCjjXHLXtp/vOS4an0wiz/WoiVLAACpe5EsVQ1pWAbgcQ3/4yMnnUOOAP QHKP+dRR7vcJA== 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. v3: - 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 | 118 +++++++++++++++++++++++-------------- mm/init-mm.c | 1 + mm/memory.c | 13 +++- 7 files changed, 97 insertions(+), 46 deletions(-)