Compare

170aeff46fb54f41545c85a72aec525aebc79d44..de9f6bcdb44f0e410031002e2f103826ea6b6259

drm/amdgpu: don't drop the runtime PM reference twice in amdgpu_gpr

amdgpu_debugfs_gpr_read() drops its runtime PM reference right after
reading the wave registers. When copying them to userspace fails, it
jumps to the err label, which drops the reference a second time. A read
into a bad buffer on Navi31 either steals a reference somebody else
holds (power/runtime_usage goes from 1 to 0) or underflows the count:

  amdgpu 0000:03:00.0: Runtime PM usage count underflow!

The registers are already in the kernel buffer at that point, so finish
the hardware access first and copy the buffer with a single
copy_to_user() instead of the put_user() loop with its own error path.

Fixes: 9eee152aab56 ("drm/amdgpu/debugfs: fix ref count leak when pm_runtime_get_sync fails")
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
(cherry picked from commit 31035bbd74f7c3e10c163db875b8e67f8b50ee6a)
Cc: stable@vger.kernel.org

Jesse Zhang <Jesse.Zhang@amd.com> · 2026-10-09 05:21 UTC · committed by Alex Deucher

Browse files
11 files changed, +89 −70
modified drivers/gpio/gpio-mxc.c
@@ -7,7 +7,6 @@
7 7 // Authors: Daniel Mack, Juergen Beisert.
8 8 // Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
9 9
10 - #include <linux/bitops.h>
11 10 #include <linux/cleanup.h>
12 11 #include <linux/clk.h>
13 12 #include <linux/err.h>
@@ -69,11 +68,11 @@ struct mxc_gpio_port {
69 68 struct irq_domain *domain;
70 69 struct gpio_generic_chip gen_gc;
71 70 struct device *dev;
72 - unsigned long both_edges;
71 + u32 both_edges;
73 72 struct mxc_gpio_reg_saved gpio_saved_reg;
74 73 bool power_off;
74 + u32 wakeup_pads;
75 75 bool is_pad_wakeup;
76 - unsigned long wakeup_pads;
77 76 u32 pad_type[32];
78 77 const struct mxc_gpio_hwdata *hwdata;
79 78 };
@@ -169,7 +168,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
169 168 int edge;
170 169 void __iomem *reg = port->base;
171 170
172 - clear_bit(gpio_idx, &port->both_edges);
171 + port->both_edges &= ~(1 << gpio_idx);
173 172 switch (type) {
174 173 case IRQ_TYPE_EDGE_RISING:
175 174 edge = GPIO_INT_RISE_EDGE;
@@ -189,7 +188,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type)
189 188 edge = GPIO_INT_HIGH_LEV;
190 189 pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx);
191 190 }
192 - set_bit(gpio_idx, &port->both_edges);
191 + port->both_edges |= 1 << gpio_idx;
193 192 }
194 193 break;
195 194 case IRQ_TYPE_LEVEL_LOW:
@@ -260,7 +259,7 @@ static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
260 259 while (irq_stat != 0) {
261 260 int irqoffset = fls(irq_stat) - 1;
262 261
263 - if (test_bit(irqoffset, &port->both_edges))
262 + if (port->both_edges & (1 << irqoffset))
264 263 mxc_flip_edge(port, irqoffset);
265 264
266 265 generic_handle_domain_irq(port->domain, irqoffset);
@@ -331,18 +330,16 @@ static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
331 330 ret = enable_irq_wake(port->irq_high);
332 331 else
333 332 ret = enable_irq_wake(port->irq);
333 + port->wakeup_pads |= BIT(gpio_idx);
334 334 } else {
335 335 if (port->irq_high && (gpio_idx >= 16))
336 336 ret = disable_irq_wake(port->irq_high);
337 337 else
338 338 ret = disable_irq_wake(port->irq);
339 + port->wakeup_pads &= ~BIT(gpio_idx);
339 340 }
340 - if (ret)
341 - return ret;
342 -
343 - assign_bit(gpio_idx, &port->wakeup_pads, enable);
344 341
345 - return 0;
342 + return ret;
346 343 }
347 344
348 345 static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base)
@@ -477,6 +474,8 @@ static int mxc_gpio_probe(struct platform_device *pdev)
477 474 } else
478 475 port->mx_irq_handler = mx3_gpio_irq_handler;
479 476
477 + mxc_update_irq_chained_handler(port, true);
478 +
480 479 config.dev = &pdev->dev;
481 480 config.sz = 4;
482 481 config.dat = port->base + GPIO_PSR;
@@ -526,8 +525,6 @@ static int mxc_gpio_probe(struct platform_device *pdev)
526 525
527 526 list_add_tail(&port->node, &mxc_gpio_ports);
528 527
529 - mxc_update_irq_chained_handler(port, true);
530 -
531 528 platform_set_drvdata(pdev, port);
532 529 pm_runtime_put_autosuspend(&pdev->dev);
533 530
modified drivers/gpio/gpio-nct6694.c
@@ -444,7 +444,7 @@ static int nct6694_gpio_probe(struct platform_device *pdev)
444 444 data->gpio.set_config = nct6694_set_config;
445 445 data->gpio.init_valid_mask = nct6694_init_valid_mask;
446 446 data->gpio.base = -1;
447 - data->gpio.can_sleep = true;
447 + data->gpio.can_sleep = false;
448 448 data->gpio.owner = THIS_MODULE;
449 449 data->gpio.ngpio = NCT6694_NR_GPIO;
450 450
modified drivers/gpio/gpiolib-kunit.c
@@ -28,8 +28,6 @@
28 28
29 29 #define GPIO_TEST_PROVIDER_NGPIO 4
30 30
31 - KUNIT_DEFINE_ACTION_WRAPPER(gpio_device_put_wrapper, gpio_device_put, struct gpio_device *);
32 -
33 31 /*
34 32 * The test provider tracks per-line direction and value so that lines can be
35 33 * driven as both inputs and outputs - this is needed to exercise input as well
@@ -772,7 +770,6 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset,
772 770 struct property_entry properties[2];
773 771 struct platform_device *pdev;
774 772 struct fwnode_handle *fwnode;
775 - struct gpio_device *gdev;
776 773 struct gpio_desc *desc;
777 774 bool bound = true;
778 775 int ret;
@@ -792,12 +789,10 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset,
792 789 wait_for_device_probe();
793 790
794 791 /* The hog must have configured the line with the expected direction. */
795 - gdev = gpio_device_find_by_label(GPIO_CONSUMER_NAME);
792 + struct gpio_device *gdev __free(gpio_device_put) =
793 + gpio_device_find_by_label(GPIO_CONSUMER_NAME);
796 794 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gdev);
797 795
798 - ret = kunit_add_action_or_reset(test, gpio_device_put_wrapper, gdev);
799 - KUNIT_ASSERT_EQ(test, ret, 0);
800 -
801 796 desc = gpio_device_get_desc(gdev, offset);
802 797 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
803 798
modified drivers/gpio/gpiolib-shared.c
@@ -449,14 +449,10 @@ static bool gpio_shared_dev_is_reset_gpio(struct device *consumer,
449 449 int gpio_shared_add_proxy_lookup(struct device *consumer, struct fwnode_handle *fwnode,
450 450 const char *con_id, unsigned long lflags)
451 451 {
452 + const char *dev_id = dev_name(consumer);
453 + struct gpiod_lookup_table *lookup;
452 454 struct gpio_shared_entry *entry;
453 455 struct gpio_shared_ref *ref;
454 - const char *dev_id;
455 -
456 - if (!consumer)
457 - return -EOPNOTSUPP;
458 -
459 - dev_id = dev_name(consumer);
460 456
461 457 list_for_each_entry(entry, &gpio_shared_list, list) {
462 458 list_for_each_entry(ref, &entry->refs, list) {
@@ -484,22 +480,18 @@ int gpio_shared_add_proxy_lookup(struct device *consumer, struct fwnode_handle *
484 480 if (!key)
485 481 return -ENOMEM;
486 482
487 - struct gpiod_lookup_table *lookup __free(kfree) =
488 - kzalloc_flex(*lookup, table, 2);
483 + lookup = kzalloc_flex(*lookup, table, 2);
489 484 if (!lookup)
490 485 return -ENOMEM;
491 486
492 487 pr_debug("Adding machine lookup entry for a shared GPIO for consumer %s, with key '%s' and con_id '%s'\n",
493 488 dev_id, key, ref->con_id ?: "none");
494 489
495 - lookup->dev_id = kstrdup(dev_id, GFP_KERNEL);
496 - if (!lookup->dev_id)
497 - return -ENOMEM;
498 -
490 + lookup->dev_id = dev_id;
499 491 lookup->table[0] = GPIO_LOOKUP(no_free_ptr(key), 0,
500 492 ref->con_id, lflags);
501 493
502 - ref->lookup = no_free_ptr(lookup);
494 + ref->lookup = lookup;
503 495 gpiod_add_lookup_table(ref->lookup);
504 496
505 497 return 0;
@@ -619,7 +611,6 @@ void gpio_device_teardown_shared(struct gpio_device *gdev)
619 611 if (ref->lookup) {
620 612 gpiod_remove_lookup_table(ref->lookup);
621 613 kfree(ref->lookup->table[0].key);
622 - kfree(ref->lookup->dev_id);
623 614 kfree(ref->lookup);
624 615 ref->lookup = NULL;
625 616 }
modified drivers/gpio/gpiolib.c
@@ -3954,17 +3954,16 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3954 3954 } while ((i < array_size) &&
3955 3955 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc));
3956 3956 /* push collected bits to outputs */
3957 - ret = 0;
3958 - if (count != 0)
3957 + if (count != 0) {
3959 3958 ret = gpiochip_set_multiple(guard.gc, mask, bits);
3959 + if (ret)
3960 + return ret;
3961 + }
3960 3962
3961 3963 if (mask != fastpath_mask)
3962 3964 bitmap_free(mask);
3963 3965 if (bits != fastpath_bits)
3964 3966 bitmap_free(bits);
3965 -
3966 - if (ret)
3967 - return ret;
3968 3967 }
3969 3968 return 0;
3970 3969 }
modified drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c
@@ -1266,7 +1266,6 @@ static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
1266 1266 {
1267 1267 struct amdgpu_device *adev = f->f_inode->i_private;
1268 1268 int r;
1269 - ssize_t result = 0;
1270 1269 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
1271 1270
1272 1271 if (size > 4096 || size & 3 || *pos & 3)
@@ -1310,25 +1309,11 @@ static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
1310 1309 mutex_unlock(&adev->grbm_idx_mutex);
1311 1310
1312 1311 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1312 + amdgpu_virt_disable_access_debugfs(adev);
1313 1313
1314 - while (size) {
1315 - uint32_t value;
1316 -
1317 - value = data[result >> 2];
1318 - r = put_user(value, (uint32_t *)buf);
1319 - if (r) {
1320 - amdgpu_virt_disable_access_debugfs(adev);
1321 - goto err;
1322 - }
1323 -
1324 - result += 4;
1325 - buf += 4;
1326 - size -= 4;
1327 - }
1328 -
1314 + r = copy_to_user(buf, data, size) ? -EFAULT : size;
1329 1315 kfree(data);
1330 - amdgpu_virt_disable_access_debugfs(adev);
1331 - return result;
1316 + return r;
1332 1317
1333 1318 err:
1334 1319 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
modified drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
@@ -567,8 +567,10 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
567 567
568 568 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
569 569 range = amdgpu_hmm_range_alloc(NULL);
570 - if (unlikely(!range))
571 - return -ENOMEM;
570 + if (unlikely(!range)) {
571 + r = -ENOMEM;
572 + goto release_object;
573 + }
572 574 r = amdgpu_ttm_tt_get_user_pages(bo, range);
573 575 if (r) {
574 576 amdgpu_hmm_range_free(range);
modified drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -2065,14 +2065,15 @@ static int amdgpu_ttm_buffer_entity_init(struct amdgpu_gtt_mgr *mgr,
2065 2065 int i, r, num_pages;
2066 2066 const u64 GTT_MAX_PAGES = (AMDGPU_GTT_MAX_TRANSFER_SIZE >> PAGE_SHIFT);
2067 2067
2068 + if (ARRAY_SIZE(entity->gart_window_offs) < num_gart_windows)
2069 + return -EINVAL;
2070 +
2068 2071 r = drm_sched_entity_init(&entity->base, prio, scheds, num_schedulers, NULL);
2069 2072 if (r)
2070 2073 return r;
2071 2074
2072 2075 mutex_init(&entity->lock);
2073 2076
2074 - if (ARRAY_SIZE(entity->gart_window_offs) < num_gart_windows)
2075 - return -EINVAL;
2076 2077 if (num_gart_windows == 0)
2077 2078 return 0;
2078 2079
@@ -2080,6 +2081,7 @@ static int amdgpu_ttm_buffer_entity_init(struct amdgpu_gtt_mgr *mgr,
2080 2081 r = amdgpu_gtt_mgr_alloc_entries(mgr, &entity->gart_node, num_pages,
2081 2082 DRM_MM_INSERT_BEST);
2082 2083 if (r) {
2084 + mutex_destroy(&entity->lock);
2083 2085 drm_sched_entity_destroy(&entity->base);
2084 2086 return r;
2085 2087 }
@@ -2097,6 +2099,7 @@ static void amdgpu_ttm_buffer_entity_fini(struct amdgpu_gtt_mgr *mgr,
2097 2099 struct amdgpu_ttm_buffer_entity *entity)
2098 2100 {
2099 2101 amdgpu_gtt_mgr_free_entries(mgr, &entity->gart_node);
2102 + mutex_destroy(&entity->lock);
2100 2103 drm_sched_entity_destroy(&entity->base);
2101 2104 }
2102 2105
modified drivers/gpu/drm/amd/amdgpu/uvd_v3_1.c
@@ -288,7 +288,7 @@ static int uvd_v3_1_fw_validate(struct amdgpu_device *adev)
288 288 int i;
289 289 uint32_t keysel = adev->uvd.keyselect;
290 290
291 - if (RREG32(mmUVD_FW_START) & UVD_FW_STATUS__PASS_MASK) {
291 + if (RREG32(mmUVD_FW_START)) {
292 292 dev_dbg(adev->dev, "UVD keyselect already set: 0x%x (on CPU: 0x%x)\n",
293 293 RREG32(mmUVD_FW_START), adev->uvd.keyselect);
294 294 return 0;
modified drivers/gpu/drm/amd/pm/swsmu/smu14/smu_v14_0_2_ppt.c
@@ -89,6 +89,7 @@ static const struct smu_feature_bits smu_v14_0_2_dpm_features = {
89 89 #define PP_OD_FEATURE_FAN_TARGET_TEMPERATURE 9
90 90 #define PP_OD_FEATURE_FAN_MINIMUM_PWM 10
91 91 #define PP_OD_FEATURE_FAN_ZERO_RPM_ENABLE 11
92 + #define PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP 12
92 93
93 94 static struct cmn2asic_msg_mapping smu_v14_0_2_message_map[SMU_MSG_MAX_COUNT] = {
94 95 MSG_MAP(TestMessage, PPSMC_MSG_TestMessage, 1),
@@ -1020,6 +1021,10 @@ static void smu_v14_0_2_get_od_setting_limits(struct smu_context *smu,
1020 1021 od_min_setting = overdrive_lowerlimits->FanZeroRpmEnable;
1021 1022 od_max_setting = overdrive_upperlimits->FanZeroRpmEnable;
1022 1023 break;
1024 + case PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP:
1025 + od_min_setting = pptable->SkuTable.MsgLimits.FanStopTempMin;
1026 + od_max_setting = pptable->SkuTable.MsgLimits.FanStopTempMax;
1027 + break;
1023 1028 default:
1024 1029 od_min_setting = od_max_setting = INT_MAX;
1025 1030 break;
@@ -1244,6 +1249,24 @@ static int smu_v14_0_2_emit_clk_levels(struct smu_context *smu,
1244 1249 min_value, max_value);
1245 1250 break;
1246 1251
1252 + case SMU_OD_FAN_ZERO_RPM_STOP_TEMP:
1253 + if (!smu_v14_0_2_is_od_feature_supported(smu,
1254 + PP_OD_FEATURE_ZERO_FAN_BIT))
1255 + break;
1256 +
1257 + size += sysfs_emit_at(buf, size, "FAN_ZERO_RPM_STOP_TEMPERATURE:\n");
1258 + size += sysfs_emit_at(buf, size, "%d\n",
1259 + (int)od_table->OverDriveTable.FanZeroRpmStopTemp);
1260 +
1261 + size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
1262 + smu_v14_0_2_get_od_setting_limits(smu,
1263 + PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP,
1264 + &min_value,
1265 + &max_value);
1266 + size += sysfs_emit_at(buf, size, "ZERO_RPM_STOP_TEMPERATURE: %u %u\n",
1267 + min_value, max_value);
1268 + break;
1269 +
1247 1270 case SMU_OD_RANGE:
1248 1271 if (!smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_GFXCLK_BIT) &&
1249 1272 !smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_UCLK_BIT) &&
@@ -2311,7 +2334,9 @@ static void smu_v14_0_2_set_supported_od_feature_mask(struct smu_context *smu)
2311 2334 OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE |
2312 2335 OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET |
2313 2336 OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_RETRIEVE |
2314 - OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET;
2337 + OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET |
2338 + OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_RETRIEVE |
2339 + OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_SET;
2315 2340 }
2316 2341
2317 2342 static int smu_v14_0_2_get_overdrive_table(struct smu_context *smu,
@@ -2392,6 +2417,8 @@ static int smu_v14_0_2_set_default_od_settings(struct smu_context *smu)
2392 2417 user_od_table_bak.OverDriveTable.FanMinimumPwm;
2393 2418 user_od_table->OverDriveTable.FanZeroRpmEnable =
2394 2419 user_od_table_bak.OverDriveTable.FanZeroRpmEnable;
2420 + user_od_table->OverDriveTable.FanZeroRpmStopTemp =
2421 + user_od_table_bak.OverDriveTable.FanZeroRpmStopTemp;
2395 2422 }
2396 2423
2397 2424 smu_v14_0_2_set_supported_od_feature_mask(smu);
@@ -2446,6 +2473,11 @@ static int smu_v14_0_2_od_restore_table_single(struct smu_context *smu, long inp
2446 2473 boot_overdrive_table->OverDriveTable.FanZeroRpmEnable;
2447 2474 od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2448 2475 break;
2476 + case PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP:
2477 + od_table->OverDriveTable.FanZeroRpmStopTemp =
2478 + boot_overdrive_table->OverDriveTable.FanZeroRpmStopTemp;
2479 + od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2480 + break;
2449 2481 case PP_OD_EDIT_ACOUSTIC_LIMIT:
2450 2482 od_table->OverDriveTable.AcousticLimitRpmThreshold =
2451 2483 boot_overdrive_table->OverDriveTable.AcousticLimitRpmThreshold;
@@ -2752,6 +2784,27 @@ static int smu_v14_0_2_od_edit_dpm_table(struct smu_context *smu,
2752 2784 od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2753 2785 break;
2754 2786
2787 + case PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP:
2788 + if (!smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_ZERO_FAN_BIT)) {
2789 + dev_warn(adev->dev, "Zero RPM setting not supported!\n");
2790 + return -ENOTSUPP;
2791 + }
2792 +
2793 + smu_v14_0_2_get_od_setting_limits(smu,
2794 + PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP,
2795 + &minimum,
2796 + &maximum);
2797 + if (input[0] < minimum ||
2798 + input[0] > maximum) {
2799 + dev_info(adev->dev, "zero RPM stop temperature setting(%ld) must be within [%d, %d]!\n",
2800 + input[0], minimum, maximum);
2801 + return -EINVAL;
2802 + }
2803 +
2804 + od_table->OverDriveTable.FanZeroRpmStopTemp = input[0];
2805 + od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2806 + break;
2807 +
2755 2808 case PP_OD_RESTORE_DEFAULT_TABLE:
2756 2809 if (size == 1) {
2757 2810 ret = smu_v14_0_2_od_restore_table_single(smu, input[0]);
modified tools/gpio/Makefile
@@ -7,13 +7,7 @@ bindir ?= /usr/bin
7 7 # isn't set and when invoked from selftests build, where srctree
8 8 # is set to ".". building_out_of_srctree is undefined for in srctree
9 9 # builds
10 - ifeq ($(srctree),)
11 - update_srctree := 1
12 - endif
13 10 ifndef building_out_of_srctree
14 - update_srctree := 1
15 - endif
16 - ifeq ($(update_srctree),1)
17 11 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
18 12 srctree := $(patsubst %/,%,$(dir $(srctree)))
19 13 endif