public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Brett Creeley <brett.creeley@amd.com>
To: <jgg@ziepe.ca>, <yishaih@nvidia.com>,
	<shameerali.kolothum.thodi@huawei.com>, <kevin.tian@intel.com>,
	<alex.williamson@redhat.com>, <dan.carpenter@linaro.org>
Cc: <kvm@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<brett.creeley@amd.com>, <shannon.nelson@amd.com>
Subject: [PATCH v2 vfio 3/3] pds/vfio: Fix possible sleep while in atomic context
Date: Wed, 11 Oct 2023 16:01:15 -0700	[thread overview]
Message-ID: <20231011230115.35719-4-brett.creeley@amd.com> (raw)
In-Reply-To: <20231011230115.35719-1-brett.creeley@amd.com>

The driver could possibly sleep while in atomic context resulting
in the following call trace while CONFIG_DEBUG_ATOMIC_SLEEP=y is
set:

BUG: sleeping function called from invalid context at kernel/locking/mutex.c:283
in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 2817, name: bash
preempt_count: 1, expected: 0
RCU nest depth: 0, expected: 0
Call Trace:
 <TASK>
 dump_stack_lvl+0x36/0x50
 __might_resched+0x123/0x170
 mutex_lock+0x1e/0x50
 pds_vfio_put_lm_file+0x1e/0xa0 [pds_vfio_pci]
 pds_vfio_put_save_file+0x19/0x30 [pds_vfio_pci]
 pds_vfio_state_mutex_unlock+0x2e/0x80 [pds_vfio_pci]
 pci_reset_function+0x4b/0x70
 reset_store+0x5b/0xa0
 kernfs_fop_write_iter+0x137/0x1d0
 vfs_write+0x2de/0x410
 ksys_write+0x5d/0xd0
 do_syscall_64+0x3b/0x90
 entry_SYSCALL_64_after_hwframe+0x6e/0xd8

This can happen if pds_vfio_put_restore_file() and/or
pds_vfio_put_save_file() grab the mutex_lock(&lm_file->lock)
while the spin_lock(&pds_vfio->reset_lock) is held, which can
happen during while calling pds_vfio_state_mutex_unlock().

Fix this by using a local state variable to represent the current
vfio device state and update all the next state values. Then
release the spin_unlock(&pds_vfio->reset_lock) before checking if
the current vfio device state is VFIO_DEVICE_STATE_ERROR. Then
calling pds_vfio_put_restore_file() and pds_vfio_put_save_file() are
no longer being done while holding the reset_lock.

The only possible concerns are other threads that may call
pds_vfio_put_restore_file(), pds_vfio_put_save_file(), and/or
pds_vfio_dirty_disable(). However, those paths are already protected
by the state mutex_lock(), which is held in this context.

Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/kvm/1f9bc27b-3de9-4891-9687-ba2820c1b390@moroto.mountain/
Signed-off-by: Brett Creeley <brett.creeley@amd.com>
Reviewed-by: Shannon Nelson <shannon.nelson@amd.com>
---
 drivers/vfio/pci/pds/vfio_dev.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/vfio/pci/pds/vfio_dev.c b/drivers/vfio/pci/pds/vfio_dev.c
index 306b1c25f016..cf7f639ba0ec 100644
--- a/drivers/vfio/pci/pds/vfio_dev.c
+++ b/drivers/vfio/pci/pds/vfio_dev.c
@@ -31,15 +31,17 @@ void pds_vfio_state_mutex_unlock(struct pds_vfio_pci_device *pds_vfio)
 again:
 	spin_lock(&pds_vfio->reset_lock);
 	if (pds_vfio->deferred_reset) {
+		enum vfio_device_mig_state current_state = pds_vfio->state;
+
 		pds_vfio->deferred_reset = false;
-		if (pds_vfio->state == VFIO_DEVICE_STATE_ERROR) {
+		pds_vfio->state = pds_vfio->deferred_reset_state;
+		pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING;
+		spin_unlock(&pds_vfio->reset_lock);
+		if (current_state == VFIO_DEVICE_STATE_ERROR) {
 			pds_vfio_put_restore_file(pds_vfio);
 			pds_vfio_put_save_file(pds_vfio);
 			pds_vfio_dirty_disable(pds_vfio, false);
 		}
-		pds_vfio->state = pds_vfio->deferred_reset_state;
-		pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING;
-		spin_unlock(&pds_vfio->reset_lock);
 		goto again;
 	}
 	mutex_unlock(&pds_vfio->state_mutex);
-- 
2.17.1


  parent reply	other threads:[~2023-10-11 23:01 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-11 23:01 [PATCH v2 vfio 0/3] pds/vfio: Fixes for locking bugs Brett Creeley
2023-10-11 23:01 ` [PATCH v2 vfio 1/3] pds/vfio: Fix spinlock bad magic BUG Brett Creeley
2023-10-12  8:44   ` Shameerali Kolothum Thodi
2023-10-11 23:01 ` [PATCH v2 vfio 2/3] pds/vfio: Fix mutex lock->magic != lock warning Brett Creeley
2023-10-12  9:00   ` Shameerali Kolothum Thodi
2023-10-12 13:50     ` Jason Gunthorpe
2023-10-11 23:01 ` Brett Creeley [this message]
2023-10-13 21:09 ` [PATCH v2 vfio 0/3] pds/vfio: Fixes for locking bugs Alex Williamson
2023-10-16 14:49   ` Brett Creeley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231011230115.35719-4-brett.creeley@amd.com \
    --to=brett.creeley@amd.com \
    --cc=alex.williamson@redhat.com \
    --cc=dan.carpenter@linaro.org \
    --cc=jgg@ziepe.ca \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shameerali.kolothum.thodi@huawei.com \
    --cc=shannon.nelson@amd.com \
    --cc=yishaih@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox