All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/6] Fix crash after reloading a driver using ttm
@ 2019-04-16  0:35 Karol Herbst
  2019-04-16  0:35 ` [PATCH 1/6] Revert "drm: Remove drm_global.{c,h} v2" Karol Herbst
                   ` (6 more replies)
  0 siblings, 7 replies; 18+ messages in thread
From: Karol Herbst @ 2019-04-16  0:35 UTC (permalink / raw)
  To: dri-devel; +Cc: Alex Deucher, Karol Herbst

Kobjects are supposed to be dynamically allocated, but with recent changes
this rule was violated. Reverting those commits fixes crashes when a drm
driver using TTM gets loaded again.

The object in question is "ttm_mem_glob" declared inside
"include/drm/ttm/ttm_memory.h" and instatiated inside
"drivers/gpu/drm/ttm/ttm_memory.c".

from "Documentation/kobject.txt":
"Because kobjects are dynamic, they must not be declared statically or on
the stack, but instead, always allocated dynamically.  Future versions of
the kernel will contain a run-time check for kobjects that are created
statically and will warn the developer of this improper usage."

Unloading ttm before reloading the driver workarounds that crash, because
the memory backing the kobject member "kobj" is cleaned up. The kobject_del
and kobject_put function never free or clean up the kobject object leaving
it in an undefined state.

I reverted a few more commits to make it less painful for me to rever this
rather big change.

dmesg output:
[54758.418036] kobject (00000000687a067d): tried to init an initialized object, something is seriously wrong.
[54758.418040] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
[54758.418041] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
[54758.418041] Call Trace:
[54758.418049]  dump_stack+0x5c/0x80
[54758.418054]  kobject_init.cold.9+0x31/0x3f
[54758.418057]  kobject_init_and_add+0x35/0xa0
[54758.418063]  ttm_mem_global_init+0x8f/0x2b0 [ttm]
[54758.418067]  ? __debugfs_create_file+0xe1/0x110
[54758.418071]  ttm_bo_device_init+0x198/0x2a0 [ttm]
[54758.418144]  nouveau_ttm_init+0xbf/0x340 [nouveau]
[54758.418206]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
[54758.418210]  ? pci_bus_read_config_word+0x49/0x70
[54758.418266]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
[54758.418270]  local_pci_probe+0x41/0x90
[54758.418272]  pci_device_probe+0x118/0x1a0
[54758.418275]  really_probe+0xf8/0x3b0
[54758.418277]  driver_probe_device+0xb3/0xf0
[54758.418278]  __driver_attach+0xdd/0x110
[54758.418280]  ? driver_probe_device+0xf0/0xf0
[54758.418282]  bus_for_each_dev+0x77/0xc0
[54758.418285]  ? klist_add_tail+0x3b/0x60
[54758.418287]  bus_add_driver+0x152/0x230
[54758.418288]  ? 0xffffffffc1027000
[54758.418290]  driver_register+0x6b/0xb0
[54758.418291]  ? 0xffffffffc1027000
[54758.418294]  do_one_initcall+0x46/0x1c3
[54758.418296]  ? _cond_resched+0x15/0x30
[54758.418299]  ? kmem_cache_alloc_trace+0x154/0x1d0
[54758.418302]  do_init_module+0x5a/0x210
[54758.418304]  load_module+0x2096/0x22d0
[54758.418308]  ? ima_post_read_file+0xf4/0x100
[54758.418310]  ? __do_sys_finit_module+0xa8/0x110
[54758.418312]  __do_sys_finit_module+0xa8/0x110
[54758.418315]  do_syscall_64+0x5b/0x160
[54758.418317]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[54758.418319] RIP: 0033:0x7fc0b38b6edd
[54758.418321] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
[54758.418322] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
[54758.418323] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
[54758.418324] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
[54758.418325] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
[54758.418326] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
[54758.418326] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
[54758.418346] BUG: unable to handle kernel paging request at 00000004ee194700
[54758.418348] #PF error: [WRITE]
[54758.418349] PGD 0 P4D 0 
[54758.418352] Oops: 0002 [#1] SMP PTI
[54758.418354] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
[54758.418355] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
[54758.418360] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
[54758.418361] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
[54758.418363] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
[54758.418364] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
[54758.418366] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
[54758.418367] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
[54758.418368] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
[54758.418370] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
[54758.418371] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
[54758.418373] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[54758.418374] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0
[54758.418375] Call Trace:
[54758.418381]  ttm_bo_device_init+0x198/0x2a0 [ttm]
[54758.418439]  nouveau_ttm_init+0xbf/0x340 [nouveau]
[54758.418495]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
[54758.418499]  ? pci_bus_read_config_word+0x49/0x70
[54758.418552]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
[54758.418556]  local_pci_probe+0x41/0x90
[54758.418558]  pci_device_probe+0x118/0x1a0
[54758.418561]  really_probe+0xf8/0x3b0
[54758.418563]  driver_probe_device+0xb3/0xf0
[54758.418565]  __driver_attach+0xdd/0x110
[54758.418567]  ? driver_probe_device+0xf0/0xf0
[54758.418570]  bus_for_each_dev+0x77/0xc0
[54758.418573]  ? klist_add_tail+0x3b/0x60
[54758.418574]  bus_add_driver+0x152/0x230
[54758.418576]  ? 0xffffffffc1027000
[54758.418578]  driver_register+0x6b/0xb0
[54758.418580]  ? 0xffffffffc1027000
[54758.418583]  do_one_initcall+0x46/0x1c3
[54758.418585]  ? _cond_resched+0x15/0x30
[54758.418587]  ? kmem_cache_alloc_trace+0x154/0x1d0
[54758.418591]  do_init_module+0x5a/0x210
[54758.418593]  load_module+0x2096/0x22d0
[54758.418596]  ? ima_post_read_file+0xf4/0x100
[54758.418599]  ? __do_sys_finit_module+0xa8/0x110
[54758.418601]  __do_sys_finit_module+0xa8/0x110
[54758.418604]  do_syscall_64+0x5b/0x160
[54758.418607]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[54758.418609] RIP: 0033:0x7fc0b38b6edd
[54758.418610] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
[54758.418612] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
[54758.418614] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
[54758.418615] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
[54758.418616] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
[54758.418618] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
[54758.418619] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
[54758.418620] Modules linked in: nouveau(OE+) acpi_call(OE) ttm rfcomm ccm xt_CHECKSUM ipt_MASQUERADE tun bridge stp llc devlink ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 xt_conntrack ebtable_nat ip6table_nat nf_nat_ipv6 ip6table_mangle ip6table_raw ip6table_security iptable_nat nf_nat_ipv4 nf_nat iptable_mangle iptable_raw iptable_security nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 libcrc32c ip_set nfnetlink ebtable_filter ebtables ip6table_filter ip6_tables cmac bnep sunrpc vfat fat btusb btrtl btbcm btintel uvcvideo videobuf2_vmalloc bluetooth videobuf2_memops videobuf2_v4l2 videobuf2_common videodev media ecdh_generic arc4 snd_hda_codec_hdmi ath10k_pci ath10k_core snd_hda_codec_realtek iTCO_wdt mei_wdt iTCO_vendor_support snd_hda_codec_generic mac80211 intel_rapl snd_hda_intel x86_pkg_temp_thermal intel_powerclamp coretemp snd_hda_codec dell_laptop ledtrig_audio dell_smm_hwmon snd_hda_core kvm_intel snd_hwdep ath snd_seq snd_seq_device intel_cstate cfg80211 snd_pcm dell_wmi intel_uncore
[54758.418653]  dell_smbios dcdbas intel_rapl_perf snd_timer joydev dell_wmi_descriptor intel_wmi_thunderbolt wmi_bmof snd idma64 soundcore i2c_i801 rfkill rtsx_pci_ms memstick mei_me mei processor_thermal_device intel_lpss_pci intel_soc_dts_iosf intel_pch_thermal intel_lpss int3400_thermal acpi_pad acpi_thermal_rel int3403_thermal intel_hid pcc_cpufreq int340x_thermal_zone sparse_keymap binfmt_misc zram dm_crypt hid_multitouch i915 kvmgt mdev vfio kvm irqbypass i2c_algo_bit crct10dif_pclmul drm_kms_helper rtsx_pci_sdmmc crc32_pclmul mmc_core crc32c_intel mxm_wmi drm nvme ghash_clmulni_intel nvme_core serio_raw rtsx_pci i2c_hid video wmi i2c_dev lz4 lz4_compress [last unloaded: nouveau]
[54758.418679] CR2: 00000004ee194700
[54758.418681] ---[ end trace c5175234e6efc034 ]---
[54758.418686] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
[54758.418688] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
[54758.418689] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
[54758.418690] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
[54758.418691] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
[54758.418692] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
[54758.418694] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
[54758.418695] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
[54758.418696] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
[54758.418698] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[54758.418699] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0

Karol Herbst (6):
  Revert "drm: Remove drm_global.{c,h} v2"
  Revert "drm/ttm: initialize globals during device init (v2)"
  Revert "drm/ttm: Fix bo_global and mem_global kfree error"
  Revert "drm/ttm: use a static ttm_bo_global instance"
  Revert "drm/ttm: make the device list mutex static"
  Revert "drm/ttm: use a static ttm_mem_global instance"

 drivers/gpu/drm/Makefile                      |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 103 ++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |   2 +
 drivers/gpu/drm/ast/ast_drv.h                 |   2 +
 drivers/gpu/drm/ast/ast_ttm.c                 |  64 ++++++++
 drivers/gpu/drm/bochs/bochs.h                 |   2 +
 drivers/gpu/drm/bochs/bochs_mm.c              |  61 ++++++++
 drivers/gpu/drm/cirrus/cirrus_drv.h           |   2 +
 drivers/gpu/drm/cirrus/cirrus_ttm.c           |  64 ++++++++
 drivers/gpu/drm/drm_drv.c                     |   2 +
 drivers/gpu/drm/drm_global.c                  | 137 ++++++++++++++++++
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h   |   2 +
 drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c   |  57 ++++++++
 drivers/gpu/drm/mgag200/mgag200_drv.h         |   2 +
 drivers/gpu/drm/mgag200/mgag200_ttm.c         |  64 ++++++++
 drivers/gpu/drm/nouveau/nouveau_drv.h         |   2 +
 drivers/gpu/drm/nouveau/nouveau_ttm.c         |  67 +++++++++
 drivers/gpu/drm/qxl/qxl_drv.h                 |   3 +
 drivers/gpu/drm/qxl/qxl_ttm.c                 |  61 ++++++++
 drivers/gpu/drm/radeon/radeon.h               |   3 +
 drivers/gpu/drm/radeon/radeon_ttm.c           |  65 +++++++++
 drivers/gpu/drm/ttm/ttm_bo.c                  |  67 +++------
 drivers/gpu/drm/ttm/ttm_memory.c              |  14 +-
 drivers/gpu/drm/virtio/virtgpu_drv.h          |   3 +
 drivers/gpu/drm/virtio/virtgpu_ttm.c          |  62 ++++++++
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  15 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.h           |   6 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c      |  54 +++++++
 drivers/staging/vboxvideo/vbox_drv.h          |   2 +
 drivers/staging/vboxvideo/vbox_ttm.c          |  65 ++++++++-
 include/drm/drmP.h                            |   1 +
 include/drm/drm_global.h                      |  53 +++++++
 include/drm/ttm/ttm_bo_driver.h               |  54 ++++++-
 include/drm/ttm/ttm_memory.h                  |   4 +-
 34 files changed, 1102 insertions(+), 65 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_global.c
 create mode 100644 include/drm/drm_global.h

-- 
2.20.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH 1/6] Revert "drm: Remove drm_global.{c,h} v2"
  2019-04-16  0:35 [PATCH 0/6] Fix crash after reloading a driver using ttm Karol Herbst
@ 2019-04-16  0:35 ` Karol Herbst
  2019-04-16  0:35 ` [PATCH 2/6] Revert "drm/ttm: initialize globals during device init (v2)" Karol Herbst
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 18+ messages in thread
From: Karol Herbst @ 2019-04-16  0:35 UTC (permalink / raw)
  To: dri-devel; +Cc: Alex Deucher, Karol Herbst

This reverts commit 2bb42410b1bd324912389c6ac748df1c1befd69f.

Signed-off-by: Karol Herbst <kherbst@redhat.com>
---
 drivers/gpu/drm/Makefile        |   2 +-
 drivers/gpu/drm/drm_drv.c       |   2 +
 drivers/gpu/drm/drm_global.c    | 137 ++++++++++++++++++++++++++++++++
 include/drm/drmP.h              |   1 +
 include/drm/drm_global.h        |  53 ++++++++++++
 include/drm/ttm/ttm_bo_driver.h |   1 +
 6 files changed, 195 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/drm_global.c
 create mode 100644 include/drm/drm_global.h

diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index ce8d1d384319..a3a19e9020e3 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -11,7 +11,7 @@ drm-y       :=	drm_auth.o drm_bufs.o drm_cache.o \
 		drm_sysfs.o drm_hashtab.o drm_mm.o \
 		drm_crtc.o drm_fourcc.o drm_modes.o drm_edid.o \
 		drm_encoder_slave.o \
-		drm_trace_points.o drm_prime.o \
+		drm_trace_points.o drm_global.o drm_prime.o \
 		drm_rect.o drm_vma_manager.o drm_flip_work.o \
 		drm_modeset_lock.o drm_atomic.o drm_bridge.o \
 		drm_framebuffer.o drm_connector.o drm_blend.o \
diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
index 12e5e2be7890..5f7e99bf4fa4 100644
--- a/drivers/gpu/drm/drm_drv.c
+++ b/drivers/gpu/drm/drm_drv.c
@@ -960,12 +960,14 @@ static void drm_core_exit(void)
 	drm_sysfs_destroy();
 	idr_destroy(&drm_minors_idr);
 	drm_connector_ida_destroy();
+	drm_global_release();
 }
 
 static int __init drm_core_init(void)
 {
 	int ret;
 
+	drm_global_init();
 	drm_connector_ida_init();
 	idr_init(&drm_minors_idr);
 
diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
new file mode 100644
index 000000000000..5799e2782dd1
--- /dev/null
+++ b/drivers/gpu/drm/drm_global.c
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+/**************************************************************************
+ *
+ * Copyright 2008-2009 VMware, Inc., Palo Alto, CA., USA
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+/*
+ * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
+ */
+
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <drm/drm_global.h>
+
+struct drm_global_item {
+	struct mutex mutex;
+	void *object;
+	int refcount;
+};
+
+static struct drm_global_item glob[DRM_GLOBAL_NUM];
+
+void drm_global_init(void)
+{
+	int i;
+
+	for (i = 0; i < DRM_GLOBAL_NUM; ++i) {
+		struct drm_global_item *item = &glob[i];
+		mutex_init(&item->mutex);
+		item->object = NULL;
+		item->refcount = 0;
+	}
+}
+
+void drm_global_release(void)
+{
+	int i;
+	for (i = 0; i < DRM_GLOBAL_NUM; ++i) {
+		struct drm_global_item *item = &glob[i];
+		BUG_ON(item->object != NULL);
+		BUG_ON(item->refcount != 0);
+	}
+}
+
+/**
+ * drm_global_item_ref - Initialize and acquire reference to memory
+ * object
+ * @ref: Object for initialization
+ *
+ * This initializes a memory object, allocating memory and calling the
+ * .init() hook. Further calls will increase the reference count for
+ * that item.
+ *
+ * Returns:
+ * Zero on success, non-zero otherwise.
+ */
+int drm_global_item_ref(struct drm_global_reference *ref)
+{
+	int ret = 0;
+	struct drm_global_item *item = &glob[ref->global_type];
+
+	mutex_lock(&item->mutex);
+	if (item->refcount == 0) {
+		ref->object = kzalloc(ref->size, GFP_KERNEL);
+		if (unlikely(ref->object == NULL)) {
+			ret = -ENOMEM;
+			goto error_unlock;
+		}
+		ret = ref->init(ref);
+		if (unlikely(ret != 0))
+			goto error_free;
+
+		item->object = ref->object;
+	} else {
+		ref->object = item->object;
+	}
+
+	++item->refcount;
+	mutex_unlock(&item->mutex);
+	return 0;
+
+error_free:
+	kfree(ref->object);
+	ref->object = NULL;
+error_unlock:
+	mutex_unlock(&item->mutex);
+	return ret;
+}
+EXPORT_SYMBOL(drm_global_item_ref);
+
+/**
+ * drm_global_item_unref - Drop reference to memory
+ * object
+ * @ref: Object being removed
+ *
+ * Drop a reference to the memory object and eventually call the
+ * release() hook.  The allocated object should be dropped in the
+ * release() hook or before calling this function
+ *
+ */
+
+void drm_global_item_unref(struct drm_global_reference *ref)
+{
+	struct drm_global_item *item = &glob[ref->global_type];
+
+	mutex_lock(&item->mutex);
+	BUG_ON(item->refcount == 0);
+	BUG_ON(ref->object != item->object);
+	if (--item->refcount == 0) {
+		ref->release(ref);
+		item->object = NULL;
+	}
+	mutex_unlock(&item->mutex);
+}
+EXPORT_SYMBOL(drm_global_item_unref);
+
diff --git a/include/drm/drmP.h b/include/drm/drmP.h
index bdb0d5548f39..514beb2d483a 100644
--- a/include/drm/drmP.h
+++ b/include/drm/drmP.h
@@ -68,6 +68,7 @@
 #include <drm/drm_agpsupport.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_fourcc.h>
+#include <drm/drm_global.h>
 #include <drm/drm_hashtab.h>
 #include <drm/drm_mm.h>
 #include <drm/drm_os_linux.h>
diff --git a/include/drm/drm_global.h b/include/drm/drm_global.h
new file mode 100644
index 000000000000..3a830602a2e4
--- /dev/null
+++ b/include/drm/drm_global.h
@@ -0,0 +1,53 @@
+/**************************************************************************
+ *
+ * Copyright 2008-2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+/*
+ * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
+ */
+
+#ifndef _DRM_GLOBAL_H_
+#define _DRM_GLOBAL_H_
+enum drm_global_types {
+	DRM_GLOBAL_TTM_MEM = 0,
+	DRM_GLOBAL_TTM_BO,
+	DRM_GLOBAL_TTM_OBJECT,
+	DRM_GLOBAL_NUM
+};
+
+struct drm_global_reference {
+	enum drm_global_types global_type;
+	size_t size;
+	void *object;
+	int (*init) (struct drm_global_reference *);
+	void (*release) (struct drm_global_reference *);
+};
+
+void drm_global_init(void);
+void drm_global_release(void);
+int drm_global_item_ref(struct drm_global_reference *ref);
+void drm_global_item_unref(struct drm_global_reference *ref);
+
+#endif
diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
index 1021106438b2..6fb589f64633 100644
--- a/include/drm/ttm/ttm_bo_driver.h
+++ b/include/drm/ttm/ttm_bo_driver.h
@@ -31,6 +31,7 @@
 #define _TTM_BO_DRIVER_H_
 
 #include <drm/drm_mm.h>
+#include <drm/drm_global.h>
 #include <drm/drm_vma_manager.h>
 #include <linux/workqueue.h>
 #include <linux/fs.h>
-- 
2.20.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 2/6] Revert "drm/ttm: initialize globals during device init (v2)"
  2019-04-16  0:35 [PATCH 0/6] Fix crash after reloading a driver using ttm Karol Herbst
  2019-04-16  0:35 ` [PATCH 1/6] Revert "drm: Remove drm_global.{c,h} v2" Karol Herbst
@ 2019-04-16  0:35 ` Karol Herbst
  2019-04-16  0:35 ` [PATCH 3/6] Revert "drm/ttm: Fix bo_global and mem_global kfree error" Karol Herbst
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 18+ messages in thread
From: Karol Herbst @ 2019-04-16  0:35 UTC (permalink / raw)
  To: dri-devel; +Cc: Alex Deucher, Karol Herbst

This reverts commit a64f784bb14a56bfdfad2dc397dd67e4564e3a29.

Signed-off-by: Karol Herbst <kherbst@redhat.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 59 ++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  1 +
 drivers/gpu/drm/ast/ast_drv.h                 |  1 +
 drivers/gpu/drm/ast/ast_ttm.c                 | 36 +++++++++++
 drivers/gpu/drm/bochs/bochs.h                 |  1 +
 drivers/gpu/drm/bochs/bochs_mm.c              | 35 +++++++++++
 drivers/gpu/drm/cirrus/cirrus_drv.h           |  1 +
 drivers/gpu/drm/cirrus/cirrus_ttm.c           | 36 +++++++++++
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h   |  1 +
 drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c   | 34 +++++++++++
 drivers/gpu/drm/mgag200/mgag200_drv.h         |  1 +
 drivers/gpu/drm/mgag200/mgag200_ttm.c         | 36 +++++++++++
 drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 +
 drivers/gpu/drm/nouveau/nouveau_ttm.c         | 39 ++++++++++++
 drivers/gpu/drm/qxl/qxl_drv.h                 |  2 +
 drivers/gpu/drm/qxl/qxl_ttm.c                 | 33 +++++++++++
 drivers/gpu/drm/radeon/radeon.h               |  2 +
 drivers/gpu/drm/radeon/radeon_ttm.c           | 39 ++++++++++++
 drivers/gpu/drm/ttm/ttm_bo.c                  | 19 +++---
 drivers/gpu/drm/virtio/virtgpu_drv.h          |  2 +
 drivers/gpu/drm/virtio/virtgpu_ttm.c          | 35 +++++++++++
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           | 11 +++-
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.h           |  3 +
 drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c      | 27 +++++++++
 drivers/staging/vboxvideo/vbox_drv.h          |  1 +
 drivers/staging/vboxvideo/vbox_ttm.c          | 41 ++++++++++++-
 include/drm/ttm/ttm_bo_driver.h               | 41 ++++++++++++-
 27 files changed, 521 insertions(+), 17 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index c91ec3101d00..213d29d0ad80 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -61,6 +61,56 @@ static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
 static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
 static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
 
+/*
+ * Global memory.
+ */
+
+/**
+ * amdgpu_ttm_global_init - Initialize global TTM memory reference structures.
+ *
+ * @adev: AMDGPU device for which the global structures need to be registered.
+ *
+ * This is called as part of the AMDGPU ttm init from amdgpu_ttm_init()
+ * during bring up.
+ */
+static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
+{
+	struct drm_global_reference *global_ref;
+	int r;
+
+	/* ensure reference is false in case init fails */
+	adev->mman.mem_global_referenced = false;
+
+	global_ref = &adev->mman.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+	r = drm_global_item_ref(global_ref);
+	if (r) {
+		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		goto error_bo;
+	}
+
+	mutex_init(&adev->mman.gtt_window_lock);
+
+	adev->mman.mem_global_referenced = true;
+
+	return 0;
+
+error_bo:
+	return r;
+}
+
+static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
+{
+	if (adev->mman.mem_global_referenced) {
+		mutex_destroy(&adev->mman.gtt_window_lock);
+		drm_global_item_unref(&adev->mman.bo_global_ref.ref);
+		adev->mman.mem_global_referenced = false;
+	}
+}
+
 static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
 {
 	return 0;
@@ -1664,10 +1714,14 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
 	int r;
 	u64 vis_vram_limit;
 
-	mutex_init(&adev->mman.gtt_window_lock);
-
+	/* initialize global references for vram/gtt */
+	r = amdgpu_ttm_global_init(adev);
+	if (r) {
+		return r;
+	}
 	/* No others user of address space so set it to 0 */
 	r = ttm_bo_device_init(&adev->mman.bdev,
+			       adev->mman.bo_global_ref.ref.object,
 			       &amdgpu_bo_driver,
 			       adev->ddev->anon_inode->i_mapping,
 			       DRM_FILE_PAGE_OFFSET,
@@ -1824,6 +1878,7 @@ void amdgpu_ttm_fini(struct amdgpu_device *adev)
 	ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
 	ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
 	ttm_bo_device_release(&adev->mman.bdev);
+	amdgpu_ttm_global_fini(adev);
 	adev->mman.initialized = false;
 	DRM_INFO("amdgpu: ttm finalized\n");
 }
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
index b5b2d101f7db..e114f209b701 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
@@ -39,6 +39,7 @@
 #define AMDGPU_GTT_NUM_TRANSFER_WINDOWS	2
 
 struct amdgpu_mman {
+	struct ttm_bo_global_ref        bo_global_ref;
 	struct ttm_bo_device		bdev;
 	bool				mem_global_referenced;
 	bool				initialized;
diff --git a/drivers/gpu/drm/ast/ast_drv.h b/drivers/gpu/drm/ast/ast_drv.h
index bfc65040dfcb..6ae11a477643 100644
--- a/drivers/gpu/drm/ast/ast_drv.h
+++ b/drivers/gpu/drm/ast/ast_drv.h
@@ -104,6 +104,7 @@ struct ast_private {
 	int fb_mtrr;
 
 	struct {
+		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 	} ttm;
 
diff --git a/drivers/gpu/drm/ast/ast_ttm.c b/drivers/gpu/drm/ast/ast_ttm.c
index c168d62fe8f9..8a59d6fc1160 100644
--- a/drivers/gpu/drm/ast/ast_ttm.c
+++ b/drivers/gpu/drm/ast/ast_ttm.c
@@ -36,6 +36,35 @@ ast_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct ast_private, ttm.bdev);
 }
 
+static int ast_ttm_global_init(struct ast_private *ast)
+{
+	struct drm_global_reference *global_ref;
+	int r;
+
+	global_ref = &ast->ttm.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		return r;
+	}
+	return 0;
+}
+
+static void
+ast_ttm_global_release(struct ast_private *ast)
+{
+	if (ast->ttm.bo_global_ref.ref.release == NULL)
+		return;
+
+	drm_global_item_unref(&ast->ttm.bo_global_ref.ref);
+	ast->ttm.bo_global_ref.ref.release = NULL;
+}
+
+
 static void ast_bo_ttm_destroy(struct ttm_buffer_object *tbo)
 {
 	struct ast_bo *bo;
@@ -175,7 +204,12 @@ int ast_mm_init(struct ast_private *ast)
 	struct drm_device *dev = ast->dev;
 	struct ttm_bo_device *bdev = &ast->ttm.bdev;
 
+	ret = ast_ttm_global_init(ast);
+	if (ret)
+		return ret;
+
 	ret = ttm_bo_device_init(&ast->ttm.bdev,
+				 ast->ttm.bo_global_ref.ref.object,
 				 &ast_bo_driver,
 				 dev->anon_inode->i_mapping,
 				 DRM_FILE_PAGE_OFFSET,
@@ -206,6 +240,8 @@ void ast_mm_fini(struct ast_private *ast)
 
 	ttm_bo_device_release(&ast->ttm.bdev);
 
+	ast_ttm_global_release(ast);
+
 	arch_phys_wc_del(ast->fb_mtrr);
 	arch_io_free_memtype_wc(pci_resource_start(dev->pdev, 0),
 				pci_resource_len(dev->pdev, 0));
diff --git a/drivers/gpu/drm/bochs/bochs.h b/drivers/gpu/drm/bochs/bochs.h
index fb38c8b857b5..c1d88ea6b419 100644
--- a/drivers/gpu/drm/bochs/bochs.h
+++ b/drivers/gpu/drm/bochs/bochs.h
@@ -77,6 +77,7 @@ struct bochs_device {
 
 	/* ttm */
 	struct {
+		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 		bool initialized;
 	} ttm;
diff --git a/drivers/gpu/drm/bochs/bochs_mm.c b/drivers/gpu/drm/bochs/bochs_mm.c
index 0980411e41bf..cad65774a973 100644
--- a/drivers/gpu/drm/bochs/bochs_mm.c
+++ b/drivers/gpu/drm/bochs/bochs_mm.c
@@ -16,6 +16,35 @@ static inline struct bochs_device *bochs_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct bochs_device, ttm.bdev);
 }
 
+static int bochs_ttm_global_init(struct bochs_device *bochs)
+{
+	struct drm_global_reference *global_ref;
+	int r;
+
+	global_ref = &bochs->ttm.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		return r;
+	}
+
+	return 0;
+}
+
+static void bochs_ttm_global_release(struct bochs_device *bochs)
+{
+	if (bochs->ttm.bo_global_ref.ref.release == NULL)
+		return;
+
+	drm_global_item_unref(&bochs->ttm.bo_global_ref.ref);
+	bochs->ttm.bo_global_ref.ref.release = NULL;
+}
+
+
 static void bochs_bo_ttm_destroy(struct ttm_buffer_object *tbo)
 {
 	struct bochs_bo *bo;
@@ -153,7 +182,12 @@ int bochs_mm_init(struct bochs_device *bochs)
 	struct ttm_bo_device *bdev = &bochs->ttm.bdev;
 	int ret;
 
+	ret = bochs_ttm_global_init(bochs);
+	if (ret)
+		return ret;
+
 	ret = ttm_bo_device_init(&bochs->ttm.bdev,
+				 bochs->ttm.bo_global_ref.ref.object,
 				 &bochs_bo_driver,
 				 bochs->dev->anon_inode->i_mapping,
 				 DRM_FILE_PAGE_OFFSET,
@@ -180,6 +214,7 @@ void bochs_mm_fini(struct bochs_device *bochs)
 		return;
 
 	ttm_bo_device_release(&bochs->ttm.bdev);
+	bochs_ttm_global_release(bochs);
 	bochs->ttm.initialized = false;
 }
 
diff --git a/drivers/gpu/drm/cirrus/cirrus_drv.h b/drivers/gpu/drm/cirrus/cirrus_drv.h
index f2b2e0d169fa..01852fbda9da 100644
--- a/drivers/gpu/drm/cirrus/cirrus_drv.h
+++ b/drivers/gpu/drm/cirrus/cirrus_drv.h
@@ -136,6 +136,7 @@ struct cirrus_device {
 	int fb_mtrr;
 
 	struct {
+		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 	} ttm;
 	bool mm_inited;
diff --git a/drivers/gpu/drm/cirrus/cirrus_ttm.c b/drivers/gpu/drm/cirrus/cirrus_ttm.c
index e075810b4bd4..7801c56c3c9b 100644
--- a/drivers/gpu/drm/cirrus/cirrus_ttm.c
+++ b/drivers/gpu/drm/cirrus/cirrus_ttm.c
@@ -36,6 +36,35 @@ cirrus_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct cirrus_device, ttm.bdev);
 }
 
+static int cirrus_ttm_global_init(struct cirrus_device *cirrus)
+{
+	struct drm_global_reference *global_ref;
+	int r;
+
+	global_ref = &cirrus->ttm.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		return r;
+	}
+	return 0;
+}
+
+static void
+cirrus_ttm_global_release(struct cirrus_device *cirrus)
+{
+	if (cirrus->ttm.bo_global_ref.ref.release == NULL)
+		return;
+
+	drm_global_item_unref(&cirrus->ttm.bo_global_ref.ref);
+	cirrus->ttm.bo_global_ref.ref.release = NULL;
+}
+
+
 static void cirrus_bo_ttm_destroy(struct ttm_buffer_object *tbo)
 {
 	struct cirrus_bo *bo;
@@ -175,7 +204,12 @@ int cirrus_mm_init(struct cirrus_device *cirrus)
 	struct drm_device *dev = cirrus->dev;
 	struct ttm_bo_device *bdev = &cirrus->ttm.bdev;
 
+	ret = cirrus_ttm_global_init(cirrus);
+	if (ret)
+		return ret;
+
 	ret = ttm_bo_device_init(&cirrus->ttm.bdev,
+				 cirrus->ttm.bo_global_ref.ref.object,
 				 &cirrus_bo_driver,
 				 dev->anon_inode->i_mapping,
 				 DRM_FILE_PAGE_OFFSET,
@@ -211,6 +245,8 @@ void cirrus_mm_fini(struct cirrus_device *cirrus)
 
 	ttm_bo_device_release(&cirrus->ttm.bdev);
 
+	cirrus_ttm_global_release(cirrus);
+
 	arch_phys_wc_del(cirrus->fb_mtrr);
 	cirrus->fb_mtrr = 0;
 	arch_io_free_memtype_wc(pci_resource_start(dev->pdev, 0),
diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h
index 3c168ae77b0c..60479502e277 100644
--- a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h
+++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h
@@ -49,6 +49,7 @@ struct hibmc_drm_private {
 	bool mode_config_initialized;
 
 	/* ttm */
+	struct ttm_bo_global_ref bo_global_ref;
 	struct ttm_bo_device bdev;
 	bool initialized;
 
diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c b/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c
index dd383267884c..14071c849121 100644
--- a/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c
+++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c
@@ -29,6 +29,32 @@ hibmc_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct hibmc_drm_private, bdev);
 }
 
+static int hibmc_ttm_global_init(struct hibmc_drm_private *hibmc)
+{
+	int ret;
+
+	hibmc->bo_global_ref.ref.global_type = DRM_GLOBAL_TTM_BO;
+	hibmc->bo_global_ref.ref.size = sizeof(struct ttm_bo_global);
+	hibmc->bo_global_ref.ref.init = &ttm_bo_global_ref_init;
+	hibmc->bo_global_ref.ref.release = &ttm_bo_global_ref_release;
+	ret = drm_global_item_ref(&hibmc->bo_global_ref.ref);
+	if (ret) {
+		DRM_ERROR("failed setting up TTM BO subsystem: %d\n", ret);
+		return ret;
+	}
+	return 0;
+}
+
+static void
+hibmc_ttm_global_release(struct hibmc_drm_private *hibmc)
+{
+	if (hibmc->bo_global_ref.ref.release == NULL)
+		return;
+
+	drm_global_item_unref(&hibmc->bo_global_ref.ref);
+	hibmc->bo_global_ref.ref.release = NULL;
+}
+
 static void hibmc_bo_ttm_destroy(struct ttm_buffer_object *tbo)
 {
 	struct hibmc_bo *bo = container_of(tbo, struct hibmc_bo, bo);
@@ -188,12 +214,18 @@ int hibmc_mm_init(struct hibmc_drm_private *hibmc)
 	struct drm_device *dev = hibmc->dev;
 	struct ttm_bo_device *bdev = &hibmc->bdev;
 
+	ret = hibmc_ttm_global_init(hibmc);
+	if (ret)
+		return ret;
+
 	ret = ttm_bo_device_init(&hibmc->bdev,
+				 hibmc->bo_global_ref.ref.object,
 				 &hibmc_bo_driver,
 				 dev->anon_inode->i_mapping,
 				 DRM_FILE_PAGE_OFFSET,
 				 true);
 	if (ret) {
+		hibmc_ttm_global_release(hibmc);
 		DRM_ERROR("error initializing bo driver: %d\n", ret);
 		return ret;
 	}
@@ -201,6 +233,7 @@ int hibmc_mm_init(struct hibmc_drm_private *hibmc)
 	ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
 			     hibmc->fb_size >> PAGE_SHIFT);
 	if (ret) {
+		hibmc_ttm_global_release(hibmc);
 		DRM_ERROR("failed ttm VRAM init: %d\n", ret);
 		return ret;
 	}
@@ -215,6 +248,7 @@ void hibmc_mm_fini(struct hibmc_drm_private *hibmc)
 		return;
 
 	ttm_bo_device_release(&hibmc->bdev);
+	hibmc_ttm_global_release(hibmc);
 	hibmc->mm_inited = false;
 }
 
diff --git a/drivers/gpu/drm/mgag200/mgag200_drv.h b/drivers/gpu/drm/mgag200/mgag200_drv.h
index 0aaedc554879..e5348955a3cc 100644
--- a/drivers/gpu/drm/mgag200/mgag200_drv.h
+++ b/drivers/gpu/drm/mgag200/mgag200_drv.h
@@ -212,6 +212,7 @@ struct mga_device {
 	int fb_mtrr;
 
 	struct {
+		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 	} ttm;
 
diff --git a/drivers/gpu/drm/mgag200/mgag200_ttm.c b/drivers/gpu/drm/mgag200/mgag200_ttm.c
index d96a9b32455e..11bdc8121a7d 100644
--- a/drivers/gpu/drm/mgag200/mgag200_ttm.c
+++ b/drivers/gpu/drm/mgag200/mgag200_ttm.c
@@ -36,6 +36,35 @@ mgag200_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct mga_device, ttm.bdev);
 }
 
+static int mgag200_ttm_global_init(struct mga_device *ast)
+{
+	struct drm_global_reference *global_ref;
+	int r;
+
+	global_ref = &ast->ttm.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		return r;
+	}
+	return 0;
+}
+
+static void
+mgag200_ttm_global_release(struct mga_device *ast)
+{
+	if (ast->ttm.bo_global_ref.ref.release == NULL)
+		return;
+
+	drm_global_item_unref(&ast->ttm.bo_global_ref.ref);
+	ast->ttm.bo_global_ref.ref.release = NULL;
+}
+
+
 static void mgag200_bo_ttm_destroy(struct ttm_buffer_object *tbo)
 {
 	struct mgag200_bo *bo;
@@ -175,7 +204,12 @@ int mgag200_mm_init(struct mga_device *mdev)
 	struct drm_device *dev = mdev->dev;
 	struct ttm_bo_device *bdev = &mdev->ttm.bdev;
 
+	ret = mgag200_ttm_global_init(mdev);
+	if (ret)
+		return ret;
+
 	ret = ttm_bo_device_init(&mdev->ttm.bdev,
+				 mdev->ttm.bo_global_ref.ref.object,
 				 &mgag200_bo_driver,
 				 dev->anon_inode->i_mapping,
 				 DRM_FILE_PAGE_OFFSET,
@@ -206,6 +240,8 @@ void mgag200_mm_fini(struct mga_device *mdev)
 
 	ttm_bo_device_release(&mdev->ttm.bdev);
 
+	mgag200_ttm_global_release(mdev);
+
 	arch_io_free_memtype_wc(pci_resource_start(dev->pdev, 0),
 				pci_resource_len(dev->pdev, 0));
 	arch_phys_wc_del(mdev->fb_mtrr);
diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/nouveau_drv.h
index d20b9ba4b1c1..0f3bb1a11fc9 100644
--- a/drivers/gpu/drm/nouveau/nouveau_drv.h
+++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
@@ -146,6 +146,7 @@ struct nouveau_drm {
 
 	/* TTM interface support */
 	struct {
+		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 		atomic_t validate_sequence;
 		int (*move)(struct nouveau_channel *,
diff --git a/drivers/gpu/drm/nouveau/nouveau_ttm.c b/drivers/gpu/drm/nouveau/nouveau_ttm.c
index 1543c2f8d3d3..69448b02649e 100644
--- a/drivers/gpu/drm/nouveau/nouveau_ttm.c
+++ b/drivers/gpu/drm/nouveau/nouveau_ttm.c
@@ -174,6 +174,38 @@ nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma)
 	return ttm_bo_mmap(filp, vma, &drm->ttm.bdev);
 }
 
+int
+nouveau_ttm_global_init(struct nouveau_drm *drm)
+{
+	struct drm_global_reference *global_ref;
+	int ret;
+
+	global_ref = &drm->ttm.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+
+	ret = drm_global_item_ref(global_ref);
+	if (unlikely(ret != 0)) {
+		DRM_ERROR("Failed setting up TTM BO subsystem\n");
+		drm->ttm.bo_global_ref.ref.release = NULL;
+		return ret;
+	}
+
+	return 0;
+}
+
+void
+nouveau_ttm_global_release(struct nouveau_drm *drm)
+{
+	if (drm->ttm.bo_global_ref.ref.release == NULL)
+		return;
+
+	drm_global_item_unref(&drm->ttm.bo_global_ref.ref);
+	drm->ttm.bo_global_ref.ref.release = NULL;
+}
+
 static int
 nouveau_ttm_init_host(struct nouveau_drm *drm, u8 kind)
 {
@@ -236,7 +268,12 @@ nouveau_ttm_init(struct nouveau_drm *drm)
 		drm->agp.cma = pci->agp.cma;
 	}
 
+	ret = nouveau_ttm_global_init(drm);
+	if (ret)
+		return ret;
+
 	ret = ttm_bo_device_init(&drm->ttm.bdev,
+				  drm->ttm.bo_global_ref.ref.object,
 				  &nouveau_bo_driver,
 				  dev->anon_inode->i_mapping,
 				  DRM_FILE_PAGE_OFFSET,
@@ -291,6 +328,8 @@ nouveau_ttm_fini(struct nouveau_drm *drm)
 
 	ttm_bo_device_release(&drm->ttm.bdev);
 
+	nouveau_ttm_global_release(drm);
+
 	arch_phys_wc_del(drm->ttm.mtrr);
 	drm->ttm.mtrr = 0;
 	arch_io_free_memtype_wc(device->func->resource_addr(device, 1),
diff --git a/drivers/gpu/drm/qxl/qxl_drv.h b/drivers/gpu/drm/qxl/qxl_drv.h
index 13a0254b59a1..ac9374028b7d 100644
--- a/drivers/gpu/drm/qxl/qxl_drv.h
+++ b/drivers/gpu/drm/qxl/qxl_drv.h
@@ -126,6 +126,8 @@ struct qxl_output {
 #define drm_encoder_to_qxl_output(x) container_of(x, struct qxl_output, enc)
 
 struct qxl_mman {
+	struct ttm_bo_global_ref        bo_global_ref;
+	bool				mem_global_referenced;
 	struct ttm_bo_device		bdev;
 };
 
diff --git a/drivers/gpu/drm/qxl/qxl_ttm.c b/drivers/gpu/drm/qxl/qxl_ttm.c
index 886f61e94f24..b7c4eb3bd179 100644
--- a/drivers/gpu/drm/qxl/qxl_ttm.c
+++ b/drivers/gpu/drm/qxl/qxl_ttm.c
@@ -46,6 +46,34 @@ static struct qxl_device *qxl_get_qdev(struct ttm_bo_device *bdev)
 	return qdev;
 }
 
+static int qxl_ttm_global_init(struct qxl_device *qdev)
+{
+	struct drm_global_reference *global_ref;
+	int r;
+
+	global_ref = &qdev->mman.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		return r;
+	}
+
+	qdev->mman.mem_global_referenced = true;
+	return 0;
+}
+
+static void qxl_ttm_global_fini(struct qxl_device *qdev)
+{
+	if (qdev->mman.mem_global_referenced) {
+		drm_global_item_unref(&qdev->mman.bo_global_ref.ref);
+		qdev->mman.mem_global_referenced = false;
+	}
+}
+
 static struct vm_operations_struct qxl_ttm_vm_ops;
 static const struct vm_operations_struct *ttm_vm_ops;
 
@@ -316,8 +344,12 @@ int qxl_ttm_init(struct qxl_device *qdev)
 	int r;
 	int num_io_pages; /* != rom->num_io_pages, we include surface0 */
 
+	r = qxl_ttm_global_init(qdev);
+	if (r)
+		return r;
 	/* No others user of address space so set it to 0 */
 	r = ttm_bo_device_init(&qdev->mman.bdev,
+			       qdev->mman.bo_global_ref.ref.object,
 			       &qxl_bo_driver,
 			       qdev->ddev.anon_inode->i_mapping,
 			       DRM_FILE_PAGE_OFFSET, 0);
@@ -353,6 +385,7 @@ void qxl_ttm_fini(struct qxl_device *qdev)
 	ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_VRAM);
 	ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_PRIV);
 	ttm_bo_device_release(&qdev->mman.bdev);
+	qxl_ttm_global_fini(qdev);
 	DRM_INFO("qxl: ttm finalized\n");
 }
 
diff --git a/drivers/gpu/drm/radeon/radeon.h b/drivers/gpu/drm/radeon/radeon.h
index 32808e50be12..06fb952b6290 100644
--- a/drivers/gpu/drm/radeon/radeon.h
+++ b/drivers/gpu/drm/radeon/radeon.h
@@ -448,7 +448,9 @@ struct radeon_surface_reg {
  * TTM.
  */
 struct radeon_mman {
+	struct ttm_bo_global_ref        bo_global_ref;
 	struct ttm_bo_device		bdev;
+	bool				mem_global_referenced;
 	bool				initialized;
 
 #if defined(CONFIG_DEBUG_FS)
diff --git a/drivers/gpu/drm/radeon/radeon_ttm.c b/drivers/gpu/drm/radeon/radeon_ttm.c
index 9920a6fc11bf..2104429ddbac 100644
--- a/drivers/gpu/drm/radeon/radeon_ttm.c
+++ b/drivers/gpu/drm/radeon/radeon_ttm.c
@@ -60,6 +60,39 @@ static struct radeon_device *radeon_get_rdev(struct ttm_bo_device *bdev)
 	return rdev;
 }
 
+
+/*
+ * Global memory.
+ */
+static int radeon_ttm_global_init(struct radeon_device *rdev)
+{
+	struct drm_global_reference *global_ref;
+	int r;
+
+	rdev->mman.mem_global_referenced = false;
+	global_ref = &rdev->mman.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		return r;
+	}
+
+	rdev->mman.mem_global_referenced = true;
+	return 0;
+}
+
+static void radeon_ttm_global_fini(struct radeon_device *rdev)
+{
+	if (rdev->mman.mem_global_referenced) {
+		drm_global_item_unref(&rdev->mman.bo_global_ref.ref);
+		rdev->mman.mem_global_referenced = false;
+	}
+}
+
 static int radeon_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
 {
 	return 0;
@@ -788,8 +821,13 @@ int radeon_ttm_init(struct radeon_device *rdev)
 {
 	int r;
 
+	r = radeon_ttm_global_init(rdev);
+	if (r) {
+		return r;
+	}
 	/* No others user of address space so set it to 0 */
 	r = ttm_bo_device_init(&rdev->mman.bdev,
+			       rdev->mman.bo_global_ref.ref.object,
 			       &radeon_bo_driver,
 			       rdev->ddev->anon_inode->i_mapping,
 			       DRM_FILE_PAGE_OFFSET,
@@ -861,6 +899,7 @@ void radeon_ttm_fini(struct radeon_device *rdev)
 	ttm_bo_clean_mm(&rdev->mman.bdev, TTM_PL_TT);
 	ttm_bo_device_release(&rdev->mman.bdev);
 	radeon_gart_fini(rdev);
+	radeon_ttm_global_fini(rdev);
 	rdev->mman.initialized = false;
 	DRM_INFO("radeon: ttm finalized\n");
 }
diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 0ec08394e17a..57c89161b5c6 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -1530,7 +1530,7 @@ static void ttm_bo_global_kobj_release(struct kobject *kobj)
 	__free_page(glob->dummy_read_page);
 }
 
-static void ttm_bo_global_release(void)
+void ttm_bo_global_release(void)
 {
 	struct ttm_bo_global *glob = &ttm_bo_glob;
 
@@ -1544,8 +1544,9 @@ static void ttm_bo_global_release(void)
 out:
 	mutex_unlock(&ttm_global_mutex);
 }
+EXPORT_SYMBOL(ttm_bo_global_release);
 
-static int ttm_bo_global_init(void)
+int ttm_bo_global_init(void)
 {
 	struct ttm_bo_global *glob = &ttm_bo_glob;
 	int ret = 0;
@@ -1582,6 +1583,8 @@ static int ttm_bo_global_init(void)
 	mutex_unlock(&ttm_global_mutex);
 	return ret;
 }
+EXPORT_SYMBOL(ttm_bo_global_init);
+
 
 int ttm_bo_device_release(struct ttm_bo_device *bdev)
 {
@@ -1620,25 +1623,18 @@ int ttm_bo_device_release(struct ttm_bo_device *bdev)
 
 	drm_vma_offset_manager_destroy(&bdev->vma_manager);
 
-	if (!ret)
-		ttm_bo_global_release();
-
 	return ret;
 }
 EXPORT_SYMBOL(ttm_bo_device_release);
 
 int ttm_bo_device_init(struct ttm_bo_device *bdev,
+		       struct ttm_bo_global *glob,
 		       struct ttm_bo_driver *driver,
 		       struct address_space *mapping,
 		       uint64_t file_page_offset,
 		       bool need_dma32)
 {
-	struct ttm_bo_global *glob = &ttm_bo_glob;
-	int ret;
-
-	ret = ttm_bo_global_init();
-	if (ret)
-		return ret;
+	int ret = -EINVAL;
 
 	bdev->driver = driver;
 
@@ -1665,7 +1661,6 @@ int ttm_bo_device_init(struct ttm_bo_device *bdev,
 
 	return 0;
 out_no_sys:
-	ttm_bo_global_release();
 	return ret;
 }
 EXPORT_SYMBOL(ttm_bo_device_init);
diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h
index 0c15000f926e..74628b5cf4e2 100644
--- a/drivers/gpu/drm/virtio/virtgpu_drv.h
+++ b/drivers/gpu/drm/virtio/virtgpu_drv.h
@@ -145,6 +145,8 @@ struct virtio_gpu_fbdev {
 };
 
 struct virtio_gpu_mman {
+	struct ttm_bo_global_ref        bo_global_ref;
+	bool				mem_global_referenced;
 	struct ttm_bo_device		bdev;
 };
 
diff --git a/drivers/gpu/drm/virtio/virtgpu_ttm.c b/drivers/gpu/drm/virtio/virtgpu_ttm.c
index 4bfbf25fabff..5fecf55ff1ce 100644
--- a/drivers/gpu/drm/virtio/virtgpu_ttm.c
+++ b/drivers/gpu/drm/virtio/virtgpu_ttm.c
@@ -50,6 +50,35 @@ virtio_gpu_device *virtio_gpu_get_vgdev(struct ttm_bo_device *bdev)
 	return vgdev;
 }
 
+static int virtio_gpu_ttm_global_init(struct virtio_gpu_device *vgdev)
+{
+	struct drm_global_reference *global_ref;
+	int r;
+
+	vgdev->mman.mem_global_referenced = false;
+	global_ref = &vgdev->mman.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		return r;
+	}
+
+	vgdev->mman.mem_global_referenced = true;
+	return 0;
+}
+
+static void virtio_gpu_ttm_global_fini(struct virtio_gpu_device *vgdev)
+{
+	if (vgdev->mman.mem_global_referenced) {
+		drm_global_item_unref(&vgdev->mman.bo_global_ref.ref);
+		vgdev->mman.mem_global_referenced = false;
+	}
+}
+
 int virtio_gpu_mmap(struct file *filp, struct vm_area_struct *vma)
 {
 	struct drm_file *file_priv;
@@ -326,8 +355,12 @@ int virtio_gpu_ttm_init(struct virtio_gpu_device *vgdev)
 {
 	int r;
 
+	r = virtio_gpu_ttm_global_init(vgdev);
+	if (r)
+		return r;
 	/* No others user of address space so set it to 0 */
 	r = ttm_bo_device_init(&vgdev->mman.bdev,
+			       vgdev->mman.bo_global_ref.ref.object,
 			       &virtio_gpu_bo_driver,
 			       vgdev->ddev->anon_inode->i_mapping,
 			       DRM_FILE_PAGE_OFFSET, 0);
@@ -346,11 +379,13 @@ int virtio_gpu_ttm_init(struct virtio_gpu_device *vgdev)
 err_mm_init:
 	ttm_bo_device_release(&vgdev->mman.bdev);
 err_dev_init:
+	virtio_gpu_ttm_global_fini(vgdev);
 	return r;
 }
 
 void virtio_gpu_ttm_fini(struct virtio_gpu_device *vgdev)
 {
 	ttm_bo_device_release(&vgdev->mman.bdev);
+	virtio_gpu_ttm_global_fini(vgdev);
 	DRM_INFO("virtio_gpu: ttm finalized\n");
 }
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
index 7ef5dcb06104..e24d9771e056 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
@@ -785,6 +785,11 @@ static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)
 	DRM_INFO("MMIO at 0x%08x size is %u kiB\n",
 		 dev_priv->mmio_start, dev_priv->mmio_size / 1024);
 
+	ret = vmw_ttm_global_init(dev_priv);
+	if (unlikely(ret != 0))
+		goto out_err0;
+
+
 	vmw_master_init(&dev_priv->fbdev_master);
 	ttm_lock_set_kill(&dev_priv->fbdev_master.lock, false, SIGTERM);
 	dev_priv->active_master = &dev_priv->fbdev_master;
@@ -795,7 +800,7 @@ static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)
 	if (unlikely(dev_priv->mmio_virt == NULL)) {
 		ret = -ENOMEM;
 		DRM_ERROR("Failed mapping MMIO.\n");
-		goto out_err0;
+		goto out_err3;
 	}
 
 	/* Need mmio memory to check for fifo pitchlock cap. */
@@ -849,6 +854,7 @@ static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)
 	}
 
 	ret = ttm_bo_device_init(&dev_priv->bdev,
+				 dev_priv->bo_global_ref.ref.object,
 				 &vmw_bo_driver,
 				 dev->anon_inode->i_mapping,
 				 VMWGFX_FILE_PAGE_OFFSET,
@@ -970,6 +976,8 @@ static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)
 	ttm_object_device_release(&dev_priv->tdev);
 out_err4:
 	memunmap(dev_priv->mmio_virt);
+out_err3:
+	vmw_ttm_global_release(dev_priv);
 out_err0:
 	for (i = vmw_res_context; i < vmw_res_max; ++i)
 		idr_destroy(&dev_priv->res_idr[i]);
@@ -1021,6 +1029,7 @@ static void vmw_driver_unload(struct drm_device *dev)
 	memunmap(dev_priv->mmio_virt);
 	if (dev_priv->ctx.staged_bindings)
 		vmw_binding_state_free(dev_priv->ctx.staged_bindings);
+	vmw_ttm_global_release(dev_priv);
 
 	for (i = vmw_res_context; i < vmw_res_max; ++i)
 		idr_destroy(&dev_priv->res_idr[i]);
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
index cd607ba9c2fe..7a3c64bd82b3 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
@@ -417,6 +417,7 @@ enum {
 
 struct vmw_private {
 	struct ttm_bo_device bdev;
+	struct ttm_bo_global_ref bo_global_ref;
 
 	struct vmw_fifo_state fifo;
 
@@ -832,6 +833,8 @@ extern int vmw_fifo_flush(struct vmw_private *dev_priv,
  * TTM glue - vmwgfx_ttm_glue.c
  */
 
+extern int vmw_ttm_global_init(struct vmw_private *dev_priv);
+extern void vmw_ttm_global_release(struct vmw_private *dev_priv);
 extern int vmw_mmap(struct file *filp, struct vm_area_struct *vma);
 
 extern void vmw_validation_mem_init_ttm(struct vmw_private *dev_priv,
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c
index e6d75e377dd8..8e3203766882 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c
@@ -78,3 +78,30 @@ void vmw_validation_mem_init_ttm(struct vmw_private *dev_priv, size_t gran)
 	vvm->unreserve_mem = vmw_vmt_unreserve;
 	vvm->gran = gran;
 }
+
+int vmw_ttm_global_init(struct vmw_private *dev_priv)
+{
+	struct drm_global_reference *global_ref;
+	int ret;
+
+	global_ref = &dev_priv->bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+	ret = drm_global_item_ref(global_ref);
+
+	if (unlikely(ret != 0)) {
+		DRM_ERROR("Failed setting up TTM buffer objects.\n");
+		goto out_no_bo;
+	}
+
+	return 0;
+out_no_bo:
+	return ret;
+}
+
+void vmw_ttm_global_release(struct vmw_private *dev_priv)
+{
+	drm_global_item_unref(&dev_priv->bo_global_ref.ref);
+}
diff --git a/drivers/staging/vboxvideo/vbox_drv.h b/drivers/staging/vboxvideo/vbox_drv.h
index aa40e5cc2861..24e54d5dda6b 100644
--- a/drivers/staging/vboxvideo/vbox_drv.h
+++ b/drivers/staging/vboxvideo/vbox_drv.h
@@ -79,6 +79,7 @@ struct vbox_private {
 	int fb_mtrr;
 
 	struct {
+		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 	} ttm;
 
diff --git a/drivers/staging/vboxvideo/vbox_ttm.c b/drivers/staging/vboxvideo/vbox_ttm.c
index 30f270027acf..ceacbc1d3ff7 100644
--- a/drivers/staging/vboxvideo/vbox_ttm.c
+++ b/drivers/staging/vboxvideo/vbox_ttm.c
@@ -16,6 +16,37 @@ static inline struct vbox_private *vbox_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct vbox_private, ttm.bdev);
 }
 
+/**
+ * Adds the vbox memory manager object/structures to the global memory manager.
+ */
+static int vbox_ttm_global_init(struct vbox_private *vbox)
+{
+	struct drm_global_reference *global_ref;
+	int ret;
+
+	global_ref = &vbox->ttm.bo_global_ref.ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_BO;
+	global_ref->size = sizeof(struct ttm_bo_global);
+	global_ref->init = &ttm_bo_global_ref_init;
+	global_ref->release = &ttm_bo_global_ref_release;
+
+	ret = drm_global_item_ref(global_ref);
+	if (ret) {
+		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+/**
+ * Removes the vbox memory manager object from the global memory manager.
+ */
+static void vbox_ttm_global_release(struct vbox_private *vbox)
+{
+	drm_global_item_unref(&vbox->ttm.bo_global_ref.ref);
+}
+
 static void vbox_bo_ttm_destroy(struct ttm_buffer_object *tbo)
 {
 	struct vbox_bo *bo;
@@ -153,13 +184,18 @@ int vbox_mm_init(struct vbox_private *vbox)
 	struct drm_device *dev = &vbox->ddev;
 	struct ttm_bo_device *bdev = &vbox->ttm.bdev;
 
+	ret = vbox_ttm_global_init(vbox);
+	if (ret)
+		return ret;
+
 	ret = ttm_bo_device_init(&vbox->ttm.bdev,
+				 vbox->ttm.bo_global_ref.ref.object,
 				 &vbox_bo_driver,
 				 dev->anon_inode->i_mapping,
 				 DRM_FILE_PAGE_OFFSET, true);
 	if (ret) {
 		DRM_ERROR("Error initialising bo driver; %d\n", ret);
-		return ret;
+		goto err_ttm_global_release;
 	}
 
 	ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
@@ -181,6 +217,8 @@ int vbox_mm_init(struct vbox_private *vbox)
 
 err_device_release:
 	ttm_bo_device_release(&vbox->ttm.bdev);
+err_ttm_global_release:
+	vbox_ttm_global_release(vbox);
 	return ret;
 }
 
@@ -194,6 +232,7 @@ void vbox_mm_fini(struct vbox_private *vbox)
 	arch_phys_wc_del(vbox->fb_mtrr);
 #endif
 	ttm_bo_device_release(&vbox->ttm.bdev);
+	vbox_ttm_global_release(vbox);
 }
 
 void vbox_ttm_placement(struct vbox_bo *bo, int domain)
diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
index 6fb589f64633..26be74939f10 100644
--- a/include/drm/ttm/ttm_bo_driver.h
+++ b/include/drm/ttm/ttm_bo_driver.h
@@ -569,6 +569,9 @@ void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem);
 void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
 			   struct ttm_mem_reg *mem);
 
+void ttm_bo_global_release(void);
+int ttm_bo_global_init(void);
+
 int ttm_bo_device_release(struct ttm_bo_device *bdev);
 
 /**
@@ -586,7 +589,7 @@ int ttm_bo_device_release(struct ttm_bo_device *bdev);
  * Returns:
  * !0: Failure.
  */
-int ttm_bo_device_init(struct ttm_bo_device *bdev,
+int ttm_bo_device_init(struct ttm_bo_device *bdev, struct ttm_bo_global *glob,
 		       struct ttm_bo_driver *driver,
 		       struct address_space *mapping,
 		       uint64_t file_page_offset, bool need_dma32);
@@ -885,4 +888,40 @@ pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
 
 extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
 
+/**
+ * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
+ */
+
+struct ttm_bo_global_ref {
+	struct drm_global_reference ref;
+};
+
+/**
+ * ttm_bo_global_ref_init
+ *
+ * @ref: DRM global reference
+ *
+ * Helper function that initializes a struct ttm_bo_global. This function
+ * is used as init call-back function for DRM global references of type
+ * DRM_GLOBAL_TTM_BO_REF.
+ */
+static inline int ttm_bo_global_ref_init(struct drm_global_reference *ref)
+{
+	return ttm_bo_global_init();
+}
+
+/**
+ * ttm_bo_global_ref_release
+ *
+ * @ref: DRM global reference
+ *
+ * Helper function that releases a struct ttm_bo_global. This function
+ * is used as release call-back function for DRM global references of type
+ * DRM_GLOBAL_TTM_BO_REF.
+ */
+static inline void ttm_bo_global_ref_release(struct drm_global_reference *ref)
+{
+	ttm_bo_global_release();
+}
+
 #endif
-- 
2.20.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 3/6] Revert "drm/ttm: Fix bo_global and mem_global kfree error"
  2019-04-16  0:35 [PATCH 0/6] Fix crash after reloading a driver using ttm Karol Herbst
  2019-04-16  0:35 ` [PATCH 1/6] Revert "drm: Remove drm_global.{c,h} v2" Karol Herbst
  2019-04-16  0:35 ` [PATCH 2/6] Revert "drm/ttm: initialize globals during device init (v2)" Karol Herbst
@ 2019-04-16  0:35 ` Karol Herbst
  2019-04-16  0:35 ` [PATCH 4/6] Revert "drm/ttm: use a static ttm_bo_global instance" Karol Herbst
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 18+ messages in thread
From: Karol Herbst @ 2019-04-16  0:35 UTC (permalink / raw)
  To: dri-devel; +Cc: Alex Deucher, Karol Herbst

This reverts commit 30f33126feca0fe16df9e9302ffc28a953e2eb37.

Signed-off-by: Karol Herbst <kherbst@redhat.com>
---
 drivers/gpu/drm/ttm/ttm_bo.c     | 1 +
 drivers/gpu/drm/ttm/ttm_memory.c | 9 +++++++++
 2 files changed, 10 insertions(+)

diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 57c89161b5c6..6941562ef2fa 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -1528,6 +1528,7 @@ static void ttm_bo_global_kobj_release(struct kobject *kobj)
 		container_of(kobj, struct ttm_bo_global, kobj);
 
 	__free_page(glob->dummy_read_page);
+	kfree(glob);
 }
 
 void ttm_bo_global_release(void)
diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c
index f1567c353b54..7704e17c402f 100644
--- a/drivers/gpu/drm/ttm/ttm_memory.c
+++ b/drivers/gpu/drm/ttm/ttm_memory.c
@@ -219,6 +219,14 @@ static ssize_t ttm_mem_global_store(struct kobject *kobj,
 	return size;
 }
 
+static void ttm_mem_global_kobj_release(struct kobject *kobj)
+{
+	struct ttm_mem_global *glob =
+		container_of(kobj, struct ttm_mem_global, kobj);
+
+	kfree(glob);
+}
+
 static struct attribute *ttm_mem_global_attrs[] = {
 	&ttm_mem_global_lower_mem_limit,
 	NULL
@@ -230,6 +238,7 @@ static const struct sysfs_ops ttm_mem_global_ops = {
 };
 
 static struct kobj_type ttm_mem_glob_kobj_type = {
+	.release = &ttm_mem_global_kobj_release,
 	.sysfs_ops = &ttm_mem_global_ops,
 	.default_attrs = ttm_mem_global_attrs,
 };
-- 
2.20.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 4/6] Revert "drm/ttm: use a static ttm_bo_global instance"
  2019-04-16  0:35 [PATCH 0/6] Fix crash after reloading a driver using ttm Karol Herbst
                   ` (2 preceding siblings ...)
  2019-04-16  0:35 ` [PATCH 3/6] Revert "drm/ttm: Fix bo_global and mem_global kfree error" Karol Herbst
@ 2019-04-16  0:35 ` Karol Herbst
  2019-04-16  0:35 ` [PATCH 5/6] Revert "drm/ttm: make the device list mutex static" Karol Herbst
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 18+ messages in thread
From: Karol Herbst @ 2019-04-16  0:35 UTC (permalink / raw)
  To: dri-devel; +Cc: Alex Deucher, Karol Herbst

This reverts commit 62b53b37e4b1500d4eb4624a44ad861cf8d3cd18.

Signed-off-by: Karol Herbst <kherbst@redhat.com>
---
 drivers/gpu/drm/ttm/ttm_bo.c    | 31 ++++++++-----------------------
 include/drm/ttm/ttm_bo_driver.h | 15 +++++++--------
 2 files changed, 15 insertions(+), 31 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 6941562ef2fa..c0efe4c89baf 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -49,9 +49,6 @@ static void ttm_bo_global_kobj_release(struct kobject *kobj);
  * ttm_global_mutex - protecting the global BO state
  */
 DEFINE_MUTEX(ttm_global_mutex);
-struct ttm_bo_global ttm_bo_glob = {
-	.use_count = 0
-};
 
 static struct attribute ttm_bo_count = {
 	.name = "bo_count",
@@ -1531,35 +1528,22 @@ static void ttm_bo_global_kobj_release(struct kobject *kobj)
 	kfree(glob);
 }
 
-void ttm_bo_global_release(void)
+void ttm_bo_global_release(struct ttm_bo_global *glob)
 {
-	struct ttm_bo_global *glob = &ttm_bo_glob;
-
-	mutex_lock(&ttm_global_mutex);
-	if (--glob->use_count > 0)
-		goto out;
-
 	kobject_del(&glob->kobj);
 	kobject_put(&glob->kobj);
 	ttm_mem_global_release(&ttm_mem_glob);
-out:
-	mutex_unlock(&ttm_global_mutex);
 }
 EXPORT_SYMBOL(ttm_bo_global_release);
 
-int ttm_bo_global_init(void)
+int ttm_bo_global_init(struct ttm_bo_global *glob)
 {
-	struct ttm_bo_global *glob = &ttm_bo_glob;
-	int ret = 0;
+	int ret;
 	unsigned i;
 
-	mutex_lock(&ttm_global_mutex);
-	if (++glob->use_count > 1)
-		goto out;
-
 	ret = ttm_mem_global_init(&ttm_mem_glob);
 	if (ret)
-		goto out;
+		return ret;
 
 	spin_lock_init(&glob->lru_lock);
 	glob->mem_glob = &ttm_mem_glob;
@@ -1568,7 +1552,7 @@ int ttm_bo_global_init(void)
 
 	if (unlikely(glob->dummy_read_page == NULL)) {
 		ret = -ENOMEM;
-		goto out;
+		goto out_no_drp;
 	}
 
 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
@@ -1580,8 +1564,9 @@ int ttm_bo_global_init(void)
 		&glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
 	if (unlikely(ret != 0))
 		kobject_put(&glob->kobj);
-out:
-	mutex_unlock(&ttm_global_mutex);
+	return ret;
+out_no_drp:
+	kfree(glob);
 	return ret;
 }
 EXPORT_SYMBOL(ttm_bo_global_init);
diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
index 26be74939f10..9cec8835b88f 100644
--- a/include/drm/ttm/ttm_bo_driver.h
+++ b/include/drm/ttm/ttm_bo_driver.h
@@ -398,7 +398,7 @@ struct ttm_bo_driver {
  * @swap_lru: Lru list of buffer objects used for swapping.
  */
 
-extern struct ttm_bo_global {
+struct ttm_bo_global {
 
 	/**
 	 * Constant after init.
@@ -410,9 +410,8 @@ extern struct ttm_bo_global {
 	spinlock_t lru_lock;
 
 	/**
-	 * Protected by ttm_global_mutex.
+	 * Protected by device_list_mutex.
 	 */
-	unsigned int use_count;
 	struct list_head device_list;
 
 	/**
@@ -424,7 +423,7 @@ extern struct ttm_bo_global {
 	 * Internal protection.
 	 */
 	atomic_t bo_count;
-} ttm_bo_glob;
+};
 
 
 #define TTM_NUM_MEM_TYPES 8
@@ -569,8 +568,8 @@ void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem);
 void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
 			   struct ttm_mem_reg *mem);
 
-void ttm_bo_global_release(void);
-int ttm_bo_global_init(void);
+void ttm_bo_global_release(struct ttm_bo_global *glob);
+int ttm_bo_global_init(struct ttm_bo_global *glob);
 
 int ttm_bo_device_release(struct ttm_bo_device *bdev);
 
@@ -907,7 +906,7 @@ struct ttm_bo_global_ref {
  */
 static inline int ttm_bo_global_ref_init(struct drm_global_reference *ref)
 {
-	return ttm_bo_global_init();
+	return ttm_bo_global_init(ref->object);
 }
 
 /**
@@ -921,7 +920,7 @@ static inline int ttm_bo_global_ref_init(struct drm_global_reference *ref)
  */
 static inline void ttm_bo_global_ref_release(struct drm_global_reference *ref)
 {
-	ttm_bo_global_release();
+	ttm_bo_global_release(ref->object);
 }
 
 #endif
-- 
2.20.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 5/6] Revert "drm/ttm: make the device list mutex static"
  2019-04-16  0:35 [PATCH 0/6] Fix crash after reloading a driver using ttm Karol Herbst
                   ` (3 preceding siblings ...)
  2019-04-16  0:35 ` [PATCH 4/6] Revert "drm/ttm: use a static ttm_bo_global instance" Karol Herbst
@ 2019-04-16  0:35 ` Karol Herbst
  2019-04-16  0:35 ` [PATCH 6/6] Revert "drm/ttm: use a static ttm_mem_global instance" Karol Herbst
  2019-04-16  6:38 ` [PATCH 0/6] Fix crash after reloading a driver using ttm Christian König
  6 siblings, 0 replies; 18+ messages in thread
