From patchwork Mon Jan 18 21:01:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Grodzovsky X-Patchwork-Id: 12028235 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10BA9C433E6 for ; Mon, 18 Jan 2021 21:02:22 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B017B22C9E for ; Mon, 18 Jan 2021 21:02:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B017B22C9E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C08C66E593; Mon, 18 Jan 2021 21:02:20 +0000 (UTC) Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2066.outbound.protection.outlook.com [40.107.220.66]) by gabe.freedesktop.org (Postfix) with ESMTPS id 48B976E591; Mon, 18 Jan 2021 21:02:19 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gNUnHAbECbY5fySfB6sHC2RwojmWOFqB6+XE9OWQtVKn5zEiQUVYDSZGSawHS/2nOfsci4ahH8IHt+XXWShTOUMwnJV8gCmrOkm5KOPu3NwnXF4w0SkAxS4nZXuIv0CxoSX5cF6d4F1cVpD7PvWCyTHMgBelElD94ZGwuNTbdLr2svOr25Qcgp23jCm8AKW/GtV3zNEoVdYNEU1f3nHDvhOSAke5iP3bJ5z0co1jjOs0ziVOvYHOD4m0SvAW7Hd9vmBQQL89PRxJoHtD9Fs0f6yP03mZ5AEklYenGy1hulKHsywVyj/av0Qnhe0ANDAuPCHUObb3n8DWM7KehnB/xQ== 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-SenderADCheck; bh=mxArjqIQd8mdWnuKs/ykjW/NMmp8abNtjGupteRZuYQ=; b=MNFaKlIvW3r8MBqsZbQVktVd7tYiMP83g1V5oD0Ps7Z1Eu847M8N2v0Nf6P5RsxaP7nvL+bqCzMTl7DLZ6pXBQ64vyj5yqQHF55zhHpspQGoyKKndlr+fNIArp6Wf752PsuBTo3brQVU9TYQnIGp1VfZTyuEh5O81jsg8vLPu8YCSyeJAWFWNgWzVnxEwM8eVzEh33aMGBGb3G8cGQBSj8l/kvq+HmyjNHwQB6uo4QjOT7xSGj8kCJidmYkWFUhyU/LRHnaGXY3IvmWn2474D4EU/0iVl0msxZwaoFaOYpcpxSEmFwXTdp+X+hOIkZa+3/oPlK//lqf7IVo2DADTfw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=mxArjqIQd8mdWnuKs/ykjW/NMmp8abNtjGupteRZuYQ=; b=iKyrhOgFw6w0OHOjNSYLqvgAoh23iwOvbt0xMwGc6Jnq/wFGTVjeAaiQHQ5AWCB9jI2PmimqisALp+fDnMvHfSrbmjOMbP1cGdbQr0N+ibt6hg55+g8CJH1ep1zPkqABwQIGg/8tb5MeRYLqoq/MvgFEA+Vz6JyomGWqtfZtWdo= Authentication-Results: lists.freedesktop.org; dkim=none (message not signed) header.d=none; lists.freedesktop.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB4623.namprd12.prod.outlook.com (2603:10b6:805:e9::17) by SN6PR12MB4767.namprd12.prod.outlook.com (2603:10b6:805:e5::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.10; Mon, 18 Jan 2021 21:02:17 +0000 Received: from SN6PR12MB4623.namprd12.prod.outlook.com ([fe80::5d30:b29d:5f5b:6921]) by SN6PR12MB4623.namprd12.prod.outlook.com ([fe80::5d30:b29d:5f5b:6921%5]) with mapi id 15.20.3763.014; Mon, 18 Jan 2021 21:02:17 +0000 From: Andrey Grodzovsky To: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, ckoenig.leichtzumerken@gmail.com, daniel.vetter@ffwll.ch, robh@kernel.org, l.stach@pengutronix.de, yuq825@gmail.com, eric@anholt.net Subject: [PATCH v4 00/14] RFC Support hot device unplug in amdgpu Date: Mon, 18 Jan 2021 16:01:09 -0500 Message-Id: <1611003683-3534-1-git-send-email-andrey.grodzovsky@amd.com> X-Mailer: git-send-email 2.7.4 X-Originating-IP: [2607:fea8:3edf:49b0:84d3:21cc:478c:efa7] X-ClientProxiedBy: YTXPR0101CA0020.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b00::33) To SN6PR12MB4623.namprd12.prod.outlook.com (2603:10b6:805:e9::17) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ubuntu-1604-test.hitronhub.home (2607:fea8:3edf:49b0:84d3:21cc:478c:efa7) by YTXPR0101CA0020.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b00::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.20.3763.9 via Frontend Transport; Mon, 18 Jan 2021 21:02:16 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: aec121b1-0d69-48b9-e117-08d8bbf45685 X-MS-TrafficTypeDiagnostic: SN6PR12MB4767: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PAb73J19TEDrlTF0EnKcjmP70lIWFexNJ1l5yAMoJ//jeKWzcMF+5TzyIEEhoCok4iA7qoxxxWUpJjhgE61FQyvxzkGuwBQN/9nPDyWdb7m0QS+sKK1m3XZP9nFTQ3KUX5JCcjDNlwTu+PgJsFPQ5YC2rIXZaBAlQ5wD43eM+dHlYxPKd3JXZcwF58/e9X/hQDx3kJ+tnFAOC5ia56hHvvHKVAjJL3fs8c0g08wZeXAzw6LP6X2ytPO4FOnGSA5jfzTrwxxIKtoZpWKDTDMIlP0AeecJlTd8iFIpTRzwpi8uiV8PYbxVVdbk3QMqa8dI9kMJr7eeAQINXsMBmEOgdo02axdLrOYLQ5Rwegcv6oIePDAM2oe/wHhTHzUZtAQXtt7XrFjnsFSRtWFEUidbGUoFwunx/XzZAvNURWuUnL+344EJMFf14T8DA5/+7kWud46ETfc+0OT3Nzs3IWmPCQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB4623.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(376002)(346002)(39860400002)(366004)(136003)(86362001)(66946007)(6666004)(83380400001)(44832011)(8936002)(66556008)(316002)(6512007)(6486002)(36756003)(2616005)(966005)(66476007)(6506007)(4326008)(478600001)(5660300002)(7416002)(2906002)(52116002)(16526019)(8676002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?utf-8?q?u8cjExz054L6trcMqva2RrW+rWQ/76?= =?utf-8?q?hoh8/qDwn8DqW5/D44FHTVey3pBvqT+Nh2lK818fEdbhcpszwFu3ad8WP0LtJtS5k?= =?utf-8?q?A1zzPFe0TGE/lH3vjXU0U+49od8TDjuRoK3i8PIfaaUv+3AGfpY3wLE9NW9YQSQmC?= =?utf-8?q?tUAYTEitBnBljHNHh4Fk9P9SfTIN6Y497jvXWEhidIKhAQAtHQ7p4JtJ5BCT54fGW?= =?utf-8?q?dEAXkdOuOp7X7aikNMEALmRhVd3hc1i6SMdUfMYqELOfwHKDtG16v191wKa4HSSLw?= =?utf-8?q?x8ddpRzSRZvwKBReUmD5Znblfa6X3/b2xdWIRUgVb0HVa+yKibH6u3Lymybe5ylVC?= =?utf-8?q?pCBmiWR71pu+22Jm5lpkoX51Rl5DlqMEUr9s9cYulv35C2oqiE1MgnSeiVeqTpYG0?= =?utf-8?q?khWKmukNmmC8mhCUyKFcQfbURv/AzW5wJywe34LQuivNoJ7B6nKjXF/MZIjzyLp9/?= =?utf-8?q?8iUrOIiC+3j6FyA7tKOZLdtyJdfOOHDU8cssQE7dMCqgKCcrDy+0sJDI8TQcOpi3i?= =?utf-8?q?QE0feDndjcInNqr68B62/ofv4eSHCvAGsKSFmQ/YJ6I3Y7JXT/6RYJIbmIHqhL1ch?= =?utf-8?q?JsZSZui2GQpZYyL0tRcBB2gEKGDgbnOFb7pif/+BZz17TqemoOFEEQXLbWZge6pXk?= =?utf-8?q?0PMRLU6nrGDkQHmJIo9zXGlQdePiGX+vE/2ZhHqUtgDpdb6OXXdce4YpaaMpHSaIa?= =?utf-8?q?YCbrjK3nSWEqZZJYv4Pfpd3eBlsS0xgS16kyzBAnog4wj/rTmPYfDr6T9Mbe4O0Ln?= =?utf-8?q?uy7qTYW63oHjAXVXWnY+CeActN1uhx6YCHKNV+pwrNTNZDr1E3IU4WC+bpkwMMptN?= =?utf-8?q?FTEdmxaul9NrfQYMxdDDfchdPcJtImNEuyfSpWN491ZmNarJ/nDcaGaryHfB17eIV?= =?utf-8?q?QnK77LgYtZNEkHXdvK46jJswZ/r/qum7TiBOhGKEBfjF/UhESAGyxF3yHBuswpGou?= =?utf-8?q?fuvbHzcqJiaKIBwYcJ+fKaT8Mw/5xUAASLiYaslv2JiSLz0bV8rU5TLwzIUiVMi19?= =?utf-8?q?1UGPF8+KjIkNA4VtzWmFDCn7kcuZbJDhfwt0PXDIOyG5R281EOooZi45aTK14YGZ2?= =?utf-8?q?ZG9xN/wqvYV0SghGYRZTUlq3DCYoYiG6llHA0gg=3D=3D?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: aec121b1-0d69-48b9-e117-08d8bbf45685 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB4623.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2021 21:02:17.2383 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: OR0fe8DNFpm1Fp3dom6gtIgUqswK4A21PSrpInWhs9zhXMARYZcWnpMO7nBG4ANISMuEQdoIyOKqBidPBBGyCQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB4767 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexander.Deucher@amd.com, gregkh@linuxfoundation.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Until now extracting a card either by physical extraction (e.g. eGPU with thunderbolt connection or by emulation through syfs -> /sys/bus/pci/devices/device_id/remove) would cause random crashes in user apps. The random crashes in apps were mostly due to the app having mapped a device backed BO into its address space was still trying to access the BO while the backing device was gone. To answer this first problem Christian suggested to fix the handling of mapped memory in the clients when the device goes away by forcibly unmap all buffers the user processes has by clearing their respective VMAs mapping the device BOs. Then when the VMAs try to fill in the page tables again we check in the fault handlerif the device is removed and if so, return an error. This will generate a SIGBUS to the application which can then cleanly terminate.This indeed was done but this in turn created a problem of kernel OOPs were the OOPSes were due to the fact that while the app was terminating because of the SIGBUSit would trigger use after free in the driver by calling to accesses device structures that were already released from the pci remove sequence.This was handled by introducing a 'flush' sequence during device removal were we wait for drm file reference to drop to 0 meaning all user clients directly using this device terminated. v2: Based on discussions in the mailing list with Daniel and Pekka [1] and based on the document produced by Pekka from those discussions [2] the whole approach with returning SIGBUS and waiting for all user clients having CPU mapping of device BOs to die was dropped. Instead as per the document suggestion the device structures are kept alive until the last reference to the device is dropped by user client and in the meanwhile all existing and new CPU mappings of the BOs belonging to the device directly or by dma-buf import are rerouted to per user process dummy rw page.Also, I skipped the 'Requirements for KMS UAPI' section of [2] since i am trying to get the minimal set of requirements that still give useful solution to work and this is the'Requirements for Render and Cross-Device UAPI' section and so my test case is removing a secondary device, which is render only and is not involved in KMS. v3: More updates following comments from v2 such as removing loop to find DRM file when rerouting page faults to dummy page,getting rid of unnecessary sysfs handling refactoring and moving prevention of GPU recovery post device unplug from amdgpu to scheduler layer. On top of that added unplug support for the IOMMU enabled system. v4: Drop last sysfs hack and use sysfs default attribute. Guard against write accesses after device removal to avoid modifying released memory. Update dummy pages handling to on demand allocation and release through drm managed framework. Add return value to scheduler job TO handler (by Luben Tuikov) and use this in amdgpu for prevention of GPU recovery post device unplug Also rebase on top of drm-misc-mext instead of amd-staging-drm-next With these patches I am able to gracefully remove the secondary card using sysfs remove hook while glxgears is running off of secondary card (DRI_PRIME=1) without kernel oopses or hangs and keep working with the primary card or soft reset the device without hangs or oopses TODOs for followup work: Convert AMDGPU code to use devm (for hw stuff) and drmm (for sw stuff and allocations) (Daniel) Support plugging the secondary device back after unplug - currently still experiencing HW error on plugging back. Add support for 'Requirements for KMS UAPI' section of [2] - unplugging primary, display connected card. [1] - Discussions during v3 of the patchset https://www.spinics.net/lists/amd-gfx/msg55576.html [2] - drm/doc: device hot-unplug for userspace https://www.spinics.net/lists/dri-devel/msg259755.html [3] - Related gitlab ticket https://gitlab.freedesktop.org/drm/amd/-/issues/1081 Andrey Grodzovsky (13): drm/ttm: Remap all page faults to per process dummy page. drm: Unamp the entire device address space on device unplug drm/ttm: Expose ttm_tt_unpopulate for driver use drm/sched: Cancel and flush all oustatdning jobs before finish. drm/amdgpu: Split amdgpu_device_fini into early and late drm/amdgpu: Add early fini callback drm/amdgpu: Register IOMMU topology notifier per device. drm/amdgpu: Fix a bunch of sdma code crash post device unplug drm/amdgpu: Remap all page faults to per process dummy page. dmr/amdgpu: Move some sysfs attrs creation to default_attr drm/amdgpu: Guard against write accesses after device removal drm/sched: Make timeout timer rearm conditional. drm/amdgpu: Prevent any job recoveries after device is unplugged. Luben Tuikov (1): drm/scheduler: Job timeout handler returns status drivers/gpu/drm/amd/amdgpu/amdgpu.h | 11 +- drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c | 17 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 149 ++++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 20 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 15 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 9 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 25 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c | 26 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_irq.h | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 19 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 12 +- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 2 + drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 53 +++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h | 3 + drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 70 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h | 52 +------- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 21 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 14 +- drivers/gpu/drm/amd/amdgpu/cik_ih.c | 2 +- drivers/gpu/drm/amd/amdgpu/cz_ih.c | 2 +- drivers/gpu/drm/amd/amdgpu/iceland_ih.c | 2 +- drivers/gpu/drm/amd/amdgpu/navi10_ih.c | 2 +- drivers/gpu/drm/amd/amdgpu/psp_v11_0.c | 16 +-- drivers/gpu/drm/amd/amdgpu/psp_v12_0.c | 8 +- drivers/gpu/drm/amd/amdgpu/psp_v3_1.c | 8 +- drivers/gpu/drm/amd/amdgpu/si_ih.c | 2 +- drivers/gpu/drm/amd/amdgpu/tonga_ih.c | 2 +- drivers/gpu/drm/amd/amdgpu/vega10_ih.c | 2 +- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 12 +- drivers/gpu/drm/amd/include/amd_shared.h | 2 + drivers/gpu/drm/drm_drv.c | 3 + drivers/gpu/drm/etnaviv/etnaviv_sched.c | 10 +- drivers/gpu/drm/lima/lima_sched.c | 4 +- drivers/gpu/drm/panfrost/panfrost_job.c | 9 +- drivers/gpu/drm/scheduler/sched_main.c | 18 ++- drivers/gpu/drm/ttm/ttm_bo_vm.c | 82 +++++++++++- drivers/gpu/drm/ttm/ttm_tt.c | 1 + drivers/gpu/drm/v3d/v3d_sched.c | 32 ++--- include/drm/gpu_scheduler.h | 17 ++- include/drm/ttm/ttm_bo_api.h | 2 + 45 files changed, 583 insertions(+), 198 deletions(-)