hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/virt/kvm/eventfd.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * kvm eventfd support - use eventfd objects to signal various KVM events
34 *
....@@ -6,19 +7,6 @@
67 *
78 * Author:
89 * Gregory Haskins <ghaskins@novell.com>
9
- *
10
- * This file is free software; you can redistribute it and/or modify
11
- * it under the terms of version 2 of the GNU General Public License
12
- * as published by the Free Software Foundation.
13
- *
14
- * This program is distributed in the hope that it will be useful,
15
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
- * GNU General Public License for more details.
18
- *
19
- * You should have received a copy of the GNU General Public License
20
- * along with this program; if not, write to the Free Software Foundation,
21
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
2210 */
2311
2412 #include <linux/kvm_host.h>
....@@ -128,7 +116,7 @@
128116 struct kvm *kvm = irqfd->kvm;
129117 u64 cnt;
130118
131
- /* Make sure irqfd has been initalized in assign path. */
119
+ /* Make sure irqfd has been initialized in assign path. */
132120 synchronize_srcu(&kvm->irq_srcu);
133121
134122 /*
....@@ -220,9 +208,9 @@
220208
221209 if (flags & EPOLLHUP) {
222210 /* The eventfd is closing, detach from KVM */
223
- unsigned long flags;
211
+ unsigned long iflags;
224212
225
- spin_lock_irqsave(&kvm->irqfds.lock, flags);
213
+ spin_lock_irqsave(&kvm->irqfds.lock, iflags);
226214
227215 /*
228216 * We must check if someone deactivated the irqfd before
....@@ -236,7 +224,7 @@
236224 if (irqfd_is_active(irqfd))
237225 irqfd_deactivate(irqfd);
238226
239
- spin_unlock_irqrestore(&kvm->irqfds.lock, flags);
227
+ spin_unlock_irqrestore(&kvm->irqfds.lock, iflags);
240228 }
241229
242230 return 0;
....@@ -306,7 +294,7 @@
306294 if (!kvm_arch_irqfd_allowed(kvm, args))
307295 return -EINVAL;
308296
309
- irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
297
+ irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL_ACCOUNT);
310298 if (!irqfd)
311299 return -ENOMEM;
312300
....@@ -315,7 +303,7 @@
315303 INIT_LIST_HEAD(&irqfd->list);
316304 INIT_WORK(&irqfd->inject, irqfd_inject);
317305 INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
318
- seqcount_init(&irqfd->irq_entry_sc);
306
+ seqcount_spinlock_init(&irqfd->irq_entry_sc, &kvm->irqfds.lock);
319307
320308 f = fdget(args->fd);
321309 if (!f.file) {
....@@ -354,7 +342,8 @@
354342 }
355343
356344 if (!irqfd->resampler) {
357
- resampler = kzalloc(sizeof(*resampler), GFP_KERNEL);
345
+ resampler = kzalloc(sizeof(*resampler),
346
+ GFP_KERNEL_ACCOUNT);
358347 if (!resampler) {
359348 ret = -ENOMEM;
360349 mutex_unlock(&kvm->irqfds.resampler_lock);
....@@ -462,8 +451,8 @@
462451 idx = srcu_read_lock(&kvm->irq_srcu);
463452 gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
464453 if (gsi != -1)
465
- hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
466
- link)
454
+ hlist_for_each_entry_srcu(kian, &kvm->irq_ack_notifier_list,
455
+ link, srcu_read_lock_held(&kvm->irq_srcu))
467456 if (kian->gsi == gsi) {
468457 srcu_read_unlock(&kvm->irq_srcu, idx);
469458 return true;
....@@ -479,8 +468,8 @@
479468 {
480469 struct kvm_irq_ack_notifier *kian;
481470
482
- hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
483
- link)
471
+ hlist_for_each_entry_srcu(kian, &kvm->irq_ack_notifier_list,
472
+ link, srcu_read_lock_held(&kvm->irq_srcu))
484473 if (kian->gsi == gsi)
485474 kian->irq_acked(kian);
486475 }
....@@ -732,7 +721,7 @@
732721 return false;
733722 }
734723
735
- return _val == p->datamatch ? true : false;
724
+ return _val == p->datamatch;
736725 }
737726
738727 /* MMIO/PIO writes trigger an event if the addr/val match */
....@@ -806,7 +795,7 @@
806795 if (IS_ERR(eventfd))
807796 return PTR_ERR(eventfd);
808797
809
- p = kzalloc(sizeof(*p), GFP_KERNEL);
798
+ p = kzalloc(sizeof(*p), GFP_KERNEL_ACCOUNT);
810799 if (!p) {
811800 ret = -ENOMEM;
812801 goto fail;
....@@ -864,15 +853,17 @@
864853 struct eventfd_ctx *eventfd;
865854 struct kvm_io_bus *bus;
866855 int ret = -ENOENT;
856
+ bool wildcard;
867857
868858 eventfd = eventfd_ctx_fdget(args->fd);
869859 if (IS_ERR(eventfd))
870860 return PTR_ERR(eventfd);
871861
862
+ wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
863
+
872864 mutex_lock(&kvm->slots_lock);
873865
874866 list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
875
- bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
876867
877868 if (p->bus_idx != bus_idx ||
878869 p->eventfd != eventfd ||