From: Karol Herbst @ 2019-04-16  0:35 UTC (permalink / raw)
  To: dri-devel; +Cc: Alex Deucher, Karol Herbst

This reverts commit 56b3d20413587fab6a790cfc8bc075ca94bc8ed9.

Signed-off-by: Karol Herbst <kherbst@redhat.com>
---
 drivers/gpu/drm/ttm/ttm_bo.c    | 14 +++++---------
 include/drm/ttm/ttm_bo_driver.h |  1 +
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index c0efe4c89baf..99322c3d6573 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -45,11 +45,6 @@
 
 static void ttm_bo_global_kobj_release(struct kobject *kobj);
 
-/**
- * ttm_global_mutex - protecting the global BO state
- */
-DEFINE_MUTEX(ttm_global_mutex);
-
 static struct attribute ttm_bo_count = {
 	.name = "bo_count",
 	.mode = S_IRUGO
@@ -1545,6 +1540,7 @@ int ttm_bo_global_init(struct ttm_bo_global *glob)
 	if (ret)
 		return ret;
 
+	mutex_init(&glob->device_list_mutex);
 	spin_lock_init(&glob->lru_lock);
 	glob->mem_glob = &ttm_mem_glob;
 	glob->mem_glob->bo_glob = glob;
@@ -1592,9 +1588,9 @@ int ttm_bo_device_release(struct ttm_bo_device *bdev)
 		}
 	}
 
-	mutex_lock(&ttm_global_mutex);
+	mutex_lock(&glob->device_list_mutex);
 	list_del(&bdev->device_list);
-	mutex_unlock(&ttm_global_mutex);
+	mutex_unlock(&glob->device_list_mutex);
 
 	cancel_delayed_work_sync(&bdev->wq);
 
