From 53af096726afcd5bc9eea54728f6742fc00b4359 Mon Sep 17 00:00:00 2001 From: Li Fei1 Date: Thu, 14 May 2020 13:31:21 +0800 Subject: [PATCH] hv: ptirq: refine find_ptirq_entry by hashing Refine find_ptirq_entry by hashing instead of walk each of the PTIRQ entries one by one. Tracked-On: #4550 Signed-off-by: Li Fei1 Acked-by: Eddie Dong --- hypervisor/arch/x86/guest/assign.c | 34 ---------------------- hypervisor/common/ptdev.c | 45 ++++++++++++++++++++++++++++++ hypervisor/include/common/ptdev.h | 18 ++++++++++++ hypervisor/include/lib/list.h | 37 ++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 34 deletions(-) diff --git a/hypervisor/arch/x86/guest/assign.c b/hypervisor/arch/x86/guest/assign.c index d3ce819c2..3c2166e83 100644 --- a/hypervisor/arch/x86/guest/assign.c +++ b/hypervisor/arch/x86/guest/assign.c @@ -49,40 +49,6 @@ static struct acrn_vcpu *is_single_destination(struct acrn_vm *vm, const struct return vcpu; } -/* - * lookup a ptdev entry by sid - * Before adding a ptdev remapping, should lookup by physical sid to check - * whether the resource has been token by others. - * When updating a ptdev remapping, should lookup by virtual sid to check - * whether this resource is valid. - * @pre: vm must be NULL when lookup by physical sid, otherwise, - * vm must not be NULL when lookup by virtual sid. - */ -static inline struct ptirq_remapping_info * -find_ptirq_entry(uint32_t intr_type, - const union source_id *sid, const struct acrn_vm *vm) -{ - uint16_t idx; - struct ptirq_remapping_info *entry; - struct ptirq_remapping_info *entry_found = NULL; - - for (idx = 0U; idx < CONFIG_MAX_PT_IRQ_ENTRIES; idx++) { - entry = &ptirq_entries[idx]; - if (!is_entry_active(entry)) { - continue; - } - if ((intr_type == entry->intr_type) && - ((vm == NULL) ? - (sid->value == entry->phys_sid.value) : - ((vm == entry->vm) && - (sid->value == entry->virt_sid.value)))) { - entry_found = entry; - break; - } - } - return entry_found; -} - static uint32_t calculate_logical_dest_mask(uint64_t pdmask) { uint32_t dest_mask = 0UL; diff --git a/hypervisor/common/ptdev.c b/hypervisor/common/ptdev.c index 714141491..ebcdcbc58 100644 --- a/hypervisor/common/ptdev.c +++ b/hypervisor/common/ptdev.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ +#include #include #include #include @@ -11,11 +12,18 @@ #include #include +#define PTIRQ_ENTRY_HASHBITS 9U +#define PTIRQ_ENTRY_HASHSIZE (1U << PTIRQ_ENTRY_HASHBITS) + #define PTIRQ_BITMAP_ARRAY_SIZE INT_DIV_ROUNDUP(CONFIG_MAX_PT_IRQ_ENTRIES, 64U) struct ptirq_remapping_info ptirq_entries[CONFIG_MAX_PT_IRQ_ENTRIES]; static uint64_t ptirq_entry_bitmaps[PTIRQ_BITMAP_ARRAY_SIZE]; spinlock_t ptdev_lock; +static struct ptirq_entry_head { + struct hlist_head list; +} ptirq_entry_heads[PTIRQ_ENTRY_HASHSIZE]; + static inline uint16_t ptirq_alloc_entry_id(void) { uint16_t id = (uint16_t)ffz64_ex(ptirq_entry_bitmaps, CONFIG_MAX_PT_IRQ_ENTRIES); @@ -30,6 +38,35 @@ static inline uint16_t ptirq_alloc_entry_id(void) return (id < CONFIG_MAX_PT_IRQ_ENTRIES) ? id: INVALID_PTDEV_ENTRY_ID; } +struct ptirq_remapping_info *find_ptirq_entry(uint32_t intr_type, + const union source_id *sid, const struct acrn_vm *vm) +{ + struct hlist_node *p; + struct ptirq_remapping_info *n, *entry = NULL; + uint64_t key = hash64(sid->value, PTIRQ_ENTRY_HASHBITS); + struct ptirq_entry_head *b = &ptirq_entry_heads[key]; + + hlist_for_each(p, &b->list) { + if (vm == NULL) { + n = hlist_entry(p, struct ptirq_remapping_info, phys_link); + } else { + n = hlist_entry(p, struct ptirq_remapping_info, virt_link); + } + + if (is_entry_active(n)) { + if ((intr_type == n->intr_type) && + ((vm == NULL) ? + (sid->value == n->phys_sid.value) : + ((vm == n->vm) && (sid->value == n->virt_sid.value)))) { + entry = n; + break; + } + } + } + + return entry; +} + static void ptirq_enqueue_softirq(struct ptirq_remapping_info *entry) { uint64_t rflags; @@ -154,6 +191,7 @@ static void ptirq_interrupt_handler(__unused uint32_t irq, void *data) int32_t ptirq_activate_entry(struct ptirq_remapping_info *entry, uint32_t phys_irq) { int32_t retval; + uint64_t key; /* register and allocate host vector/irq */ retval = request_irq(phys_irq, ptirq_interrupt_handler, (void *)entry, IRQF_PT); @@ -163,6 +201,11 @@ int32_t ptirq_activate_entry(struct ptirq_remapping_info *entry, uint32_t phys_i } else { entry->allocated_pirq = (uint32_t)retval; entry->active = true; + + key = hash64(entry->phys_sid.value, PTIRQ_ENTRY_HASHBITS); + hlist_add_head(&entry->phys_link, &(ptirq_entry_heads[key].list)); + key = hash64(entry->virt_sid.value, PTIRQ_ENTRY_HASHBITS); + hlist_add_head(&entry->virt_link, &(ptirq_entry_heads[key].list)); } return retval; @@ -170,6 +213,8 @@ int32_t ptirq_activate_entry(struct ptirq_remapping_info *entry, uint32_t phys_i void ptirq_deactivate_entry(struct ptirq_remapping_info *entry) { + hlist_del(&entry->phys_link); + hlist_del(&entry->virt_link); entry->active = false; free_irq(entry->allocated_pirq); } diff --git a/hypervisor/include/common/ptdev.h b/hypervisor/include/common/ptdev.h index 86d17f55c..3be3d4616 100644 --- a/hypervisor/include/common/ptdev.h +++ b/hypervisor/include/common/ptdev.h @@ -120,6 +120,8 @@ typedef void (*ptirq_arch_release_fn_t)(const struct ptirq_remapping_info *entry * with interrupt handler and softirq. */ struct ptirq_remapping_info { + struct hlist_node phys_link; + struct hlist_node virt_link; uint16_t ptdev_entry_id; uint32_t intr_type; union source_id phys_sid; @@ -159,6 +161,22 @@ extern spinlock_t ptdev_lock; */ +/* + * @brief Find a ptdev entry by sid + * + * param[in] intr_type interrupt type of the ptirq entry + * param[in] sid source id of the ptirq entry + * param[in] vm vm pointer of the ptirq entry if find the ptdev entry by virtual sid + * + * @retval NULL when \p when no ptirq entry match the sid + * @retval ptirq entry when \p there is available ptirq entry match the sid + * + * @pre: vm must be NULL when lookup by physical sid, otherwise, + * vm must not be NULL when lookup by virtual sid. + */ +struct ptirq_remapping_info *find_ptirq_entry(uint32_t intr_type, + const union source_id *sid, const struct acrn_vm *vm); + /** * @brief Handler of softirq for passthrough device. * diff --git a/hypervisor/include/lib/list.h b/hypervisor/include/lib/list.h index 9e0c2f6c3..f8c21c8ef 100644 --- a/hypervisor/include/lib/list.h +++ b/hypervisor/include/lib/list.h @@ -29,10 +29,20 @@ #ifndef LIST_H_ #define LIST_H_ +#include + struct list_head { struct list_head *next, *prev; }; +struct hlist_head { + struct hlist_node *first; +}; + +struct hlist_node { + struct hlist_node *next, **pprev; +}; + #define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } \ while (0) @@ -121,4 +131,31 @@ static inline void list_splice_init(struct list_head *list, #define get_first_item(attached, type, member) \ ((type *)((char *)((attached)->next)-(uint64_t)(&((type *)0)->member))) +static inline void +hlist_del(struct hlist_node *n) +{ + + if (n->next != NULL) { + n->next->pprev = n->pprev; + } + *n->pprev = n->next; +} + +static inline void +hlist_add_head(struct hlist_node *n, struct hlist_head *h) +{ + + n->next = h->first; + if (h->first != NULL) { + h->first->pprev = &n->next; + } + h->first = n; + n->pprev = &h->first; +} + +#define hlist_entry(ptr, type, member) container_of(ptr,type,member) + +#define hlist_for_each(pos, head) \ + for (pos = (head)->first; (pos) != NULL; pos = (pos)->next) + #endif /* LIST_H_ */