From patchwork Mon Feb 7 04:26:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12736907 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 E61F3C433EF for ; Mon, 7 Feb 2022 06:18:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237409AbiBGGRt (ORCPT ); Mon, 7 Feb 2022 01:17:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351618AbiBGE1T (ORCPT ); Sun, 6 Feb 2022 23:27:19 -0500 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2057.outbound.protection.outlook.com [40.107.96.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1702FC061A73; Sun, 6 Feb 2022 20:27:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ip9FjhPsLXsClgXYq/+npo1fDVlt8g/VBGsawW13wBIkjCJ4U/TprNaMjX2PDisWvjqDdsytJfCv5MX+5XYXQ+qco1JawNJSL1zOfDO3Qlx2z6JjbIF9RqNjs1qcKO/dildSr+YIDDSPyqJQF4AOfl2m/HHYWgSSEkv/7hGAsA3UIiogA8eX/3pDIzdOw9rYgvIyi2ErpT5YPk+Rj3O0S6EA3UTaP9SoZMmGIyShP4FQTmLLF46N21w8hjNB9fyfbj+k9YxsfVZQ4ElCukWRPlZx8xZh7PwKElgCHlQ8xT8yY31/eRp/Tth2hKWIAtfx7LLdme2BV0DXlQsU3CYg9A== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=bkCCpS26hw2Y862kKJ7D5CWLltn6i0oJOjrup6h+qYw=; b=jiJEisczFw/E5NahcRjKKNGBy5zoHoGrr9ibyjqojQh1XkZQKclYwbTspCejioyvVfAIfiDKKBKoR3EHRTz91323TeTquHfovFdInU7iS/UoEark48JVIkfTx4BpHpulBCR5dEUS27+gbST1pR/NJo+F/E6St+/s6GQEGPBRbQgsoLHKGPzFNpM7QbHXxh29Qb/MwXVS+KygFDCiwW9CnsoW6dqnl5HrLP1LatmbaMnFwSJcaRbabJZdEp7pemz0aqQ88ppt81752G0+vOB34dJ9yi/cPEGmYUSKU8NI3XQa5x1aVgs3rygwydR56OiCHjM0IcJvFb6bGrGYWddGAg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bkCCpS26hw2Y862kKJ7D5CWLltn6i0oJOjrup6h+qYw=; b=ZUq35hzOLA51MTSPYgYVVJF1Mh2hhHd11tUTe5SLJDoDG+Ir0pxvaLqAusCU5ZfHel0Im+nRkZ5FLq8mV10JYPJEbJ9r6TbkvJG7hNzqD2y56UddxmG90k9q8YZa6yV5Kw6GaSwsiiRbB7kFxqIdl8b9u4rLwFd7cCSSAYyYB3lxfhCnomR8M4TU9cYuQb5w9Th3pFI5PBZhWveBLRx0S1RWBG/naTYeADSf5qaK2R2qpl+s3MwKHCqG0W8YAUhTCpvxUPsDz0o1zzjT2+mNCkDHpkBQsVS7bxvOqZpUSubk1hNXs6pygNbkOUpULL4gns2BysfG4tIY2LZqnD+6KQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by BL0PR12MB4705.namprd12.prod.outlook.com (2603:10b6:208:88::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.14; Mon, 7 Feb 2022 04:27:17 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::1528:82a2:aa0:7aa6]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::1528:82a2:aa0:7aa6%5]) with mapi id 15.20.4951.018; Mon, 7 Feb 2022 04:27:17 +0000 From: Alistair Popple To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: Felix.Kuehling@amd.com, rcampbell@nvidia.com, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, hch@lst.de, jgg@nvidia.com, jglisse@redhat.com, willy@infradead.org, alex.sierra@amd.com, jhubbard@nvidia.com, Alistair Popple Subject: [PATCH v2 1/3] migrate.c: Remove vma check in migrate_vma_setup() Date: Mon, 7 Feb 2022 15:26:46 +1100 Message-Id: <6831bf69f7c7699be83b31c9c56212b5fb07f873.1644207242.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-ClientProxiedBy: SJ0P220CA0004.NAMP220.PROD.OUTLOOK.COM (2603:10b6:a03:41b::9) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 56364539-0659-4834-2024-08d9e9f21f8c X-MS-TrafficTypeDiagnostic: BL0PR12MB4705:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5236; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PcwLGcEYFltu9vjb1KDAdusecsWLx9NiW6J+q+NsVAtH3cAgW+Ea6vHHxthAu3XlD5egTSpDzgUCUe/7lCQYqlTLocu/l+uYpluFy+Op28+XbDRJuymMy4SLv2BQxUZGMoKDNOF4mo1kNmH2nKKLJjWEakBwOwbLYndWQMdiq650oX3EdYw5DCrCZazm2wV4kbEGi0rm4TavZTuH5kfGeWEZkkArkDH8RKtLtOizoWJPgu2jxPCsGUSRdsxZ16UoqvMxCTTxAM0FVsghr5fRGXT/hqrWkDdjQLq2DSJLXeDiHRBi9vSwiuRQINmP8Xu/hks+DaeQbA1oe1jhOD0KIe14jwX+3o4ud9koqgQwFMAJuef8jlLcrhXjZ70opULcgZm2eFhJ1rYqhCGZq4oHXOgYkMA2QslmFd38O+YvmMEkQMMtm5uyMeMOGrnrTh3Yq3/7zClHhBKpz1ljGvoMWOxSJCXW+2+fjl7XhwF6+4ZtJWcd4rttR6DkjrHFBEW+rKUmMGFXHHYn8zAxdsnNcUNvBPBst/G2o46f7DVDTJ6om3VhaceBP7LrrrWWF3e8VhJlkc/d253pVldA4gnKMMR2weQdypvRKRks9L3nGhABkyj6IqDkT7DpWQUCQeoGghf3pp2crWoHLMGxVDJJsg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(86362001)(107886003)(6486002)(2906002)(508600001)(26005)(186003)(6666004)(6506007)(2616005)(6512007)(316002)(66946007)(8936002)(36756003)(83380400001)(7416002)(38100700002)(5660300002)(4326008)(66476007)(66556008)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: WBmy1DBefPgkW902O9LJg1JrrOxelPjRIpkuobgDkkMqhJq+bZTTQOPL1AghsJ5FxsWYVv4mqub/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iHCGIME8E93MLJ15srLIOBUE4XGWrw0jjh+g7xl5WRJMF37XNnyH9dDcxlPyhnQ86C3zD1M7gnWsrBnFBMnCki0kPHt7L2sZkFbYFIFtBNq8WmpZgdhB18y7hX3f8eqLyHErEhRa8vdPSYI8465MVaXfC7zKzzsPh7NJZuWHc03uAhmVJKhvPwDVSMtdRGWZggsy/eIhW/Y5dZ6Q5r60886DcRjE8ZvvUjbRlf8/t9z/tXdHCkadgpxtYqcBblfpOYZ6Syv0XIPMK3Qs7E/3bqi0sJJXVuJyao0mbzjqHLd78WMSginxhzllpW8D9TWzfzCORjrY5lXphuD0g1IRE3HW0jG+Djc+3IddLwYzNpS40ORWgprhcR9uZHGRFvoEfftuQGQV0Qv8ODsrsvxkKWQNFutL1P8UswfB10cXsms9HqDuWs0BSsyYdkeEbtADOUZDrKc8zJJDYSfNs9XYpPQ/hCGTW72mO/dVAWQ= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 56364539-0659-4834-2024-08d9e9f21f8c X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Feb 2022 04:27:17.1207 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 38sdliCVsPWpSQ4tbKIE8qDW7jmtX52dG4cr3cnWe1edizXE97SmpTURA5emzlxrpJFIo1NpAV19qlqH1zIn3g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB4705 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org migrate_vma_setup() checks that a valid vma is passed so that the page tables can be walked to find the pfns associated with a given address range. However in some cases the pfns are already known, such as when migrating device coherent pages during pin_user_pages() meaning a valid vma isn't required. Signed-off-by: Alistair Popple Acked-by: Felix Kuehling --- Changes for v2: - Added Felix's Acked-by mm/migrate.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index a9aed12..0d6570d 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2602,24 +2602,24 @@ int migrate_vma_setup(struct migrate_vma *args) args->start &= PAGE_MASK; args->end &= PAGE_MASK; - if (!args->vma || is_vm_hugetlb_page(args->vma) || - (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) - return -EINVAL; - if (nr_pages <= 0) - return -EINVAL; - if (args->start < args->vma->vm_start || - args->start >= args->vma->vm_end) - return -EINVAL; - if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) - return -EINVAL; if (!args->src || !args->dst) return -EINVAL; - - memset(args->src, 0, sizeof(*args->src) * nr_pages); - args->cpages = 0; - args->npages = 0; - - migrate_vma_collect(args); + if (args->vma) { + if (is_vm_hugetlb_page(args->vma) || + (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) + return -EINVAL; + if (args->start < args->vma->vm_start || + args->start >= args->vma->vm_end) + return -EINVAL; + if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) + return -EINVAL; + + memset(args->src, 0, sizeof(*args->src) * nr_pages); + args->cpages = 0; + args->npages = 0; + + migrate_vma_collect(args); + } if (args->cpages) migrate_vma_unmap(args); @@ -2804,7 +2804,7 @@ void migrate_vma_pages(struct migrate_vma *migrate) continue; } - if (!page) { + if (!page && migrate->vma) { if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) continue; if (!notified) { From patchwork Mon Feb 7 04:26:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12736906 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 2563EC433FE for ; Mon, 7 Feb 2022 06:17:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236646AbiBGGRf (ORCPT ); Mon, 7 Feb 2022 01:17:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351637AbiBGE1X (ORCPT ); Sun, 6 Feb 2022 23:27:23 -0500 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2076.outbound.protection.outlook.com [40.107.96.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBD83C043181; Sun, 6 Feb 2022 20:27:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QaT1Knlm5cChfrSE/P8R8yIyWi0QW5Ej+PFgg7asBjfNMtpDzJOWMEW3yl7PImf5eHYC7OcF5e3f8wAOH8ICwZEG3LGZOEHwQ/Go3xNkNvzFD3e7JG7S0PdcWEuW9PCiQZDwZMbzTtAoAZnVmPRkQ24UtVD5YSmxSOW7qVi1gSIC4P6lj6PuFsVLFBdAAqYOdwWJnQQtQaiJOMhpzpPl4X8vR2LkIspHpr3KapafcN1wlyazEIe93zExbN3Utf6VrOeEqxWz9viE4fQVb1b7jjjylGzQaKXgYseGrSEKbfIDLU/hpZIRmo/T0I1VP0LSFqrYU1dUoHwyvqiICXufaA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PVWwywowYnYpDCBBurDM6uN+lzL3XuJ6mzPfSe2PfX4=; b=cpYHe/yItr+TygUmG3reynNytkkscMOnnlB2crNNYUHEaQRnp+BJxy/bQKoi1RkeP3Qj74eD2UfTEh/Hq3558z2gSB5pDl41UcBeZ+OmeVkbTG++IiqoTVc6PfE2dcjNMJkXD7GuM+uoO0ZNanXRe2z/bne3csmukcv8V90rT2q1KHqTmBuFqwT5OnBGVbL/n+AEWE0DQtAFRji3p04jcikuV7aPqgl7+m+boY5ayvEgX8R5Ot/pOfDQvksjao7a5EJGSPgvS+lyBkEIMpSIBkONjt7V23LHIhLIWk+0u3w7/aoOtAxMj7k6iekP0m3BbwhAp6RlEAh5rlIjlvDXvQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PVWwywowYnYpDCBBurDM6uN+lzL3XuJ6mzPfSe2PfX4=; b=GU9JzCPD2Xj0rm3FzrVXq4zdGz2hmg3magGfAIOdOTO1c7OU3E+RUxv3Abp2WNEdgxf3e+mKdF/Zfi9jxQ4z+7T1EFIgw3gHJSE6vrvN7aoC0NhGOzOPLVPPBZt14g1wCq4i/9V7KJEnfheYWwcYKm7p4aFkZ9LIT1Lcet/xEidlzrJABj00hcWhGpdLmhUHxmrg1NzlIwjxdVX61l6My13pxuOPrk2xTtDNwtcbQ1TSORgbF29MRHDkcTQs1PrnSkt/ax9wmEaScH9rEAfh0rkaSreL6y8GRaxste96DGYMEJX3M0Pi5tWBuk3P5d+x+bvzkDU8XshE7l7k5Db2hw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by BL0PR12MB4705.namprd12.prod.outlook.com (2603:10b6:208:88::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.14; Mon, 7 Feb 2022 04:27:21 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::1528:82a2:aa0:7aa6]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::1528:82a2:aa0:7aa6%5]) with mapi id 15.20.4951.018; Mon, 7 Feb 2022 04:27:20 +0000 From: Alistair Popple To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: Felix.Kuehling@amd.com, rcampbell@nvidia.com, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, hch@lst.de, jgg@nvidia.com, jglisse@redhat.com, willy@infradead.org, alex.sierra@amd.com, jhubbard@nvidia.com, Alistair Popple Subject: [PATCH v2 2/3] mm/gup.c: Migrate device coherent pages when pinning instead of failing Date: Mon, 7 Feb 2022 15:26:47 +1100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-ClientProxiedBy: SJ0P220CA0030.NAMP220.PROD.OUTLOOK.COM (2603:10b6:a03:41b::6) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c4d3e71b-ed7c-4a72-af22-08d9e9f221ab X-MS-TrafficTypeDiagnostic: BL0PR12MB4705:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 59UOEl+p+Mu8YBtp58ed4Y/LbG2vJ/hJeud8REHr8t6SHY+cPfWqO3El9C/aP01vckJPKh6uCvf4A+SZcI+de/kVdwVgvk0ivLCLLaofPiWi/Qbmsl4KEbAmtpByrHJOc1TsV3ChE79rE9Jx/vkxAjs7kbzRlJ8SwaDVG+CI6IzVyw3Ly4cEjHtCfH0gJ6VE8MRd2G+b80c1cylds5Em7u4fvgUxfenZEtZDi68j8mICr9mAjd+65QspRApYOzhRZR18zepRCSlRdE1Jf5R9RzHY8ZBBYNQNMMWohCUmIIkSdB9+PO4H8JYT8VVcjOwiXCy6TDOS/LISpH02aq3HCIu+iaZB7WFolpzmx0dotEDOqD1kHL8Zcgu6n3C6zjAIXBPCEBM26bX7FHWxHCLuaAPHrnYZw3B24p4vjQKzHZfLWxRusl6sdDO7Gt3DSpHKy8xn/9WkYbRZKZheh+sutXwYbfrSvF+W9al730o9mpmvxlMiG69DLTqkP2laBR+8BDGK29oKoPDOYQ6/qxhgGeRP6paP+dKk5yzhbV5zaXKuMCTlczqXoEG6hMsMoLooV6Szn84HHAY80Y+Q9/khc5nQ4Ik2Xr1hxfxNnfpVCDxpF5QohoaKMNs5UjcGWvnIYWySnCuSS2HCyelDk1XTEQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(86362001)(107886003)(6486002)(2906002)(508600001)(26005)(186003)(6666004)(6506007)(2616005)(6512007)(316002)(66946007)(8936002)(36756003)(83380400001)(7416002)(38100700002)(5660300002)(4326008)(66476007)(66556008)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: KWgbDTWqTXKMQ4UT35aTAWeJnmUvo7ADGd9oCgg25q+Z/HjX2yYQtgTsN4rNbpiE16pSAuBtxF8ZP5K4fuklp1eOwyoxbLFyOHcH8blo/k1rKk0G/qIKxhFILexhpWFEOA9sgM7X2cJRdlYLHTUMPtEPlFacZbhVfVjb4KILTEAzgyOEvoIknR+qF5FoGoYTBmtvw75VIXAp7OIbD21pZLD42wZqXqD/dEVFzHAB6Uzj+fZNgTWYVSq/sUVKYDuobbyTN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v6xRUh4ct6Wsz5KNv1hE7Z5pBhp8YauLFpkKV0aBLB8+Fo3I4rAhYTJcpWMOnxlDOKePLaam/2rEK8K6e7CYfM4UxX/eKbni2RVnWzU+NzaFfdUskRrtLLTd3QJ/nHcyuv9pgCX7ZlEoeByR69NcGAN7hNoe7Op/sETFTa7MSI4PkuqASVi4YMpEeEheJEW8LX5kN1g/TUlk6X/FTq/G/WBf5X43A7rbNngD3GU94s1INUsLzX68VJkKkG+goePuMITfXN76orGJW4QDtLx4gVpZwFekFLZ2+HfIAGXf/PlmU= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c4d3e71b-ed7c-4a72-af22-08d9e9f221ab X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Feb 2022 04:27:20.6674 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ib1EhYmyzaWQRdiMVW1EVl954Dq78iUOuqeA5XTs9eaIjv7gdFyX/RnDB60J2tjVwGbV+ka0kOHU6x6qyNIxdw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB4705 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device. However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE. Signed-off-by: Alistair Popple Acked-by: Felix Kuehling --- Changes for v2: - Added Felix's Acked-by - Fixed missing check for dpage == NULL mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr) #ifdef CONFIG_MIGRATION /* + * Migrates a device coherent page back to normal memory. Caller should have a + * reference on page which will be copied to the new page if migration is + * successful or dropped on failure. + */ +static struct page *migrate_device_page(struct page *page, + unsigned int gup_flags) +{ + struct page *dpage; + struct migrate_vma args; + unsigned long src_pfn, dst_pfn = 0; + + lock_page(page); + src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE; + args.src = &src_pfn; + args.dst = &dst_pfn; + args.cpages = 1; + args.npages = 1; + args.vma = NULL; + migrate_vma_setup(&args); + if (!(src_pfn & MIGRATE_PFN_MIGRATE)) + return NULL; + + dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0); + + /* + * get/pin the new page now so we don't have to retry gup after + * migrating. We already have a reference so this should never fail. + */ + if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) { + __free_pages(dpage, 0); + dpage = NULL; + } + + if (dpage) { + lock_page(dpage); + dst_pfn = migrate_pfn(page_to_pfn(dpage)); + } + + migrate_vma_pages(&args); + if (src_pfn & MIGRATE_PFN_MIGRATE) + copy_highpage(dpage, page); + migrate_vma_finalize(&args); + if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) { + if (gup_flags & FOLL_PIN) + unpin_user_page(dpage); + else + put_page(dpage); + dpage = NULL; + } + + return dpage; +} + +/* * Check whether all pages are pinnable, if so return number of pages. If some * pages are not pinnable, migrate them, and unpin all pages. Return zero if * pages were migrated, or if some pages were not successfully isolated. @@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /* - * If we get a movable page, since we are going to be pinning - * these entries, try to move them out if possible. + * Device coherent pages are managed by a driver and should not + * be pinned indefinitely as it prevents the driver moving the + * page. So when trying to pin with FOLL_LONGTERM instead try + * migrating page out of device memory. */ if (is_dev_private_or_coherent_page(head)) { + /* + * device private pages will get faulted in during gup + * so it shouldn't be possible to see one here. + */ WARN_ON_ONCE(is_device_private_page(head)); - ret = -EFAULT; - goto unpin_pages; + WARN_ON_ONCE(PageCompound(head)); + + /* + * migration will fail if the page is pinned, so convert + * the pin on the source page to a normal reference. + */ + if (gup_flags & FOLL_PIN) { + get_page(head); + unpin_user_page(head); + } + + pages[i] = migrate_device_page(head, gup_flags); + if (!pages[i]) { + ret = -EBUSY; + break; + } + continue; } + /* + * If we get a movable page, since we are going to be pinning + * these entries, try to move them out if possible. + */ if (!is_pinnable_page(head)) { if (PageHuge(head)) { if (!isolate_huge_page(head, &movable_page_list)) @@ -1924,16 +2003,22 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, * If list is empty, and no isolation errors, means that all pages are * in the correct zone. */ - if (list_empty(&movable_page_list) && !isolation_error_count) + if (!ret && list_empty(&movable_page_list) && !isolation_error_count) return nr_pages; -unpin_pages: - if (gup_flags & FOLL_PIN) { - unpin_user_pages(pages, nr_pages); - } else { - for (i = 0; i < nr_pages; i++) + for (i = 0; i < nr_pages; i++) + if (!pages[i]) + continue; + else if (gup_flags & FOLL_PIN) + unpin_user_page(pages[i]); + else put_page(pages[i]); + + if (ret && !list_empty(&movable_page_list)) { + putback_movable_pages(&movable_page_list); + return ret; } + if (!list_empty(&movable_page_list)) { ret = migrate_pages(&movable_page_list, alloc_migration_target, NULL, (unsigned long)&mtc, MIGRATE_SYNC, From patchwork Mon Feb 7 04:26:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12736909 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 6015AC4167B for ; Mon, 7 Feb 2022 06:18:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238406AbiBGGRv (ORCPT ); Mon, 7 Feb 2022 01:17:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351697AbiBGE10 (ORCPT ); Sun, 6 Feb 2022 23:27:26 -0500 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2076.outbound.protection.outlook.com [40.107.96.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A95F0C043181; Sun, 6 Feb 2022 20:27:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ogA0w9wa8TkhN5HTcNTR+JGgJS7h3bF+Ded9d2sr7/zsUsy7otMPIG7lkZrg7GF7CRbyPhn5gNuGaMNUqKjRPWlTr/rK0rEYrHc2IDDB1GoBCaUzq0tRIPgg16/ZmJ4vH5TDo/yuH9uatATp6nl5Ra2W0hLIlbQXzxClocIuqtE+0pAogkp/+J5aOVdi+04eJw9t99HBhppdIT82w5aGblk+YesWL1z4vCx2RaucRTwg73Dj5q2zHLlLt6KTHAINULVpcilXARUDTlgxPaw1bV1GmFq6zCutyYI7pqWDemrI94YmqYv0GnQoP7nISMzu/YLfsu1w5lqq+WppW+XEyw== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PWYhz0Sv1Rkjnmi/uR+BbDa+rT94UmYsyQ+O00QdUhU=; b=USFoxUpcNrN81VtksTj0smRIKCaCkPCGFOPhVYL0LC0NMIWgpHosVYgXf8OseJvkioAls/qs+wIDdM8pbBWcXfmAl507/ZMCcv4LD7Xepk+sZwrpTNx4OOGBjzvmDAcaO8rNdsEoEsMUiitixP7PtGBcmHYTYSDkNynnuY+s2mp8kxeOIcnTmPEInTb1sDbl+xoSh8Z4VQba9f7Pr+4VC3TlZCgjHdLa9vBXv3y/nW3uU7T2W6lhx1CjVzcoxP+dNvn7emp786tIFoQnOa5jmb67vFcU7yegPkSukKDpu+7lFdbjEcJnj8Ijyy338nmZpoBBvUTvs0MIgaA9uLUIgA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PWYhz0Sv1Rkjnmi/uR+BbDa+rT94UmYsyQ+O00QdUhU=; b=R6iAs1xt6CUxucLkLJOssho0VrV3mI5OGfBu4GyKM/5G3ALQFPcnr8OD/pCgUCM/p0TwhN6fn52At+b7TQBDGHEEkoxnzI1oDlHq5dTAhsuBmzZG6ulUqjLdPlHLBTLvJ06G5wJ7Zn88ooD4jph1GW5MUSySVbmJ2vJaIBPNgSHi5qS5EL5tsh6DGDwvJrnJkjn5NYmxZC9p2S4OSGdD5b+4VBsCT4hXvhiuuStBTlAquGDxEJUBU5FMXVmsMHaX1xN6ILglomAN7BIU8tZPsQbk0ysJrV0qakfzEyGLjvpnFrdZF0jdkHLTjs4QpKbNg7j18hFmfHJ1xzmtBk4hAQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by BL0PR12MB4705.namprd12.prod.outlook.com (2603:10b6:208:88::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.14; Mon, 7 Feb 2022 04:27:24 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::1528:82a2:aa0:7aa6]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::1528:82a2:aa0:7aa6%5]) with mapi id 15.20.4951.018; Mon, 7 Feb 2022 04:27:24 +0000 From: Alistair Popple To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: Felix.Kuehling@amd.com, rcampbell@nvidia.com, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, hch@lst.de, jgg@nvidia.com, jglisse@redhat.com, willy@infradead.org, alex.sierra@amd.com, jhubbard@nvidia.com, Alistair Popple Subject: [PATCH v2 3/3] tools: add hmm gup test for long term pinned device pages Date: Mon, 7 Feb 2022 15:26:48 +1100 Message-Id: <6a2145be66877fd3a08c9acec4ec3d53d82a864f.1644207242.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-ClientProxiedBy: SJ0P220CA0002.NAMP220.PROD.OUTLOOK.COM (2603:10b6:a03:41b::10) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: bb3447cc-a0e0-4b4a-f24b-08d9e9f2239d X-MS-TrafficTypeDiagnostic: BL0PR12MB4705:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SjFFvZzVhXBpJlIKvdwicyXL6IWHU9q4DDN5A2hsR8kdgn7oVV/9nT0IThYCP4tQv6Cl21TPmW7LienhI8LIWXoosFem69lc2WxIG4VXzIjds3cKW4C0AIbg/498WTk9XMmogy7WKegl8f0LVpgpNa85AwGY15PcNxXBw70OZP5km8XECSUyxLEfeY4ps7B2/W4if4Jx12gdXra/DBX774qpj9Vg9uptq5+zQwPQBsY9jB01F2hS86dz96K6YnnbbIEf0AJmC2BWuzExUfdfp5gQL/vNowb+Q5gmF91hD0n0FlcWYJEwCrA+Y/vTIh9LRv4eMQGC4eb8bxqTOiWJjndXOvZtePJhqbQdt1T2yi2UiWeFAlJ5w/fqx9Azq9RZiHlXO66G74OmnbPRsqSb5h0GbaWN2XG4otHZuN5Z+1nWKhNGpz6EBwLhTxSthWBzBtAp+HAWFQzK0soerbtiOGH7SAHSfwkQaoWrSMKXm0zZ+qY18nfLHBOVclXEmGCW1lkUHoDUGD2FFCgwL4xCrolGsyeoFuzS87shBtFeVmCrK6Xb2isEdyVgN+ZrxKbetyl4BT3nWNmHVygjoVaouQDM0ssFrS97pOFWNAvBJo3wpUY0qP3c+Pl/HMsFh+froZPfSo1xmWhX9gwborLStQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(86362001)(107886003)(6486002)(2906002)(508600001)(26005)(186003)(6666004)(6506007)(2616005)(6512007)(316002)(66946007)(8936002)(36756003)(83380400001)(7416002)(38100700002)(5660300002)(4326008)(66476007)(66556008)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mry0VoqQoqGL4dKymdquKqr4N7+JrRbPoGs00o1HBKqPdLG0wKA9W/Un5Xi92Ww27XtNJWl/2tAEThMP6yRzourqhtu+4c/Uvd90+UgZQBBWTRjZ0uuZD2ZuTBp/IxZtZyGXuojPOK8Mw5wYyzZQncvAWpiDdTUfHW+InegvIu4JEIQdQpqiD5de2sXU7gwhzCuq0k/Bxb0YADKCPDRUJ86i+TPdoEWWbM1ShbJKgxEBxy7ScLkLTrcHxRWFDIACFJlAufHkPYHbnv8dbOes5/uMoqSS7anV9RniElVqX0HjGTAtjvon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srzI30YRUg/dVBvyxSaKXHChjm5TW/H6neDzBCElDlDJTLM8YLGAJCPhXoB2H0iGvToS1iLQCxVVcBqmgbkh0ZPEmOGh4UqN2ALkoFABOr8F8YnSqVBplCC9irAMOmUQ+Db7uBZ79u0GQJDb/LSnD4bGxCZH8zhRrFo7CS27UwEUm3IYWyt3cqRF+oc4BCCemhKsJOdaCSUbZ8ebl9NCg0cFeNdl9qr0GC262A5d2D6H5dJP/4Nj6k1L5FfPXRJIYURtYKfGcE= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: bb3447cc-a0e0-4b4a-f24b-08d9e9f2239d X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Feb 2022 04:27:23.9328 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: tykI1F9MB78yKbRXZkREQtCbJuUNUybhay+Y4lcYBcJuwuaoZqJbB4U7zyzngManKzCPJZVDxqTxiMxdhwJX0g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB4705 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Alex Sierra The intention is to test device coherent type pages that have been called through get user pages with PIN_LONGTERM flag set. These pages should get migrated back to normal system memory. Signed-off-by: Alex Sierra Signed-off-by: Alistair Popple Reviewed-by: Felix Kuehling --- Changes for v2: - Added Felix's Reviewed-by (thanks!) tools/testing/selftests/vm/Makefile | 2 +- tools/testing/selftests/vm/hmm-tests.c | 81 +++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index 96714d2..32032c7 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -143,7 +143,7 @@ $(OUTPUT)/mlock-random-test $(OUTPUT)/memfd_secret: LDLIBS += -lcap $(OUTPUT)/gup_test: ../../../../mm/gup_test.h -$(OUTPUT)/hmm-tests: local_config.h +$(OUTPUT)/hmm-tests: local_config.h ../../../../mm/gup_test.h # HMM_EXTRA_LIBS may get set in local_config.mk, or it may be left empty. $(OUTPUT)/hmm-tests: LDLIBS += $(HMM_EXTRA_LIBS) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index 84ec8c4..11b83a8 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -36,6 +36,7 @@ * in the usual include/uapi/... directory. */ #include "../../../../lib/test_hmm_uapi.h" +#include "../../../../mm/gup_test.h" struct hmm_buffer { void *ptr; @@ -60,6 +61,8 @@ enum { #define NTIMES 10 #define ALIGN(x, a) (((x) + (a - 1)) & (~((a) - 1))) +/* Just the flags we need, copied from mm.h: */ +#define FOLL_WRITE 0x01 /* check pte is writable */ FIXTURE(hmm) { @@ -1766,4 +1769,82 @@ TEST_F(hmm, exclusive_cow) hmm_buffer_free(buffer); } +/* + * Test get user device pages through gup_test. Setting PIN_LONGTERM flag. + * This should trigger a migration back to system memory for both, private + * and coherent type pages. + * This test makes use of gup_test module. Make sure GUP_TEST_CONFIG is added + * to your configuration before you run it. + */ +TEST_F(hmm, hmm_gup_test) +{ + struct hmm_buffer *buffer; + struct gup_test gup; + int gup_fd; + unsigned long npages; + unsigned long size; + unsigned long i; + int *ptr; + int ret; + unsigned char *m; + + gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR); + if (gup_fd == -1) + SKIP(return, "Skipping test, could not find gup_test driver"); + + npages = 4; + ASSERT_NE(npages, 0); + size = npages << self->page_shift; + + buffer = malloc(sizeof(*buffer)); + ASSERT_NE(buffer, NULL); + + buffer->fd = -1; + buffer->size = size; + buffer->mirror = malloc(size); + ASSERT_NE(buffer->mirror, NULL); + + buffer->ptr = mmap(NULL, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, + buffer->fd, 0); + ASSERT_NE(buffer->ptr, MAP_FAILED); + + /* Initialize buffer in system memory. */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ptr[i] = i; + + /* Migrate memory to device. */ + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + /* Check what the device read. */ + for (i = 0, ptr = buffer->mirror; i < size / sizeof(*ptr); ++i) + ASSERT_EQ(ptr[i], i); + + gup.nr_pages_per_call = npages; + gup.addr = (unsigned long)buffer->ptr; + gup.gup_flags = FOLL_WRITE; + gup.size = size; + /* + * Calling gup_test ioctl. It will try to PIN_LONGTERM these device pages + * causing a migration back to system memory for both, private and coherent + * type pages. + */ + if (ioctl(gup_fd, PIN_LONGTERM_BENCHMARK, &gup)) { + perror("ioctl on PIN_LONGTERM_BENCHMARK\n"); + goto out_test; + } + + /* Take snapshot to make sure pages have been migrated to sys memory */ + ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_SNAPSHOT, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + m = buffer->mirror; + for (i = 0; i < npages; i++) + ASSERT_EQ(m[i], HMM_DMIRROR_PROT_WRITE); +out_test: + close(gup_fd); + hmm_buffer_free(buffer); +} TEST_HARNESS_MAIN