@@ -1641,9 +1637,9 @@ int ttm_bo_device_init(struct ttm_bo_device *bdev,
 	bdev->dev_mapping = mapping;
 	bdev->glob = glob;
 	bdev->need_dma32 = need_dma32;
-	mutex_lock(&ttm_global_mutex);
+	mutex_lock(&glob->device_list_mutex);
 	list_add_tail(&bdev->device_list, &glob->device_list);
-	mutex_unlock(&ttm_global_mutex);
+	mutex_unlock(&glob->device_list_mutex);
 
 	return 0;
 out_no_sys:
diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
index 9cec8835b88f..4ae6fc33f761 100644
--- a/include/drm/ttm/ttm_bo_driver.h
+++ b/include/drm/ttm/ttm_bo_driver.h
@@ -407,6 +407,7 @@ struct ttm_bo_global {
 	struct kobject kobj;
 	struct ttm_mem_global *mem_glob;
 	struct page *dummy_read_page;
+	struct mutex device_list_mutex;
 	spinlock_t lru_lock;
 
 	/**
-- 
2.20.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 6/6] Revert "drm/ttm: use a static ttm_mem_global instance"
  2019-04-16  0:35 [PATCH 0/6] Fix crash after reloading a driver using ttm Karol Herbst
                   ` (4 preceding siblings ...)
  2019-04-16  0:35 ` [PATCH 5/6] Revert "drm/ttm: make the device list mutex static" Karol Herbst
@ 2019-04-16  0:35 ` Karol Herbst
  2019-04-16  6:38 ` [PATCH 0/6] Fix crash after reloading a driver using ttm Christian König
  6 siblings, 0 replies; 18+ messages in thread
From: Karol Herbst @ 2019-04-16  0:35 UTC (permalink / raw)
  To: dri-devel; +Cc: Alex Deucher, Karol Herbst

This reverts commit 27eb1fa9130a98edd2b321d4dbce5c8b244ee7af.

Signed-off-by: Karol Herbst <kherbst@redhat.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 44 +++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  1 +
 drivers/gpu/drm/ast/ast_drv.h                 |  1 +
 drivers/gpu/drm/ast/ast_ttm.c                 | 32 +++++++++++++-
 drivers/gpu/drm/bochs/bochs.h                 |  1 +
 drivers/gpu/drm/bochs/bochs_mm.c              | 30 ++++++++++++-
 drivers/gpu/drm/cirrus/cirrus_drv.h           |  1 +
 drivers/gpu/drm/cirrus/cirrus_ttm.c           | 32 +++++++++++++-
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h   |  1 +
 drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c   | 31 +++++++++++--
 drivers/gpu/drm/mgag200/mgag200_drv.h         |  1 +
 drivers/gpu/drm/mgag200/mgag200_ttm.c         | 32 +++++++++++++-
 drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 +
 drivers/gpu/drm/nouveau/nouveau_ttm.c         | 34 ++++++++++++--
 drivers/gpu/drm/qxl/qxl_drv.h                 |  1 +
 drivers/gpu/drm/qxl/qxl_ttm.c                 | 28 ++++++++++++
 drivers/gpu/drm/radeon/radeon.h               |  1 +
 drivers/gpu/drm/radeon/radeon_ttm.c           | 26 +++++++++++
 drivers/gpu/drm/ttm/ttm_bo.c                  | 10 ++---
 drivers/gpu/drm/ttm/ttm_memory.c              |  5 +--
 drivers/gpu/drm/virtio/virtgpu_drv.h          |  1 +
 drivers/gpu/drm/virtio/virtgpu_ttm.c          | 27 ++++++++++++
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  4 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.h           |  3 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c      | 27 ++++++++++++
 drivers/staging/vboxvideo/vbox_drv.h          |  1 +
 drivers/staging/vboxvideo/vbox_ttm.c          | 24 ++++++++++
 include/drm/ttm/ttm_bo_driver.h               |  8 +++-
 include/drm/ttm/ttm_memory.h                  |  4 +-
 29 files changed, 380 insertions(+), 32 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index 213d29d0ad80..d76895c7b0ef 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -65,6 +65,33 @@ static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
  * Global memory.
  */
 
+/**
+ * amdgpu_ttm_mem_global_init - Initialize and acquire reference to
+ * memory object
+ *
+ * @ref: Object for initialization.
+ *
+ * This is called by drm_global_item_ref() when an object is being
+ * initialized.
+ */
+static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+/**
+ * amdgpu_ttm_mem_global_release - Drop reference to a memory object
+ *
+ * @ref: Object being removed
+ *
+ * This is called by drm_global_item_unref() when an object is being
+ * released.
+ */
+static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 /**
  * amdgpu_ttm_global_init - Initialize global TTM memory reference structures.
  *
@@ -81,6 +108,20 @@ static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
 	/* ensure reference is false in case init fails */
 	adev->mman.mem_global_referenced = false;
 
+	global_ref = &adev->mman.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &amdgpu_ttm_mem_global_init;
+	global_ref->release = &amdgpu_ttm_mem_global_release;
+	r = drm_global_item_ref(global_ref);
+	if (r) {
+		DRM_ERROR("Failed setting up TTM memory accounting "
+			  "subsystem.\n");
+		goto error_mem;
+	}
+
+	adev->mman.bo_global_ref.mem_glob =
+		adev->mman.mem_global_ref.object;
 	global_ref = &adev->mman.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -99,6 +140,8 @@ static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
 	return 0;
 
 error_bo:
+	drm_global_item_unref(&adev->mman.mem_global_ref);
+error_mem:
 	return r;
 }
 
@@ -107,6 +150,7 @@ static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
 	if (adev->mman.mem_global_referenced) {
 		mutex_destroy(&adev->mman.gtt_window_lock);
 		drm_global_item_unref(&adev->mman.bo_global_ref.ref);
+		drm_global_item_unref(&adev->mman.mem_global_ref);
 		adev->mman.mem_global_referenced = false;
 	}
 }
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
index e114f209b701..fe8f276e9811 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
@@ -40,6 +40,7 @@
 
 struct amdgpu_mman {
 	struct ttm_bo_global_ref        bo_global_ref;
+	struct drm_global_reference	mem_global_ref;
 	struct ttm_bo_device		bdev;
 	bool				mem_global_referenced;
 	bool				initialized;
diff --git a/drivers/gpu/drm/ast/ast_drv.h b/drivers/gpu/drm/ast/ast_drv.h
index 6ae11a477643..e6c4cd3dc50e 100644
--- a/drivers/gpu/drm/ast/ast_drv.h
+++ b/drivers/gpu/drm/ast/ast_drv.h
@@ -104,6 +104,7 @@ struct ast_private {
 	int fb_mtrr;
 
 	struct {
+		struct drm_global_reference mem_global_ref;
 		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 	} ttm;
diff --git a/drivers/gpu/drm/ast/ast_ttm.c b/drivers/gpu/drm/ast/ast_ttm.c
index 8a59d6fc1160..d21fbd26785a 100644
--- a/drivers/gpu/drm/ast/ast_ttm.c
+++ b/drivers/gpu/drm/ast/ast_ttm.c
@@ -36,11 +36,37 @@ ast_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct ast_private, ttm.bdev);
 }
 
+static int
+ast_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void
+ast_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 static int ast_ttm_global_init(struct ast_private *ast)
 {
 	struct drm_global_reference *global_ref;
 	int r;
 
+	global_ref = &ast->ttm.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &ast_ttm_mem_global_init;
+	global_ref->release = &ast_ttm_mem_global_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM memory accounting "
+			  "subsystem.\n");
+		return r;
+	}
+
+	ast->ttm.bo_global_ref.mem_glob =
+		ast->ttm.mem_global_ref.object;
 	global_ref = &ast->ttm.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -49,6 +75,7 @@ static int ast_ttm_global_init(struct ast_private *ast)
 	r = drm_global_item_ref(global_ref);
 	if (r != 0) {
 		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		drm_global_item_unref(&ast->ttm.mem_global_ref);
 		return r;
 	}
 	return 0;
@@ -57,11 +84,12 @@ static int ast_ttm_global_init(struct ast_private *ast)
 static void
 ast_ttm_global_release(struct ast_private *ast)
 {
-	if (ast->ttm.bo_global_ref.ref.release == NULL)
+	if (ast->ttm.mem_global_ref.release == NULL)
 		return;
 
 	drm_global_item_unref(&ast->ttm.bo_global_ref.ref);
-	ast->ttm.bo_global_ref.ref.release = NULL;
+	drm_global_item_unref(&ast->ttm.mem_global_ref);
+	ast->ttm.mem_global_ref.release = NULL;
 }
 
 
diff --git a/drivers/gpu/drm/bochs/bochs.h b/drivers/gpu/drm/bochs/bochs.h
index c1d88ea6b419..577a8b917cb9 100644
--- a/drivers/gpu/drm/bochs/bochs.h
+++ b/drivers/gpu/drm/bochs/bochs.h
@@ -77,6 +77,7 @@ struct bochs_device {
 
 	/* ttm */
 	struct {
+		struct drm_global_reference mem_global_ref;
 		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 		bool initialized;
diff --git a/drivers/gpu/drm/bochs/bochs_mm.c b/drivers/gpu/drm/bochs/bochs_mm.c
index cad65774a973..ff4f41dec228 100644
--- a/drivers/gpu/drm/bochs/bochs_mm.c
+++ b/drivers/gpu/drm/bochs/bochs_mm.c
@@ -16,11 +16,35 @@ static inline struct bochs_device *bochs_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct bochs_device, ttm.bdev);
 }
 
+static int bochs_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void bochs_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 static int bochs_ttm_global_init(struct bochs_device *bochs)
 {
 	struct drm_global_reference *global_ref;
 	int r;
 
+	global_ref = &bochs->ttm.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &bochs_ttm_mem_global_init;
+	global_ref->release = &bochs_ttm_mem_global_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM memory accounting "
+			  "subsystem.\n");
+		return r;
+	}
+
+	bochs->ttm.bo_global_ref.mem_glob =
+		bochs->ttm.mem_global_ref.object;
 	global_ref = &bochs->ttm.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -29,6 +53,7 @@ static int bochs_ttm_global_init(struct bochs_device *bochs)
 	r = drm_global_item_ref(global_ref);
 	if (r != 0) {
 		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		drm_global_item_unref(&bochs->ttm.mem_global_ref);
 		return r;
 	}
 
@@ -37,11 +62,12 @@ static int bochs_ttm_global_init(struct bochs_device *bochs)
 
 static void bochs_ttm_global_release(struct bochs_device *bochs)
 {
-	if (bochs->ttm.bo_global_ref.ref.release == NULL)
+	if (bochs->ttm.mem_global_ref.release == NULL)
 		return;
 
 	drm_global_item_unref(&bochs->ttm.bo_global_ref.ref);
-	bochs->ttm.bo_global_ref.ref.release = NULL;
+	drm_global_item_unref(&bochs->ttm.mem_global_ref);
+	bochs->ttm.mem_global_ref.release = NULL;
 }
 
 
diff --git a/drivers/gpu/drm/cirrus/cirrus_drv.h b/drivers/gpu/drm/cirrus/cirrus_drv.h
index 01852fbda9da..a29f87e98d9d 100644
--- a/drivers/gpu/drm/cirrus/cirrus_drv.h
+++ b/drivers/gpu/drm/cirrus/cirrus_drv.h
@@ -136,6 +136,7 @@ struct cirrus_device {
 	int fb_mtrr;
 
 	struct {
+		struct drm_global_reference mem_global_ref;
 		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 	} ttm;
diff --git a/drivers/gpu/drm/cirrus/cirrus_ttm.c b/drivers/gpu/drm/cirrus/cirrus_ttm.c
index 7801c56c3c9b..2e2141f26c5b 100644
--- a/drivers/gpu/drm/cirrus/cirrus_ttm.c
+++ b/drivers/gpu/drm/cirrus/cirrus_ttm.c
@@ -36,11 +36,37 @@ cirrus_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct cirrus_device, ttm.bdev);
 }
 
+static int
+cirrus_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void
+cirrus_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 static int cirrus_ttm_global_init(struct cirrus_device *cirrus)
 {
 	struct drm_global_reference *global_ref;
 	int r;
 
+	global_ref = &cirrus->ttm.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &cirrus_ttm_mem_global_init;
+	global_ref->release = &cirrus_ttm_mem_global_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM memory accounting "
+			  "subsystem.\n");
+		return r;
+	}
+
+	cirrus->ttm.bo_global_ref.mem_glob =
+		cirrus->ttm.mem_global_ref.object;
 	global_ref = &cirrus->ttm.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -49,6 +75,7 @@ static int cirrus_ttm_global_init(struct cirrus_device *cirrus)
 	r = drm_global_item_ref(global_ref);
 	if (r != 0) {
 		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		drm_global_item_unref(&cirrus->ttm.mem_global_ref);
 		return r;
 	}
 	return 0;
@@ -57,11 +84,12 @@ static int cirrus_ttm_global_init(struct cirrus_device *cirrus)
 static void
 cirrus_ttm_global_release(struct cirrus_device *cirrus)
 {
-	if (cirrus->ttm.bo_global_ref.ref.release == NULL)
+	if (cirrus->ttm.mem_global_ref.release == NULL)
 		return;
 
 	drm_global_item_unref(&cirrus->ttm.bo_global_ref.ref);
-	cirrus->ttm.bo_global_ref.ref.release = NULL;
+	drm_global_item_unref(&cirrus->ttm.mem_global_ref);
+	cirrus->ttm.mem_global_ref.release = NULL;
 }
 
 
diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h
index 60479502e277..45c25a488f42 100644
--- a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h
+++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h
@@ -49,6 +49,7 @@ struct hibmc_drm_private {
 	bool mode_config_initialized;
 
 	/* ttm */
+	struct drm_global_reference mem_global_ref;
 	struct ttm_bo_global_ref bo_global_ref;
 	struct ttm_bo_device bdev;
 	bool initialized;
diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c b/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c
index 14071c849121..0454aa43ffc6 100644
--- a/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c
+++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c
@@ -29,10 +29,34 @@ hibmc_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct hibmc_drm_private, bdev);
 }
 
+static int
+hibmc_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void
+hibmc_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 static int hibmc_ttm_global_init(struct hibmc_drm_private *hibmc)
 {
 	int ret;
 
+	hibmc->mem_global_ref.global_type = DRM_GLOBAL_TTM_MEM;
+	hibmc->mem_global_ref.size = sizeof(struct ttm_mem_global);
+	hibmc->mem_global_ref.init = &hibmc_ttm_mem_global_init;
+	hibmc->mem_global_ref.release = &hibmc_ttm_mem_global_release;
+	ret = drm_global_item_ref(&hibmc->mem_global_ref);
+	if (ret) {
+		DRM_ERROR("could not get ref on ttm global: %d\n", ret);
+		return ret;
+	}
+
+	hibmc->bo_global_ref.mem_glob =
+		hibmc->mem_global_ref.object;
 	hibmc->bo_global_ref.ref.global_type = DRM_GLOBAL_TTM_BO;
 	hibmc->bo_global_ref.ref.size = sizeof(struct ttm_bo_global);
 	hibmc->bo_global_ref.ref.init = &ttm_bo_global_ref_init;
@@ -40,6 +64,7 @@ static int hibmc_ttm_global_init(struct hibmc_drm_private *hibmc)
 	ret = drm_global_item_ref(&hibmc->bo_global_ref.ref);
 	if (ret) {
 		DRM_ERROR("failed setting up TTM BO subsystem: %d\n", ret);
+		drm_global_item_unref(&hibmc->mem_global_ref);
 		return ret;
 	}
 	return 0;
@@ -48,11 +73,9 @@ static int hibmc_ttm_global_init(struct hibmc_drm_private *hibmc)
 static void
 hibmc_ttm_global_release(struct hibmc_drm_private *hibmc)
 {
-	if (hibmc->bo_global_ref.ref.release == NULL)
-		return;
-
 	drm_global_item_unref(&hibmc->bo_global_ref.ref);
-	hibmc->bo_global_ref.ref.release = NULL;
+	drm_global_item_unref(&hibmc->mem_global_ref);
+	hibmc->mem_global_ref.release = NULL;
 }
 
 static void hibmc_bo_ttm_destroy(struct ttm_buffer_object *tbo)
diff --git a/drivers/gpu/drm/mgag200/mgag200_drv.h b/drivers/gpu/drm/mgag200/mgag200_drv.h
index e5348955a3cc..04f1dfba12e5 100644
--- a/drivers/gpu/drm/mgag200/mgag200_drv.h
+++ b/drivers/gpu/drm/mgag200/mgag200_drv.h
@@ -212,6 +212,7 @@ struct mga_device {
 	int fb_mtrr;
 
 	struct {
+		struct drm_global_reference mem_global_ref;
 		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 	} ttm;
diff --git a/drivers/gpu/drm/mgag200/mgag200_ttm.c b/drivers/gpu/drm/mgag200/mgag200_ttm.c
index 11bdc8121a7d..3444b539e7f4 100644
--- a/drivers/gpu/drm/mgag200/mgag200_ttm.c
+++ b/drivers/gpu/drm/mgag200/mgag200_ttm.c
@@ -36,11 +36,37 @@ mgag200_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct mga_device, ttm.bdev);
 }
 
+static int
+mgag200_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void
+mgag200_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 static int mgag200_ttm_global_init(struct mga_device *ast)
 {
 	struct drm_global_reference *global_ref;
 	int r;
 
+	global_ref = &ast->ttm.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &mgag200_ttm_mem_global_init;
+	global_ref->release = &mgag200_ttm_mem_global_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM memory accounting "
+			  "subsystem.\n");
+		return r;
+	}
+
+	ast->ttm.bo_global_ref.mem_glob =
+		ast->ttm.mem_global_ref.object;
 	global_ref = &ast->ttm.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -49,6 +75,7 @@ static int mgag200_ttm_global_init(struct mga_device *ast)
 	r = drm_global_item_ref(global_ref);
 	if (r != 0) {
 		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		drm_global_item_unref(&ast->ttm.mem_global_ref);
 		return r;
 	}
 	return 0;
@@ -57,11 +84,12 @@ static int mgag200_ttm_global_init(struct mga_device *ast)
 static void
 mgag200_ttm_global_release(struct mga_device *ast)
 {
-	if (ast->ttm.bo_global_ref.ref.release == NULL)
+	if (ast->ttm.mem_global_ref.release == NULL)
 		return;
 
 	drm_global_item_unref(&ast->ttm.bo_global_ref.ref);
-	ast->ttm.bo_global_ref.ref.release = NULL;
+	drm_global_item_unref(&ast->ttm.mem_global_ref);
+	ast->ttm.mem_global_ref.release = NULL;
 }
 
 
diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/nouveau_drv.h
index 0f3bb1a11fc9..0b2191fa96f7 100644
--- a/drivers/gpu/drm/nouveau/nouveau_drv.h
+++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
@@ -146,6 +146,7 @@ struct nouveau_drm {
 
 	/* TTM interface support */
 	struct {
+		struct drm_global_reference mem_global_ref;
 		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 		atomic_t validate_sequence;
diff --git a/drivers/gpu/drm/nouveau/nouveau_ttm.c b/drivers/gpu/drm/nouveau/nouveau_ttm.c
index 69448b02649e..a293383c8654 100644
--- a/drivers/gpu/drm/nouveau/nouveau_ttm.c
+++ b/drivers/gpu/drm/nouveau/nouveau_ttm.c
@@ -174,12 +174,38 @@ nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma)
 	return ttm_bo_mmap(filp, vma, &drm->ttm.bdev);
 }
 
+static int
+nouveau_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void
+nouveau_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 int
 nouveau_ttm_global_init(struct nouveau_drm *drm)
 {
 	struct drm_global_reference *global_ref;
 	int ret;
 
+	global_ref = &drm->ttm.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &nouveau_ttm_mem_global_init;
+	global_ref->release = &nouveau_ttm_mem_global_release;
+
+	ret = drm_global_item_ref(global_ref);
+	if (unlikely(ret != 0)) {
+		DRM_ERROR("Failed setting up TTM memory accounting\n");
+		drm->ttm.mem_global_ref.release = NULL;
+		return ret;
+	}
+
+	drm->ttm.bo_global_ref.mem_glob = global_ref->object;
 	global_ref = &drm->ttm.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -189,7 +215,8 @@ nouveau_ttm_global_init(struct nouveau_drm *drm)
 	ret = drm_global_item_ref(global_ref);
 	if (unlikely(ret != 0)) {
 		DRM_ERROR("Failed setting up TTM BO subsystem\n");
-		drm->ttm.bo_global_ref.ref.release = NULL;
+		drm_global_item_unref(&drm->ttm.mem_global_ref);
+		drm->ttm.mem_global_ref.release = NULL;
 		return ret;
 	}
 
@@ -199,11 +226,12 @@ nouveau_ttm_global_init(struct nouveau_drm *drm)
 void
 nouveau_ttm_global_release(struct nouveau_drm *drm)
 {
-	if (drm->ttm.bo_global_ref.ref.release == NULL)
+	if (drm->ttm.mem_global_ref.release == NULL)
 		return;
 
 	drm_global_item_unref(&drm->ttm.bo_global_ref.ref);
-	drm->ttm.bo_global_ref.ref.release = NULL;
+	drm_global_item_unref(&drm->ttm.mem_global_ref);
+	drm->ttm.mem_global_ref.release = NULL;
 }
 
 static int
diff --git a/drivers/gpu/drm/qxl/qxl_drv.h b/drivers/gpu/drm/qxl/qxl_drv.h
index ac9374028b7d..65be4e94a998 100644
--- a/drivers/gpu/drm/qxl/qxl_drv.h
+++ b/drivers/gpu/drm/qxl/qxl_drv.h
@@ -127,6 +127,7 @@ struct qxl_output {
 
 struct qxl_mman {
 	struct ttm_bo_global_ref        bo_global_ref;
+	struct drm_global_reference	mem_global_ref;
 	bool				mem_global_referenced;
 	struct ttm_bo_device		bdev;
 };
diff --git a/drivers/gpu/drm/qxl/qxl_ttm.c b/drivers/gpu/drm/qxl/qxl_ttm.c
index b7c4eb3bd179..a86c7461cfa2 100644
--- a/drivers/gpu/drm/qxl/qxl_ttm.c
+++ b/drivers/gpu/drm/qxl/qxl_ttm.c
@@ -46,11 +46,37 @@ static struct qxl_device *qxl_get_qdev(struct ttm_bo_device *bdev)
 	return qdev;
 }
 
+static int qxl_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void qxl_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 static int qxl_ttm_global_init(struct qxl_device *qdev)
 {
 	struct drm_global_reference *global_ref;
 	int r;
 
+	qdev->mman.mem_global_referenced = false;
+	global_ref = &qdev->mman.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &qxl_ttm_mem_global_init;
+	global_ref->release = &qxl_ttm_mem_global_release;
+
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM memory accounting "
+			  "subsystem.\n");
+		return r;
+	}
+
+	qdev->mman.bo_global_ref.mem_glob =
+		qdev->mman.mem_global_ref.object;
 	global_ref = &qdev->mman.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -59,6 +85,7 @@ static int qxl_ttm_global_init(struct qxl_device *qdev)
 	r = drm_global_item_ref(global_ref);
 	if (r != 0) {
 		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		drm_global_item_unref(&qdev->mman.mem_global_ref);
 		return r;
 	}
 
@@ -70,6 +97,7 @@ static void qxl_ttm_global_fini(struct qxl_device *qdev)
 {
 	if (qdev->mman.mem_global_referenced) {
 		drm_global_item_unref(&qdev->mman.bo_global_ref.ref);
+		drm_global_item_unref(&qdev->mman.mem_global_ref);
 		qdev->mman.mem_global_referenced = false;
 	}
 }
