public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Brett Creeley <bcreeley@amd.com>
To: Alex Williamson <alex.williamson@redhat.com>,
	Brett Creeley <brett.creeley@amd.com>
Cc: jgg@ziepe.ca, yishaih@nvidia.com,
	shameerali.kolothum.thodi@huawei.com, kevin.tian@intel.com,
	dan.carpenter@linaro.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, shannon.nelson@amd.com
Subject: Re: [PATCH vfio 3/3] pds/vfio: Fix possible sleep while in atomic context
Date: Fri, 15 Sep 2023 08:54:01 -0700	[thread overview]
Message-ID: <011ad9da-1df9-3129-1f8d-543e8f598ee5@amd.com> (raw)
In-Reply-To: <20230914163837.07607d8a.alex.williamson@redhat.com>

On 9/14/2023 3:38 PM, Alex Williamson wrote:
> Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
> 
> 
> On Thu, 14 Sep 2023 12:15:40 -0700
> Brett Creeley <brett.creeley@amd.com> wrote:
> 
>> The driver could possibly sleep while in atomic context resulting
>> in the following call trace while CONFIG_DEBUG_ATOMIC_SLEEP=y is
>> set:
>>
>> [  227.229806] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:283
>> [  227.229818] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 2817, name: bash
>> [  227.229824] preempt_count: 1, expected: 0
>> [  227.229827] RCU nest depth: 0, expected: 0
>> [  227.229832] CPU: 5 PID: 2817 Comm: bash Tainted: G S         OE      6.6.0-rc1-next-20230911 #1
>> [  227.229839] Hardware name: HPE ProLiant DL360 Gen10/ProLiant DL360 Gen10, BIOS U32 01/23/2021
>> [  227.229843] Call Trace:
>> [  227.229848]  <TASK>
>> [  227.229853]  dump_stack_lvl+0x36/0x50
>> [  227.229865]  __might_resched+0x123/0x170
>> [  227.229877]  mutex_lock+0x1e/0x50
>> [  227.229891]  pds_vfio_put_lm_file+0x1e/0xa0 [pds_vfio_pci]
>> [  227.229909]  pds_vfio_put_save_file+0x19/0x30 [pds_vfio_pci]
>> [  227.229923]  pds_vfio_state_mutex_unlock+0x2e/0x80 [pds_vfio_pci]
>> [  227.229937]  pci_reset_function+0x4b/0x70
>> [  227.229948]  reset_store+0x5b/0xa0
>> [  227.229959]  kernfs_fop_write_iter+0x137/0x1d0
>> [  227.229972]  vfs_write+0x2de/0x410
>> [  227.229986]  ksys_write+0x5d/0xd0
>> [  227.229996]  do_syscall_64+0x3b/0x90
>> [  227.230004]  entry_SYSCALL_64_after_hwframe+0x6e/0xd8
>> [  227.230017] RIP: 0033:0x7fb202b1fa28
>> [  227.230023] Code: 89 02 48 c7 c0 ff ff ff ff eb b3 0f 1f 80 00 00 00 00 f3 0f 1e fa 48 8d 05 15 4d 2a 00 8b 00 85 c0 75 17 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 58 c3 0f 1f 80 00 00 00 00 41 54 49 89 d4 55
>> [  227.230028] RSP: 002b:00007fff6915fbd8 EFLAGS: 00000246 ORIG_RAX: 0000000000000001
>> [  227.230036] RAX: ffffffffffffffda RBX: 0000000000000002 RCX: 00007fb202b1fa28
>> [  227.230040] RDX: 0000000000000002 RSI: 000055f3834d5aa0 RDI: 0000000000000001
>> [  227.230044] RBP: 000055f3834d5aa0 R08: 000000000000000a R09: 00007fb202b7fae0
>> [  227.230047] R10: 000000000000000a R11: 0000000000000246 R12: 00007fb202dc06e0
>> [  227.230050] R13: 0000000000000002 R14: 00007fb202dbb860 R15: 0000000000000002
>> [  227.230056]  </TASK>
>>
>> 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 releasing the spin_unlock(&pds_vfio->reset_lock) before
>> calling pds_vfio_put_restore_file() and pds_vfio_put_save_file() and
>> re-acquiring spin_lock(&pds_vfio->reset_lock) after the previously
>> mentioned functions are called to protect setting the subsequent
>> state/deferred reset settings.
>>
>> The only possible concerns are other threads that may call
>> pds_vfio_put_restore_file() and/or pds_vfio_put_save_file(). However,
>> those paths are already protected by the state mutex_lock().
> 
> Is there another viable solution to change reset_lock to a mutex?
> 
> I think this is the origin of this algorithm:
> 
> https://lore.kernel.org/all/20211019191025.GA4072278@nvidia.com/
> 
> But it's not clear to me why Jason chose an example with a spinlock and
> if some subtlety here requires it.  Thanks,
> 
> Alex

It would be good to get some feedback from Jason on this before thinking 
about a different solution.

Thanks,

Brett

> 
>> 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 | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/drivers/vfio/pci/pds/vfio_dev.c b/drivers/vfio/pci/pds/vfio_dev.c
>> index 9db5f2c8f1ea..6e664cb05dd1 100644
>> --- a/drivers/vfio/pci/pds/vfio_dev.c
>> +++ b/drivers/vfio/pci/pds/vfio_dev.c
>> @@ -33,8 +33,10 @@ void pds_vfio_state_mutex_unlock(struct pds_vfio_pci_device *pds_vfio)
>>        if (pds_vfio->deferred_reset) {
>>                pds_vfio->deferred_reset = false;
>>                if (pds_vfio->state == VFIO_DEVICE_STATE_ERROR) {
>> +                     spin_unlock(&pds_vfio->reset_lock);
>>                        pds_vfio_put_restore_file(pds_vfio);
>>                        pds_vfio_put_save_file(pds_vfio);
>> +                     spin_lock(&pds_vfio->reset_lock);
>>                        pds_vfio_dirty_disable(pds_vfio, false);
>>                }
>>                pds_vfio->state = pds_vfio->deferred_reset_state;
> 

  reply	other threads:[~2023-09-15 15:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-14 19:15 [PATCH vfio 0/3] pds/vfio: Fixes for locking bugs Brett Creeley
2023-09-14 19:15 ` [PATCH vfio 1/3] pds/vfio: Fix spinlock bad magic BUG Brett Creeley
2023-09-14 19:15 ` [PATCH vfio 2/3] pds/vfio: Fix mutex lock->magic != lock warning Brett Creeley
2023-09-14 19:15 ` [PATCH vfio 3/3] pds/vfio: Fix possible sleep while in atomic context Brett Creeley
2023-09-14 22:38   ` Alex Williamson
2023-09-15 15:54     ` Brett Creeley [this message]
2023-09-19 18:59     ` Jason Gunthorpe
2023-09-21 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=011ad9da-1df9-3129-1f8d-543e8f598ee5@amd.com \
    --to=bcreeley@amd.com \
    --cc=alex.williamson@redhat.com \
    --cc=brett.creeley@amd.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