From patchwork Mon Jul 11 05:17:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qiang Yu X-Patchwork-Id: 9222901 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id F3E0B60760 for ; Mon, 11 Jul 2016 05:23:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E139E27B81 for ; Mon, 11 Jul 2016 05:23:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D57BD27BF4; Mon, 11 Jul 2016 05:23:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E4EB627B81 for ; Mon, 11 Jul 2016 05:23:16 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 129866E123; Mon, 11 Jul 2016 05:23:13 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org X-Greylist: delayed 5504 seconds by postgrey-1.35 at gabe; Mon, 11 Jul 2016 05:23:11 UTC Received: from NAM02-BL2-obe.outbound.protection.outlook.com (mail-bl2nam02on0070.outbound.protection.outlook.com [104.47.38.70]) by gabe.freedesktop.org (Postfix) with ESMTPS id 82AF16E105; Mon, 11 Jul 2016 05:23:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=cwEqvDdr7fUT4mbmTpGAKtAh87rikMLHUC5eX66ixLE=; b=lXIaiHgNoptMmd52cRDmLwtpxaYbIrozbrmZ9U1tKBKMAyldKAmem0Nln4KYHz7unYPibbpY4Nsg8DfiXc6eCcC+7BoAc0CF5hiCEDurAu+s3eWopTD+u6+zq9qPZVNdJtFQGbipSvkLDEkFXkzV39lwWnpYCCamNSryX9aTZXk= Received: from SN1PR12CA0037.namprd12.prod.outlook.com (10.162.96.175) by BLUPR12MB0612.namprd12.prod.outlook.com (10.163.217.18) with Microsoft SMTP Server (TLS) id 15.1.528.16; Mon, 11 Jul 2016 03:51:24 +0000 Received: from BY2NAM03FT020.eop-NAM03.prod.protection.outlook.com (2a01:111:f400:7e4a::207) by SN1PR12CA0037.outlook.office365.com (2a01:111:e400:5174::47) with Microsoft SMTP Server (TLS) id 15.1.528.16 via Frontend Transport; Mon, 11 Jul 2016 03:51:24 +0000 Authentication-Results: spf=none (sender IP is 165.204.84.21) smtp.mailfrom=amd.com; lists.freedesktop.org; dkim=none (message not signed) header.d=none;lists.freedesktop.org; dmarc=permerror action=none header.from=amd.com; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from SATLEXCHOV01.amd.com (165.204.84.21) by BY2NAM03FT020.mail.protection.outlook.com (10.152.84.224) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.534.7 via Frontend Transport; Mon, 11 Jul 2016 03:51:24 +0000 Received: from SCYBEXDAG01.amd.com (10.34.11.11) by SATLEXCHOV01.amd.com (10.181.40.71) with Microsoft SMTP Server (TLS) id 14.3.266.1; Sun, 10 Jul 2016 22:51:19 -0500 Received: from shlnxcqesrv2.amd.com (10.237.70.153) by SCYBEXDAG01.amd.com (10.34.11.11) with Microsoft SMTP Server id 14.3.266.1; Mon, 11 Jul 2016 11:51:15 +0800 From: Qiang Yu To: Subject: [PATCH][libdrm] drm: Fix multi GPU drmGetDevice return wrong device Date: Mon, 11 Jul 2016 13:17:25 +0800 Message-ID: <1468214245-4716-1-git-send-email-Qiang.Yu@amd.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:165.204.84.21; IPV:CAL; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(7916002)(2980300002)(428002)(189002)(199003)(189998001)(110136002)(2906002)(4326007)(77096005)(53416004)(92566002)(50226002)(105586002)(2351001)(586003)(229853001)(106466001)(450100001)(87936001)(11100500001)(101416001)(246002)(26826002)(48376002)(7846002)(86362001)(5003600100003)(7696003)(305945005)(5003940100001)(356003)(8676002)(8936002)(47776003)(36756003)(50986999)(19580395003)(19580405001)(50466002)(16796002)(5001870100001); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR12MB0612; H:SATLEXCHOV01.amd.com; FPR:; SPF:None; PTR:hybridsmtp.amd.com,hybridsmtp01.amd.com; MX:1; A:1; LANG:en; X-MS-Office365-Filtering-Correlation-Id: b956ea38-5158-4145-b590-08d3a93ea10c X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0612; 2:gUsCW6P1jfT0mKJ4pskaoYCfLfszsATejBZudnfBiWxlb/oRZ5Zdd+zSBrP0YuToVGOYS4TOVhEW18zeDLb6XUBdGZYGNUqM9KiY8Zl4LngJyM0LCaGYw9tUOtp/n7Ce4yyhJkczbXJ6rAUl8fUSNMaBsufAEPivB9IvFNvPvKgpjfKR/amKDCEFPCybCJV2; 3:UhopYljdG9QtDi+ZFLWHvxtL+rn21z8TCDRjxP1NFBCGDR8xMQkDEkbE1IRR5bWwpOH7IycY3EUheUkOSkweX5OxMpTw6qf+IuLlXbwEj/Fowoa9YgkNqFrBk83Oaouc0Pomz7HqAk/giWn+QFd+vTe+3hDMIE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uxe5wwEtjHskxG1xE94rJ7UWYGHNDF2zhDU/L8JI+GhqSgMOZ4/37/arNUyLXSSnAUanDz1kou475VD48ME2VyXbA/hYmA2/CDSkPiKht341E2y+MGZ9Ht2Ry/cP6zbjBxnk0rNaXVCSL9I4Y/aLMsbNYkFMPieBx9125Z9xj7H6IaEZ02hIQIFWbFv+m8SbALItqdgWrPIW+NJOd9P8qM9JEw/w+UsuiOh7C2g21eey7S2RqSX9p3J/SMZIYr X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13023025)(13021025)(13013025)(5005006)(8121501046)(10201501046)(3002001)(6055026); SRVR:BLUPR12MB0612; BCL:0; PCL:0; RULEID:; SRVR:BLUPR12MB0612; X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0612; 4:1Zm4d020fZq8yMg6iDdnY/q2tyuV6EY6XI7lyz3GFmfGmuGdaep/dZ9CjWS1TFLYIVptMb1JiZBq+M4omX2781uEJWc+39uSBOc471dPQN4qblkVzrGhnwPqDiXnWDXG7ncFoRls8qjQ8HJwYgwP4oxTVyCGixDZ3UKn3oid7meSlI11kyWu7upD/90X65jiB8p51cm7crQrMO+jyEQKGAsk0YbtFKwqNOfH+aY7usreHMG7uZp55mk0bXX55igf8aUMwKaijIRVLEpN1i1kULI7rkmSLnpK9Znps3BETYvbQVK5cNZz9nokah3n9OIlzXSysgkeGS4RfW8L3wiS4bc+UDbedQ8C/W2FXOpdoyk12pf0dUrq3889pD5ZY+RW2mGAWSP10wi+7yqUFBi/Y4sUJ86u1thXesxN7ROS99vqj727zHXoLxNWvM6oIo8mB+YjmTm2+mHBToCoEIsyG1Fo6F0CddzI1p3UcLEJXHM= X-Forefront-PRVS: 00003DBFE7 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BLUPR12MB0612; 23:OeUbhtqL4qQkfdDpP6tStj7ZJAUKAS/dVujb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7GyCgGx/bdYUL5DeZTIh5NVsOZmGlDzQyKEUoeTDMqv3QiZQRpI1wRg9bqOnhoS2ZdkQOVQEjWCVJ SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0612; 20:9XrmmepTj814Rp1qko64lXAhu1/v82u0qPRYUuU8X259EFItbcdsffZPZmjQonTmIgHalyM8rfwVH7AsQ362s67XQCv9MxNJaxaOkIAndQaD21FQUVFwdo9eJL09tZDjLzyIvPXpJ7PaPIwas7inbHSkwV/PYAAO1zxceJB+GAsYfMoQuqZgSwLd7dJN/moTgMtBFox1f9oU3JI5UgSsCt3nP2G65Jh6iqMgbs6pRs5QQ4oqjT0x9Llj6Pc03mvL X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2016 03:51:24.2840 (UTC) 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.21]; Helo=[SATLEXCHOV01.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR12MB0612 Cc: Qiang Yu , dri-devel@lists.freedesktop.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP drmGetDevice will always return the first device it find under /dev/dri/. This is not true for multi GPU situation. Plus fix the memory leak in error handling path of drmGetDevices. Change-Id: I2a85a8a4feba8a5cc517ad75c6afb532fa07c53d Signed-off-by: Qiang Yu --- xf86drm.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index 6689f7c..e90e8e5 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -3064,6 +3064,17 @@ static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count) } } } + + // move all devices to the beginning of local_devices continuously + for (i = 0, j = 0; i < count; i++) { + if (local_devices[i]) { + if (i != j) { + local_devices[j] = local_devices[i]; + local_devices[i] = NULL; + } + j++; + } + } } /** @@ -3087,6 +3098,7 @@ int drmGetDevice(int fd, drmDevicePtr *device) int maj, min; int ret, i, node_count; int max_count = 16; + dev_t find_rdev; if (fd == -1 || device == NULL) return -EINVAL; @@ -3094,6 +3106,7 @@ int drmGetDevice(int fd, drmDevicePtr *device) if (fstat(fd, &sbuf)) return -errno; + find_rdev = sbuf.st_rdev; maj = major(sbuf.st_rdev); min = minor(sbuf.st_rdev); @@ -3154,7 +3167,13 @@ int drmGetDevice(int fd, drmDevicePtr *device) local_devices = temp; } - local_devices[i] = d; + /* move target to the first of local_devices */ + if (find_rdev == sbuf.st_rdev && i) { + local_devices[i] = local_devices[0]; + local_devices[0] = d; + } + else + local_devices[i] = d; i++; } node_count = i; @@ -3267,10 +3286,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices) drmFoldDuplicatedDevices(local_devices, node_count); device_count = 0; - for (i = 0; i < node_count; i++) { - if (!local_devices[i]) - continue; - + for (i = 0; i < node_count && local_devices[i]; i++) { if ((devices != NULL) && (device_count < max_devices)) devices[device_count] = local_devices[i]; else