From patchwork Tue Feb 1 07:05:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12731423 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9C11C43217 for ; Tue, 1 Feb 2022 07:06:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 288598D0059; Tue, 1 Feb 2022 02:06:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 238828D0054; Tue, 1 Feb 2022 02:06:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 100928D0059; Tue, 1 Feb 2022 02:06:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0177.hostedemail.com [216.40.44.177]) by kanga.kvack.org (Postfix) with ESMTP id 0215D8D0054 for ; Tue, 1 Feb 2022 02:06:57 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B907C90098 for ; Tue, 1 Feb 2022 07:06:56 +0000 (UTC) X-FDA: 79093328832.10.27A024F Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2083.outbound.protection.outlook.com [40.107.236.83]) by imf06.hostedemail.com (Postfix) with ESMTP id 416FC180003 for ; Tue, 1 Feb 2022 07:06:56 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LVqnM6q4UGMyG3mVm2JVV+uKvfDsW+vfItdZq1dkjrNB0OUOfLFNM3n79QtsQooxPXMHbd3P6+Vl2MpISWHQ1mYqaqSKhlLhIyr4cpTySbWaTyl07+JVpNtbFCi1AsFJgMSNotBK710Y9N4kZxkeQWPvofDSJ1MGAbv8otXHHNLGj1wpD0nXgzgrLiEmH0Xq3bR30Hig+QGmaVyAT4SnymzDjxmOsqTZ5+2coM3rmsCGd1aBmc+2sLcSyvxco3PRwGWw2ZMuoTB+3lc8mLBg/9KK0hq72s8zYMPn7yRvfT43WaRFrx1Ab+yuJfHOMfPe05TMEaG5CN4JbPCmGWtmtQ== 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=dd81HyG71D8pTfKOZda6N8+bA8ij9IGX6lSZqZkGGWE=; b=U4L4OPtRoiD3DYILyM6ilJGFzYcBBX2+DXA6KwMBaSE8Z31X+phJWroZp2jW/7+cCBtxzhgXbcDq84CVZ13lSS5jIpXkRcFt8bVhFzJdKKJrjN8B3xiehu9mAucwgY6J6ORSTjawoHPeM/IJkq8r6MZhn3GgVCptAzOo65KxFnkUehwhRzqEwHpiWFhncf2ggivqNQXZuvXqn7aGhx5kYIpNb8msxOnGv63xYFSxMbA47KNqgp84UxOrr6yw++X4XvHoX65UhjgEXyETM8kaF4rbGTqa9SdGMsMgvKdMUy8UERBkmpQ+DCXajcpqYD46/rBgn3+kPIJDf5oLL/GmIw== 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=dd81HyG71D8pTfKOZda6N8+bA8ij9IGX6lSZqZkGGWE=; b=EaEn71y7itik3sADmPHrxXRcXBVI2U0kM0YneFfYqI9CQ2MyIguUPCZtEZkfu7+3VR/38Jrzd8W6hAjh4xIZy00p8gvWrMUKmElEVy9wt9jCaIhl2VAViM4YfHaRbFgNAgmY6mk4Zn8X4s7FAniWQfHU0+Lp4WQoMqf0EdYLPvYcjXTDvU8C+qlRWpIWLIyH1hCAIeyumaCEX1yT//WWzsPN/zkQOjLKgViYcY8BkfzZ7FrCb9sDsg2cH44JRqDsK4UCXij6KMjEDo3VvfvhgsXu2n2bBpjdoQn4QK6gjBAjISvQ0i5RkmzsbPgTWPZ6HMkPaur1LSet19Xknz8lkQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by CY4PR1201MB0214.namprd12.prod.outlook.com (2603:10b6:910:25::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.20; Tue, 1 Feb 2022 07:06:54 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::48bc:2cb6:45e3:41fd]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::48bc:2cb6:45e3:41fd%4]) with mapi id 15.20.4930.022; Tue, 1 Feb 2022 07:06:54 +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 3/3] tools: add hmm gup test for long term pinned device pages Date: Tue, 1 Feb 2022 18:05:57 +1100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-ClientProxiedBy: SJ0PR13CA0012.namprd13.prod.outlook.com (2603:10b6:a03:2c0::17) 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: 4d5b6f9a-73d4-40f4-1dea-08d9e5516d65 X-MS-TrafficTypeDiagnostic: CY4PR1201MB0214: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: S/QGslVbU7mnyw/nbcWMaIf/h4SxF9AGNdH+dGhwA/nsOtn+ksQTlX0IVAsEJSs0F5bQb9TxEqAq4CRL2+DjWZa47LdG4aELRi31oXVPsa5+X7qaubIDUFydAGthsTi9BCqbqaPFwvxYu2Yt1u2lnUVh5AV+LMPQuQhWWHUGQVGBYq3pGKALcpVn4Ttota8Z93MdMcySMJsKaywNPqSLpGesWlahne3d2NNMH4F0vV+YreGzlxJyEoJeafRWNL4MGEbiR7LngbWkoHVRI5bam8kQ1tCnzx464oEBtHxXPghfbQZP7WYFsau9UmQtiG2ISLuPL7XalSgBemrqbmjQE/GOtCoofmUeDwWtay2AjrMG+/486g2Mm1u2iXQwoZWcZcFJA9+MmH+sWBk3BggOM6nEB2eLfPLqm3Uj7VVhOz5j2+OnuM0m6hryap5mu0HYIkeL9rKMyDnhnzYSnnhX1nG5yC+LuoGa5X3DOWn1ejXyHCtxrgZDCbXlu+MNpnuI+0BQxeS9v3yzJC1LtRMc8T7Z6rJGihAcUjzgfd94jg41nHR1n4JsipuZhDlszrBT+s5ZOOrSeYHrL0rYA4or1il09Xb+SXK+RyzuQXpD0AEoHvPT01nLSeExknhpZTovlJgLrrSTE2khHaCaK22u1g== 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)(107886003)(38100700002)(5660300002)(4326008)(7416002)(316002)(186003)(508600001)(83380400001)(8676002)(2616005)(66946007)(36756003)(2906002)(66476007)(66556008)(86362001)(6486002)(6512007)(6506007)(26005)(8936002)(20210929001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: fUfoR9CFruCVMAD/8Z27S6d6haot7/ItGPYj4CKRVx1/qtqalbbNA73FuvDe8YOqtrQjfcrvDrh1OgSKAFNZD/afvuPfST/R04vraKIUVApGdGuZ/Ffhpc+SqJJ+mn6ND/WvdoOE11b2NpdrHim7Fc3j49Am3Y+ypSgMT7/2uMHwZuMFwFZ588QUOUMntXrFWyW/0HV2sggtOY/7wCpoh6c2g1BklViDS6iE3w7JWJjZb3biBkpBvDpRXytEldBU3OoKRGxq8Bk3UmD+rhiN/5ZpD8B4oeHbZfBTNDSLw8eiWzjBiqN3AmLCrNUex9KfquR4mR2BkQkc7Q34WFHzS+NB/ltq3X/zlCzb48NsQfrInPUwpiRXyS9mG0rt9IOEVZIF47KiqeTL67RGZpBa5dSuvbG20FmWg2go5N9J5Y3SE34plXM31gUJVGqM0rtIjo0NxhVIbXSMfEYoFTyPQh077agVtIog6Mc4tQigQgHWfFSBBcd0wVsrZlj530C78qIoEC1NOq4JsFlBfjQMd82EWylpXN7nCHcfvYZ8l+RdIcZNfPYBF+a4iazaKX34NVans2AggfIyPQPTuQ9D7uNmXUJ8A2LKAajAgIkcLHsPI3nB1j2OeJw7dCPkw/++rH+ZTt1OvyW+DFRRV55upD++bMAej8CDn3OoVGvDV55+5iUSWcqZsCgdt0o3y5JmelwQDtIUxT1/dueJ8BQ/n7oHLRUj6y3SYW0O6B0LvF/qYRodiVHG3qnKAYN70FD/Tq3as4Q7bURFka6cTze8LQEhmUCjwznweM5u4oh1jdt3BRmudN6Y/0H5gtKIjvqJz8ZXVQlSdOROAnUbSGz+61ahwo2nx7wDF1EroVHSOX9kSJ/B7qF+k1v+6etk6KR8XBO2Ff/0UHlRBe59aAjMQc0ZgyEonzIApOTIeqCOOlMgjB8ceexpzaWAGAkAoslecQrYgo8gBC+Vf9jlzJSm11SwNxJQQjAq9SLZ44dyhffUBah59eQ2IACFhVrz/VqC11Ag5bCyLwLY9b1aYxHzmGlmFSmyTtC4y+7jQCPisNbjC0y9GPBdn6x7ZjsSB1LY4rJvH2A7VdSfF4XdOsIz/duEzhYEGLYubxj+utr6/Z2j263rVQRSgU46to+u60jtk3aGZgdlJpiAbdBrsZAKDoyPyPOqYK8dD5sGB6lYMgj6iQkekBRRrEGdK/dCfLwYtEFQKy3+FFBBbQ2o8vmor8JnCLW4EljELAAvGwISk2zmGeIvex4wMiJjM8IRqmHUYvxS+X0pRZb245TK2lipofZJdtSdEbWnlwLRjvu3UE2DkK0+G32ZhQ9tSbCrnigsoc1a/bbxwkGMrj+r5K3yjs/AbwaxHO5fP+/6nkCXHHpAI/KEe9a9/zMHe7HNWRMpQiHn+ASMOjmwjC4Ejl6fpSYW0jfANpI2l96BdNjbnCSoTyFmCvEK60mw9iO0HSmNW2mRgwL5EZFWX3NR4G1w+SPN0eeWd0FqAb1uh/6D+0Y5FFOpwstDYkDbBrwr8KQZ9iKgTp4uGWPRUJqr8534zVvsRZuwCR9E66CxW5v4FGgkpswJX/N21aIMFutxdtnrUJ1F/kSkED4ZceipLoEgKxCqfUPGBEuOrl8YrRUEgkM= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4d5b6f9a-73d4-40f4-1dea-08d9e5516d65 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Feb 2022 07:06:54.1653 (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: 0CUfUNUf2GXl32hjY+aoYxSxNYL6wq6WtUSlhYMTkt3EJMtjMgT1CPLZEpNkE8mru7QNzyq9AjcT1hX1UVPByA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR1201MB0214 X-Stat-Signature: w64sxsa1qxi7brj48rct1bs4gym8ym6a X-Rspam-User: nil Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=EaEn71y7; spf=none (imf06.hostedemail.com: domain of apopple@nvidia.com has no SPF policy when checking 40.107.236.83) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 416FC180003 X-HE-Tag: 1643699216-25363 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: 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 --- 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 1607322..58c8427 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -142,7 +142,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