diff --git a/drivers/gpu/drm/radeon/radeon.h b/drivers/gpu/drm/radeon/radeon.h
index 06fb952b6290..1a6f6edb3515 100644
--- a/drivers/gpu/drm/radeon/radeon.h
+++ b/drivers/gpu/drm/radeon/radeon.h
@@ -449,6 +449,7 @@ struct radeon_surface_reg {
  */
 struct radeon_mman {
 	struct ttm_bo_global_ref        bo_global_ref;
+	struct drm_global_reference	mem_global_ref;
 	struct ttm_bo_device		bdev;
 	bool				mem_global_referenced;
 	bool				initialized;
diff --git a/drivers/gpu/drm/radeon/radeon_ttm.c b/drivers/gpu/drm/radeon/radeon_ttm.c
index 2104429ddbac..dac4ec5a120b 100644
--- a/drivers/gpu/drm/radeon/radeon_ttm.c
+++ b/drivers/gpu/drm/radeon/radeon_ttm.c
@@ -64,12 +64,36 @@ static struct radeon_device *radeon_get_rdev(struct ttm_bo_device *bdev)
 /*
  * Global memory.
  */
+static int radeon_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void radeon_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 static int radeon_ttm_global_init(struct radeon_device *rdev)
 {
 	struct drm_global_reference *global_ref;
 	int r;
 
 	rdev->mman.mem_global_referenced = false;
+	global_ref = &rdev->mman.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &radeon_ttm_mem_global_init;
+	global_ref->release = &radeon_ttm_mem_global_release;
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM memory accounting "
+			  "subsystem.\n");
+		return r;
+	}
+
+	rdev->mman.bo_global_ref.mem_glob =
+		rdev->mman.mem_global_ref.object;
 	global_ref = &rdev->mman.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -78,6 +102,7 @@ static int radeon_ttm_global_init(struct radeon_device *rdev)
 	r = drm_global_item_ref(global_ref);
 	if (r != 0) {
 		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		drm_global_item_unref(&rdev->mman.mem_global_ref);
 		return r;
 	}
 
@@ -89,6 +114,7 @@ static void radeon_ttm_global_fini(struct radeon_device *rdev)
 {
 	if (rdev->mman.mem_global_referenced) {
 		drm_global_item_unref(&rdev->mman.bo_global_ref.ref);
+		drm_global_item_unref(&rdev->mman.mem_global_ref);
 		rdev->mman.mem_global_referenced = false;
 	}
 }
diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 99322c3d6573..48bd611027b6 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -1527,22 +1527,18 @@ void ttm_bo_global_release(struct ttm_bo_global *glob)
 {
 	kobject_del(&glob->kobj);
 	kobject_put(&glob->kobj);
-	ttm_mem_global_release(&ttm_mem_glob);
 }
 EXPORT_SYMBOL(ttm_bo_global_release);
 
-int ttm_bo_global_init(struct ttm_bo_global *glob)
+int ttm_bo_global_init(struct ttm_bo_global *glob,
+		       struct ttm_mem_global *mem_glob)
 {
 	int ret;
 	unsigned i;
 
-	ret = ttm_mem_global_init(&ttm_mem_glob);
-	if (ret)
-		return ret;
-
 	mutex_init(&glob->device_list_mutex);
 	spin_lock_init(&glob->lru_lock);
-	glob->mem_glob = &ttm_mem_glob;
+	glob->mem_glob = mem_glob;
 	glob->mem_glob->bo_glob = glob;
 	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
 
diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c
index 7704e17c402f..450387c92b63 100644
--- a/drivers/gpu/drm/ttm/ttm_memory.c
+++ b/drivers/gpu/drm/ttm/ttm_memory.c
@@ -41,9 +41,6 @@
 
 #define TTM_MEMORY_ALLOC_RETRIES 4
 
-struct ttm_mem_global ttm_mem_glob;
-EXPORT_SYMBOL(ttm_mem_glob);
-
 struct ttm_mem_zone {
 	struct kobject kobj;
 	struct ttm_mem_global *glob;
@@ -467,6 +464,7 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
 	ttm_mem_global_release(glob);
 	return ret;
 }
+EXPORT_SYMBOL(ttm_mem_global_init);
 
 void ttm_mem_global_release(struct ttm_mem_global *glob)
 {
@@ -488,6 +486,7 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
 	kobject_del(&glob->kobj);
 	kobject_put(&glob->kobj);
 }
+EXPORT_SYMBOL(ttm_mem_global_release);
 
 static void ttm_check_swapping(struct ttm_mem_global *glob)
 {
diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h
index 74628b5cf4e2..5465d87f6629 100644
--- a/drivers/gpu/drm/virtio/virtgpu_drv.h
+++ b/drivers/gpu/drm/virtio/virtgpu_drv.h
@@ -146,6 +146,7 @@ struct virtio_gpu_fbdev {
 
 struct virtio_gpu_mman {
 	struct ttm_bo_global_ref        bo_global_ref;
+	struct drm_global_reference	mem_global_ref;
 	bool				mem_global_referenced;
 	struct ttm_bo_device		bdev;
 };
diff --git a/drivers/gpu/drm/virtio/virtgpu_ttm.c b/drivers/gpu/drm/virtio/virtgpu_ttm.c
index 5fecf55ff1ce..a3e02335da18 100644
--- a/drivers/gpu/drm/virtio/virtgpu_ttm.c
+++ b/drivers/gpu/drm/virtio/virtgpu_ttm.c
@@ -50,12 +50,37 @@ virtio_gpu_device *virtio_gpu_get_vgdev(struct ttm_bo_device *bdev)
 	return vgdev;
 }
 
+static int virtio_gpu_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void virtio_gpu_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 static int virtio_gpu_ttm_global_init(struct virtio_gpu_device *vgdev)
 {
 	struct drm_global_reference *global_ref;
 	int r;
 
 	vgdev->mman.mem_global_referenced = false;
+	global_ref = &vgdev->mman.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &virtio_gpu_ttm_mem_global_init;
+	global_ref->release = &virtio_gpu_ttm_mem_global_release;
+
+	r = drm_global_item_ref(global_ref);
+	if (r != 0) {
+		DRM_ERROR("Failed setting up TTM memory accounting "
+			  "subsystem.\n");
+		return r;
+	}
+
+	vgdev->mman.bo_global_ref.mem_glob =
+		vgdev->mman.mem_global_ref.object;
 	global_ref = &vgdev->mman.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -64,6 +89,7 @@ static int virtio_gpu_ttm_global_init(struct virtio_gpu_device *vgdev)
 	r = drm_global_item_ref(global_ref);
 	if (r != 0) {
 		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		drm_global_item_unref(&vgdev->mman.mem_global_ref);
 		return r;
 	}
 
@@ -75,6 +101,7 @@ static void virtio_gpu_ttm_global_fini(struct virtio_gpu_device *vgdev)
 {
 	if (vgdev->mman.mem_global_referenced) {
 		drm_global_item_unref(&vgdev->mman.bo_global_ref.ref);
+		drm_global_item_unref(&vgdev->mman.mem_global_ref);
 		vgdev->mman.mem_global_referenced = false;
 	}
 }
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
index e24d9771e056..e2d33761c99f 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
@@ -812,8 +812,8 @@ static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)
 		goto out_err4;
 	}
 
-	dev_priv->tdev = ttm_object_device_init(&ttm_mem_glob, 12,
-						&vmw_prime_dmabuf_ops);
+	dev_priv->tdev = ttm_object_device_init
+		(dev_priv->mem_global_ref.object, 12, &vmw_prime_dmabuf_ops);
 
 	if (unlikely(dev_priv->tdev == NULL)) {
 		DRM_ERROR("Unable to initialize TTM object management.\n");
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
index 7a3c64bd82b3..8d9cd8ff8deb 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
@@ -418,6 +418,7 @@ enum {
 struct vmw_private {
 	struct ttm_bo_device bdev;
 	struct ttm_bo_global_ref bo_global_ref;
+	struct drm_global_reference mem_global_ref;
 
 	struct vmw_fifo_state fifo;
 
@@ -1356,7 +1357,7 @@ vmw_bo_reference(struct vmw_buffer_object *buf)
 
 static inline struct ttm_mem_global *vmw_mem_glob(struct vmw_private *dev_priv)
 {
-	return &ttm_mem_glob;
+	return (struct ttm_mem_global *) dev_priv->mem_global_ref.object;
 }
 
 static inline void vmw_fifo_resource_inc(struct vmw_private *dev_priv)
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c
index 8e3203766882..cb0a12cc4090 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c
@@ -79,11 +79,36 @@ void vmw_validation_mem_init_ttm(struct vmw_private *dev_priv, size_t gran)
 	vvm->gran = gran;
 }
 
+static int vmw_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	DRM_INFO("global init.\n");
+	return ttm_mem_global_init(ref->object);
+}
+
+static void vmw_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 int vmw_ttm_global_init(struct vmw_private *dev_priv)
 {
 	struct drm_global_reference *global_ref;
 	int ret;
 
+	global_ref = &dev_priv->mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &vmw_ttm_mem_global_init;
+	global_ref->release = &vmw_ttm_mem_global_release;
+
+	ret = drm_global_item_ref(global_ref);
+	if (unlikely(ret != 0)) {
+		DRM_ERROR("Failed setting up TTM memory accounting.\n");
+		return ret;
+	}
+
+	dev_priv->bo_global_ref.mem_glob =
+		dev_priv->mem_global_ref.object;
 	global_ref = &dev_priv->bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -98,10 +123,12 @@ int vmw_ttm_global_init(struct vmw_private *dev_priv)
 
 	return 0;
 out_no_bo:
+	drm_global_item_unref(&dev_priv->mem_global_ref);
 	return ret;
 }
 
 void vmw_ttm_global_release(struct vmw_private *dev_priv)
 {
 	drm_global_item_unref(&dev_priv->bo_global_ref.ref);
+	drm_global_item_unref(&dev_priv->mem_global_ref);
 }
diff --git a/drivers/staging/vboxvideo/vbox_drv.h b/drivers/staging/vboxvideo/vbox_drv.h
index 24e54d5dda6b..e2a6b2b5bfc3 100644
--- a/drivers/staging/vboxvideo/vbox_drv.h
+++ b/drivers/staging/vboxvideo/vbox_drv.h
@@ -79,6 +79,7 @@ struct vbox_private {
 	int fb_mtrr;
 
 	struct {
+		struct drm_global_reference mem_global_ref;
 		struct ttm_bo_global_ref bo_global_ref;
 		struct ttm_bo_device bdev;
 	} ttm;
diff --git a/drivers/staging/vboxvideo/vbox_ttm.c b/drivers/staging/vboxvideo/vbox_ttm.c
index ceacbc1d3ff7..958c928c0d4b 100644
--- a/drivers/staging/vboxvideo/vbox_ttm.c
+++ b/drivers/staging/vboxvideo/vbox_ttm.c
@@ -16,6 +16,16 @@ static inline struct vbox_private *vbox_bdev(struct ttm_bo_device *bd)
 	return container_of(bd, struct vbox_private, ttm.bdev);
 }
 
+static int vbox_ttm_mem_global_init(struct drm_global_reference *ref)
+{
+	return ttm_mem_global_init(ref->object);
+}
+
+static void vbox_ttm_mem_global_release(struct drm_global_reference *ref)
+{
+	ttm_mem_global_release(ref->object);
+}
+
 /**
  * Adds the vbox memory manager object/structures to the global memory manager.
  */
@@ -24,6 +34,18 @@ static int vbox_ttm_global_init(struct vbox_private *vbox)
 	struct drm_global_reference *global_ref;
 	int ret;
 
+	global_ref = &vbox->ttm.mem_global_ref;
+	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
+	global_ref->size = sizeof(struct ttm_mem_global);
+	global_ref->init = &vbox_ttm_mem_global_init;
+	global_ref->release = &vbox_ttm_mem_global_release;
+	ret = drm_global_item_ref(global_ref);
+	if (ret) {
+		DRM_ERROR("Failed setting up TTM memory subsystem.\n");
+		return ret;
+	}
+
+	vbox->ttm.bo_global_ref.mem_glob = vbox->ttm.mem_global_ref.object;
 	global_ref = &vbox->ttm.bo_global_ref.ref;
 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
 	global_ref->size = sizeof(struct ttm_bo_global);
@@ -33,6 +55,7 @@ static int vbox_ttm_global_init(struct vbox_private *vbox)
 	ret = drm_global_item_ref(global_ref);
 	if (ret) {
 		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
+		drm_global_item_unref(&vbox->ttm.mem_global_ref);
 		return ret;
 	}
 
@@ -45,6 +68,7 @@ static int vbox_ttm_global_init(struct vbox_private *vbox)
 static void vbox_ttm_global_release(struct vbox_private *vbox)
 {
 	drm_global_item_unref(&vbox->ttm.bo_global_ref.ref);
+	drm_global_item_unref(&vbox->ttm.mem_global_ref);
 }
 
 static void vbox_bo_ttm_destroy(struct ttm_buffer_object *tbo)
diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
index 4ae6fc33f761..c6ee07d10281 100644
--- a/include/drm/ttm/ttm_bo_driver.h
+++ b/include/drm/ttm/ttm_bo_driver.h
@@ -570,7 +570,8 @@ void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
 			   struct ttm_mem_reg *mem);
 
 void ttm_bo_global_release(struct ttm_bo_global *glob);
-int ttm_bo_global_init(struct ttm_bo_global *glob);
+int ttm_bo_global_init(struct ttm_bo_global *glob,
+		       struct ttm_mem_global *mem_glob);
 
 int ttm_bo_device_release(struct ttm_bo_device *bdev);
 
@@ -894,6 +895,7 @@ extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
 
 struct ttm_bo_global_ref {
 	struct drm_global_reference ref;
+	struct ttm_mem_global *mem_glob;
 };
 
 /**
@@ -907,7 +909,9 @@ struct ttm_bo_global_ref {
  */
 static inline int ttm_bo_global_ref_init(struct drm_global_reference *ref)
 {
-	return ttm_bo_global_init(ref->object);
+	struct ttm_bo_global_ref *bo_ref =
+		container_of(ref, struct ttm_bo_global_ref, ref);
+	return ttm_bo_global_init(ref->object, bo_ref->mem_glob);
 }
 
 /**
diff --git a/include/drm/ttm/ttm_memory.h b/include/drm/ttm/ttm_memory.h
index 3ff48a0a2d7b..737b5fed8003 100644
--- a/include/drm/ttm/ttm_memory.h
+++ b/include/drm/ttm/ttm_memory.h
@@ -63,7 +63,7 @@
 
 #define TTM_MEM_MAX_ZONES 2
 struct ttm_mem_zone;
-extern struct ttm_mem_global {
+struct ttm_mem_global {
 	struct kobject kobj;
 	struct ttm_bo_global *bo_glob;
 	struct workqueue_struct *swap_queue;
@@ -78,7 +78,7 @@ extern struct ttm_mem_global {
 #else
 	struct ttm_mem_zone *zone_dma32;
 #endif
-} ttm_mem_glob;
+};
 
 extern int ttm_mem_global_init(struct ttm_mem_global *glob);
 extern void ttm_mem_global_release(struct ttm_mem_global *glob);
-- 
2.20.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16  0:35 [PATCH 0/6] Fix crash after reloading a driver using ttm Karol Herbst
                   ` (5 preceding siblings ...)
  2019-04-16  0:35 ` [PATCH 6/6] Revert "drm/ttm: use a static ttm_mem_global instance" Karol Herbst
@ 2019-04-16  6:38 ` Christian König
  2019-04-16  9:10   ` Karol Herbst
  2019-04-16 23:09   ` Eric Anholt
  6 siblings, 2 replies; 18+ messages in thread
From: Christian König @ 2019-04-16  6:38 UTC (permalink / raw)
  To: Karol Herbst, dri-devel; +Cc: Alex Deucher

Am 16.04.19 um 02:35 schrieb Karol Herbst:
> Kobjects are supposed to be dynamically allocated, but with recent changes
> this rule was violated. Reverting those commits fixes crashes when a drm
> driver using TTM gets loaded again.
>
> The object in question is "ttm_mem_glob" declared inside
> "include/drm/ttm/ttm_memory.h" and instatiated inside
> "drivers/gpu/drm/ttm/ttm_memory.c".
>
> from "Documentation/kobject.txt":
> "Because kobjects are dynamic, they must not be declared statically or on
> the stack, but instead, always allocated dynamically.  Future versions of
> the kernel will contain a run-time check for kobjects that are created
> statically and will warn the developer of this improper usage."
>
> Unloading ttm before reloading the driver workarounds that crash, because
> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
> and kobject_put function never free or clean up the kobject object leaving
> it in an undefined state.
>
> I reverted a few more commits to make it less painful for me to rever this
> rather big change.

Well, NAK. By reverting those change you also re-introduced the problems 
we originally fixed with those patches.

Please work on a proper fix instead,
Christian.

>
> dmesg output:
> [54758.418036] kobject (00000000687a067d): tried to init an initialized object, something is seriously wrong.
> [54758.418040] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
> [54758.418041] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
> [54758.418041] Call Trace:
> [54758.418049]  dump_stack+0x5c/0x80
> [54758.418054]  kobject_init.cold.9+0x31/0x3f
> [54758.418057]  kobject_init_and_add+0x35/0xa0
> [54758.418063]  ttm_mem_global_init+0x8f/0x2b0 [ttm]
> [54758.418067]  ? __debugfs_create_file+0xe1/0x110
> [54758.418071]  ttm_bo_device_init+0x198/0x2a0 [ttm]
> [54758.418144]  nouveau_ttm_init+0xbf/0x340 [nouveau]
> [54758.418206]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
> [54758.418210]  ? pci_bus_read_config_word+0x49/0x70
> [54758.418266]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
> [54758.418270]  local_pci_probe+0x41/0x90
> [54758.418272]  pci_device_probe+0x118/0x1a0
> [54758.418275]  really_probe+0xf8/0x3b0
> [54758.418277]  driver_probe_device+0xb3/0xf0
> [54758.418278]  __driver_attach+0xdd/0x110
> [54758.418280]  ? driver_probe_device+0xf0/0xf0
> [54758.418282]  bus_for_each_dev+0x77/0xc0
> [54758.418285]  ? klist_add_tail+0x3b/0x60
> [54758.418287]  bus_add_driver+0x152/0x230
> [54758.418288]  ? 0xffffffffc1027000
> [54758.418290]  driver_register+0x6b/0xb0
> [54758.418291]  ? 0xffffffffc1027000
> [54758.418294]  do_one_initcall+0x46/0x1c3
> [54758.418296]  ? _cond_resched+0x15/0x30
> [54758.418299]  ? kmem_cache_alloc_trace+0x154/0x1d0
> [54758.418302]  do_init_module+0x5a/0x210
> [54758.418304]  load_module+0x2096/0x22d0
> [54758.418308]  ? ima_post_read_file+0xf4/0x100
> [54758.418310]  ? __do_sys_finit_module+0xa8/0x110
> [54758.418312]  __do_sys_finit_module+0xa8/0x110
> [54758.418315]  do_syscall_64+0x5b/0x160
> [54758.418317]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
> [54758.418319] RIP: 0033:0x7fc0b38b6edd
> [54758.418321] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
> [54758.418322] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
> [54758.418323] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
> [54758.418324] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
> [54758.418325] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
> [54758.418326] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
> [54758.418326] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
> [54758.418346] BUG: unable to handle kernel paging request at 00000004ee194700
> [54758.418348] #PF error: [WRITE]
> [54758.418349] PGD 0 P4D 0
> [54758.418352] Oops: 0002 [#1] SMP PTI
> [54758.418354] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
> [54758.418355] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
> [54758.418360] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
> [54758.418361] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
> [54758.418363] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
> [54758.418364] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
> [54758.418366] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
> [54758.418367] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
> [54758.418368] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
> [54758.418370] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
> [54758.418371] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
> [54758.418373] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> [54758.418374] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0
> [54758.418375] Call Trace:
> [54758.418381]  ttm_bo_device_init+0x198/0x2a0 [ttm]
> [54758.418439]  nouveau_ttm_init+0xbf/0x340 [nouveau]
> [54758.418495]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
> [54758.418499]  ? pci_bus_read_config_word+0x49/0x70
> [54758.418552]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
> [54758.418556]  local_pci_probe+0x41/0x90
> [54758.418558]  pci_device_probe+0x118/0x1a0
> [54758.418561]  really_probe+0xf8/0x3b0
> [54758.418563]  driver_probe_device+0xb3/0xf0
> [54758.418565]  __driver_attach+0xdd/0x110
> [54758.418567]  ? driver_probe_device+0xf0/0xf0
> [54758.418570]  bus_for_each_dev+0x77/0xc0
> [54758.418573]  ? klist_add_tail+0x3b/0x60
> [54758.418574]  bus_add_driver+0x152/0x230
> [54758.418576]  ? 0xffffffffc1027000
> [54758.418578]  driver_register+0x6b/0xb0
> [54758.418580]  ? 0xffffffffc1027000
> [54758.418583]  do_one_initcall+0x46/0x1c3
> [54758.418585]  ? _cond_resched+0x15/0x30
> [54758.418587]  ? kmem_cache_alloc_trace+0x154/0x1d0
> [54758.418591]  do_init_module+0x5a/0x210
> [54758.418593]  load_module+0x2096/0x22d0
> [54758.418596]  ? ima_post_read_file+0xf4/0x100
> [54758.418599]  ? __do_sys_finit_module+0xa8/0x110
> [54758.418601]  __do_sys_finit_module+0xa8/0x110
> [54758.418604]  do_syscall_64+0x5b/0x160
> [54758.418607]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
> [54758.418609] RIP: 0033:0x7fc0b38b6edd
> [54758.418610] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
> [54758.418612] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
> [54758.418614] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
> [54758.418615] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
> [54758.418616] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
> [54758.418618] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
> [54758.418619] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
> [54758.418620] Modules linked in: nouveau(OE+) acpi_call(OE) ttm rfcomm ccm xt_CHECKSUM ipt_MASQUERADE tun bridge stp llc devlink ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 xt_conntrack ebtable_nat ip6table_nat nf_nat_ipv6 ip6table_mangle ip6table_raw ip6table_security iptable_nat nf_nat_ipv4 nf_nat iptable_mangle iptable_raw iptable_security nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 libcrc32c ip_set nfnetlink ebtable_filter ebtables ip6table_filter ip6_tables cmac bnep sunrpc vfat fat btusb btrtl btbcm btintel uvcvideo videobuf2_vmalloc bluetooth videobuf2_memops videobuf2_v4l2 videobuf2_common videodev media ecdh_generic arc4 snd_hda_codec_hdmi ath10k_pci ath10k_core snd_hda_codec_realtek iTCO_wdt mei_wdt iTCO_vendor_support snd_hda_codec_generic mac80211 intel_rapl snd_hda_intel x86_pkg_temp_thermal intel_powerclamp coretemp snd_hda_codec dell_laptop ledtrig_audio dell_smm_hwmon snd_hda_core kvm_intel snd_hwdep ath snd_seq snd_seq_device intel_cstate cfg80211 snd_pcm dell_wmi intel_uncore
> [54758.418653]  dell_smbios dcdbas intel_rapl_perf snd_timer joydev dell_wmi_descriptor intel_wmi_thunderbolt wmi_bmof snd idma64 soundcore i2c_i801 rfkill rtsx_pci_ms memstick mei_me mei processor_thermal_device intel_lpss_pci intel_soc_dts_iosf intel_pch_thermal intel_lpss int3400_thermal acpi_pad acpi_thermal_rel int3403_thermal intel_hid pcc_cpufreq int340x_thermal_zone sparse_keymap binfmt_misc zram dm_crypt hid_multitouch i915 kvmgt mdev vfio kvm irqbypass i2c_algo_bit crct10dif_pclmul drm_kms_helper rtsx_pci_sdmmc crc32_pclmul mmc_core crc32c_intel mxm_wmi drm nvme ghash_clmulni_intel nvme_core serio_raw rtsx_pci i2c_hid video wmi i2c_dev lz4 lz4_compress [last unloaded: nouveau]
> [54758.418679] CR2: 00000004ee194700
> [54758.418681] ---[ end trace c5175234e6efc034 ]---
> [54758.418686] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
> [54758.418688] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
> [54758.418689] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
> [54758.418690] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
> [54758.418691] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
> [54758.418692] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
> [54758.418694] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
> [54758.418695] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
> [54758.418696] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
> [54758.418698] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> [54758.418699] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0
>
> Karol Herbst (6):
>    Revert "drm: Remove drm_global.{c,h} v2"
>    Revert "drm/ttm: initialize globals during device init (v2)"
>    Revert "drm/ttm: Fix bo_global and mem_global kfree error"
>    Revert "drm/ttm: use a static ttm_bo_global instance"
>    Revert "drm/ttm: make the device list mutex static"
>    Revert "drm/ttm: use a static ttm_mem_global instance"
>
>   drivers/gpu/drm/Makefile                      |   2 +-
>   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 103 ++++++++++++-
>   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |   2 +
>   drivers/gpu/drm/ast/ast_drv.h                 |   2 +
>   drivers/gpu/drm/ast/ast_ttm.c                 |  64 ++++++++
>   drivers/gpu/drm/bochs/bochs.h                 |   2 +
>   drivers/gpu/drm/bochs/bochs_mm.c              |  61 ++++++++
>   drivers/gpu/drm/cirrus/cirrus_drv.h           |   2 +
>   drivers/gpu/drm/cirrus/cirrus_ttm.c           |  64 ++++++++
>   drivers/gpu/drm/drm_drv.c                     |   2 +
>   drivers/gpu/drm/drm_global.c                  | 137 ++++++++++++++++++
>   .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h   |   2 +
>   drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c   |  57 ++++++++
>   drivers/gpu/drm/mgag200/mgag200_drv.h         |   2 +
>   drivers/gpu/drm/mgag200/mgag200_ttm.c         |  64 ++++++++
>   drivers/gpu/drm/nouveau/nouveau_drv.h         |   2 +
>   drivers/gpu/drm/nouveau/nouveau_ttm.c         |  67 +++++++++
>   drivers/gpu/drm/qxl/qxl_drv.h                 |   3 +
>   drivers/gpu/drm/qxl/qxl_ttm.c                 |  61 ++++++++
>   drivers/gpu/drm/radeon/radeon.h               |   3 +
>   drivers/gpu/drm/radeon/radeon_ttm.c           |  65 +++++++++
>   drivers/gpu/drm/ttm/ttm_bo.c                  |  67 +++------
>   drivers/gpu/drm/ttm/ttm_memory.c              |  14 +-
>   drivers/gpu/drm/virtio/virtgpu_drv.h          |   3 +
>   drivers/gpu/drm/virtio/virtgpu_ttm.c          |  62 ++++++++
>   drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  15 +-
>   drivers/gpu/drm/vmwgfx/vmwgfx_drv.h           |   6 +-
>   drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c      |  54 +++++++
>   drivers/staging/vboxvideo/vbox_drv.h          |   2 +
>   drivers/staging/vboxvideo/vbox_ttm.c          |  65 ++++++++-
>   include/drm/drmP.h                            |   1 +
>   include/drm/drm_global.h                      |  53 +++++++
>   include/drm/ttm/ttm_bo_driver.h               |  54 ++++++-
>   include/drm/ttm/ttm_memory.h                  |   4 +-
>   34 files changed, 1102 insertions(+), 65 deletions(-)
>   create mode 100644 drivers/gpu/drm/drm_global.c
>   create mode 100644 include/drm/drm_global.h
>


_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16  6:38 ` [PATCH 0/6] Fix crash after reloading a driver using ttm Christian König
@ 2019-04-16  9:10   ` Karol Herbst
  2019-04-16  9:12     ` Koenig, Christian
  2019-04-16 23:09   ` Eric Anholt
  1 sibling, 1 reply; 18+ messages in thread
From: Karol Herbst @ 2019-04-16  9:10 UTC (permalink / raw)
  To: christian.koenig; +Cc: Alex Deucher, dri-devel

On Tue, Apr 16, 2019 at 8:38 AM Christian König
<ckoenig.leichtzumerken@gmail.com> wrote:
>
> Am 16.04.19 um 02:35 schrieb Karol Herbst:
> > Kobjects are supposed to be dynamically allocated, but with recent changes
> > this rule was violated. Reverting those commits fixes crashes when a drm
> > driver using TTM gets loaded again.
> >
> > The object in question is "ttm_mem_glob" declared inside
> > "include/drm/ttm/ttm_memory.h" and instatiated inside
> > "drivers/gpu/drm/ttm/ttm_memory.c".
> >
> > from "Documentation/kobject.txt":
> > "Because kobjects are dynamic, they must not be declared statically or on
> > the stack, but instead, always allocated dynamically.  Future versions of
> > the kernel will contain a run-time check for kobjects that are created
> > statically and will warn the developer of this improper usage."
> >
> > Unloading ttm before reloading the driver workarounds that crash, because
> > the memory backing the kobject member "kobj" is cleaned up. The kobject_del
> > and kobject_put function never free or clean up the kobject object leaving
> > it in an undefined state.
> >
> > I reverted a few more commits to make it less painful for me to rever this
> > rather big change.
>
> Well, NAK. By reverting those change you also re-introduced the problems
> we originally fixed with those patches.
>
> Please work on a proper fix instead,
> Christian.

And which problem was that besides duplicated code (or maybe even a
bit of memory consumption if multiple ttm driver were used)? If I had
to choose between duplicated code and a crash, I choose the former.

Maybe I missed the real reason why those changes are made, but the
commit messages don't really seem to tell me.

>
> >
> > dmesg output:
> > [54758.418036] kobject (00000000687a067d): tried to init an initialized object, something is seriously wrong.
> > [54758.418040] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
> > [54758.418041] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
> > [54758.418041] Call Trace:
> > [54758.418049]  dump_stack+0x5c/0x80
> > [54758.418054]  kobject_init.cold.9+0x31/0x3f
> > [54758.418057]  kobject_init_and_add+0x35/0xa0
> > [54758.418063]  ttm_mem_global_init+0x8f/0x2b0 [ttm]
> > [54758.418067]  ? __debugfs_create_file+0xe1/0x110
> > [54758.418071]  ttm_bo_device_init+0x198/0x2a0 [ttm]
> > [54758.418144]  nouveau_ttm_init+0xbf/0x340 [nouveau]
> > [54758.418206]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
> > [54758.418210]  ? pci_bus_read_config_word+0x49/0x70
> > [54758.418266]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
> > [54758.418270]  local_pci_probe+0x41/0x90
> > [54758.418272]  pci_device_probe+0x118/0x1a0
> > [54758.418275]  really_probe+0xf8/0x3b0
> > [54758.418277]  driver_probe_device+0xb3/0xf0
> > [54758.418278]  __driver_attach+0xdd/0x110
> > [54758.418280]  ? driver_probe_device+0xf0/0xf0
> > [54758.418282]  bus_for_each_dev+0x77/0xc0
> > [54758.418285]  ? klist_add_tail+0x3b/0x60
> > [54758.418287]  bus_add_driver+0x152/0x230
> > [54758.418288]  ? 0xffffffffc1027000
> > [54758.418290]  driver_register+0x6b/0xb0
> > [54758.418291]  ? 0xffffffffc1027000
> > [54758.418294]  do_one_initcall+0x46/0x1c3
> > [54758.418296]  ? _cond_resched+0x15/0x30
> > [54758.418299]  ? kmem_cache_alloc_trace+0x154/0x1d0
> > [54758.418302]  do_init_module+0x5a/0x210
> > [54758.418304]  load_module+0x2096/0x22d0
> > [54758.418308]  ? ima_post_read_file+0xf4/0x100
> > [54758.418310]  ? __do_sys_finit_module+0xa8/0x110
> > [54758.418312]  __do_sys_finit_module+0xa8/0x110
> > [54758.418315]  do_syscall_64+0x5b/0x160
> > [54758.418317]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
> > [54758.418319] RIP: 0033:0x7fc0b38b6edd
> > [54758.418321] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
> > [54758.418322] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
> > [54758.418323] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
> > [54758.418324] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
> > [54758.418325] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
> > [54758.418326] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
> > [54758.418326] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
> > [54758.418346] BUG: unable to handle kernel paging request at 00000004ee194700
> > [54758.418348] #PF error: [WRITE]
> > [54758.418349] PGD 0 P4D 0
> > [54758.418352] Oops: 0002 [#1] SMP PTI
> > [54758.418354] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
> > [54758.418355] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
> > [54758.418360] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
> > [54758.418361] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
> > [54758.418363] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
> > [54758.418364] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
> > [54758.418366] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
> > [54758.418367] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
> > [54758.418368] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
> > [54758.418370] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
> > [54758.418371] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
> > [54758.418373] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> > [54758.418374] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0
> > [54758.418375] Call Trace:
> > [54758.418381]  ttm_bo_device_init+0x198/0x2a0 [ttm]
> > [54758.418439]  nouveau_ttm_init+0xbf/0x340 [nouveau]
> > [54758.418495]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
> > [54758.418499]  ? pci_bus_read_config_word+0x49/0x70
> > [54758.418552]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
> > [54758.418556]  local_pci_probe+0x41/0x90
> > [54758.418558]  pci_device_probe+0x118/0x1a0
> > [54758.418561]  really_probe+0xf8/0x3b0
> > [54758.418563]  driver_probe_device+0xb3/0xf0
> > [54758.418565]  __driver_attach+0xdd/0x110
> > [54758.418567]  ? driver_probe_device+0xf0/0xf0
> > [54758.418570]  bus_for_each_dev+0x77/0xc0
> > [54758.418573]  ? klist_add_tail+0x3b/0x60
> > [54758.418574]  bus_add_driver+0x152/0x230
> > [54758.418576]  ? 0xffffffffc1027000
> > [54758.418578]  driver_register+0x6b/0xb0
> > [54758.418580]  ? 0xffffffffc1027000
> > [54758.418583]  do_one_initcall+0x46/0x1c3
> > [54758.418585]  ? _cond_resched+0x15/0x30
> > [54758.418587]  ? kmem_cache_alloc_trace+0x154/0x1d0
> > [54758.418591]  do_init_module+0x5a/0x210
> > [54758.418593]  load_module+0x2096/0x22d0
> > [54758.418596]  ? ima_post_read_file+0xf4/0x100
> > [54758.418599]  ? __do_sys_finit_module+0xa8/0x110
> > [54758.418601]  __do_sys_finit_module+0xa8/0x110
> > [54758.418604]  do_syscall_64+0x5b/0x160
> > [54758.418607]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
> > [54758.418609] RIP: 0033:0x7fc0b38b6edd
> > [54758.418610] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
> > [54758.418612] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
> > [54758.418614] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
> > [54758.418615] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
> > [54758.418616] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
> > [54758.418618] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
> > [54758.418619] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
> > [54758.418620] Modules linked in: nouveau(OE+) acpi_call(OE) ttm rfcomm ccm xt_CHECKSUM ipt_MASQUERADE tun bridge stp llc devlink ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 xt_conntrack ebtable_nat ip6table_nat nf_nat_ipv6 ip6table_mangle ip6table_raw ip6table_security iptable_nat nf_nat_ipv4 nf_nat iptable_mangle iptable_raw iptable_security nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 libcrc32c ip_set nfnetlink ebtable_filter ebtables ip6table_filter ip6_tables cmac bnep sunrpc vfat fat btusb btrtl btbcm btintel uvcvideo videobuf2_vmalloc bluetooth videobuf2_memops videobuf2_v4l2 videobuf2_common videodev media ecdh_generic arc4 snd_hda_codec_hdmi ath10k_pci ath10k_core snd_hda_codec_realtek iTCO_wdt mei_wdt iTCO_vendor_support snd_hda_codec_generic mac80211 intel_rapl snd_hda_intel x86_pkg_temp_thermal intel_powerclamp coretemp snd_hda_codec dell_laptop ledtrig_audio dell_smm_hwmon snd_hda_core kvm_intel snd_hwdep ath snd_seq snd_seq_device intel_cstate cfg80211 snd_pcm dell_wmi intel_uncore
> > [54758.418653]  dell_smbios dcdbas intel_rapl_perf snd_timer joydev dell_wmi_descriptor intel_wmi_thunderbolt wmi_bmof snd idma64 soundcore i2c_i801 rfkill rtsx_pci_ms memstick mei_me mei processor_thermal_device intel_lpss_pci intel_soc_dts_iosf intel_pch_thermal intel_lpss int3400_thermal acpi_pad acpi_thermal_rel int3403_thermal intel_hid pcc_cpufreq int340x_thermal_zone sparse_keymap binfmt_misc zram dm_crypt hid_multitouch i915 kvmgt mdev vfio kvm irqbypass i2c_algo_bit crct10dif_pclmul drm_kms_helper rtsx_pci_sdmmc crc32_pclmul mmc_core crc32c_intel mxm_wmi drm nvme ghash_clmulni_intel nvme_core serio_raw rtsx_pci i2c_hid video wmi i2c_dev lz4 lz4_compress [last unloaded: nouveau]
> > [54758.418679] CR2: 00000004ee194700
> > [54758.418681] ---[ end trace c5175234e6efc034 ]---
> > [54758.418686] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
> > [54758.418688] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
> > [54758.418689] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
> > [54758.418690] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
> > [54758.418691] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
> > [54758.418692] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
> > [54758.418694] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
> > [54758.418695] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
> > [54758.418696] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
> > [54758.418698] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> > [54758.418699] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0
> >
> > Karol Herbst (6):
> >    Revert "drm: Remove drm_global.{c,h} v2"
> >    Revert "drm/ttm: initialize globals during device init (v2)"
> >    Revert "drm/ttm: Fix bo_global and mem_global kfree error"
> >    Revert "drm/ttm: use a static ttm_bo_global instance"
> >    Revert "drm/ttm: make the device list mutex static"
> >    Revert "drm/ttm: use a static ttm_mem_global instance"
> >
> >   drivers/gpu/drm/Makefile                      |   2 +-
> >   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 103 ++++++++++++-
> >   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |   2 +
> >   drivers/gpu/drm/ast/ast_drv.h                 |   2 +
> >   drivers/gpu/drm/ast/ast_ttm.c                 |  64 ++++++++
> >   drivers/gpu/drm/bochs/bochs.h                 |   2 +
> >   drivers/gpu/drm/bochs/bochs_mm.c              |  61 ++++++++
> >   drivers/gpu/drm/cirrus/cirrus_drv.h           |   2 +
> >   drivers/gpu/drm/cirrus/cirrus_ttm.c           |  64 ++++++++
> >   drivers/gpu/drm/drm_drv.c                     |   2 +
> >   drivers/gpu/drm/drm_global.c                  | 137 ++++++++++++++++++
> >   .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h   |   2 +
> >   drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c   |  57 ++++++++
> >   drivers/gpu/drm/mgag200/mgag200_drv.h         |   2 +
> >   drivers/gpu/drm/mgag200/mgag200_ttm.c         |  64 ++++++++
> >   drivers/gpu/drm/nouveau/nouveau_drv.h         |   2 +
> >   drivers/gpu/drm/nouveau/nouveau_ttm.c         |  67 +++++++++
> >   drivers/gpu/drm/qxl/qxl_drv.h                 |   3 +
> >   drivers/gpu/drm/qxl/qxl_ttm.c                 |  61 ++++++++
> >   drivers/gpu/drm/radeon/radeon.h               |   3 +
> >   drivers/gpu/drm/radeon/radeon_ttm.c           |  65 +++++++++
> >   drivers/gpu/drm/ttm/ttm_bo.c                  |  67 +++------
> >   drivers/gpu/drm/ttm/ttm_memory.c              |  14 +-
> >   drivers/gpu/drm/virtio/virtgpu_drv.h          |   3 +
> >   drivers/gpu/drm/virtio/virtgpu_ttm.c          |  62 ++++++++
> >   drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  15 +-
> >   drivers/gpu/drm/vmwgfx/vmwgfx_drv.h           |   6 +-
> >   drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c      |  54 +++++++
> >   drivers/staging/vboxvideo/vbox_drv.h          |   2 +
> >   drivers/staging/vboxvideo/vbox_ttm.c          |  65 ++++++++-
> >   include/drm/drmP.h                            |   1 +
> >   include/drm/drm_global.h                      |  53 +++++++
> >   include/drm/ttm/ttm_bo_driver.h               |  54 ++++++-
> >   include/drm/ttm/ttm_memory.h                  |   4 +-
> >   34 files changed, 1102 insertions(+), 65 deletions(-)
> >   create mode 100644 drivers/gpu/drm/drm_global.c
> >   create mode 100644 include/drm/drm_global.h
> >
>
>
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16  9:10   ` Karol Herbst
@ 2019-04-16  9:12     ` Koenig, Christian
  2019-04-16 10:54       ` Karol Herbst
  0 siblings, 1 reply; 18+ messages in thread
From: Koenig, Christian @ 2019-04-16  9:12 UTC (permalink / raw)
  To: Karol Herbst; +Cc: Deucher, Alexander, dri-devel

Am 16.04.19 um 11:10 schrieb Karol Herbst:
> On Tue, Apr 16, 2019 at 8:38 AM Christian König
> <ckoenig.leichtzumerken@gmail.com> wrote:
>> Am 16.04.19 um 02:35 schrieb Karol Herbst:
>>> Kobjects are supposed to be dynamically allocated, but with recent changes
>>> this rule was violated. Reverting those commits fixes crashes when a drm
>>> driver using TTM gets loaded again.
>>>
>>> The object in question is "ttm_mem_glob" declared inside
>>> "include/drm/ttm/ttm_memory.h" and instatiated inside
>>> "drivers/gpu/drm/ttm/ttm_memory.c".
>>>
>>> from "Documentation/kobject.txt":
>>> "Because kobjects are dynamic, they must not be declared statically or on
>>> the stack, but instead, always allocated dynamically.  Future versions of
>>> the kernel will contain a run-time check for kobjects that are created
>>> statically and will warn the developer of this improper usage."
>>>
>>> Unloading ttm before reloading the driver workarounds that crash, because
>>> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
>>> and kobject_put function never free or clean up the kobject object leaving
>>> it in an undefined state.
>>>
>>> I reverted a few more commits to make it less painful for me to rever this
>>> rather big change.
>> Well, NAK. By reverting those change you also re-introduced the problems
>> we originally fixed with those patches.
>>
>> Please work on a proper fix instead,
>> Christian.
> And which problem was that besides duplicated code (or maybe even a
> bit of memory consumption if multiple ttm driver were used)? If I had
> to choose between duplicated code and a crash, I choose the former.
>
> Maybe I missed the real reason why those changes are made, but the
> commit messages don't really seem to tell me.

The old implementation crashed because different drivers tried to 
allocate the same kobj.

Crashing in one way is not better than crashing in another way.

Christian.

>
>>> dmesg output:
>>> [54758.418036] kobject (00000000687a067d): tried to init an initialized object, something is seriously wrong.
>>> [54758.418040] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
>>> [54758.418041] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
>>> [54758.418041] Call Trace:
>>> [54758.418049]  dump_stack+0x5c/0x80
>>> [54758.418054]  kobject_init.cold.9+0x31/0x3f
>>> [54758.418057]  kobject_init_and_add+0x35/0xa0
>>> [54758.418063]  ttm_mem_global_init+0x8f/0x2b0 [ttm]
>>> [54758.418067]  ? __debugfs_create_file+0xe1/0x110
>>> [54758.418071]  ttm_bo_device_init+0x198/0x2a0 [ttm]
>>> [54758.418144]  nouveau_ttm_init+0xbf/0x340 [nouveau]
>>> [54758.418206]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
>>> [54758.418210]  ? pci_bus_read_config_word+0x49/0x70
>>> [54758.418266]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
>>> [54758.418270]  local_pci_probe+0x41/0x90
>>> [54758.418272]  pci_device_probe+0x118/0x1a0
>>> [54758.418275]  really_probe+0xf8/0x3b0
>>> [54758.418277]  driver_probe_device+0xb3/0xf0
>>> [54758.418278]  __driver_attach+0xdd/0x110
>>> [54758.418280]  ? driver_probe_device+0xf0/0xf0
>>> [54758.418282]  bus_for_each_dev+0x77/0xc0
>>> [54758.418285]  ? klist_add_tail+0x3b/0x60
>>> [54758.418287]  bus_add_driver+0x152/0x230
>>> [54758.418288]  ? 0xffffffffc1027000
>>> [54758.418290]  driver_register+0x6b/0xb0
>>> [54758.418291]  ? 0xffffffffc1027000
>>> [54758.418294]  do_one_initcall+0x46/0x1c3
>>> [54758.418296]  ? _cond_resched+0x15/0x30
>>> [54758.418299]  ? kmem_cache_alloc_trace+0x154/0x1d0
>>> [54758.418302]  do_init_module+0x5a/0x210
>>> [54758.418304]  load_module+0x2096/0x22d0
>>> [54758.418308]  ? ima_post_read_file+0xf4/0x100
>>> [54758.418310]  ? __do_sys_finit_module+0xa8/0x110
>>> [54758.418312]  __do_sys_finit_module+0xa8/0x110
>>> [54758.418315]  do_syscall_64+0x5b/0x160
>>> [54758.418317]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
>>> [54758.418319] RIP: 0033:0x7fc0b38b6edd
>>> [54758.418321] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
>>> [54758.418322] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
>>> [54758.418323] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
>>> [54758.418324] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
>>> [54758.418325] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
>>> [54758.418326] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
>>> [54758.418326] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
>>> [54758.418346] BUG: unable to handle kernel paging request at 00000004ee194700
>>> [54758.418348] #PF error: [WRITE]
>>> [54758.418349] PGD 0 P4D 0
>>> [54758.418352] Oops: 0002 [#1] SMP PTI
>>> [54758.418354] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
>>> [54758.418355] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
>>> [54758.418360] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
>>> [54758.418361] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
>>> [54758.418363] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
>>> [54758.418364] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
>>> [54758.418366] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
>>> [54758.418367] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
>>> [54758.418368] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
>>> [54758.418370] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
>>> [54758.418371] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
>>> [54758.418373] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
>>> [54758.418374] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0
>>> [54758.418375] Call Trace:
>>> [54758.418381]  ttm_bo_device_init+0x198/0x2a0 [ttm]
>>> [54758.418439]  nouveau_ttm_init+0xbf/0x340 [nouveau]
>>> [54758.418495]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
>>> [54758.418499]  ? pci_bus_read_config_word+0x49/0x70
>>> [54758.418552]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
>>> [54758.418556]  local_pci_probe+0x41/0x90
>>> [54758.418558]  pci_device_probe+0x118/0x1a0
>>> [54758.418561]  really_probe+0xf8/0x3b0
>>> [54758.418563]  driver_probe_device+0xb3/0xf0
>>> [54758.418565]  __driver_attach+0xdd/0x110
>>> [54758.418567]  ? driver_probe_device+0xf0/0xf0
>>> [54758.418570]  bus_for_each_dev+0x77/0xc0
>>> [54758.418573]  ? klist_add_tail+0x3b/0x60
>>> [54758.418574]  bus_add_driver+0x152/0x230
>>> [54758.418576]  ? 0xffffffffc1027000
>>> [54758.418578]  driver_register+0x6b/0xb0
>>> [54758.418580]  ? 0xffffffffc1027000
>>> [54758.418583]  do_one_initcall+0x46/0x1c3
>>> [54758.418585]  ? _cond_resched+0x15/0x30
>>> [54758.418587]  ? kmem_cache_alloc_trace+0x154/0x1d0
>>> [54758.418591]  do_init_module+0x5a/0x210
>>> [54758.418593]  load_module+0x2096/0x22d0
>>> [54758.418596]  ? ima_post_read_file+0xf4/0x100
>>> [54758.418599]  ? __do_sys_finit_module+0xa8/0x110
>>> [54758.418601]  __do_sys_finit_module+0xa8/0x110
>>> [54758.418604]  do_syscall_64+0x5b/0x160
>>> [54758.418607]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
>>> [54758.418609] RIP: 0033:0x7fc0b38b6edd
>>> [54758.418610] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
>>> [54758.418612] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
>>> [54758.418614] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
>>> [54758.418615] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
>>> [54758.418616] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
>>> [54758.418618] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
>>> [54758.418619] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
>>> [54758.418620] Modules linked in: nouveau(OE+) acpi_call(OE) ttm rfcomm ccm xt_CHECKSUM ipt_MASQUERADE tun bridge stp llc devlink ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 xt_conntrack ebtable_nat ip6table_nat nf_nat_ipv6 ip6table_mangle ip6table_raw ip6table_security iptable_nat nf_nat_ipv4 nf_nat iptable_mangle iptable_raw iptable_security nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 libcrc32c ip_set nfnetlink ebtable_filter ebtables ip6table_filter ip6_tables cmac bnep sunrpc vfat fat btusb btrtl btbcm btintel uvcvideo videobuf2_vmalloc bluetooth videobuf2_memops videobuf2_v4l2 videobuf2_common videodev media ecdh_generic arc4 snd_hda_codec_hdmi ath10k_pci ath10k_core snd_hda_codec_realtek iTCO_wdt mei_wdt iTCO_vendor_support snd_hda_codec_generic mac80211 intel_rapl snd_hda_intel x86_pkg_temp_thermal intel_powerclamp coretemp snd_hda_codec dell_laptop ledtrig_audio dell_smm_hwmon snd_hda_core kvm_intel snd_hwdep ath snd_seq snd_seq_device intel_cstate cfg80211 snd_pcm dell_wmi intel_uncore
>>> [54758.418653]  dell_smbios dcdbas intel_rapl_perf snd_timer joydev dell_wmi_descriptor intel_wmi_thunderbolt wmi_bmof snd idma64 soundcore i2c_i801 rfkill rtsx_pci_ms memstick mei_me mei processor_thermal_device intel_lpss_pci intel_soc_dts_iosf intel_pch_thermal intel_lpss int3400_thermal acpi_pad acpi_thermal_rel int3403_thermal intel_hid pcc_cpufreq int340x_thermal_zone sparse_keymap binfmt_misc zram dm_crypt hid_multitouch i915 kvmgt mdev vfio kvm irqbypass i2c_algo_bit crct10dif_pclmul drm_kms_helper rtsx_pci_sdmmc crc32_pclmul mmc_core crc32c_intel mxm_wmi drm nvme ghash_clmulni_intel nvme_core serio_raw rtsx_pci i2c_hid video wmi i2c_dev lz4 lz4_compress [last unloaded: nouveau]
>>> [54758.418679] CR2: 00000004ee194700
>>> [54758.418681] ---[ end trace c5175234e6efc034 ]---
>>> [54758.418686] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
>>> [54758.418688] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
>>> [54758.418689] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
>>> [54758.418690] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
>>> [54758.418691] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
>>> [54758.418692] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
>>> [54758.418694] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
>>> [54758.418695] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
>>> [54758.418696] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
>>> [54758.418698] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
>>> [54758.418699] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0
>>>
>>> Karol Herbst (6):
>>>     Revert "drm: Remove drm_global.{c,h} v2"
>>>     Revert "drm/ttm: initialize globals during device init (v2)"
>>>     Revert "drm/ttm: Fix bo_global and mem_global kfree error"
>>>     Revert "drm/ttm: use a static ttm_bo_global instance"
>>>     Revert "drm/ttm: make the device list mutex static"
>>>     Revert "drm/ttm: use a static ttm_mem_global instance"
>>>
>>>    drivers/gpu/drm/Makefile                      |   2 +-
>>>    drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 103 ++++++++++++-
>>>    drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |   2 +
>>>    drivers/gpu/drm/ast/ast_drv.h                 |   2 +
>>>    drivers/gpu/drm/ast/ast_ttm.c                 |  64 ++++++++
>>>    drivers/gpu/drm/bochs/bochs.h                 |   2 +
>>>    drivers/gpu/drm/bochs/bochs_mm.c              |  61 ++++++++
>>>    drivers/gpu/drm/cirrus/cirrus_drv.h           |   2 +
>>>    drivers/gpu/drm/cirrus/cirrus_ttm.c           |  64 ++++++++
>>>    drivers/gpu/drm/drm_drv.c                     |   2 +
>>>    drivers/gpu/drm/drm_global.c                  | 137 ++++++++++++++++++
>>>    .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h   |   2 +
>>>    drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c   |  57 ++++++++
>>>    drivers/gpu/drm/mgag200/mgag200_drv.h         |   2 +
>>>    drivers/gpu/drm/mgag200/mgag200_ttm.c         |  64 ++++++++
>>>    drivers/gpu/drm/nouveau/nouveau_drv.h         |   2 +
>>>    drivers/gpu/drm/nouveau/nouveau_ttm.c         |  67 +++++++++
>>>    drivers/gpu/drm/qxl/qxl_drv.h                 |   3 +
>>>    drivers/gpu/drm/qxl/qxl_ttm.c                 |  61 ++++++++
>>>    drivers/gpu/drm/radeon/radeon.h               |   3 +
>>>    drivers/gpu/drm/radeon/radeon_ttm.c           |  65 +++++++++
>>>    drivers/gpu/drm/ttm/ttm_bo.c                  |  67 +++------
>>>    drivers/gpu/drm/ttm/ttm_memory.c              |  14 +-
>>>    drivers/gpu/drm/virtio/virtgpu_drv.h          |   3 +
>>>    drivers/gpu/drm/virtio/virtgpu_ttm.c          |  62 ++++++++
>>>    drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  15 +-
>>>    drivers/gpu/drm/vmwgfx/vmwgfx_drv.h           |   6 +-
>>>    drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c      |  54 +++++++
>>>    drivers/staging/vboxvideo/vbox_drv.h          |   2 +
>>>    drivers/staging/vboxvideo/vbox_ttm.c          |  65 ++++++++-
>>>    include/drm/drmP.h                            |   1 +
>>>    include/drm/drm_global.h                      |  53 +++++++
>>>    include/drm/ttm/ttm_bo_driver.h               |  54 ++++++-
>>>    include/drm/ttm/ttm_memory.h                  |   4 +-
>>>    34 files changed, 1102 insertions(+), 65 deletions(-)
>>>    create mode 100644 drivers/gpu/drm/drm_global.c
>>>    create mode 100644 include/drm/drm_global.h
>>>
>>


_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16  9:12     ` Koenig, Christian
@ 2019-04-16 10:54       ` Karol Herbst
  2019-04-16 11:06         ` Koenig, Christian
  0 siblings, 1 reply; 18+ messages in thread
From: Karol Herbst @ 2019-04-16 10:54 UTC (permalink / raw)
  To: Koenig, Christian; +Cc: Deucher, Alexander, dri-devel

On Tue, Apr 16, 2019 at 11:12 AM Koenig, Christian
<Christian.Koenig@amd.com> wrote:
>
> Am 16.04.19 um 11:10 schrieb Karol Herbst:
> > On Tue, Apr 16, 2019 at 8:38 AM Christian König
> > <ckoenig.leichtzumerken@gmail.com> wrote:
> >> Am 16.04.19 um 02:35 schrieb Karol Herbst:
> >>> Kobjects are supposed to be dynamically allocated, but with recent changes
> >>> this rule was violated. Reverting those commits fixes crashes when a drm
> >>> driver using TTM gets loaded again.
> >>>
> >>> The object in question is "ttm_mem_glob" declared inside
> >>> "include/drm/ttm/ttm_memory.h" and instatiated inside
> >>> "drivers/gpu/drm/ttm/ttm_memory.c".
> >>>
> >>> from "Documentation/kobject.txt":
> >>> "Because kobjects are dynamic, they must not be declared statically or on
> >>> the stack, but instead, always allocated dynamically.  Future versions of
> >>> the kernel will contain a run-time check for kobjects that are created
> >>> statically and will warn the developer of this improper usage."
> >>>
> >>> Unloading ttm before reloading the driver workarounds that crash, because
> >>> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
> >>> and kobject_put function never free or clean up the kobject object leaving
> >>> it in an undefined state.
> >>>
> >>> I reverted a few more commits to make it less painful for me to rever this
> >>> rather big change.
> >> Well, NAK. By reverting those change you also re-introduced the problems
> >> we originally fixed with those patches.
> >>
> >> Please work on a proper fix instead,
> >> Christian.
> > And which problem was that besides duplicated code (or maybe even a
> > bit of memory consumption if multiple ttm driver were used)? If I had
> > to choose between duplicated code and a crash, I choose the former.
> >
> > Maybe I missed the real reason why those changes are made, but the
> > commit messages don't really seem to tell me.
>
> The old implementation crashed because different drivers tried to
> allocate the same kobj.
>
> Crashing in one way is not better than crashing in another way.
>
> Christian.
>

how can that old crash be triggered? By loading two ttm based drivers
at the same time or by other means?

> >
> >>> dmesg output:
> >>> [54758.418036] kobject (00000000687a067d): tried to init an initialized object, something is seriously wrong.
> >>> [54758.418040] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
> >>> [54758.418041] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
> >>> [54758.418041] Call Trace:
> >>> [54758.418049]  dump_stack+0x5c/0x80
> >>> [54758.418054]  kobject_init.cold.9+0x31/0x3f
> >>> [54758.418057]  kobject_init_and_add+0x35/0xa0
> >>> [54758.418063]  ttm_mem_global_init+0x8f/0x2b0 [ttm]
> >>> [54758.418067]  ? __debugfs_create_file+0xe1/0x110
> >>> [54758.418071]  ttm_bo_device_init+0x198/0x2a0 [ttm]
> >>> [54758.418144]  nouveau_ttm_init+0xbf/0x340 [nouveau]
> >>> [54758.418206]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
> >>> [54758.418210]  ? pci_bus_read_config_word+0x49/0x70
> >>> [54758.418266]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
> >>> [54758.418270]  local_pci_probe+0x41/0x90
> >>> [54758.418272]  pci_device_probe+0x118/0x1a0
> >>> [54758.418275]  really_probe+0xf8/0x3b0
> >>> [54758.418277]  driver_probe_device+0xb3/0xf0
> >>> [54758.418278]  __driver_attach+0xdd/0x110
> >>> [54758.418280]  ? driver_probe_device+0xf0/0xf0
> >>> [54758.418282]  bus_for_each_dev+0x77/0xc0
> >>> [54758.418285]  ? klist_add_tail+0x3b/0x60
> >>> [54758.418287]  bus_add_driver+0x152/0x230
> >>> [54758.418288]  ? 0xffffffffc1027000
> >>> [54758.418290]  driver_register+0x6b/0xb0
> >>> [54758.418291]  ? 0xffffffffc1027000
> >>> [54758.418294]  do_one_initcall+0x46/0x1c3
> >>> [54758.418296]  ? _cond_resched+0x15/0x30
> >>> [54758.418299]  ? kmem_cache_alloc_trace+0x154/0x1d0
> >>> [54758.418302]  do_init_module+0x5a/0x210
> >>> [54758.418304]  load_module+0x2096/0x22d0
> >>> [54758.418308]  ? ima_post_read_file+0xf4/0x100
> >>> [54758.418310]  ? __do_sys_finit_module+0xa8/0x110
> >>> [54758.418312]  __do_sys_finit_module+0xa8/0x110
> >>> [54758.418315]  do_syscall_64+0x5b/0x160
> >>> [54758.418317]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
> >>> [54758.418319] RIP: 0033:0x7fc0b38b6edd
> >>> [54758.418321] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
> >>> [54758.418322] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
> >>> [54758.418323] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
> >>> [54758.418324] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
> >>> [54758.418325] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
> >>> [54758.418326] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
> >>> [54758.418326] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
> >>> [54758.418346] BUG: unable to handle kernel paging request at 00000004ee194700
> >>> [54758.418348] #PF error: [WRITE]
> >>> [54758.418349] PGD 0 P4D 0
> >>> [54758.418352] Oops: 0002 [#1] SMP PTI
> >>> [54758.418354] CPU: 6 PID: 26746 Comm: insmod Tainted: G     U     OE     5.0.6-200.fc29.x86_64 #1
> >>> [54758.418355] Hardware name: Dell Inc. XPS 15 9560/05FFDN, BIOS 1.12.1 10/02/2018
> >>> [54758.418360] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
> >>> [54758.418361] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
> >>> [54758.418363] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
> >>> [54758.418364] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
> >>> [54758.418366] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
> >>> [54758.418367] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
> >>> [54758.418368] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
> >>> [54758.418370] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
> >>> [54758.418371] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
> >>> [54758.418373] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> >>> [54758.418374] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0
> >>> [54758.418375] Call Trace:
> >>> [54758.418381]  ttm_bo_device_init+0x198/0x2a0 [ttm]
> >>> [54758.418439]  nouveau_ttm_init+0xbf/0x340 [nouveau]
> >>> [54758.418495]  nouveau_drm_device_init+0x125/0x7d0 [nouveau]
> >>> [54758.418499]  ? pci_bus_read_config_word+0x49/0x70
> >>> [54758.418552]  nouveau_drm_probe+0x26f/0x2c0 [nouveau]
> >>> [54758.418556]  local_pci_probe+0x41/0x90
> >>> [54758.418558]  pci_device_probe+0x118/0x1a0
> >>> [54758.418561]  really_probe+0xf8/0x3b0
> >>> [54758.418563]  driver_probe_device+0xb3/0xf0
> >>> [54758.418565]  __driver_attach+0xdd/0x110
> >>> [54758.418567]  ? driver_probe_device+0xf0/0xf0
> >>> [54758.418570]  bus_for_each_dev+0x77/0xc0
> >>> [54758.418573]  ? klist_add_tail+0x3b/0x60
> >>> [54758.418574]  bus_add_driver+0x152/0x230
> >>> [54758.418576]  ? 0xffffffffc1027000
> >>> [54758.418578]  driver_register+0x6b/0xb0
> >>> [54758.418580]  ? 0xffffffffc1027000
> >>> [54758.418583]  do_one_initcall+0x46/0x1c3
> >>> [54758.418585]  ? _cond_resched+0x15/0x30
> >>> [54758.418587]  ? kmem_cache_alloc_trace+0x154/0x1d0
> >>> [54758.418591]  do_init_module+0x5a/0x210
> >>> [54758.418593]  load_module+0x2096/0x22d0
> >>> [54758.418596]  ? ima_post_read_file+0xf4/0x100
> >>> [54758.418599]  ? __do_sys_finit_module+0xa8/0x110
> >>> [54758.418601]  __do_sys_finit_module+0xa8/0x110
> >>> [54758.418604]  do_syscall_64+0x5b/0x160
> >>> [54758.418607]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
> >>> [54758.418609] RIP: 0033:0x7fc0b38b6edd
> >>> [54758.418610] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 7b 7f 0c 00 f7 d8 64 89 01 48
> >>> [54758.418612] RSP: 002b:00007ffc7f1620d8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
> >>> [54758.418614] RAX: ffffffffffffffda RBX: 00005629a3996ac0 RCX: 00007fc0b38b6edd
> >>> [54758.418615] RDX: 0000000000000000 RSI: 00005629a3996260 RDI: 0000000000000003
> >>> [54758.418616] RBP: 00005629a3996260 R08: 0000000000000000 R09: 0000000000000000
> >>> [54758.418618] R10: 0000000000000003 R11: 0000000000000246 R12: 0000000000000000
> >>> [54758.418619] R13: 00005629a3996a80 R14: 0000000000000000 R15: 00005629a3996260
> >>> [54758.418620] Modules linked in: nouveau(OE+) acpi_call(OE) ttm rfcomm ccm xt_CHECKSUM ipt_MASQUERADE tun bridge stp llc devlink ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 xt_conntrack ebtable_nat ip6table_nat nf_nat_ipv6 ip6table_mangle ip6table_raw ip6table_security iptable_nat nf_nat_ipv4 nf_nat iptable_mangle iptable_raw iptable_security nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 libcrc32c ip_set nfnetlink ebtable_filter ebtables ip6table_filter ip6_tables cmac bnep sunrpc vfat fat btusb btrtl btbcm btintel uvcvideo videobuf2_vmalloc bluetooth videobuf2_memops videobuf2_v4l2 videobuf2_common videodev media ecdh_generic arc4 snd_hda_codec_hdmi ath10k_pci ath10k_core snd_hda_codec_realtek iTCO_wdt mei_wdt iTCO_vendor_support snd_hda_codec_generic mac80211 intel_rapl snd_hda_intel x86_pkg_temp_thermal intel_powerclamp coretemp snd_hda_codec dell_laptop ledtrig_audio dell_smm_hwmon snd_hda_core kvm_intel snd_hwdep ath snd_seq snd_seq_device intel_cstate cfg80211 snd_pcm dell_wmi intel_uncore
> >>> [54758.418653]  dell_smbios dcdbas intel_rapl_perf snd_timer joydev dell_wmi_descriptor intel_wmi_thunderbolt wmi_bmof snd idma64 soundcore i2c_i801 rfkill rtsx_pci_ms memstick mei_me mei processor_thermal_device intel_lpss_pci intel_soc_dts_iosf intel_pch_thermal intel_lpss int3400_thermal acpi_pad acpi_thermal_rel int3403_thermal intel_hid pcc_cpufreq int340x_thermal_zone sparse_keymap binfmt_misc zram dm_crypt hid_multitouch i915 kvmgt mdev vfio kvm irqbypass i2c_algo_bit crct10dif_pclmul drm_kms_helper rtsx_pci_sdmmc crc32_pclmul mmc_core crc32c_intel mxm_wmi drm nvme ghash_clmulni_intel nvme_core serio_raw rtsx_pci i2c_hid video wmi i2c_dev lz4 lz4_compress [last unloaded: nouveau]
> >>> [54758.418679] CR2: 00000004ee194700
> >>> [54758.418681] ---[ end trace c5175234e6efc034 ]---
> >>> [54758.418686] RIP: 0010:ttm_mem_global_init+0x1fe/0x2b0 [ttm]
> >>> [54758.418688] Code: 00 00 00 48 89 5d 40 48 89 ab a0 00 00 00 e8 79 ba 95 f8 85 c0 0f 85 a2 00 00 00 8b 83 90 00 00 00 8d 50 01 89 93 90 00 00 00 <48> 89 ac c3 80 00 00 00 85 d2 0f 85 c6 99 00 00 48 8b 83 98 00 00
> >>> [54758.418689] RSP: 0018:ffffc24e53c1f988 EFLAGS: 00010246
> >>> [54758.418690] RAX: 00000000a5a2f300 RBX: ffffffffc101ae80 RCX: 0000000000000000
> >>> [54758.418691] RDX: 00000000a5a2f301 RSI: 0000000000000000 RDI: ffff9ebe78b67330
> >>> [54758.418692] RBP: ffff9ebe42c79d00 R08: 0000000000000044 R09: 0000000000000228
> >>> [54758.418694] R10: 0000000000000000 R11: ffff9ebb62720bb8 R12: 0000000000000000
> >>> [54758.418695] R13: ffff9ebe27d71640 R14: ffff9eba9f258600 R15: ffffffffc1453880
> >>> [54758.418696] FS:  00007fc0b379c280(0000) GS:ffff9ebeae380000(0000) knlGS:0000000000000000
> >>> [54758.418698] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> >>> [54758.418699] CR2: 00000004ee194700 CR3: 000000045069e005 CR4: 00000000003606e0
> >>>
> >>> Karol Herbst (6):
> >>>     Revert "drm: Remove drm_global.{c,h} v2"
> >>>     Revert "drm/ttm: initialize globals during device init (v2)"
> >>>     Revert "drm/ttm: Fix bo_global and mem_global kfree error"
> >>>     Revert "drm/ttm: use a static ttm_bo_global instance"
> >>>     Revert "drm/ttm: make the device list mutex static"
> >>>     Revert "drm/ttm: use a static ttm_mem_global instance"
> >>>
> >>>    drivers/gpu/drm/Makefile                      |   2 +-
> >>>    drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 103 ++++++++++++-
> >>>    drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |   2 +
> >>>    drivers/gpu/drm/ast/ast_drv.h                 |   2 +
> >>>    drivers/gpu/drm/ast/ast_ttm.c                 |  64 ++++++++
> >>>    drivers/gpu/drm/bochs/bochs.h                 |   2 +
> >>>    drivers/gpu/drm/bochs/bochs_mm.c              |  61 ++++++++
> >>>    drivers/gpu/drm/cirrus/cirrus_drv.h           |   2 +
> >>>    drivers/gpu/drm/cirrus/cirrus_ttm.c           |  64 ++++++++
> >>>    drivers/gpu/drm/drm_drv.c                     |   2 +
> >>>    drivers/gpu/drm/drm_global.c                  | 137 ++++++++++++++++++
> >>>    .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h   |   2 +
> >>>    drivers/gpu/drm/hisilicon/hibmc/hibmc_ttm.c   |  57 ++++++++
> >>>    drivers/gpu/drm/mgag200/mgag200_drv.h         |   2 +
> >>>    drivers/gpu/drm/mgag200/mgag200_ttm.c         |  64 ++++++++
> >>>    drivers/gpu/drm/nouveau/nouveau_drv.h         |   2 +
> >>>    drivers/gpu/drm/nouveau/nouveau_ttm.c         |  67 +++++++++
> >>>    drivers/gpu/drm/qxl/qxl_drv.h                 |   3 +
> >>>    drivers/gpu/drm/qxl/qxl_ttm.c                 |  61 ++++++++
> >>>    drivers/gpu/drm/radeon/radeon.h               |   3 +
> >>>    drivers/gpu/drm/radeon/radeon_ttm.c           |  65 +++++++++
> >>>    drivers/gpu/drm/ttm/ttm_bo.c                  |  67 +++------
> >>>    drivers/gpu/drm/ttm/ttm_memory.c              |  14 +-
> >>>    drivers/gpu/drm/virtio/virtgpu_drv.h          |   3 +
> >>>    drivers/gpu/drm/virtio/virtgpu_ttm.c          |  62 ++++++++
> >>>    drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  15 +-
> >>>    drivers/gpu/drm/vmwgfx/vmwgfx_drv.h           |   6 +-
> >>>    drivers/gpu/drm/vmwgfx/vmwgfx_ttm_glue.c      |  54 +++++++
> >>>    drivers/staging/vboxvideo/vbox_drv.h          |   2 +
> >>>    drivers/staging/vboxvideo/vbox_ttm.c          |  65 ++++++++-
> >>>    include/drm/drmP.h                            |   1 +
> >>>    include/drm/drm_global.h                      |  53 +++++++
> >>>    include/drm/ttm/ttm_bo_driver.h               |  54 ++++++-
> >>>    include/drm/ttm/ttm_memory.h                  |   4 +-
> >>>    34 files changed, 1102 insertions(+), 65 deletions(-)
> >>>    create mode 100644 drivers/gpu/drm/drm_global.c
> >>>    create mode 100644 include/drm/drm_global.h
> >>>
> >>
>
>
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16 10:54       ` Karol Herbst
@ 2019-04-16 11:06         ` Koenig, Christian
  2019-04-16 11:20           ` Karol Herbst
  2019-04-16 12:18           ` Daniel Vetter
  0 siblings, 2 replies; 18+ messages in thread
From: Koenig, Christian @ 2019-04-16 11:06 UTC (permalink / raw)
  To: Karol Herbst; +Cc: Deucher, Alexander, dri-devel

Am 16.04.19 um 12:54 schrieb Karol Herbst:
> On Tue, Apr 16, 2019 at 11:12 AM Koenig, Christian
> <Christian.Koenig@amd.com> wrote:
>> Am 16.04.19 um 11:10 schrieb Karol Herbst:
>>> On Tue, Apr 16, 2019 at 8:38 AM Christian König
>>> <ckoenig.leichtzumerken@gmail.com> wrote:
>>>> Am 16.04.19 um 02:35 schrieb Karol Herbst:
>>>>> Kobjects are supposed to be dynamically allocated, but with recent changes
>>>>> this rule was violated. Reverting those commits fixes crashes when a drm
>>>>> driver using TTM gets loaded again.
>>>>>
>>>>> The object in question is "ttm_mem_glob" declared inside
>>>>> "include/drm/ttm/ttm_memory.h" and instatiated inside
>>>>> "drivers/gpu/drm/ttm/ttm_memory.c".
>>>>>
>>>>> from "Documentation/kobject.txt":
>>>>> "Because kobjects are dynamic, they must not be declared statically or on
>>>>> the stack, but instead, always allocated dynamically.  Future versions of
>>>>> the kernel will contain a run-time check for kobjects that are created
>>>>> statically and will warn the developer of this improper usage."
>>>>>
>>>>> Unloading ttm before reloading the driver workarounds that crash, because
>>>>> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
>>>>> and kobject_put function never free or clean up the kobject object leaving
>>>>> it in an undefined state.
>>>>>
>>>>> I reverted a few more commits to make it less painful for me to rever this
>>>>> rather big change.
>>>> Well, NAK. By reverting those change you also re-introduced the problems
>>>> we originally fixed with those patches.
>>>>
>>>> Please work on a proper fix instead,
>>>> Christian.
>>> And which problem was that besides duplicated code (or maybe even a
>>> bit of memory consumption if multiple ttm driver were used)? If I had
>>> to choose between duplicated code and a crash, I choose the former.
>>>
>>> Maybe I missed the real reason why those changes are made, but the
>>> commit messages don't really seem to tell me.
>> The old implementation crashed because different drivers tried to
>> allocate the same kobj.
>>
>> Crashing in one way is not better than crashing in another way.
>>
>> Christian.
>>
> how can that old crash be triggered? By loading two ttm based drivers
> at the same time or by other means?

Yes, exactly. Even worse it could be triggered by one driver 
instantiating multiple times at the same time, e.g two AMD GPUs in the 
same system.

On the other hand I completely agree that using kobj static is 
completely nuts. The problem is that using a kobj was the wrong approach 
in the first place.

In other words when you have something which controls a global behavior 
of a module, what do you use? Right, a module parameter.

Point is that we can't get away from those kobj without breaking the 
uapi, so that is something which can't be done easily.

Regards,
Christian.
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16 11:06         ` Koenig, Christian
@ 2019-04-16 11:20           ` Karol Herbst
  2019-04-16 11:25             ` Christian König
  2019-04-16 12:18           ` Daniel Vetter
  1 sibling, 1 reply; 18+ messages in thread
From: Karol Herbst @ 2019-04-16 11:20 UTC (permalink / raw)
  To: Koenig, Christian; +Cc: Deucher, Alexander, dri-devel

On Tue, Apr 16, 2019 at 1:07 PM Koenig, Christian
<Christian.Koenig@amd.com> wrote:
>
> Am 16.04.19 um 12:54 schrieb Karol Herbst:
> > On Tue, Apr 16, 2019 at 11:12 AM Koenig, Christian
> > <Christian.Koenig@amd.com> wrote:
> >> Am 16.04.19 um 11:10 schrieb Karol Herbst:
> >>> On Tue, Apr 16, 2019 at 8:38 AM Christian König
> >>> <ckoenig.leichtzumerken@gmail.com> wrote:
> >>>> Am 16.04.19 um 02:35 schrieb Karol Herbst:
> >>>>> Kobjects are supposed to be dynamically allocated, but with recent changes
> >>>>> this rule was violated. Reverting those commits fixes crashes when a drm
> >>>>> driver using TTM gets loaded again.
> >>>>>
> >>>>> The object in question is "ttm_mem_glob" declared inside
> >>>>> "include/drm/ttm/ttm_memory.h" and instatiated inside
> >>>>> "drivers/gpu/drm/ttm/ttm_memory.c".
> >>>>>
> >>>>> from "Documentation/kobject.txt":
> >>>>> "Because kobjects are dynamic, they must not be declared statically or on
> >>>>> the stack, but instead, always allocated dynamically.  Future versions of
> >>>>> the kernel will contain a run-time check for kobjects that are created
> >>>>> statically and will warn the developer of this improper usage."
> >>>>>
> >>>>> Unloading ttm before reloading the driver workarounds that crash, because
> >>>>> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
> >>>>> and kobject_put function never free or clean up the kobject object leaving
> >>>>> it in an undefined state.
> >>>>>
> >>>>> I reverted a few more commits to make it less painful for me to rever this
> >>>>> rather big change.
> >>>> Well, NAK. By reverting those change you also re-introduced the problems
> >>>> we originally fixed with those patches.
> >>>>
> >>>> Please work on a proper fix instead,
> >>>> Christian.
> >>> And which problem was that besides duplicated code (or maybe even a
> >>> bit of memory consumption if multiple ttm driver were used)? If I had
> >>> to choose between duplicated code and a crash, I choose the former.
> >>>
> >>> Maybe I missed the real reason why those changes are made, but the
> >>> commit messages don't really seem to tell me.
> >> The old implementation crashed because different drivers tried to
> >> allocate the same kobj.
> >>
> >> Crashing in one way is not better than crashing in another way.
> >>
> >> Christian.
> >>
> > how can that old crash be triggered? By loading two ttm based drivers
> > at the same time or by other means?
>
> Yes, exactly. Even worse it could be triggered by one driver
> instantiating multiple times at the same time, e.g two AMD GPUs in the
> same system.
>
> On the other hand I completely agree that using kobj static is
> completely nuts. The problem is that using a kobj was the wrong approach
> in the first place.
>
> In other words when you have something which controls a global behavior
> of a module, what do you use? Right, a module parameter.
>
> Point is that we can't get away from those kobj without breaking the
> uapi, so that is something which can't be done easily.
>
> Regards,
> Christian.

okay, thanks for the explanation!

Currently I will be testing your first patch you sent out on top of
5.0.7, so that we could propose that for stable. The second didn't
apply, but it didn't look relevant for the actual fix. Will reply to
the patch with my results then.

Thanks
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16 11:20           ` Karol Herbst
@ 2019-04-16 11:25             ` Christian König
  0 siblings, 0 replies; 18+ messages in thread
From: Christian König @ 2019-04-16 11:25 UTC (permalink / raw)
  To: Karol Herbst, Koenig, Christian; +Cc: Deucher, Alexander, dri-devel

Am 16.04.19 um 13:20 schrieb Karol Herbst:
> On Tue, Apr 16, 2019 at 1:07 PM Koenig, Christian
> <Christian.Koenig@amd.com> wrote:
>> Am 16.04.19 um 12:54 schrieb Karol Herbst:
>>> On Tue, Apr 16, 2019 at 11:12 AM Koenig, Christian
>>> <Christian.Koenig@amd.com> wrote:
>>>> Am 16.04.19 um 11:10 schrieb Karol Herbst:
>>>>> On Tue, Apr 16, 2019 at 8:38 AM Christian König
>>>>> <ckoenig.leichtzumerken@gmail.com> wrote:
>>>>>> Am 16.04.19 um 02:35 schrieb Karol Herbst:
>>>>>>> Kobjects are supposed to be dynamically allocated, but with recent changes
>>>>>>> this rule was violated. Reverting those commits fixes crashes when a drm
>>>>>>> driver using TTM gets loaded again.
>>>>>>>
>>>>>>> The object in question is "ttm_mem_glob" declared inside
>>>>>>> "include/drm/ttm/ttm_memory.h" and instatiated inside
>>>>>>> "drivers/gpu/drm/ttm/ttm_memory.c".
>>>>>>>
>>>>>>> from "Documentation/kobject.txt":
>>>>>>> "Because kobjects are dynamic, they must not be declared statically or on
>>>>>>> the stack, but instead, always allocated dynamically.  Future versions of
>>>>>>> the kernel will contain a run-time check for kobjects that are created
>>>>>>> statically and will warn the developer of this improper usage."
>>>>>>>
>>>>>>> Unloading ttm before reloading the driver workarounds that crash, because
>>>>>>> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
>>>>>>> and kobject_put function never free or clean up the kobject object leaving
>>>>>>> it in an undefined state.
>>>>>>>
>>>>>>> I reverted a few more commits to make it less painful for me to rever this
>>>>>>> rather big change.
>>>>>> Well, NAK. By reverting those change you also re-introduced the problems
>>>>>> we originally fixed with those patches.
>>>>>>
>>>>>> Please work on a proper fix instead,
>>>>>> Christian.
>>>>> And which problem was that besides duplicated code (or maybe even a
>>>>> bit of memory consumption if multiple ttm driver were used)? If I had
>>>>> to choose between duplicated code and a crash, I choose the former.
>>>>>
>>>>> Maybe I missed the real reason why those changes are made, but the
>>>>> commit messages don't really seem to tell me.
>>>> The old implementation crashed because different drivers tried to
>>>> allocate the same kobj.
>>>>
>>>> Crashing in one way is not better than crashing in another way.
>>>>
>>>> Christian.
>>>>
>>> how can that old crash be triggered? By loading two ttm based drivers
>>> at the same time or by other means?
>> Yes, exactly. Even worse it could be triggered by one driver
>> instantiating multiple times at the same time, e.g two AMD GPUs in the
>> same system.
>>
>> On the other hand I completely agree that using kobj static is
>> completely nuts. The problem is that using a kobj was the wrong approach
>> in the first place.
>>
>> In other words when you have something which controls a global behavior
>> of a module, what do you use? Right, a module parameter.
>>
>> Point is that we can't get away from those kobj without breaking the
>> uapi, so that is something which can't be done easily.
>>
>> Regards,
>> Christian.
> okay, thanks for the explanation!
>
> Currently I will be testing your first patch you sent out on top of
> 5.0.7, so that we could propose that for stable. The second didn't
> apply, but it didn't look relevant for the actual fix. Will reply to
> the patch with my results then.

The second is just a further clean, please ignore that one for backporting.

Let me know if the first one works on 5.0.7, if yes I'm going to add a 
CC stable tag before pushing.

Thanks,
Christian.

>
> Thanks
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16 11:06         ` Koenig, Christian
  2019-04-16 11:20           ` Karol Herbst
@ 2019-04-16 12:18           ` Daniel Vetter
  2019-04-16 12:29             ` Koenig, Christian
  1 sibling, 1 reply; 18+ messages in thread
From: Daniel Vetter @ 2019-04-16 12:18 UTC (permalink / raw)
  To: Koenig, Christian; +Cc: Deucher, Alexander, dri-devel, Karol Herbst

On Tue, Apr 16, 2019 at 11:06:54AM +0000, Koenig, Christian wrote:
> Am 16.04.19 um 12:54 schrieb Karol Herbst:
> > On Tue, Apr 16, 2019 at 11:12 AM Koenig, Christian
> > <Christian.Koenig@amd.com> wrote:
> >> Am 16.04.19 um 11:10 schrieb Karol Herbst:
> >>> On Tue, Apr 16, 2019 at 8:38 AM Christian König
> >>> <ckoenig.leichtzumerken@gmail.com> wrote:
> >>>> Am 16.04.19 um 02:35 schrieb Karol Herbst:
> >>>>> Kobjects are supposed to be dynamically allocated, but with recent changes
> >>>>> this rule was violated. Reverting those commits fixes crashes when a drm
> >>>>> driver using TTM gets loaded again.
> >>>>>
> >>>>> The object in question is "ttm_mem_glob" declared inside
> >>>>> "include/drm/ttm/ttm_memory.h" and instatiated inside
> >>>>> "drivers/gpu/drm/ttm/ttm_memory.c".
> >>>>>
> >>>>> from "Documentation/kobject.txt":
> >>>>> "Because kobjects are dynamic, they must not be declared statically or on
> >>>>> the stack, but instead, always allocated dynamically.  Future versions of
> >>>>> the kernel will contain a run-time check for kobjects that are created
> >>>>> statically and will warn the developer of this improper usage."
> >>>>>
> >>>>> Unloading ttm before reloading the driver workarounds that crash, because
> >>>>> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
> >>>>> and kobject_put function never free or clean up the kobject object leaving
> >>>>> it in an undefined state.
> >>>>>
> >>>>> I reverted a few more commits to make it less painful for me to rever this
> >>>>> rather big change.
> >>>> Well, NAK. By reverting those change you also re-introduced the problems
> >>>> we originally fixed with those patches.
> >>>>
> >>>> Please work on a proper fix instead,
> >>>> Christian.
> >>> And which problem was that besides duplicated code (or maybe even a
> >>> bit of memory consumption if multiple ttm driver were used)? If I had
> >>> to choose between duplicated code and a crash, I choose the former.
> >>>
> >>> Maybe I missed the real reason why those changes are made, but the
> >>> commit messages don't really seem to tell me.
> >> The old implementation crashed because different drivers tried to
> >> allocate the same kobj.
> >>
> >> Crashing in one way is not better than crashing in another way.
> >>
> >> Christian.
> >>
> > how can that old crash be triggered? By loading two ttm based drivers
> > at the same time or by other means?
> 
> Yes, exactly. Even worse it could be triggered by one driver 
> instantiating multiple times at the same time, e.g two AMD GPUs in the 
> same system.
> 
> On the other hand I completely agree that using kobj static is 
> completely nuts. The problem is that using a kobj was the wrong approach 
> in the first place.
> 
> In other words when you have something which controls a global behavior 
> of a module, what do you use? Right, a module parameter.
> 
> Point is that we can't get away from those kobj without breaking the 
> uapi, so that is something which can't be done easily.

Randome idea: Push the kobj setup into drm.ko (and shrugg it off as
another lesson in how maybe uapi shouldn't have been designed, but hey not
our worst mistake by far). I think that'd be totally ok.
-Daniel
-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16 12:18           ` Daniel Vetter
@ 2019-04-16 12:29             ` Koenig, Christian
  0 siblings, 0 replies; 18+ messages in thread
From: Koenig, Christian @ 2019-04-16 12:29 UTC (permalink / raw)
  To: Daniel Vetter; +Cc: Deucher, Alexander, dri-devel, Karol Herbst

Am 16.04.19 um 14:18 schrieb Daniel Vetter:
> On Tue, Apr 16, 2019 at 11:06:54AM +0000, Koenig, Christian wrote:
>> Am 16.04.19 um 12:54 schrieb Karol Herbst:
>>> On Tue, Apr 16, 2019 at 11:12 AM Koenig, Christian
>>> <Christian.Koenig@amd.com> wrote:
>>>> Am 16.04.19 um 11:10 schrieb Karol Herbst:
>>>>> On Tue, Apr 16, 2019 at 8:38 AM Christian König
>>>>> <ckoenig.leichtzumerken@gmail.com> wrote:
>>>>>> Am 16.04.19 um 02:35 schrieb Karol Herbst:
>>>>>>> Kobjects are supposed to be dynamically allocated, but with recent changes
>>>>>>> this rule was violated. Reverting those commits fixes crashes when a drm
>>>>>>> driver using TTM gets loaded again.
>>>>>>>
>>>>>>> The object in question is "ttm_mem_glob" declared inside
>>>>>>> "include/drm/ttm/ttm_memory.h" and instatiated inside
>>>>>>> "drivers/gpu/drm/ttm/ttm_memory.c".
>>>>>>>
>>>>>>> from "Documentation/kobject.txt":
>>>>>>> "Because kobjects are dynamic, they must not be declared statically or on
>>>>>>> the stack, but instead, always allocated dynamically.  Future versions of
>>>>>>> the kernel will contain a run-time check for kobjects that are created
>>>>>>> statically and will warn the developer of this improper usage."
>>>>>>>
>>>>>>> Unloading ttm before reloading the driver workarounds that crash, because
>>>>>>> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
>>>>>>> and kobject_put function never free or clean up the kobject object leaving
>>>>>>> it in an undefined state.
>>>>>>>
>>>>>>> I reverted a few more commits to make it less painful for me to rever this
>>>>>>> rather big change.
>>>>>> Well, NAK. By reverting those change you also re-introduced the problems
>>>>>> we originally fixed with those patches.
>>>>>>
>>>>>> Please work on a proper fix instead,
>>>>>> Christian.
>>>>> And which problem was that besides duplicated code (or maybe even a
>>>>> bit of memory consumption if multiple ttm driver were used)? If I had
>>>>> to choose between duplicated code and a crash, I choose the former.
>>>>>
>>>>> Maybe I missed the real reason why those changes are made, but the
>>>>> commit messages don't really seem to tell me.
>>>> The old implementation crashed because different drivers tried to
>>>> allocate the same kobj.
>>>>
>>>> Crashing in one way is not better than crashing in another way.
>>>>
>>>> Christian.
>>>>
>>> how can that old crash be triggered? By loading two ttm based drivers
>>> at the same time or by other means?
>> Yes, exactly. Even worse it could be triggered by one driver
>> instantiating multiple times at the same time, e.g two AMD GPUs in the
>> same system.
>>
>> On the other hand I completely agree that using kobj static is
>> completely nuts. The problem is that using a kobj was the wrong approach
>> in the first place.
>>
>> In other words when you have something which controls a global behavior
>> of a module, what do you use? Right, a module parameter.
>>
>> Point is that we can't get away from those kobj without breaking the
>> uapi, so that is something which can't be done easily.
> Randome idea: Push the kobj setup into drm.ko (and shrugg it off as
> another lesson in how maybe uapi shouldn't have been designed, but hey not
> our worst mistake by far). I think that'd be totally ok.

Yeah, thought about that as well.

But I would rather re-design this from the scratch instead of just 
moving it over.

And yes I agree with a bit of luck that UAPI is actually not used at 
all, so we could remove it sooner or later.

Regards,
Christian.

> -Daniel

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16  6:38 ` [PATCH 0/6] Fix crash after reloading a driver using ttm Christian König
  2019-04-16  9:10   ` Karol Herbst
@ 2019-04-16 23:09   ` Eric Anholt
  2019-04-17  0:10     ` Karol Herbst
  1 sibling, 1 reply; 18+ messages in thread
From: Eric Anholt @ 2019-04-16 23:09 UTC (permalink / raw)
  To: christian.koenig, Karol Herbst, dri-devel; +Cc: Alex Deucher


[-- Attachment #1.1: Type: text/plain, Size: 2541 bytes --]

Christian König <ckoenig.leichtzumerken@gmail.com> writes:

> Am 16.04.19 um 02:35 schrieb Karol Herbst:
>> Kobjects are supposed to be dynamically allocated, but with recent changes
>> this rule was violated. Reverting those commits fixes crashes when a drm
>> driver using TTM gets loaded again.
>>
>> The object in question is "ttm_mem_glob" declared inside
>> "include/drm/ttm/ttm_memory.h" and instatiated inside
>> "drivers/gpu/drm/ttm/ttm_memory.c".
>>
>> from "Documentation/kobject.txt":
>> "Because kobjects are dynamic, they must not be declared statically or on
>> the stack, but instead, always allocated dynamically.  Future versions of
>> the kernel will contain a run-time check for kobjects that are created
>> statically and will warn the developer of this improper usage."
>>
>> Unloading ttm before reloading the driver workarounds that crash, because
>> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
>> and kobject_put function never free or clean up the kobject object leaving
>> it in an undefined state.
>>
>> I reverted a few more commits to make it less painful for me to rever this
>> rather big change.
>
> Well, NAK. By reverting those change you also re-introduced the problems 
> we originally fixed with those patches.
>
> Please work on a proper fix instead,

That's not Karol's responsibility, that's yours as the author.  I would
like to remind about Linux's regressions policy, quoting from
Documentation/process/4.Coding.rst:

"One final hazard worth mentioning is this: it can be tempting to make a
change (which may bring big improvements) which causes something to break
for existing users.  This kind of change is called a "regression," and
regressions have become most unwelcome in the mainline kernel.  With few
exceptions, changes which cause regressions will be backed out if the
regression cannot be fixed in a timely manner.  Far better to avoid the
regression in the first place.

It is often argued that a regression can be justified if it causes things
to work for more people than it creates problems for.  Why not make a
change if it brings new functionality to ten systems for each one it
breaks?  The best answer to this question was expressed by Linus in July,
2007:

::

	So we don't fix bugs by introducing new problems.  That way lies
	madness, and nobody ever knows if you actually make any real
	progress at all. Is it two steps forwards, one step back, or one
	step forward and two steps back?"

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

[-- Attachment #2: Type: text/plain, Size: 159 bytes --]

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/6] Fix crash after reloading a driver using ttm
  2019-04-16 23:09   ` Eric Anholt
@ 2019-04-17  0:10     ` Karol Herbst
  0 siblings, 0 replies; 18+ messages in thread
From: Karol Herbst @ 2019-04-17  0:10 UTC (permalink / raw)
  To: Eric Anholt; +Cc: Alex Deucher, Koenig, Christian, dri-devel

On Wed, Apr 17, 2019 at 1:09 AM Eric Anholt <eric@anholt.net> wrote:
>
> Christian König <ckoenig.leichtzumerken@gmail.com> writes:
>
> > Am 16.04.19 um 02:35 schrieb Karol Herbst:
> >> Kobjects are supposed to be dynamically allocated, but with recent changes
> >> this rule was violated. Reverting those commits fixes crashes when a drm
> >> driver using TTM gets loaded again.
> >>
> >> The object in question is "ttm_mem_glob" declared inside
> >> "include/drm/ttm/ttm_memory.h" and instatiated inside
> >> "drivers/gpu/drm/ttm/ttm_memory.c".
> >>
> >> from "Documentation/kobject.txt":
> >> "Because kobjects are dynamic, they must not be declared statically or on
> >> the stack, but instead, always allocated dynamically.  Future versions of
> >> the kernel will contain a run-time check for kobjects that are created
> >> statically and will warn the developer of this improper usage."
> >>
> >> Unloading ttm before reloading the driver workarounds that crash, because
> >> the memory backing the kobject member "kobj" is cleaned up. The kobject_del
> >> and kobject_put function never free or clean up the kobject object leaving
> >> it in an undefined state.
> >>
> >> I reverted a few more commits to make it less painful for me to rever this
> >> rather big change.
> >
> > Well, NAK. By reverting those change you also re-introduced the problems
> > we originally fixed with those patches.
> >
> > Please work on a proper fix instead,
>
> That's not Karol's responsibility, that's yours as the author.  I would
> like to remind about Linux's regressions policy, quoting from
> Documentation/process/4.Coding.rst:
>
> "One final hazard worth mentioning is this: it can be tempting to make a
> change (which may bring big improvements) which causes something to break
> for existing users.  This kind of change is called a "regression," and
> regressions have become most unwelcome in the mainline kernel.  With few
> exceptions, changes which cause regressions will be backed out if the
> regression cannot be fixed in a timely manner.  Far better to avoid the
> regression in the first place.
>
> It is often argued that a regression can be justified if it causes things
> to work for more people than it creates problems for.  Why not make a
> change if it brings new functionality to ten systems for each one it
> breaks?  The best answer to this question was expressed by Linus in July,
> 2007:
>
> ::
>
>         So we don't fix bugs by introducing new problems.  That way lies
>         madness, and nobody ever knows if you actually make any real
>         progress at all. Is it two steps forwards, one step back, or one
>         step forward and two steps back?"

he already wrote a fix. Search for "[PATCH 1/2] drm/ttm: fix re-init
of global structures"
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2019-04-17  0:11 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-16  0:35 [PATCH 0/6] Fix crash after reloading a driver using ttm Karol Herbst
2019-04-16  0:35 ` [PATCH 1/6] Revert "drm: Remove drm_global.{c,h} v2" Karol Herbst
2019-04-16  0:35 ` [PATCH 2/6] Revert "drm/ttm: initialize globals during device init (v2)" Karol Herbst
2019-04-16  0:35 ` [PATCH 3/6] Revert "drm/ttm: Fix bo_global and mem_global kfree error" Karol Herbst
2019-04-16  0:35 ` [PATCH 4/6] Revert "drm/ttm: use a static ttm_bo_global instance" Karol Herbst
2019-04-16  0:35 ` [PATCH 5/6] Revert "drm/ttm: make the device list mutex static" Karol Herbst
2019-04-16  0:35 ` [PATCH 6/6] Revert "drm/ttm: use a static ttm_mem_global instance" Karol Herbst
2019-04-16  6:38 ` [PATCH 0/6] Fix crash after reloading a driver using ttm Christian König
2019-04-16  9:10   ` Karol Herbst
2019-04-16  9:12     ` Koenig, Christian
2019-04-16 10:54       ` Karol Herbst
2019-04-16 11:06         ` Koenig, Christian
2019-04-16 11:20           ` Karol Herbst
2019-04-16 11:25             ` Christian König
2019-04-16 12:18           ` Daniel Vetter
2019-04-16 12:29             ` Koenig, Christian
2019-04-16 23:09   ` Eric Anholt
2019-04-17  0:10     ` Karol Herbst

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.