Path: blob/master/drivers/gpu/drm/amd/amdkfd/kfd_events.c
50693 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;749750if (valid_id_bits)751pr_debug_ratelimited("Partial ID invalid: %u (%u valid bits)\n",752partial_id, valid_id_bits);753754if (p->signal_event_count < KFD_SIGNAL_EVENT_LIMIT / 64) {755/* With relatively few events, it's faster to756* iterate over the event IDR757*/758idr_for_each_entry(&p->event_idr, ev, id) {759if (id >= KFD_SIGNAL_EVENT_LIMIT)760break;761762if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT)763set_event_from_interrupt(p, ev);764}765} else {766/* With relatively many events, it's faster to767* iterate over the signal slots and lookup768* only signaled events from the IDR.769*/770for (id = 1; id < KFD_SIGNAL_EVENT_LIMIT; id++)771if (READ_ONCE(slots[id]) != UNSIGNALED_EVENT_SLOT) {772ev = lookup_event_by_id(p, id);773set_event_from_interrupt(p, ev);774}775}776}777778rcu_read_unlock();779kfd_unref_process(p);780}781782static struct kfd_event_waiter *alloc_event_waiters(uint32_t num_events)783{784struct kfd_event_waiter *event_waiters;785uint32_t i;786787event_waiters = kcalloc(num_events, sizeof(struct kfd_event_waiter),788GFP_KERNEL);789if (!event_waiters)790return NULL;791792for (i = 0; i < num_events; i++)793init_wait(&event_waiters[i].wait);794795return event_waiters;796}797798static int init_event_waiter(struct kfd_process *p,799struct kfd_event_waiter *waiter,800struct kfd_event_data *event_data)801{802struct kfd_event *ev = lookup_event_by_id(p, event_data->event_id);803804if (!ev)805return -EINVAL;806807spin_lock(&ev->lock);808waiter->event = ev;809waiter->activated = ev->signaled;810ev->signaled = ev->signaled && !ev->auto_reset;811812/* last_event_age = 0 reserved for backward compatible */813if (waiter->event->type == KFD_EVENT_TYPE_SIGNAL &&814event_data->signal_event_data.last_event_age) {815waiter->event_age_enabled = true;816if (ev->event_age != event_data->signal_event_data.last_event_age)817waiter->activated = true;818}819820if (!waiter->activated)821add_wait_queue(&ev->wq, &waiter->wait);822spin_unlock(&ev->lock);823824return 0;825}826827/* test_event_condition - Test condition of events being waited for828* @all: Return completion only if all events have signaled829* @num_events: Number of events to wait for830* @event_waiters: Array of event waiters, one per event831*832* Returns KFD_IOC_WAIT_RESULT_COMPLETE if all (or one) event(s) have833* signaled. Returns KFD_IOC_WAIT_RESULT_TIMEOUT if no (or not all)834* events have signaled. Returns KFD_IOC_WAIT_RESULT_FAIL if any of835* the events have been destroyed.836*/837static uint32_t test_event_condition(bool all, uint32_t num_events,838struct kfd_event_waiter *event_waiters)839{840uint32_t i;841uint32_t activated_count = 0;842843for (i = 0; i < num_events; i++) {844if (!READ_ONCE(event_waiters[i].event))845return KFD_IOC_WAIT_RESULT_FAIL;846847if (READ_ONCE(event_waiters[i].activated)) {848if (!all)849return KFD_IOC_WAIT_RESULT_COMPLETE;850851activated_count++;852}853}854855return activated_count == num_events ?856KFD_IOC_WAIT_RESULT_COMPLETE : KFD_IOC_WAIT_RESULT_TIMEOUT;857}858859/*860* Copy event specific data, if defined.861* Currently only memory exception events have additional data to copy to user862*/863static int copy_signaled_event_data(uint32_t num_events,864struct kfd_event_waiter *event_waiters,865struct kfd_event_data __user *data)866{867void *src;868void __user *dst;869struct kfd_event_waiter *waiter;870struct kfd_event *event;871uint32_t i, size = 0;872873for (i = 0; i < num_events; i++) {874waiter = &event_waiters[i];875event = waiter->event;876if (!event)877return -EINVAL; /* event was destroyed */878if (waiter->activated) {879if (event->type == KFD_EVENT_TYPE_MEMORY) {880dst = &data[i].memory_exception_data;881src = &event->memory_exception_data;882size = sizeof(struct kfd_hsa_memory_exception_data);883} else if (event->type == KFD_EVENT_TYPE_HW_EXCEPTION) {884dst = &data[i].memory_exception_data;885src = &event->hw_exception_data;886size = sizeof(struct kfd_hsa_hw_exception_data);887} else if (event->type == KFD_EVENT_TYPE_SIGNAL &&888waiter->event_age_enabled) {889dst = &data[i].signal_event_data.last_event_age;890src = &event->event_age;891size = sizeof(u64);892}893if (size && copy_to_user(dst, src, size))894return -EFAULT;895}896}897898return 0;899}900901static long user_timeout_to_jiffies(uint32_t user_timeout_ms)902{903if (user_timeout_ms == KFD_EVENT_TIMEOUT_IMMEDIATE)904return 0;905906if (user_timeout_ms == KFD_EVENT_TIMEOUT_INFINITE)907return MAX_SCHEDULE_TIMEOUT;908909/*910* msecs_to_jiffies interprets all values above 2^31-1 as infinite,911* but we consider them finite.912* This hack is wrong, but nobody is likely to notice.913*/914user_timeout_ms = min_t(uint32_t, user_timeout_ms, 0x7FFFFFFF);915916return msecs_to_jiffies(user_timeout_ms) + 1;917}918919static void free_waiters(uint32_t num_events, struct kfd_event_waiter *waiters,920bool undo_auto_reset)921{922uint32_t i;923924for (i = 0; i < num_events; i++)925if (waiters[i].event) {926spin_lock(&waiters[i].event->lock);927remove_wait_queue(&waiters[i].event->wq,928&waiters[i].wait);929if (undo_auto_reset && waiters[i].activated &&930waiters[i].event && waiters[i].event->auto_reset)931set_event(waiters[i].event);932spin_unlock(&waiters[i].event->lock);933}934935kfree(waiters);936}937938int kfd_wait_on_events(struct kfd_process *p,939uint32_t num_events, void __user *data,940bool all, uint32_t *user_timeout_ms,941uint32_t *wait_result)942{943struct kfd_event_data __user *events =944(struct kfd_event_data __user *) data;945uint32_t i;946int ret = 0;947948struct kfd_event_waiter *event_waiters = NULL;949long timeout = user_timeout_to_jiffies(*user_timeout_ms);950951event_waiters = alloc_event_waiters(num_events);952if (!event_waiters) {953ret = -ENOMEM;954goto out;955}956957/* Use p->event_mutex here to protect against concurrent creation and958* destruction of events while we initialize event_waiters.959*/960mutex_lock(&p->event_mutex);961962for (i = 0; i < num_events; i++) {963struct kfd_event_data event_data;964965if (copy_from_user(&event_data, &events[i],966sizeof(struct kfd_event_data))) {967ret = -EFAULT;968goto out_unlock;969}970971ret = init_event_waiter(p, &event_waiters[i], &event_data);972if (ret)973goto out_unlock;974}975976/* Check condition once. */977*wait_result = test_event_condition(all, num_events, event_waiters);978if (*wait_result == KFD_IOC_WAIT_RESULT_COMPLETE) {979ret = copy_signaled_event_data(num_events,980event_waiters, events);981goto out_unlock;982} else if (WARN_ON(*wait_result == KFD_IOC_WAIT_RESULT_FAIL)) {983/* This should not happen. Events shouldn't be984* destroyed while we're holding the event_mutex985*/986goto out_unlock;987}988989mutex_unlock(&p->event_mutex);990991while (true) {992if (fatal_signal_pending(current)) {993ret = -EINTR;994break;995}996997if (signal_pending(current)) {998ret = -ERESTARTSYS;999if (*user_timeout_ms != KFD_EVENT_TIMEOUT_IMMEDIATE &&1000*user_timeout_ms != KFD_EVENT_TIMEOUT_INFINITE)1001*user_timeout_ms = jiffies_to_msecs(1002max(0l, timeout-1));1003break;1004}10051006/* Set task state to interruptible sleep before1007* checking wake-up conditions. A concurrent wake-up1008* will put the task back into runnable state. In that1009* case schedule_timeout will not put the task to1010* sleep and we'll get a chance to re-check the1011* updated conditions almost immediately. Otherwise,1012* this race condition would lead to a soft hang or a1013* very long sleep.1014*/1015set_current_state(TASK_INTERRUPTIBLE);10161017*wait_result = test_event_condition(all, num_events,1018event_waiters);1019if (*wait_result != KFD_IOC_WAIT_RESULT_TIMEOUT)1020break;10211022if (timeout <= 0)1023break;10241025timeout = schedule_timeout(timeout);1026}1027__set_current_state(TASK_RUNNING);10281029mutex_lock(&p->event_mutex);1030/* copy_signaled_event_data may sleep. So this has to happen1031* after the task state is set back to RUNNING.1032*1033* The event may also have been destroyed after signaling. So1034* copy_signaled_event_data also must confirm that the event1035* still exists. Therefore this must be under the p->event_mutex1036* which is also held when events are destroyed.1037*/1038if (!ret && *wait_result == KFD_IOC_WAIT_RESULT_COMPLETE)1039ret = copy_signaled_event_data(num_events,1040event_waiters, events);10411042out_unlock:1043free_waiters(num_events, event_waiters, ret == -ERESTARTSYS);1044mutex_unlock(&p->event_mutex);1045out:1046if (ret)1047*wait_result = KFD_IOC_WAIT_RESULT_FAIL;1048else if (*wait_result == KFD_IOC_WAIT_RESULT_FAIL)1049ret = -EIO;10501051return ret;1052}10531054int kfd_event_mmap(struct kfd_process *p, struct vm_area_struct *vma)1055{1056unsigned long pfn;1057struct kfd_signal_page *page;1058int ret;10591060/* check required size doesn't exceed the allocated size */1061if (get_order(KFD_SIGNAL_EVENT_LIMIT * 8) <1062get_order(vma->vm_end - vma->vm_start)) {1063pr_err("Event page mmap requested illegal size\n");1064return -EINVAL;1065}10661067page = p->signal_page;1068if (!page) {1069/* Probably KFD bug, but mmap is user-accessible. */1070pr_debug("Signal page could not be found\n");1071return -EINVAL;1072}10731074pfn = __pa(page->kernel_address);1075pfn >>= PAGE_SHIFT;10761077vm_flags_set(vma, VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_NORESERVE1078| VM_DONTDUMP | VM_PFNMAP);10791080pr_debug("Mapping signal page\n");1081pr_debug(" start user address == 0x%08lx\n", vma->vm_start);1082pr_debug(" end user address == 0x%08lx\n", vma->vm_end);1083pr_debug(" pfn == 0x%016lX\n", pfn);1084pr_debug(" vm_flags == 0x%08lX\n", vma->vm_flags);1085pr_debug(" size == 0x%08lX\n",1086vma->vm_end - vma->vm_start);10871088page->user_address = (uint64_t __user *)vma->vm_start;10891090/* mapping the page to user process */1091ret = remap_pfn_range(vma, vma->vm_start, pfn,1092vma->vm_end - vma->vm_start, vma->vm_page_prot);1093if (!ret)1094p->signal_mapped_size = vma->vm_end - vma->vm_start;10951096return ret;1097}10981099/*1100* Assumes that p is not going away.1101*/1102static void lookup_events_by_type_and_signal(struct kfd_process *p,1103int type, void *event_data)1104{1105struct kfd_hsa_memory_exception_data *ev_data;1106struct kfd_event *ev;1107uint32_t id;1108bool send_signal = true;11091110ev_data = (struct kfd_hsa_memory_exception_data *) event_data;11111112rcu_read_lock();11131114id = KFD_FIRST_NONSIGNAL_EVENT_ID;1115idr_for_each_entry_continue(&p->event_idr, ev, id)1116if (ev->type == type) {1117send_signal = false;1118dev_dbg(kfd_device,1119"Event found: id %X type %d",1120ev->event_id, ev->type);1121spin_lock(&ev->lock);1122set_event(ev);1123if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)1124ev->memory_exception_data = *ev_data;1125spin_unlock(&ev->lock);1126}11271128if (type == KFD_EVENT_TYPE_MEMORY) {1129dev_warn(kfd_device,1130"Sending SIGSEGV to process pid %d",1131p->lead_thread->pid);1132send_sig(SIGSEGV, p->lead_thread, 0);1133}11341135/* Send SIGTERM no event of type "type" has been found*/1136if (send_signal) {1137if (send_sigterm) {1138dev_warn(kfd_device,1139"Sending SIGTERM to process pid %d",1140p->lead_thread->pid);1141send_sig(SIGTERM, p->lead_thread, 0);1142} else {1143dev_err(kfd_device,1144"Process pid %d got unhandled exception",1145p->lead_thread->pid);1146}1147}11481149rcu_read_unlock();1150}11511152void kfd_signal_hw_exception_event(u32 pasid)1153{1154/*1155* Because we are called from arbitrary context (workqueue) as opposed1156* to process context, kfd_process could attempt to exit while we are1157* running so the lookup function increments the process ref count.1158*/1159struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL);11601161if (!p)1162return; /* Presumably process exited. */11631164lookup_events_by_type_and_signal(p, KFD_EVENT_TYPE_HW_EXCEPTION, NULL);1165kfd_unref_process(p);1166}11671168void kfd_signal_vm_fault_event_with_userptr(struct kfd_process *p, uint64_t gpu_va)1169{1170struct kfd_process_device *pdd;1171struct kfd_hsa_memory_exception_data exception_data;1172int i;11731174memset(&exception_data, 0, sizeof(exception_data));1175exception_data.va = gpu_va;1176exception_data.failure.NotPresent = 1;11771178// Send VM seg fault to all kfd process device1179for (i = 0; i < p->n_pdds; i++) {1180pdd = p->pdds[i];1181exception_data.gpu_id = pdd->user_gpu_id;1182kfd_evict_process_device(pdd);1183kfd_signal_vm_fault_event(pdd, NULL, &exception_data);1184}1185}11861187void kfd_signal_vm_fault_event(struct kfd_process_device *pdd,1188struct kfd_vm_fault_info *info,1189struct kfd_hsa_memory_exception_data *data)1190{1191struct kfd_event *ev;1192uint32_t id;1193struct kfd_process *p = pdd->process;1194struct kfd_hsa_memory_exception_data memory_exception_data;1195int user_gpu_id;11961197user_gpu_id = kfd_process_get_user_gpu_id(p, pdd->dev->id);1198if (unlikely(user_gpu_id == -EINVAL)) {1199WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n",1200pdd->dev->id);1201return;1202}12031204/* SoC15 chips and onwards will pass in data from now on. */1205if (!data) {1206memset(&memory_exception_data, 0, sizeof(memory_exception_data));1207memory_exception_data.gpu_id = user_gpu_id;1208memory_exception_data.failure.imprecise = true;12091210/* Set failure reason */1211if (info) {1212memory_exception_data.va = (info->page_addr) <<1213PAGE_SHIFT;1214memory_exception_data.failure.NotPresent =1215info->prot_valid ? 1 : 0;1216memory_exception_data.failure.NoExecute =1217info->prot_exec ? 1 : 0;1218memory_exception_data.failure.ReadOnly =1219info->prot_write ? 1 : 0;1220memory_exception_data.failure.imprecise = 0;1221}1222}12231224rcu_read_lock();12251226id = KFD_FIRST_NONSIGNAL_EVENT_ID;1227idr_for_each_entry_continue(&p->event_idr, ev, id)1228if (ev->type == KFD_EVENT_TYPE_MEMORY) {1229spin_lock(&ev->lock);1230ev->memory_exception_data = data ? *data :1231memory_exception_data;1232set_event(ev);1233spin_unlock(&ev->lock);1234}12351236rcu_read_unlock();1237}12381239void kfd_signal_reset_event(struct kfd_node *dev)1240{1241struct kfd_hsa_hw_exception_data hw_exception_data;1242struct kfd_hsa_memory_exception_data memory_exception_data;1243struct kfd_process *p;1244struct kfd_event *ev;1245unsigned int temp;1246uint32_t id, idx;1247int reset_cause = atomic_read(&dev->sram_ecc_flag) ?1248KFD_HW_EXCEPTION_ECC :1249KFD_HW_EXCEPTION_GPU_HANG;12501251/* Whole gpu reset caused by GPU hang and memory is lost */1252memset(&hw_exception_data, 0, sizeof(hw_exception_data));1253hw_exception_data.memory_lost = 1;1254hw_exception_data.reset_cause = reset_cause;12551256memset(&memory_exception_data, 0, sizeof(memory_exception_data));1257memory_exception_data.ErrorType = KFD_MEM_ERR_SRAM_ECC;1258memory_exception_data.failure.imprecise = true;12591260idx = srcu_read_lock(&kfd_processes_srcu);1261hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {1262int user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);1263struct kfd_process_device *pdd = kfd_get_process_device_data(dev, p);12641265if (unlikely(user_gpu_id == -EINVAL)) {1266WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);1267continue;1268}12691270if (unlikely(!pdd)) {1271WARN_ONCE(1, "Could not get device data from process pid:%d\n",1272p->lead_thread->pid);1273continue;1274}12751276if (dev->dqm->detect_hang_count && !pdd->has_reset_queue)1277continue;12781279if (dev->dqm->detect_hang_count) {1280struct amdgpu_task_info *ti;1281struct amdgpu_fpriv *drv_priv;12821283if (unlikely(amdgpu_file_to_fpriv(pdd->drm_file, &drv_priv))) {1284WARN_ONCE(1, "Could not get vm for device %x from pid:%d\n",1285dev->id, p->lead_thread->pid);1286continue;1287}12881289ti = amdgpu_vm_get_task_info_vm(&drv_priv->vm);1290if (ti) {1291dev_err(dev->adev->dev,1292"Queues reset on process %s tid %d thread %s pid %d\n",1293ti->process_name, ti->tgid, ti->task.comm, ti->task.pid);1294amdgpu_vm_put_task_info(ti);1295}1296}12971298rcu_read_lock();12991300id = KFD_FIRST_NONSIGNAL_EVENT_ID;1301idr_for_each_entry_continue(&p->event_idr, ev, id) {1302if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {1303spin_lock(&ev->lock);1304ev->hw_exception_data = hw_exception_data;1305ev->hw_exception_data.gpu_id = user_gpu_id;1306set_event(ev);1307spin_unlock(&ev->lock);1308}1309if (ev->type == KFD_EVENT_TYPE_MEMORY &&1310reset_cause == KFD_HW_EXCEPTION_ECC) {1311spin_lock(&ev->lock);1312ev->memory_exception_data = memory_exception_data;1313ev->memory_exception_data.gpu_id = user_gpu_id;1314set_event(ev);1315spin_unlock(&ev->lock);1316}1317}13181319rcu_read_unlock();1320}1321srcu_read_unlock(&kfd_processes_srcu, idx);1322}13231324void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid)1325{1326struct kfd_process *p = kfd_lookup_process_by_pasid(pasid, NULL);1327struct kfd_hsa_memory_exception_data memory_exception_data;1328struct kfd_hsa_hw_exception_data hw_exception_data;1329struct kfd_event *ev;1330uint32_t id = KFD_FIRST_NONSIGNAL_EVENT_ID;1331int user_gpu_id;13321333if (!p) {1334dev_warn(dev->adev->dev, "Not find process with pasid:%d\n", pasid);1335return; /* Presumably process exited. */1336}13371338user_gpu_id = kfd_process_get_user_gpu_id(p, dev->id);1339if (unlikely(user_gpu_id == -EINVAL)) {1340WARN_ONCE(1, "Could not get user_gpu_id from dev->id:%x\n", dev->id);1341kfd_unref_process(p);1342return;1343}13441345memset(&hw_exception_data, 0, sizeof(hw_exception_data));1346hw_exception_data.gpu_id = user_gpu_id;1347hw_exception_data.memory_lost = 1;1348hw_exception_data.reset_cause = KFD_HW_EXCEPTION_ECC;13491350memset(&memory_exception_data, 0, sizeof(memory_exception_data));1351memory_exception_data.ErrorType = KFD_MEM_ERR_POISON_CONSUMED;1352memory_exception_data.gpu_id = user_gpu_id;1353memory_exception_data.failure.imprecise = true;13541355rcu_read_lock();13561357idr_for_each_entry_continue(&p->event_idr, ev, id) {1358if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {1359spin_lock(&ev->lock);1360ev->hw_exception_data = hw_exception_data;1361set_event(ev);1362spin_unlock(&ev->lock);1363}13641365if (ev->type == KFD_EVENT_TYPE_MEMORY) {1366spin_lock(&ev->lock);1367ev->memory_exception_data = memory_exception_data;1368set_event(ev);1369spin_unlock(&ev->lock);1370}1371}13721373dev_warn(dev->adev->dev, "Send SIGBUS to process %s(pasid:%d)\n",1374p->lead_thread->comm, pasid);1375rcu_read_unlock();13761377/* user application will handle SIGBUS signal */1378send_sig(SIGBUS, p->lead_thread, 0);13791380kfd_unref_process(p);1381}138213831384