From patchwork Wed Jun 29 03:54:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12899281 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 1ACD5C433EF for ; Wed, 29 Jun 2022 03:55:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF0086B0071; Tue, 28 Jun 2022 23:55:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA0EC8E0003; Tue, 28 Jun 2022 23:55:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91A418E0002; Tue, 28 Jun 2022 23:55:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 821546B0071 for ; Tue, 28 Jun 2022 23:55:41 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5EECA612DA for ; Wed, 29 Jun 2022 03:55:41 +0000 (UTC) X-FDA: 79629909282.11.FBFA2CF Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2072.outbound.protection.outlook.com [40.107.92.72]) by imf08.hostedemail.com (Postfix) with ESMTP id E45CA16002F for ; Wed, 29 Jun 2022 03:55:40 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HyvlGl3XuwEPA3DAbviLZj0t6ezx6eS2LvWbFhQ6qwoqfhKuI8n4Vm5A3I8nSj/csCRMg0Sm4TOWXbOcpw++MjxMEJpgAewgMBD2FLBJRLb+y8g59FK71wIIbB3ak/0FY/k77FM3lGj7v/8GOfj/teEY6oM96PH6noQqrpAEKWDWZ/z0xH9TDRfS+zPnqtecLNIzkgN1a4y/188moHUVWiS7qaeLOTp3KJW3D4MpidwwVg1Mpq88itW80NFCE25bUQp80/TIF22hnzVFNaSHrT0JU4f9WxvNqkTD6EyJmOw5ehDPyMaOa/3sE1PrVUIAU89uRoAdF9JOB+Pm0fl5QA== 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=ghINYxYAaP35msqhqy+uXLJjlA8u9eNbkgf4YFEOveA=; b=dnSkhpXB6X3ZTpWF7MdLfFP/XAVsh/p905IioKjFd6jUafM3wO2ZsXpoVFYYpMUetu/+hLrUt9m5ZGPa+p/mmXhvPFz6n91wo77g38howRVINhXRig5YmVHYF851iMPFSFDTP2DPBGYQ2MZJ/RwRpAgD6AGUzP/sSarMNS+cvLAwDvRjullIBDEe6jGPqI7zGyQu/YJzE8RMqTGwlMwrp14xI9glaB4G7w3HjiYm+oJscnKE4L60YcjmggBlyeS3vcr7SX0uoZC3LsQr6jF9ufhGEVCXDquw75hXgqRZ0Rxu0ferxAIRLNpYhO1TgQvC6I3ieh/IeL0WZemc2iGwZw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ghINYxYAaP35msqhqy+uXLJjlA8u9eNbkgf4YFEOveA=; b=K81oENj98EF1QcQLUJ4pMag6GMJN/mH+DFF7PUGa3mcDfaljWtmeEBMDLK8dKQYLvOOM4tUf5IpMSldGBo9zomJ8BdEQ56HHztvyWgC46uZ3SjMcrMgKmYutDpQjHl1234tIx6KyCZl3S36Zgd9SfnLYZ/y4QozRqeBDSsK1xoc= Received: from DM6PR11CA0039.namprd11.prod.outlook.com (2603:10b6:5:14c::16) by MN2PR12MB3360.namprd12.prod.outlook.com (2603:10b6:208:c7::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.17; Wed, 29 Jun 2022 03:55:37 +0000 Received: from DM6NAM11FT026.eop-nam11.prod.protection.outlook.com (2603:10b6:5:14c:cafe::90) by DM6PR11CA0039.outlook.office365.com (2603:10b6:5:14c::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.15 via Frontend Transport; Wed, 29 Jun 2022 03:55:37 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT026.mail.protection.outlook.com (10.13.172.161) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5373.15 via Frontend Transport; Wed, 29 Jun 2022 03:55:37 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Tue, 28 Jun 2022 22:55:15 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v7 14/14] tools: add selftests to hmm for COW in device memory Date: Tue, 28 Jun 2022 22:54:26 -0500 Message-ID: <20220629035426.20013-15-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220629035426.20013-1-alex.sierra@amd.com> References: <20220629035426.20013-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 35ccc442-4936-46ef-d565-08da598339ca X-MS-TrafficTypeDiagnostic: MN2PR12MB3360:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sohPt/tLKahvcUc+fgO7WJRI0HUuXtEEmYQJUojtfwUI5p8JfST88odGxz9+58Q+eV3hQJSesDctosNpnmf5pSA9IWhi/MHqHKIJmJmckFCICqAnKaH/vU7uIG6h2MtY/jLAisfs8aCS7lrptnuYaWE8p/XhWzmnT3/VXjTYfM4gGT5YafvxLjkFpHoxQi0V9ZN8pq4crpnt5Vh7Ksff3p0bn0OxBTYfqJoHZfbrJQatk5nvLOFp84yHDDFNYeoFncjzDkCrMFxcorJ48AyhdviWHgmMEf3svLvbYSf+6KAA9YBB6347YaKGx8A65aJHLYYrc3cmp+39zkv+xIj8ARunLcwd1FFZI/H4QF5poF9drQFKsXYKHY30io28jTuxU4Ri2I5AvHJB6Mt0tEi63EneVg8omJF3qtRvmWgdsJtPtJ7vwDh6u98w9ULeqvKIBBAR1dL3j8so4thL2uNj5qP88W+kr/uBQbbBWKRuN4faE3TzIDPlvXvjk1jakJtsyDWKjc1t0JWoSLVJYveyC293pa7j2uBlY9zZpuwOjHjyatn+Rxa7oRbcyuu16GbslBST0c43dbi80WrOiIti5d5XcaRECoPG12j4gtFdVZWbdET8ZYIX2WOdoxxrNtfHnd5zA8z1+RXjoTgU4460P1p0MxxuIJX4EWzX+6f7+wTQTARVNKxSLPIu08GjUp36ozOnWXuYIPOA19/72Fhrs9UfqTj00vD3S94jWZd3ctNya434Sk+bMLWJLALkddDjxRI5Pl5wqCPBNsiR7xl6vtUsloazcDtlgzDCTZ9Chzblvbo421+qh9hIjhCZECMAOY6obTKnQoZO4aENFhQsuQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230016)(4636009)(136003)(376002)(39860400002)(346002)(396003)(46966006)(40470700004)(36840700001)(2616005)(1076003)(16526019)(47076005)(83380400001)(8936002)(336012)(186003)(86362001)(82740400003)(36860700001)(356005)(7416002)(36756003)(81166007)(5660300002)(426003)(44832011)(82310400005)(40480700001)(2906002)(41300700001)(8676002)(26005)(7696005)(70206006)(40460700003)(316002)(70586007)(6666004)(6916009)(54906003)(4326008)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Jun 2022 03:55:37.0122 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 35ccc442-4936-46ef-d565-08da598339ca X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT026.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB3360 ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=K81oENj9; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf08.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.92.72 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656474941; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ghINYxYAaP35msqhqy+uXLJjlA8u9eNbkgf4YFEOveA=; b=NZeTHwef7eI9qNYURKVwgSEJ0qnCApEVaoYiTKTMrSyDIJlQ/NvLivtqn3Lx+hYbSRhPIL y7VL5CYWAXye4AfWi9tKtDKyIRQOkWodTNFGkJhEaIdbqmY4cM82dSHscYjC6itCQI7EXS sKDqLuUD6TQEPH8thCcgyoQLe2PlYiY= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1656474941; a=rsa-sha256; cv=pass; b=pxJ31H5LetzQW7lTutksgrPdILbxa4o+itIAOsa87tix2srcOsuhgu8AnygDg4mljyhd0k 3nxxb604WqvHuQ2p7vk5FrRDNi57gejYDELPqN+Ldn/TzQ6QN8umGSLm3odYOEhgoJWxO1 LYYVue11MrnS3VNgn//s8zrFN5lLWKw= X-Stat-Signature: st6zt3yia68c7u63jd9aan14665dhzoc X-Rspamd-Queue-Id: E45CA16002F X-Rspam-User: Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=K81oENj9; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf08.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.92.72 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") X-Rspamd-Server: rspam12 X-HE-Tag: 1656474940-369866 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: The objective is to test device migration mechanism in pages marked as COW, for private and coherent device type. In case of writing to COW private page(s), a page fault will migrate pages back to system memory first. Then, these pages will be duplicated. In case of COW device coherent type, pages are duplicated directly from device memory. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling --- tools/testing/selftests/vm/hmm-tests.c | 80 ++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index bb38b9777610..716b62c05e3d 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -1874,4 +1874,84 @@ TEST_F(hmm, hmm_gup_test) close(gup_fd); hmm_buffer_free(buffer); } + +/* + * Test copy-on-write in device pages. + * In case of writing to COW private page(s), a page fault will migrate pages + * back to system memory first. Then, these pages will be duplicated. In case + * of COW device coherent type, pages are duplicated directly from device + * memory. + */ +TEST_F(hmm, hmm_cow_in_device) +{ + struct hmm_buffer *buffer; + unsigned long npages; + unsigned long size; + unsigned long i; + int *ptr; + int ret; + unsigned char *m; + pid_t pid; + int status; + + npages = 4; + 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); + + pid = fork(); + if (pid == -1) + ASSERT_EQ(pid, 0); + if (!pid) { + /* Child process waitd for SIGTERM from the parent. */ + while (1) { + } + perror("Should not reach this\n"); + exit(0); + } + /* Parent process writes to COW pages(s) and gets a + * new copy in system. In case of device private pages, + * this write causes a migration to system mem first. + */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ptr[i] = i; + + /* Terminate child and wait */ + EXPECT_EQ(0, kill(pid, SIGTERM)); + EXPECT_EQ(pid, waitpid(pid, &status, 0)); + EXPECT_NE(0, WIFSIGNALED(status)); + EXPECT_EQ(SIGTERM, WTERMSIG(status)); + + /* Take snapshot to CPU pagetables */ + 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(HMM_DMIRROR_PROT_WRITE, m[i]); + + hmm_buffer_free(buffer); +} TEST_HARNESS_MAIN