From patchwork Thu Jul 9 16:57:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ralph Campbell X-Patchwork-Id: 11654765 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 5970392A for ; Thu, 9 Jul 2020 16:57:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 40B7D207DD for ; Thu, 9 Jul 2020 16:57:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nvidia.com header.i=@nvidia.com header.b="ohj96eQb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727791AbgGIQ5b (ORCPT ); Thu, 9 Jul 2020 12:57:31 -0400 Received: from hqnvemgate26.nvidia.com ([216.228.121.65]:7900 "EHLO hqnvemgate26.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726910AbgGIQ52 (ORCPT ); Thu, 9 Jul 2020 12:57:28 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate26.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Thu, 09 Jul 2020 09:57:15 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Thu, 09 Jul 2020 09:57:27 -0700 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Thu, 09 Jul 2020 09:57:27 -0700 Received: from HQMAIL109.nvidia.com (172.20.187.15) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Thu, 9 Jul 2020 16:57:19 +0000 Received: from hqnvemgw03.nvidia.com (10.124.88.68) by HQMAIL109.nvidia.com (172.20.187.15) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Thu, 9 Jul 2020 16:57:19 +0000 Received: from rcampbell-dev.nvidia.com (Not Verified[10.110.48.66]) by hqnvemgw03.nvidia.com with Trustwave SEG (v7,5,8,10121) id ; Thu, 09 Jul 2020 09:57:19 -0700 From: Ralph Campbell To: , , , CC: Jerome Glisse , John Hubbard , Christoph Hellwig , Jason Gunthorpe , "Bharata B Rao" , Shuah Khan , Andrew Morton , Ralph Campbell Subject: [PATCH 1/2] mm/migrate: optimize migrate_vma_setup() for holes Date: Thu, 9 Jul 2020 09:57:10 -0700 Message-ID: <20200709165711.26584-2-rcampbell@nvidia.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200709165711.26584-1-rcampbell@nvidia.com> References: <20200709165711.26584-1-rcampbell@nvidia.com> MIME-Version: 1.0 X-NVConfidentiality: public DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1594313835; bh=B2D/nS1wOmFM1BnqXCP74e2MZLq0Kn5AbDcOovgyNoI=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:X-NVConfidentiality: Content-Transfer-Encoding:Content-Type; b=ohj96eQbydmV4kRF027+IYlNfuLSjZlOYmKTmjsLUdErYCgeG6G8GXVP1Ch1NvqKb VfQRNMApdYINbV1gCcw6ZXWDH5J2+QkMq6sNFAyjDF9vBnhk+O5aJGcOgeh+fNKaMx T3GXHedfsKe/5bYAi55X+WOqB8N7JNB9k4xcuKmYp1JclNurGSFI3Mq0uhgk5+L1qW MK4Mn98Zi+NIYIjqp0rG4VeUe2XMUUWdV8rzL12AzXnvYVVOl5t2Ymre5SbnJvIsoI Wa7HzIflxlyLka8WbWknLVDVyxq2jC4gMKobdNPT04IKAl8gefMk1ccDGJ+RKCECZG h5HrfNG2JVhKg== Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org When migrating system memory to device private memory, if the source address range is a valid VMA range and there is no memory or a zero page, the source PFN array is marked as valid but with no PFN. This lets the device driver allocate private memory and clear it, then insert the new device private struct page into the CPU's page tables when migrate_vma_pages() is called. migrate_vma_pages() only inserts the new page if the VMA is an anonymous range. There is no point in telling the device driver to allocate device private memory and then not migrate the page. Instead, mark the source PFN array entries as not migrating to avoid this overhead. Signed-off-by: Ralph Campbell --- mm/migrate.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mm/migrate.c b/mm/migrate.c index b0125c082549..8aa434691577 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2204,9 +2204,13 @@ static int migrate_vma_collect_hole(unsigned long start, { struct migrate_vma *migrate = walk->private; unsigned long addr; + unsigned long flags; + + /* Only allow populating anonymous memory. */ + flags = vma_is_anonymous(walk->vma) ? MIGRATE_PFN_MIGRATE : 0; for (addr = start; addr < end; addr += PAGE_SIZE) { - migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE; + migrate->src[migrate->npages] = flags; migrate->dst[migrate->npages] = 0; migrate->npages++; migrate->cpages++; From patchwork Thu Jul 9 16:57:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ralph Campbell X-Patchwork-Id: 11654767 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 3A88A60D for ; Thu, 9 Jul 2020 16:57:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 21B35207DF for ; Thu, 9 Jul 2020 16:57:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nvidia.com header.i=@nvidia.com header.b="N1NuZpOr" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728302AbgGIQ5e (ORCPT ); Thu, 9 Jul 2020 12:57:34 -0400 Received: from hqnvemgate24.nvidia.com ([216.228.121.143]:15845 "EHLO hqnvemgate24.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726890AbgGIQ51 (ORCPT ); Thu, 9 Jul 2020 12:57:27 -0400 Received: from hqpgpgate102.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate24.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Thu, 09 Jul 2020 09:55:39 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate102.nvidia.com (PGP Universal service); Thu, 09 Jul 2020 09:57:27 -0700 X-PGP-Universal: processed; by hqpgpgate102.nvidia.com on Thu, 09 Jul 2020 09:57:27 -0700 Received: from HQMAIL111.nvidia.com (172.20.187.18) by HQMAIL109.nvidia.com (172.20.187.15) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Thu, 9 Jul 2020 16:57:19 +0000 Received: from hqnvemgw03.nvidia.com (10.124.88.68) by HQMAIL111.nvidia.com (172.20.187.18) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Thu, 9 Jul 2020 16:57:20 +0000 Received: from rcampbell-dev.nvidia.com (Not Verified[10.110.48.66]) by hqnvemgw03.nvidia.com with Trustwave SEG (v7,5,8,10121) id ; Thu, 09 Jul 2020 09:57:19 -0700 From: Ralph Campbell To: , , , CC: Jerome Glisse , John Hubbard , Christoph Hellwig , Jason Gunthorpe , "Bharata B Rao" , Shuah Khan , Andrew Morton , Ralph Campbell Subject: [PATCH 2/2] mm/migrate: add migrate-shared test for migrate_vma_*() Date: Thu, 9 Jul 2020 09:57:11 -0700 Message-ID: <20200709165711.26584-3-rcampbell@nvidia.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200709165711.26584-1-rcampbell@nvidia.com> References: <20200709165711.26584-1-rcampbell@nvidia.com> MIME-Version: 1.0 X-NVConfidentiality: public DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1594313739; bh=7bNg4+W9LUQA56P48nlUXj5006Ok1vcN7L9AinD3FBk=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:X-NVConfidentiality: Content-Transfer-Encoding:Content-Type; b=N1NuZpOrbvHrgSl7AhEeXOCRv63B9Q2lx5GBdR7wv1/EB/1YG+Wv3031VziHsXXKj ERMrBXcnO322IhTci24SV9muQZuCMkihqa5YOUoduSZEXCH2iM1s4NJ0UoifO+UFf+ gUq32l0b3sU0GwN9X1Wmd5IC9wcGmGAlVXco8PLHiBQFFgmdpc5LJzkQdroOLE3OOx pAzTgDdTOD70Rpnk39ftF2js0MNcVLi7moL2LsdS87z0KlTms5+mdlBkJdrQHLxkD1 ndFpfVSGAI6HCiNEp/dSCWoiPEvsk0r2ZZ1MHFVozfe/Z65zDygxjoj5ARcJhA36a/ hCRojLyFcxRNQ== Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add a migrate_vma_*() self test for mmap(MAP_SHARED) to verify that !vma_anonymous() ranges won't be migrated. Signed-off-by: Ralph Campbell --- tools/testing/selftests/vm/hmm-tests.c | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index 79db22604019..e83d3ab37697 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -931,6 +931,41 @@ TEST_F(hmm, migrate_fault) hmm_buffer_free(buffer); } +/* + * Migrate anonymous shared memory to device private memory. + */ +TEST_F(hmm, migrate_shared) +{ + struct hmm_buffer *buffer; + unsigned long npages; + unsigned long size; + int ret; + + npages = ALIGN(HMM_BUFFER_SIZE, self->page_size) >> self->page_shift; + 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_SHARED | MAP_ANONYMOUS, + buffer->fd, 0); + ASSERT_NE(buffer->ptr, MAP_FAILED); + + /* Migrate memory to device. */ + ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_MIGRATE, buffer, npages); + ASSERT_EQ(ret, -ENOENT); + + hmm_buffer_free(buffer); +} + /* * Try to migrate various memory types to device private memory. */