/** * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved. * * This source file is released under GPL v2 license (no other versions). * See the COPYING file included in the main directory of this source * distribution for the license terms and conditions. * * @File ctvmem.c * * @Brief * This file contains the implementation of virtual memory management object * for card device. * * @Author Liu Chun * @Date Apr 1 2008 */ #include "ctvmem.h" #include "ctatc.h" #include #include #include #include #define CT_PTES_PER_PAGE (CT_PAGE_SIZE / sizeof(void *)) #define CT_ADDRS_PER_PAGE (CT_PTES_PER_PAGE * CT_PAGE_SIZE) /* * * Find or create vm block based on requested @size. * @size must be page aligned. * */ static struct ct_vm_block * get_vm_block(struct ct_vm *vm, unsigned int size, struct ct_atc *atc) { struct ct_vm_block *block = NULL, *entry; struct list_head *pos; size = CT_PAGE_ALIGN(size); if (size > vm->size) { dev_err(atc->card->dev, "Fail! No sufficient device virtual memory space available!\n"); return NULL; } mutex_lock(&vm->lock); list_for_each(pos, &vm->unused) { entry = list_entry(pos, struct ct_vm_block, list); if (entry->size >= size) break; /* found a block that is big enough */ } if (pos == &vm->unused) goto out; if (entry->size == size) { /* Move the vm node from unused list to used list directly */ list_move(&entry->list, &vm->used); vm->size -= size; block = entry; goto out; } block = kzalloc(sizeof(*block), GFP_KERNEL); if (!block) goto out; block->addr = entry->addr; block->size = size; list_add(&block->list, &vm->used); entry->addr += size; entry->size -= size; vm->size -= size; out: mutex_unlock(&vm->lock); return block; } static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block) { struct ct_vm_block *entry, *pre_ent; struct list_head *pos, *pre; block->size = CT_PAGE_ALIGN(block->size); mutex_lock(&vm->lock); list_del(&block->list); vm->size += block->size; list_for_each(pos, &vm->unused) { entry = list_entry(pos, struct ct_vm_block, list); if (entry->addr >= (block->addr + block->size)) break; /* found a position */ } if (pos == &vm->unused) { list_add_tail(&block->list, &vm->unused); entry = block; } else { if ((block->addr + block->size) == entry->addr) { entry->addr = block->addr; entry->size += block->size; kfree(block); } else { __list_add(&block->list, pos->prev, pos); entry = block; } } pos = &entry->list; pre = pos->prev; while (pre != &vm->unused) { entry = list_entry(pos, struct ct_vm_block, list); pre_ent = list_entry(pre, struct ct_vm_block, list); if ((pre_ent->addr + pre_ent->size) > entry->addr) break; pre_ent->size += entry->size; list_del(pos); kfree(entry); pos = pre; pre = pos->prev; } mutex_unlock(&vm->lock); } /* Map host addr (kmalloced/vmalloced) to device logical addr. */ static struct ct_vm_block * ct_vm_map(struct ct_vm *vm, struct snd_pcm_substream *substream, int size) { struct ct_vm_block *block; unsigned int pte_start; unsigned i, pages; unsigned long *ptp; struct ct_atc *atc = snd_pcm_substream_chip(substream); block = get_vm_block(vm, size, atc); if (block == NULL) { dev_err(atc->card->dev, "No virtual memory block that is big enough to allocate!\n"); return NULL; } ptp = (unsigned long *)vm->ptp[0].area; pte_start = (block->addr >> CT_PAGE_SHIFT); pages = block->size >> CT_PAGE_SHIFT; for (i = 0; i < pages; i++) { unsigned long addr; addr = snd_pcm_sgbuf_get_addr(substream, i << CT_PAGE_SHIFT); ptp[pte_start + i] = addr; } block->size = size; return block; } static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block) { /* do unmapping */ put_vm_block(vm, block); } /* * * return the host physical addr of the @index-th device * page table page on success, or ~0UL on failure. * The first returned ~0UL indicates the termination. * */ static dma_addr_t ct_get_ptp_phys(struct ct_vm *vm, int index) { return (index >= CT_PTP_NUM) ? ~0UL : vm->ptp[index].addr; } int ct_vm_create(struct ct_vm **rvm, struct pci_dev *pci) { struct ct_vm *vm; struct ct_vm_block *block; int i, err = 0; *rvm = NULL; vm = kzalloc(sizeof(*vm), GFP_KERNEL); if (!vm) return -ENOMEM; mutex_init(&vm->lock); /* Allocate page table pages */ for (i = 0; i < CT_PTP_NUM; i++) { err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), PAGE_SIZE, &vm->ptp[i]); if (err < 0) break; } if (err < 0) { /* no page table pages are allocated */ ct_vm_destroy(vm); return -ENOMEM; } vm->size = CT_ADDRS_PER_PAGE * i; vm->map = ct_vm_map; vm->unmap = ct_vm_unmap; vm->get_ptp_phys = ct_get_ptp_phys; INIT_LIST_HEAD(&vm->unused); INIT_LIST_HEAD(&vm->used); block = kzalloc(sizeof(*block), GFP_KERNEL); if (NULL != block) { block->addr = 0; block->size = vm->size; list_add(&block->list, &vm->unused); } *rvm = vm; return 0; } /* The caller must ensure no mapping pages are being used * by hardware before calling this function */ void ct_vm_destroy(struct ct_vm *vm) { int i; struct list_head *pos; struct ct_vm_block *entry; /* free used and unused list nodes */ while (!list_empty(&vm->used)) { pos = vm->used.next; list_del(pos); entry = list_entry(pos, struct ct_vm_block, list); kfree(entry); } while (!list_empty(&vm->unused)) { pos = vm->unused.next; list_del(pos); entry = list_entry(pos, struct ct_vm_block, list); kfree(entry); } /* free allocated page table pages */ for (i = 0; i < CT_PTP_NUM; i++) snd_dma_free_pages(&vm->ptp[i]); vm->size = 0; kfree(vm); } p and drm_atomic_ioctl set their own events in crtc_state->event. But when it's set the event is freed in 2 places. Solve this by only freeing the event in the atomic ioctl when it allocated its own event. This has been broken twice. The first time when the code was introduced, but only in the corner case when an event is allocated, but more crtc's were included by atomic check and then failing. This can mostly happen when you do an atomic modeset in i915 and the display clock is changed, which forces all crtc's to be included to the state. This has been broken worse by adding in-fences support, which caused the double free to be done unconditionally. [IGT] kms_rotation_crc: starting subtest primary-rotation-180 ============================================================================= BUG kmalloc-128 (Tainted: G U ): Object already free ----------------------------------------------------------------------------- Disabling lock debugging due to kernel taint INFO: Allocated in drm_atomic_helper_setup_commit+0x285/0x2f0 [drm_kms_helper] age=0 cpu=3 pid=1529 ___slab_alloc+0x308/0x3b0 __slab_alloc+0xd/0x20 kmem_cache_alloc_trace+0x92/0x1c0 drm_atomic_helper_setup_commit+0x285/0x2f0 [drm_kms_helper] intel_atomic_commit+0x35/0x4f0 [i915] drm_atomic_commit+0x46/0x50 [drm] drm_mode_atomic_ioctl+0x7d4/0xab0 [drm] drm_ioctl+0x2b3/0x490 [drm] do_vfs_ioctl+0x69c/0x700 SyS_ioctl+0x4e/0x80 entry_SYSCALL_64_fastpath+0x13/0x94 INFO: Freed in drm_event_cancel_free+0xa3/0xb0 [drm] age=0 cpu=3 pid=1529 __slab_free+0x48/0x2e0 kfree+0x159/0x1a0 drm_event_cancel_free+0xa3/0xb0 [drm] drm_mode_atomic_ioctl+0x86d/0xab0 [drm] drm_ioctl+0x2b3/0x490 [drm] do_vfs_ioctl+0x69c/0x700 SyS_ioctl+0x4e/0x80 entry_SYSCALL_64_fastpath+0x13/0x94 INFO: Slab 0xffffde1f0997b080 objects=17 used=2 fp=0xffff92fb65ec2578 flags=0x200000000008101 INFO: Object 0xffff92fb65ec2578 @offset=1400 fp=0xffff92fb65ec2ae8 Redzone ffff92fb65ec2570: bb bb bb bb bb bb bb bb ........ Object ffff92fb65ec2578: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk Object ffff92fb65ec2588: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk Object ffff92fb65ec2598: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk Object ffff92fb65ec25a8: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk Object ffff92fb65ec25b8: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk Object ffff92fb65ec25c8: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk Object ffff92fb65ec25d8: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk Object ffff92fb65ec25e8: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b a5 kkkkkkkkkkkkkkk. Redzone ffff92fb65ec25f8: bb bb bb bb bb bb bb bb ........ Padding ffff92fb65ec2738: 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZ CPU: 3 PID: 180 Comm: kworker/3:2 Tainted: G BU 4.10.0-rc6-patser+ #5039 Hardware name: /NUC5PPYB, BIOS PYBSWCEL.86A.0031.2015.0601.1712 06/01/2015 Workqueue: events intel_atomic_helper_free_state [i915] Call Trace: dump_stack+0x4d/0x6d print_trailer+0x20c/0x220 free_debug_processing+0x1c6/0x330 ? drm_atomic_state_default_clear+0xf7/0x1c0 [drm] __slab_free+0x48/0x2e0 ? drm_atomic_state_default_clear+0xf7/0x1c0 [drm] kfree+0x159/0x1a0 drm_atomic_state_default_clear+0xf7/0x1c0 [drm] ? drm_atomic_state_clear+0x30/0x30 [drm] intel_atomic_state_clear+0xd/0x20 [i915] drm_atomic_state_clear+0x1a/0x30 [drm] __drm_atomic_state_free+0x13/0x60 [drm] intel_atomic_helper_free_state+0x5d/0x70 [i915] process_one_work+0x260/0x4a0 worker_thread+0x2d1/0x4f0 kthread+0x127/0x130 ? process_one_work+0x4a0/0x4a0 ? kthread_stop+0x120/0x120 ret_from_fork+0x29/0x40 FIX kmalloc-128: Object at 0xffff92fb65ec2578 not freed Fixes: 3b24f7d67581 ("drm/atomic: Add struct drm_crtc_commit to track async updates") Fixes: 9626014258a5 ("drm/fence: add in-fences support") Cc: <stable@vger.kernel.org> # v4.8+ Cc: Daniel Vetter <daniel.vetter@ffwll.ch> Signed-off-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com> Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> Reviewed-by: Gustavo Padovan <gustavo.padovan@collabora.com> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch> Link: http://patchwork.freedesktop.org/patch/msgid/1485854725-27640-1-git-send-email-maarten.lankhorst@linux.intel.com
Diffstat (limited to 'sound/spi')