platform/x86/intel-uncore-freq: Fix static checker issue and potential race condition
authorSrinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Tue, 18 Feb 2020 02:28:43 +0000 (18:28 -0800)
committerAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Fri, 28 Feb 2020 10:44:11 +0000 (12:44 +0200)
commit64b73cff66acee7c1ec063676e0771534578c164
tree459d4111c6efa86616cf9628fc8eb16cd97cde3b
parent0e9c026f1b86a855cb9ab7aa270ff8db3c72015d
platform/x86/intel-uncore-freq: Fix static checker issue and potential race condition

There is a possible race condition when:
All CPUs in a package is offlined and just before the last CPU offline,
user tries to read sysfs entry and read happens while offline callback
is about to delete the sysfs entry.

Although not reproduced but this is possible scenerio and can be
reproduced by adding a msleep() in the show_min_max_freq_khz() before
mutex_lock() and read min_freq attribute from user space. Before
msleep() finishes, force every CPUs in a package offline.

This will cause deadlock, with offline and sysfs read/write operation
because of mutex_lock. The uncore_remove_die_entry() will not release
mutex till read/write callback returns because of kobject_put() and
read/write callback waiting on mutex.

We don't have to remove the sysfs folder when the package is offline.
While there is no CPU present, we can fail the read/write calls by
returning ENXIO error. So remove the kobject_put() call in offline path.

This also address the warning from static checker, as there is no
access to "data" variable after kobject_put:
"The patch 49a474c7ba51: "platform/x86: Add support for Uncore
frequency control" from Jan 13, 2020, leads to the following static
checker warning:

        drivers/platform/x86/intel-uncore-frequency.c:285 uncore_remove_die_entry()
        error: dereferencing freed memory 'data'
"

Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
drivers/platform/x86/intel-uncore-frequency.c