Path: blob/master/drivers/gpu/drm/amd/amdkfd/kfd_events.c
26516 views
// SPDX-License-Identifier: GPL-2.0 OR MIT1/*2* Copyright 2014-2022 Advanced Micro Devices, Inc.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included in12* all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR18* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,19* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR20* OTHER DEALINGS IN THE SOFTWARE.21*/2223#include <linux/mm_types.h>24#include <linux/slab.h>25#include <linux/types.h>26#include <linux/sched/signal.h>27#include <linux/sched/mm.h>28#include <linux/uaccess.h>29#include <linux/mman.h>30#include <linux/memory.h>31#include "kfd_priv.h"32#include "kfd_events.h"33#include "kfd_device_queue_manager.h"34#include <linux/device.h>3536/*37* Wrapper around wait_queue_entry_t38*/39struct kfd_event_waiter {40wait_queue_entry_t wait;41struct kfd_event *event; /* Event to wait for */42bool activated; /* Becomes true when event is signaled */43bool event_age_enabled; /* set to true when last_event_age is non-zero */44};4546/*47* Each signal event needs a 64-bit signal slot where the signaler will write48* a 1 before sending an interrupt. (This is needed because some interrupts49* do not contain enough spare data bits to identify an event.)50* We get whole pages and map them to the process VA.51* Individual signal events use their event_id as slot index.52*/53struct kfd_signal_page {54uint64_t *kernel_address;55uint64_t __user *user_address;56bool need_to_free_pages;57};5859static uint64_t *page_slots(struct kfd_signal_page *page)60{61return page->kernel_address;62}6364static struct kfd_signal_page *allocate_signal_page(struct kfd_process *p)65{66void *backing_store;67struct kfd_signal_page *page;6869page = kzalloc(sizeof(*page), GFP_KERNEL);70if (!page)71return NULL;7273backing_store = (void *) __get_free_pages(GFP_KERNEL,74get_order(KFD_SIGNAL_EVENT_LIMIT * 8));75if (!backing_store)76goto fail_alloc_signal_store;7778/* Initialize all events to unsignaled */79memset(backing_store, (uint8_t) UNSIGNALED_EVENT_SLOT,80KFD_SIGNAL_EVENT_LIMIT * 8);8182page->kernel_address = backing_store;83page->need_to_free_pages = true;84pr_debug("Allocated new event signal page at %p, for process %p\n",85page, p);8687return page;8889fail_alloc_signal_store:90kfree(page);91return NULL;92}9394static int allocate_event_notification_slot(struct kfd_process *p,95struct kfd_event *ev,96const int *restore_id)97{98int id;99100if (!p->signal_page) {101p->signal_page = allocate_signal_page(p);102if (!p->signal_page)103return -ENOMEM;104/* Oldest user mode expects 256 event slots */105p->signal_mapped_size = 256*8;106}107108if (restore_id) {109id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,110GFP_KERNEL);111} else {112/*113* Compatibility with old user mode: Only use signal slots114* user mode has mapped, may be less than115* KFD_SIGNAL_EVENT_LIMIT. This also allows future increase116* of the event limit without breaking user mode.117*/118id = idr_alloc(&p->event_idr, ev, 0, p->signal_mapped_size / 8,119GFP_KERNEL);120}121if (id < 0)122return id;123124ev->event_id = id;125page_slots(p->signal_page)[id] = UNSIGNALED_EVENT_SLOT;126127return 0;128}129130/*131* Assumes that p->event_mutex or rcu_readlock is held and of course that p is132* not going away.133*/134static struct kfd_event *lookup_event_by_id(struct kfd_process *p, uint32_t id)135{136return idr_find(&p->event_idr, id);137}138139/**140* lookup_signaled_event_by_partial_id - Lookup signaled event from partial ID141* @p: Pointer to struct kfd_process142* @id: ID to look up143* @bits: Number of valid bits in @id144*145* Finds the first signaled event with a matching partial ID. If no146* matching signaled event is found, returns NULL. In that case the147* caller should assume that the partial ID is invalid and do an148* exhaustive search of all siglaned events.149*150* If multiple events with the same partial ID signal at the same151* time, they will be found one interrupt at a time, not necessarily152* in the same order the interrupts occurred. As long as the number of153* interrupts is correct, all signaled events will be seen by the154* driver.155*/156static struct kfd_event *lookup_signaled_event_by_partial_id(157struct kfd_process *p, uint32_t id, uint32_t bits)158{159struct kfd_event *ev;160161if (!p->signal_page || id >= KFD_SIGNAL_EVENT_LIMIT)162return NULL;163164/* Fast path for the common case that @id is not a partial ID165* and we only need a single lookup.166*/167if (bits > 31 || (1U << bits) >= KFD_SIGNAL_EVENT_LIMIT) {168if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)169return NULL;170171return idr_find(&p->event_idr, id);172}173174/* General case for partial IDs: Iterate over all matching IDs175* and find the first one that has signaled.176*/177for (ev = NULL; id < KFD_SIGNAL_EVENT_LIMIT && !ev; id += 1U << bits) {178if (page_slots(p->signal_page)[id] == UNSIGNALED_EVENT_SLOT)179continue;180181ev = idr_find(&p->event_idr, id);182}183184return ev;185}186187static int create_signal_event(struct file *devkfd, struct kfd_process *p,188struct kfd_event *ev, const int *restore_id)189{190int ret;191192if (p->signal_mapped_size &&193p->signal_event_count == p->signal_mapped_size / 8) {194if (!p->signal_event_limit_reached) {195pr_debug("Signal event wasn't created because limit was reached\n");196p->signal_event_limit_reached = true;197}198return -ENOSPC;199}200201ret = allocate_event_notification_slot(p, ev, restore_id);202if (ret) {203pr_warn("Signal event wasn't created because out of kernel memory\n");204return ret;205}206207p->signal_event_count++;208209ev->user_signal_address = &p->signal_page->user_address[ev->event_id];210pr_debug("Signal event number %zu created with id %d, address %p\n",211p->signal_event_count, ev->event_id,212ev->user_signal_address);213214return 0;215}216217static int create_other_event(struct kfd_process *p, struct kfd_event *ev, const int *restore_id)218{219int id;220221if (restore_id)222id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,223GFP_KERNEL);224else225/* Cast KFD_LAST_NONSIGNAL_EVENT to uint32_t. This allows an226* intentional integer overflow to -1 without a compiler227* warning. idr_alloc treats a negative value as "maximum228* signed integer".229*/230id = idr_alloc(&p->event_idr, ev, KFD_FIRST_NONSIGNAL_EVENT_ID,231(uint32_t)KFD_LAST_NONSIGNAL_EVENT_ID + 1,232GFP_KERNEL);233234if (id < 0)235return id;236ev->event_id = id;237238return 0;239}240241int kfd_event_init_process(struct kfd_process *p)242{243int id;244245mutex_init(&p->event_mutex);246idr_init(&p->event_idr);247p->signal_page = NULL;248p->signal_event_count = 1;249/* Allocate event ID 0. It is used for a fast path to ignore bogus events250* that are sent by the CP without a context ID251*/252id = idr_alloc(&p->event_idr, NULL, 0, 1, GFP_KERNEL);253if (id < 0) {254idr_destroy(&p->event_idr);255mutex_destroy(&p->event_mutex);256return id;257}258return 0;259}260261static void destroy_event(struct kfd_process *p, struct kfd_event *ev)262{263struct kfd_event_waiter *waiter;264265/* Wake up pending waiters. They will return failure */266spin_lock(&ev->lock);267list_for_each_entry(waiter, &ev->wq.head, wait.entry)268WRITE_ONCE(waiter->event, NULL);269wake_up_all(&ev->wq);270spin_unlock(&ev->lock);271272if (ev->type == KFD_EVENT_TYPE_SIGNAL ||273ev->type == KFD_EVENT_TYPE_DEBUG)274p->signal_event_count--;275276idr_remove(&p->event_idr, ev->event_id);277kfree_rcu(ev, rcu);278}279280static void destroy_events(struct kfd_process *p)281{282struct kfd_event *ev;283uint32_t id;284285idr_for_each_entry(&p->event_idr, ev, id)286if (ev)287destroy_event(p, ev);288idr_destroy(&p->event_idr);289mutex_destroy(&p->event_mutex);290}291292/*293* We assume that the process is being destroyed and there is no need to294* unmap the pages or keep bookkeeping data in order.295*/296static void shutdown_signal_page(struct kfd_process *p)297{298struct kfd_signal_page *page = p->signal_page;299300if (page) {301if (page->need_to_free_pages)302free_pages((unsigned long)page->kernel_address,303get_order(KFD_SIGNAL_EVENT_LIMIT * 8));304kfree(page);305}306}307308void kfd_event_free_process(struct kfd_process *p)309{310destroy_events(p);311shutdown_signal_page(p);312}313314static bool event_can_be_gpu_signaled(const struct kfd_event *ev)315{316return ev->type == KFD_EVENT_TYPE_SIGNAL ||317ev->type == KFD_EVENT_TYPE_DEBUG;318}319320static bool event_can_be_cpu_signaled(const struct kfd_event *ev)321{322return ev->type == KFD_EVENT_TYPE_SIGNAL;323}324325static int kfd_event_page_set(struct kfd_process *p, void *kernel_address,326uint64_t size, uint64_t user_handle)327{328struct kfd_signal_page *page;329330if (p->signal_page)331return -EBUSY;332333page = kzalloc(sizeof(*page), GFP_KERNEL);334if (!page)335return -ENOMEM;336337/* Initialize all events to unsignaled */338memset(kernel_address, (uint8_t) UNSIGNALED_EVENT_SLOT,339KFD_SIGNAL_EVENT_LIMIT * 8);340341page->kernel_address = kernel_address;342343p->signal_page = page;344p->signal_mapped_size = size;345p->signal_handle = user_handle;346return 0;347}348349int kfd_kmap_event_page(struct kfd_process *p, uint64_t event_page_offset)350{351struct kfd_node *kfd;352struct kfd_process_device *pdd;353void *mem, *kern_addr;354uint64_t size;355int err = 0;356357if (p->signal_page) {358pr_err("Event page is already set\n");359return -EINVAL;360}361362pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(event_page_offset));363if (!pdd) {364pr_err("Getting device by id failed in %s\n", __func__);365return -EINVAL;366}367kfd = pdd->dev;368369pdd = kfd_bind_process_to_device(kfd, p);370if (IS_ERR(pdd))371return PTR_ERR(pdd);372373mem = kfd_process_device_translate_handle(pdd,374GET_IDR_HANDLE(event_page_offset));375if (!mem) {376pr_err("Can't find BO, offset is 0x%llx\n", event_page_offset);377return -EINVAL;378}379380err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(mem, &kern_addr, &size);381if (err) {382pr_err("Failed to map event page to kernel\n");383return err;384}385386err = kfd_event_page_set(p, kern_addr, size, event_page_offset);387if (err) {388pr_err("Failed to set event page\n");389amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(mem);390return err;391}392return err;393}394395int kfd_event_create(struct file *devkfd, struct kfd_process *p,396uint32_t event_type, bool auto_reset, uint32_t node_id,397uint32_t *event_id, uint32_t *event_trigger_data,398uint64_t *event_page_offset, uint32_t *event_slot_index)399{400int ret = 0;401struct kfd_event *ev = kzalloc(sizeof(*ev), GFP_KERNEL);402403if (!ev)404return -ENOMEM;405406ev->type = event_type;407ev->auto_reset = auto_reset;408ev->signaled = false;409410spin_lock_init(&ev->lock);411init_waitqueue_head(&ev->wq);412413*event_page_offset = 0;414415mutex_lock(&p->event_mutex);416417switch (event_type) {418case KFD_EVENT_TYPE_SIGNAL:419case KFD_EVENT_TYPE_DEBUG:420ret = create_signal_event(devkfd, p, ev, NULL);421if (!ret) {422*event_page_offset = KFD_MMAP_TYPE_EVENTS;423*event_slot_index = ev->event_id;424}425break;426default:427ret = create_other_event(p, ev, NULL);428break;429}430431if (!ret) {432*event_id = ev->event_id;433*event_trigger_data = ev->event_id;434ev->event_age = 1;435} else {436kfree(ev);437}438439mutex_unlock(&p->event_mutex);440441return ret;442}443444int kfd_criu_restore_event(struct file *devkfd,445struct kfd_process *p,446uint8_t __user *user_priv_ptr,447uint64_t *priv_data_offset,448uint64_t max_priv_data_size)449{450struct kfd_criu_event_priv_data *ev_priv;451struct kfd_event *ev = NULL;452int ret = 0;453454ev_priv = kmalloc(sizeof(*ev_priv), GFP_KERNEL);455if (!ev_priv)456return -ENOMEM;457458ev = kzalloc(sizeof(*ev), GFP_KERNEL);459if (!ev) {460ret = -ENOMEM;461goto exit;462}463464if (*priv_data_offset + sizeof(*ev_priv) > max_priv_data_size) {465ret = -EINVAL;466goto exit;467}468469ret = copy_from_user(ev_priv, user_priv_ptr + *priv_data_offset, sizeof(*ev_priv));470if (ret) {471ret = -EFAULT;472goto exit;473}474*priv_data_offset += sizeof(*ev_priv);475476if (ev_priv->user_handle) {477ret = kfd_kmap_event_page(p, ev_priv->user_handle);478if (ret)479goto exit;480}481482ev->type = ev_priv->type;483ev->auto_reset = ev_priv->auto_reset;484ev->signaled = ev_priv->signaled;485486spin_lock_init(&ev->lock);487init_waitqueue_head(&ev->wq);488489mutex_lock(&p->event_mutex);490switch (ev->type) {491case KFD_EVENT_TYPE_SIGNAL:492case KFD_EVENT_TYPE_DEBUG:493ret = create_signal_event(devkfd, p, ev, &ev_priv->event_id);494break;495case KFD_EVENT_TYPE_MEMORY:496memcpy(&ev->memory_exception_data,497&ev_priv->memory_exception_data,498sizeof(struct kfd_hsa_memory_exception_data));499500ret = create_other_event(p, ev, &ev_priv->event_id);501break;502case KFD_EVENT_TYPE_HW_EXCEPTION:503memcpy(&ev->hw_exception_data,504&ev_priv->hw_exception_data,505sizeof(struct kfd_hsa_hw_exception_data));506507ret = create_other_event(p, ev, &ev_priv->event_id);508break;509}510mutex_unlock(&p->event_mutex);511512exit:513if (ret)514kfree(ev);515516kfree(ev_priv);517518return ret;519}520521int kfd_criu_checkpoint_events(struct kfd_process *p,522uint8_t __user *user_priv_data,523uint64_t *priv_data_offset)524{525struct kfd_criu_event_priv_data *ev_privs;526int i = 0;527int ret = 0;528struct kfd_event *ev;529uint32_t ev_id;530531uint32_t num_events = kfd_get_num_events(p);532533if (!num_events)534return 0;535536ev_privs = kvzalloc(num_events * sizeof(*ev_privs), GFP_KERNEL);537if (!ev_privs)538return -ENOMEM;539540541idr_for_each_entry(&p->event_idr, ev, ev_id) {542struct kfd_criu_event_priv_data *ev_priv;543544/*545* Currently, all events have same size of private_data, but the current ioctl's546* and CRIU plugin supports private_data of variable sizes547*/548ev_priv = &ev_privs[i];549550ev_priv->object_type = KFD_CRIU_OBJECT_TYPE_EVENT;551552/* We store the user_handle with the first event */553if (i == 0 && p->signal_page)554ev_priv->user_handle = p->signal_handle;555556ev_priv->event_id = ev->event_id;557ev_priv->auto_reset = ev->auto_reset;558ev_priv->type = ev->type;559ev_priv->signaled = ev->signaled;560561if (ev_priv->type == KFD_EVENT_TYPE_MEMORY)562memcpy(&ev_priv->memory_exception_data,563&ev->memory_exception_data,564sizeof(struct kfd_hsa_memory_exception_data));565else if (ev_priv->type == KFD_EVENT_TYPE_HW_EXCEPTION)566memcpy(&ev_priv->hw_exception_data,567&ev->hw_exception_data,568sizeof(struct kfd_hsa_hw_exception_data));569570pr_debug("Checkpointed event[%d] id = 0x%08x auto_reset = %x type = %x signaled = %x\n",571i,572ev_priv->event_id,573ev_priv->auto_reset,574ev_priv->type,575ev_priv->signaled);576i++;577}578579ret = copy_to_user(user_priv_data + *priv_data_offset,580ev_privs, num_events * sizeof(*ev_privs));581if (ret) {582pr_err("Failed to copy events priv to user\n");583ret = -EFAULT;584}585586*priv_data_offset += num_events * sizeof(*ev_privs);587588kvfree(ev_privs);589return ret;590}591592int kfd_get_num_events(struct kfd_process *p)593{594struct kfd_event *ev;595uint32_t id;596u32 num_events = 0;597598idr_for_each_entry(&p->event_idr, ev, id)599num_events++;600601return num_events;602}603604/* Assumes that p is current. */605int kfd_event_destroy(struct kfd_process *p, uint32_t event_id)606{607struct kfd_event *ev;608int ret = 0;609610mutex_lock(&p->event_mutex);611612ev = lookup_event_by_id(p, event_id);613614if (ev)615destroy_event(p, ev);616else617ret = -EINVAL;618619mutex_unlock(&p->event_mutex);620return ret;621}622623static void set_event(struct kfd_event *ev)624{625struct kfd_event_waiter *waiter;626627/* Auto reset if the list is non-empty and we're waking628* someone. waitqueue_active is safe here because we're629* protected by the ev->lock, which is also held when630* updating the wait queues in kfd_wait_on_events.631*/632ev->signaled = !ev->auto_reset || !waitqueue_active(&ev->wq);633if (!(++ev->event_age)) {634/* Never wrap back to reserved/default event age 0/1 */635ev->event_age = 2;636WARN_ONCE(1, "event_age wrap back!");637}638639list_for_each_entry(waiter, &ev->wq.head, wait.entry)640WRITE_ONCE(waiter->activated, true);641642wake_up_all(&ev->wq);643}644645/* Assumes that p is current. */646int kfd_set_event(struct kfd_process *p, uint32_t event_id)647{648int ret = 0;649struct kfd_event *ev;650651rcu_read_lock();652653ev = lookup_event_by_id(p, event_id);654if (!ev) {655ret = -EINVAL;656goto unlock_rcu;657}658spin_lock(&ev->lock);659660if (event_can_be_cpu_signaled(ev))661set_event(ev);662else663ret = -EINVAL;664665spin_unlock(&ev->lock);666unlock_rcu:667rcu_read_unlock();668return ret;669}670671static void reset_event(struct kfd_event *ev)672{673ev->signaled = false;674}675676/* Assumes that p is current. */677int kfd_reset_event(struct kfd_process *p, uint32_t event_id)678{679int ret = 0;680struct kfd_event *ev;681682rcu_read_lock();683684ev = lookup_event_by_id(p, event_id);685if (!ev) {686ret = -EINVAL;687goto unlock_rcu;688}689spin_lock(&ev->lock);690691if (event_can_be_cpu_signaled(ev))692reset_event(ev);693else694ret = -EINVAL;695696spin_unlock(&ev->lock);697unlock_rcu:698rcu_read_unlock();699return ret;700701}702703static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev)704{705WRITE_ONCE(page_slots(p->signal_page)[ev->event_id], UNSIGNALED_EVENT_SLOT);706}707708static void set_event_from_interrupt(struct kfd_process *p,709struct kfd_event *ev)710{711if (ev && event_can_be_gpu_signaled(ev)) {712acknowledge_signal(p, ev);713spin_lock(&ev->lock);714set_event(ev);715spin_unlock(&ev->lock);716}717}718719void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,720uint32_t valid_id_bits)721{722struct kfd_event *ev = NULL;723724/*725* Because we are called from arbitrary context (workqueue) as opposed726* to process context, kfd_process could attempt to exit while we are727* running so the lookup function increments the process ref count.728*/729struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL);730731if (!p)732return; /* Presumably process exited. */733734rcu_read_lock();735736if (valid_id_bits)737ev = lookup_signaled_event_by_partial_id(p, partial_id,738valid_id_bits);739if (ev) {740set_event_from_interrupt(p, ev);741} else if (p->signal_page) {742/*743* Partial ID lookup failed. Assume that the event ID744* in the interrupt payload was invalid and do an745* exhaustive search of signaled events.746*/747uint64_t *slots = page_slots(p->signal_page);748uint32_t id;749750/*751* If id is valid but slot is not signaled, GPU may signal the same event twice752* before driver have chance to process the first interrupt, then signal slot is753* auto-reset after set_event wakeup the user space, just drop the second event as754* the application only need wakeup once.755*/756if ((valid_id_bits > 31 || (1U << valid_id_bits) >= KFD_SIGNAL_EVENT_LIMIT) &&757partial_id < KFD_SIGNAL_EVENT_LIMIT && slots[partial_id] == UNSIGNALED_EVENT_SLOT)758goto out_unlock;759760if (valid_id_bits)761pr_debug_ratelimited("Partial ID invalid: %u (%u valid bits)\n",762partial_id, valid_id_bits);763764if (p->signal_event_count < KFD_SIGNAL_EVENT_LIMIT / 64) {765/* With relatively few events, it's faster to766* iterate over the event IDR767*/768idr_for_each_entry(&p->event_idr, ev, id) {769if (id >= KFD_SIGNAL_EVENT_LIMIT)770break;771772if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT)773set_event_from_interrupt(p, ev);774}775} else {776/* With relatively many events, it's faster to777* iterate over the signal slots and lookup778* only signaled events from the IDR.779*/780for (id = 1; id < KFD_SIGNAL_EVENT_LIMIT; id++)781if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT) {782ev = lookup_event_by_id(p, id);783set_event_from_interrupt(p, ev);784}785}786}787788out_unlock:789rcu_read_unlock();790kfd_unref_process(p);791}792793static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events)794{795struct kfd_event_waiter *event_waiters;796uint32_t i;797798event_waiters = kcalloc(num_events, sizeof(struct kfd_event_waiter),799GFP_KERNEL);800if (!event_waiters)801return NULL;802803for (i = 0; i < num_events; i++)804init_wait(&event_waiters[i].wait);805806return event_waiters;807}808809static int init_event_waiter(struct kfd_process *p,810struct kfd_event_waiter *waiter,811struct kfd_event_data *event_data)812{813struct kfd_event *ev = lookup_event_by_id(p, event_data->event_id);814815if (!ev)816return -EINVAL;817818spin_lock(&ev->lock);819waiter->event = ev;820waiter->activated = ev->signaled;821ev->signaled = ev->signaled && !ev->auto_reset;822823/* last_event_age = 0 reserved for backward compatible */824if (waiter->event->type == KFD_EVENT_TYPE_SIGNAL &&825event_data->signal_event_data.last_event_age) {826waiter->event_age_enabled = true;827if (ev->event_age != event_data->signal_event_data.last_event_age)828waiter->activated = true;829}830831if (!waiter->activated)832add_wait_queue(&ev->wq, &waiter->wait);833spin_unlock(&ev->lock);834835return 0;836}837838/* test_event_condition - Test condition of events being waited for839* @all: Return completion only if all events have signaled840* @num_events: Number of events to wait for841* @event_waiters: Array of event waiters, one per event842*843* Returns KFD_IOC_WAIT_RESULT_COMPLETE if all (or one) event(s) have844* signaled. Returns KFD_IOC_WAIT_RESULT_TIMEOUT if no (or not all)845* events have signaled. Returns KFD_IOC_WAIT_RESULT_FAIL if any of846* the events have been destroyed.847*/848static uint32_t test_event_condition(bool all, uint32_t num_events,849struct kfd_event_waiter *event_waiters)850{851uint32_t i;852uint32_t activated_count = 0;853854for (i = 0; i < num_events; i++) {855if (!READ_ONCE(event_waiters[i].event))856return KFD_IOC_WAIT_RESULT_FAIL;857858if (READ_ONCE(event_waiters[i].activated)) {859if (!all)860return KFD_IOC_WAIT_RESULT_COMPLETE;861862activated_count++;863}864}865866return activated_count == num_events ?867KFD_IOC_WAIT_RESULT_COMPLETE : KFD_IOC_WAIT_RESULT_TIMEOUT;868}869870/*871* Copy event specific data, if defined.872* Currently only memory exception events have additional data to copy to user873*/874static int copy_signaled_event_data(uint32_t num_events,875struct kfd_event_waiter *event_waiters,876struct kfd_event_data __user *data)877{878void *src;879void __user *dst;880struct kfd_event_waiter *waiter;881struct kfd_event *event;882uint32_t i, size = 0;883884for (i = 0; i < num_events; i++) {885waiter = &event_waiters[i];886event = waiter->event;887if (!event)888return -EINVAL; /* event was destroyed */889if (waiter->activated) {890if (event->type == KFD_EVENT_TYPE_MEMORY) {891dst = &data[i].memory_exception_data;892src = &event->memory_exception_data;893size = sizeof(struct kfd_hsa_memory_exception_data);894} else if (event->type == KFD_EVENT_TYPE_HW_EXCEPTION) {895dst = &data[i].memory_exception_data;896src = &event->hw_exception_data;897size = sizeof(struct kfd_hsa_hw_exception_data);898} else if (event->type == KFD_EVENT_TYPE_SIGNAL &&899waiter->event_age_enabled) {900dst = &data[i].signal_event_data.last_event_age;901src = &event->event_age;902size = sizeof(u64);903}904if (size && copy_to_user(dst, src, size))905return -EFAULT;906}907}908909return 0;910}911912static long user_timeout_to_jiffies(uint32_t user_timeout_ms)913{914if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE)915return 0;916917if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE)918return MAX_SCHEDULE_TIMEOUT;919920/*921* msecs_to_jiffies interprets all values above 2^31-1 as infinite,922* but we consider them finite.923* This hack is wrong, but nobody is likely to notice.924*/925user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF);926927return msecs_to_jiffies(user_timeout_ms) + 1;928}929930static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters,931bool undo_auto_reset)932{933uint32_t i;934935for (i = 0; i < num_events; i++)936if (waiters[i].event) {937spin_lock(&waiters[i].event->lock);938remove_wait_queue(&waiters[i].event->wq,939&waiters[i].wait);940if (undo_auto_reset && waiters[i].activated &&941waiters[i].event && waiters[i].event->auto_reset)942set_event(waiters[i].event);943spin_unlock(&waiters[i].event->lock);944}945946kfree(waiters);947}948949int kfd_wait_on_events(struct kfd_process *p,950uint32_t num_events, void __user *data,951bool all, uint32_t *user_timeout_ms,952uint32_t *wait_result)953{954struct kfd_event_data __user *events =955(struct kfd_event_data __user *) data;956uint32_t i;957int ret = 0;958959struct kfd_event_waiter *event_waiters = NULL;960long timeout = user_timeout_to_jiffies(*user_timeout_ms);961962event_waiters = alloc_event_waiters(num_events);963if (!event_waiters) {964ret = -ENOMEM;965goto out;966}967968/* Use p->event_mutex here to protect against concurrent creation and969* destruction of events while we initialize event_waiters.970*/971mutex_lock(&p->event_mutex);972973for (i = 0; i < num_events; i++) {974struct kfd_event_data event_data;975976if (copy_from_user(&event_data, &events[i],977sizeof(struct kfd_event_data))) {978ret = -EFAULT;979goto out_unlock;980}981982ret = init_event_waiter(p, &event_waiters[i], &event_data);983if (ret)984goto out_unlock;985}986987/* Check condition once. */988*wait_result = test_event_condition(all, num_events, event_waiters);989if (*wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) {990ret = copy_signaled_event_data(num_events,991event_waiters, events);992goto out_unlock;993} else if (WARN_ON(*wait_result == KFD_IOC_WAIT_RESULT_FAIL)) {994/* This should not happen. Events shouldn't be995* destroyed while we're holding the event_mutex996*/997goto out_unlock;998}9991000mutex_unlock(&p->event_mutex);10011002while (true) {1003if (fatal_signal_pending(current)) {1004ret = -EINTR;1005break;1006}10071008if (signal_pending(current)) {1009ret = -ERESTARTSYS;1010if (*user_timeout_ms != KFD_EVENT_TIMEOUT_IMMEDIATE &&1011*user_timeout_ms != KFD_EVENT_TIMEOUT_INFINITE)1012*user_timeout_ms = jiffies_to_msecs(1013max(0l, timeout-1));1014break;1015}10161017/* Set task state to interruptible sleep before1018* checking wake-up conditions. A concurrent wake-up1019* will put the task back into runnable state. In that1020* case schedule_timeout will not put the task to1021* sleep and we'll get a chance to re-check the1022* updated conditions almost immediately. Otherwise,1023* this race condition would lead to a soft hang or a1024* very long sleep.1025*/1026set_current_state(TASK_INTERRUPTIBLE);10271028*wait_result = test_event_condition(all, num_events,1029event_waiters);1030if (*wait_result != KFD_IOC_WAIT_RESULT_TIMEOUT)1031break;10321033if (timeout <= 0)1034break;10351036timeout = schedule_timeout(timeout);1037}1038__set_current_state(TASK_RUNNING);10391040mutex_lock(&p->event_mutex);1041/* copy_signaled_event_data may sleep. So this has to happen1042* after the task state is set back to RUNNING.1043*1044* The event may also have been destroyed after signaling. So1045* copy_signaled_event_data also must confirm that the event1046* still exists. Therefore this must be under the p->event_mutex1047* which is also held when events are destroyed.1048*/1049if (!ret && *wait_result == KFD_IOC_WAIT_RESULT_COMPLETE)1050ret = copy_signaled_event_data(num_events,1051event_waiters, events);10521053out_unlock:1054free_waiters(num_events, event_waiters, ret == -ERESTARTSYS);1055mutex_unlock(&p->event_mutex);1056out:1057if (ret)1058*wait_result = KFD_IOC_WAIT_RESULT_FAIL;1059else if (*wait_result == KFD_IOC_WAIT_RESULT_FAIL)1060ret = -EIO;10611062return ret;1063}10641065int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma)1066{1067unsigned long pfn;1068struct kfd_signal_page *page;1069int ret;10701071/* check required size doesn't exceed the allocated size */1072if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) <1073get_order(vma->vm_end - vma->vm_start)) {1074pr_err("Event page mmap requested illegal size\n");1075return -EINVAL;1076}10771078page = p->signal_page;1079if (!page) {1080/* Probably KFD bug, but mmap is user-accessible. */1081pr_debug("Signal page could not be found\n");1082return -EINVAL;1083}10841085pfn = __pa(page->kernel_address);1086pfn >>= PAGE_SHIFT;10871088vm_flags_set(vma, VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE1089| VM_DONTDUMP | VM_PFNMAP);10901091pr_debug("Mapping signal page\n");1092pr_debug(" start user address == 0x%08lx\n", vma->vm_start);1093pr_debug(" end user address == 0x%08lx\n", vma->vm_end);1094pr_debug(" pfn == 0x%016lX\n", pfn);1095pr_debug(" vm_flags == 0x%08lX\n", vma->vm_flags);1096pr_debug(" size == 0x%08lX\n",1097vma->vm_end - vma->vm_start);10981099page->user_address = (uint64_t __user *)vma->vm_start;11001101/* mapping the page to user process */1102ret = remap_pfn_range(vma, vma->vm_start, pfn,1103vma->vm_end - vma->vm_start, vma->vm_page_prot);1104if (!ret)1105p->signal_mapped_size = vma->vm_end - vma->vm_start;11061107return ret;1108}11091110/*1111* Assumes that p is not going away.1112*/1113static void lookup_events_by_type_and_signal(struct kfd_process *p,1114int type, void *event_data)1115{1116struct kfd_hsa_memory_exception_data *ev_data;1117struct kfd_event *ev;1118uint32_t id;1119bool send_signal = true;11201121ev_data = (struct kfd_hsa_memory_exception_data *) event_data;11221123rcu_read_lock();11241125id = KFD_FIRST_NONSIGNAL_EVENT_ID;1126idr_for_each_entry_continue(&p->event_idr, ev, id)1127if (ev->type == type) {1128send_signal = false;1129dev_dbg(kfd_device,1130"Event found: id %X type %d",1131ev->event_id, ev->type);1132spin_lock(&ev->lock);1133set_event(ev);1134if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)1135ev->memory_exception_data = *ev_data;1136spin_unlock(&ev->lock);1137}11381139if (type == KFD_EVENT_TYPE_MEMORY) {1140dev_warn(kfd_device,1141"Sending SIGSEGV to process pid %d",1142p->lead_thread->pid);1143send_sig(SIGSEGV, p->lead_thread, 0);1144}11451146/* Send SIGTERM no event of type "type" has been found*/1147if (send_signal) {1148if (send_sigterm) {1149dev_warn(kfd_device,1150"Sending SIGTERM to process pid %d",1151p->lead_thread->pid);1152send_sig(SIGTERM, p->lead_thread, 0);1153} else {1154dev_err(kfd_device,1155"Process pid %d got unhandled exception",1156p->lead_thread->pid);1157}1158}11591160rcu_read_unlock();1161}11621163void kfd_signal_hw_exception_event(u32 pasid)1164{1165/*1166* Because we are called from arbitrary context (workqueue) as opposed1167* to process context, kfd_process could attempt to exit while we are1168* running so the lookup function increments the process ref count.1169*/1170struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL);11711172if (!p)1173return; /* Presumably process exited. */11741175lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL);1176kfd_unref_process(p);1177}11781179void kfd_signal_vm_fault_event_with_userptr(struct kfd_process *p, uint64_t gpu_va)1180{1181struct kfd_process_device *pdd;1182struct kfd_hsa_memory_exception_data exception_data;1183int i;11841185memset(&exception_data, 0, sizeof(exception_data));1186exception_data.va = gpu_va;1187exception_data.failure.NotPresent = 1;11881189// Send VM seg fault to all kfd process device1190for (i = 0; i < p->n_pdds; i++) {1191pdd = p->pdds[i];1192exception_data.gpu_id = pdd->user_gpu_id;1193kfd_evict_process_device(pdd);1194kfd_signal_vm_fault_event(pdd, NULL, &exception_data);1195}1196}11971198void kfd_signal_vm_fault_event(struct kfd_process_device *pdd,1199struct kfd_vm_fault_info *info,1200struct kfd_hsa_memory_exception_data *data)1201{1202struct kfd_event *ev;1203uint32_t id;1204struct kfd_process *p = pdd->process;1205struct kfd_hsa_memory_exception_data memory_exception_data;1206int user_gpu_id;12071208user_gpu_id = kfd_process_get_user_gpu_id(p, pdd->dev->id);1209if (unlikely(user_gpu_id == -EINVAL)) {1210WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n",1211pdd->dev->id);1212return;1213}12141215/* SoC15 chips and onwards will pass in data from now on. */1216if (!data) {1217memset(&memory_exception_data, 0, sizeof(memory_exception_data));1218memory_exception_data.gpu_id = user_gpu_id;1219memory_exception_data.failure.imprecise = true;12201221/* Set failure reason */1222if (info) {1223memory_exception_data.va = (info->page_addr) <<1224PAGE_SHIFT;1225memory_exception_data.failure.NotPresent =1226info->prot_valid ? 1 : 0;1227memory_exception_data.failure.NoExecute =1228info->prot_exec ? 1 : 0;1229memory_exception_data.failure.ReadOnly =1230info->prot_write ? 1 : 0;1231memory_exception_data.failure.imprecise = 0;1232}1233}12341235rcu_read_lock();12361237id = KFD_FIRST_NONSIGNAL_EVENT_ID;1238idr_for_each_entry_continue(&p->event_idr, ev, id)1239if (ev->type == KFD_EVENT_TYPE_MEMORY) {1240spin_lock(&ev->lock);1241ev->memory_exception_data = data ? *data :1242memory_exception_data;1243set_event(ev);1244spin_unlock(&ev->lock);1245}12461247rcu_read_unlock();1248}12491250void kfd_signal_reset_event(struct kfd_node *dev)1251{1252struct kfd_hsa_hw_exception_data hw_exception_data;1253struct kfd_hsa_memory_exception_data memory_exception_data;1254struct kfd_process *p;1255struct kfd_event *ev;1256unsigned int temp;1257uint32_t id, idx;1258int reset_cause = atomic_read(&dev->sram_ecc_flag) ?1259KFD_HW_EXCEPTION_ECC :1260KFD_HW_EXCEPTION_GPU_HANG;12611262/* Whole gpu reset caused by GPU hang and memory is lost */1263memset(&hw_exception_data, 0, sizeof(hw_exception_data));1264hw_exception_data.memory_lost = 1;1265hw_exception_data.reset_cause = reset_cause;12661267memset(&memory_exception_data, 0, sizeof(memory_exception_data));1268memory_exception_data.ErrorType = KFD_MEM_ERR_SRAM_ECC;1269memory_exception_data.failure.imprecise = true;12701271idx = srcu_read_lock(&kfd_processes_srcu);1272hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {1273int user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);1274struct kfd_process_device *pdd = kfd_get_process_device_data(dev, p);12751276if (unlikely(user_gpu_id == -EINVAL)) {1277WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);1278continue;1279}12801281if (unlikely(!pdd)) {1282WARN_ONCE(1, "Could not get device data from process pid:%d\n",1283p->lead_thread->pid);1284continue;1285}12861287if (dev->dqm->detect_hang_count && !pdd->has_reset_queue)1288continue;12891290if (dev->dqm->detect_hang_count) {1291struct amdgpu_task_info *ti;1292struct amdgpu_fpriv *drv_priv;12931294if (unlikely(amdgpu_file_to_fpriv(pdd->drm_file, &drv_priv))) {1295WARN_ONCE(1, "Could not get vm for device %x from pid:%d\n",1296dev->id, p->lead_thread->pid);1297continue;1298}12991300ti = amdgpu_vm_get_task_info_vm(&drv_priv->vm);1301if (ti) {1302dev_err(dev->adev->dev,1303"Queues reset on process %s tid %d thread %s pid %d\n",1304ti->process_name, ti->tgid, ti->task.comm, ti->task.pid);1305amdgpu_vm_put_task_info(ti);1306}1307}13081309rcu_read_lock();13101311id = KFD_FIRST_NONSIGNAL_EVENT_ID;1312idr_for_each_entry_continue(&p->event_idr, ev, id) {1313if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {1314spin_lock(&ev->lock);1315ev->hw_exception_data = hw_exception_data;1316ev->hw_exception_data.gpu_id = user_gpu_id;1317set_event(ev);1318spin_unlock(&ev->lock);1319}1320if (ev->type == KFD_EVENT_TYPE_MEMORY &&1321reset_cause == KFD_HW_EXCEPTION_ECC) {1322spin_lock(&ev->lock);1323ev->memory_exception_data = memory_exception_data;1324ev->memory_exception_data.gpu_id = user_gpu_id;1325set_event(ev);1326spin_unlock(&ev->lock);1327}1328}13291330rcu_read_unlock();1331}1332srcu_read_unlock(&kfd_processes_srcu, idx);1333}13341335void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid)1336{1337struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL);1338struct kfd_hsa_memory_exception_data memory_exception_data;1339struct kfd_hsa_hw_exception_data hw_exception_data;1340struct kfd_event *ev;1341uint32_t id = KFD_FIRST_NONSIGNAL_EVENT_ID;1342int user_gpu_id;13431344if (!p) {1345dev_warn(dev->adev->dev, "Not find process with pasid:%d\n", pasid);1346return; /* Presumably process exited. */1347}13481349user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);1350if (unlikely(user_gpu_id == -EINVAL)) {1351WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);1352kfd_unref_process(p);1353return;1354}13551356memset(&hw_exception_data, 0, sizeof(hw_exception_data));1357hw_exception_data.gpu_id = user_gpu_id;1358hw_exception_data.memory_lost = 1;1359hw_exception_data.reset_cause = KFD_HW_EXCEPTION_ECC;13601361memset(&memory_exception_data, 0, sizeof(memory_exception_data));1362memory_exception_data.ErrorType = KFD_MEM_ERR_POISON_CONSUMED;1363memory_exception_data.gpu_id = user_gpu_id;1364memory_exception_data.failure.imprecise = true;13651366rcu_read_lock();13671368idr_for_each_entry_continue(&p->event_idr, ev, id) {1369if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {1370spin_lock(&ev->lock);1371ev->hw_exception_data = hw_exception_data;1372set_event(ev);1373spin_unlock(&ev->lock);1374}13751376if (ev->type == KFD_EVENT_TYPE_MEMORY) {1377spin_lock(&ev->lock);1378ev->memory_exception_data = memory_exception_data;1379set_event(ev);1380spin_unlock(&ev->lock);1381}1382}13831384dev_warn(dev->adev->dev, "Send SIGBUS to process %s(pasid:%d)\n",1385p->lead_thread->comm, pasid);1386rcu_read_unlock();13871388/* user application will handle SIGBUS signal */1389send_sig(SIGBUS, p->lead_thread, 0);13901391kfd_unref_process(p);1392}139313941395