From: Halil Pasic <pasic@linux.ibm.com>
To: Christian Borntraeger <borntraeger@de.ibm.com>
Cc: Tony Krowiak <akrowiak@linux.ibm.com>,
linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org,
cohuck@redhat.com, pasic@linux.vnet.ibm.com,
jjherne@linux.ibm.com, jgg@nvidia.com,
alex.williamson@redhat.com, kwankhede@nvidia.com,
stable@vger.kernel.org, Tony Krowiak <akrowiak@stny.rr.com>
Subject: Re: [PATCH v2] s390/vfio-ap: fix memory leak in mdev remove callback
Date: Wed, 19 May 2021 13:25:49 +0200 [thread overview]
Message-ID: <20210519132549.295d48db.pasic@linux.ibm.com> (raw)
In-Reply-To: <250189ed-bded-5261-d8f3-f75787be7aeb@de.ibm.com>
On Wed, 19 May 2021 10:17:49 +0200
Christian Borntraeger <borntraeger@de.ibm.com> wrote:
> On 19.05.21 01:27, Halil Pasic wrote:
> > On Tue, 18 May 2021 19:01:42 +0200
> > Christian Borntraeger <borntraeger@de.ibm.com> wrote:
> >
> >> On 18.05.21 17:33, Halil Pasic wrote:
> >>> On Tue, 18 May 2021 15:59:36 +0200
> >>> Christian Borntraeger <borntraeger@de.ibm.com> wrote:
> > [..]
> >>>>>>
> >>>>>> Would it help, if the code in priv.c would read the hook once
> >>>>>> and then only work on the copy? We could protect that with rcu
> >>>>>> and do a synchronize rcu in vfio_ap_mdev_unset_kvm after
> >>>>>> unsetting the pointer?
> >>>
> >>> Unfortunately just "the hook" is ambiguous in this context. We
> >>> have kvm->arch.crypto.pqap_hook that is supposed to point to
> >>> a struct kvm_s390_module_hook member of struct ap_matrix_mdev
> >>> which is also called pqap_hook. And struct kvm_s390_module_hook
> >>> has function pointer member named "hook".
> >>
> >> I was referring to the full struct.
> >>>
> >>>>>
> >>>>> I'll look into this.
> >>>>
> >>>> I think it could work. in priv.c use rcu_readlock, save the
> >>>> pointer, do the check and call, call rcu_read_unlock.
> >>>> In vfio_ap use rcu_assign_pointer to set the pointer and
> >>>> after setting it to zero call sychronize_rcu.
> >>>
> >>> In my opinion, we should make the accesses to the
> >>> kvm->arch.crypto.pqap_hook pointer properly synchronized. I'm
> >>> not sure if that is what you are proposing. How do we usually
> >>> do synchronisation on the stuff that lives in kvm->arch?
> >>>
> >>
> >> RCU is a method of synchronization. We make sure that structure
> >> pqap_hook is still valid as long as we are inside the rcu read
> >> lock. So the idea is: clear pointer, wait until all old readers
> >> have finished and the proceed with getting rid of the structure.
> >
> > Yes I know that RCU is a method of synchronization, but I'm not
> > very familiar with it. I'm a little confused by "read the hook
> > once and then work on a copy". I guess, I would have to read up
> > on the RCU again to get clarity. I intend to brush up my RCU knowledge
> > once the patch comes along. I would be glad to have your help when
> > reviewing an RCU based solution for this.
>
> Just had a quick look. Its not trivial, as the hook function itself
> takes a mutex and an rcu section must not sleep. Will have a deeper
> look.
I refreshed my RCU knowledge and RCU seems to be a reasonable choice
here. I don't think we have to make the rcu read section span the
call to the callback. That is something like
--- a/arch/s390/kvm/priv.c
+++ b/arch/s390/kvm/priv.c
@@ -613,6 +613,7 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
unsigned long reg0;
int ret;
uint8_t fc;
+ int (*pqap_hook)(struct kvm_vcpu *vcpu);
/* Verify that the AP instruction are available */
if (!ap_instructions_available())
@@ -657,14 +658,21 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
* Verify that the hook callback is registered, lock the owner
* and call the hook.
*/
+ rcu_read_lock();
if (vcpu->kvm->arch.crypto.pqap_hook) {
- if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
+ if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner)) {
+ rcu_read_unlock();
return -EOPNOTSUPP;
- ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
+ }
+ pqap_hook = READ_ONCE(vcpu->kvm->arch.crypto.pqap_hook->hook);
+ rcu_read_unlock();
+ ret = pqap_hook();
module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
kvm_s390_set_psw_cc(vcpu, 3);
return ret;
+ } else {
+ rcu_read_unlock();
}
/*
* A vfio_driver must register a hook.
Should be sufficient. The module get ensures that the pointee is still
around for the duration of the call. The handle_pqap() from
vfio_ap_ops.c checks the vcpu->kvm->arch.crypto.pqap_hook the same
lock that is used to set it to NULL, and bails out if it is NULL. It
is a bit convoluted, but it should work.
Regards,
Halil
next prev parent reply other threads:[~2021-05-19 11:26 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-10 21:48 [PATCH v2] s390/vfio-ap: fix memory leak in mdev remove callback Tony Krowiak
2021-05-10 21:56 ` Tony Krowiak
2021-05-12 10:35 ` Cornelia Huck
2021-05-12 12:41 ` Jason Gunthorpe
2021-05-12 15:32 ` Christian Borntraeger
2021-05-12 16:50 ` Jason Gunthorpe
2021-05-13 14:19 ` Tony Krowiak
2021-05-13 14:18 ` Tony Krowiak
2021-05-13 17:25 ` Jason Gunthorpe
2021-05-13 17:32 ` Halil Pasic
2021-05-13 17:34 ` Jason Gunthorpe
2021-05-12 16:49 ` Christian Borntraeger
2021-05-12 18:35 ` Halil Pasic
2021-05-13 14:35 ` Tony Krowiak
2021-05-13 17:45 ` Halil Pasic
2021-05-13 19:23 ` Tony Krowiak
2021-05-14 0:15 ` Halil Pasic
2021-05-17 13:37 ` Tony Krowiak
2021-05-17 19:10 ` Halil Pasic
2021-05-18 9:30 ` Christian Borntraeger
2021-05-18 13:42 ` Tony Krowiak
2021-05-18 13:59 ` Christian Borntraeger
2021-05-18 15:33 ` Halil Pasic
2021-05-18 17:01 ` Christian Borntraeger
2021-05-18 23:27 ` Halil Pasic
2021-05-19 8:17 ` Christian Borntraeger
2021-05-19 11:22 ` Christian Borntraeger
2021-05-19 12:59 ` Halil Pasic
2021-05-19 13:02 ` Jason Gunthorpe
2021-05-19 11:25 ` Halil Pasic [this message]
2021-05-18 18:14 ` Tony Krowiak
2021-05-18 18:22 ` Christian Borntraeger
2021-05-18 18:40 ` Tony Krowiak
2021-05-18 13:41 ` Tony Krowiak
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=20210519132549.295d48db.pasic@linux.ibm.com \
--to=pasic@linux.ibm.com \
--cc=akrowiak@linux.ibm.com \
--cc=akrowiak@stny.rr.com \
--cc=alex.williamson@redhat.com \
--cc=borntraeger@de.ibm.com \
--cc=cohuck@redhat.com \
--cc=jgg@nvidia.com \
--cc=jjherne@linux.ibm.com \
--cc=kwankhede@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=pasic@linux.vnet.ibm.com \
--cc=stable@vger.kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).