From patchwork Wed Dec 18 01:04:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 22620 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oa0-f71.google.com (mail-oa0-f71.google.com [209.85.219.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 10FA423FBC for ; Wed, 18 Dec 2013 01:04:40 +0000 (UTC) Received: by mail-oa0-f71.google.com with SMTP id i4sf25526995oah.6 for ; Tue, 17 Dec 2013 17:04:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=30KN7PSraQ9xBQ+rWHGPVqWet3ZoKpz3z1EX1ew1c30=; b=UW5rfacmMbuiYwewrgYeCeEUdZEps1QvDswLEotODkIiSQHbJsuxcNt41C69MDZ+w/ PPNJW/WuTLFXgrmTdAZVO8eiLWDq3L2Gn7vEBADWISaftwTlJJEZ3ckunZpeJ1hpkqXh iQS+krG3XcwSxmpyqcdk4ilpkFpHsyhKiT5nZCI0rrQcbdyt+qTwvr+cxxqxGjIHY7Gi g9ln7iY1uhQiojBSlgTOjsata6ypWE1we5iSDRSSEATtTaGrctx8bA9s+ZwuNHuWCBVz JphDkSMrPweiEN0zu59zRZ6mYscWYQ6Gi4JVrC8Js/ITl2Fd8nI2esZHfkeVpiSfzSnM ZuHg== X-Gm-Message-State: ALoCoQk6H9KezZMiNEhCNL8iP5htAkrhKkLcUQNSmRqi7KdMWKS3LtN41+y7AFBjSG/siQPiCSX4 X-Received: by 10.42.65.138 with SMTP id l10mr1596013ici.31.1387328680002; Tue, 17 Dec 2013 17:04:40 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.128.41 with SMTP id nl9ls2718800qeb.56.gmail; Tue, 17 Dec 2013 17:04:39 -0800 (PST) X-Received: by 10.58.168.205 with SMTP id zy13mr11664958veb.19.1387328679798; Tue, 17 Dec 2013 17:04:39 -0800 (PST) Received: from mail-vb0-f53.google.com (mail-vb0-f53.google.com [209.85.212.53]) by mx.google.com with ESMTPS id xn8si5294993vdc.61.2013.12.17.17.04.39 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 17 Dec 2013 17:04:39 -0800 (PST) Received-SPF: neutral (google.com: 209.85.212.53 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.53; Received: by mail-vb0-f53.google.com with SMTP id o19so4660247vbm.12 for ; Tue, 17 Dec 2013 17:04:39 -0800 (PST) X-Received: by 10.52.245.42 with SMTP id xl10mr6239746vdc.35.1387328679616; Tue, 17 Dec 2013 17:04:39 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.59.13.131 with SMTP id ey3csp207052ved; Tue, 17 Dec 2013 17:04:38 -0800 (PST) X-Received: by 10.68.242.68 with SMTP id wo4mr3393260pbc.32.1387328678465; Tue, 17 Dec 2013 17:04:38 -0800 (PST) Received: from mail-pd0-f182.google.com (mail-pd0-f182.google.com [209.85.192.182]) by mx.google.com with ESMTPS id ob10si12952527pbb.187.2013.12.17.17.04.38 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 17 Dec 2013 17:04:38 -0800 (PST) Received-SPF: neutral (google.com: 209.85.192.182 is neither permitted nor denied by best guess record for domain of john.stultz@linaro.org) client-ip=209.85.192.182; Received: by mail-pd0-f182.google.com with SMTP id v10so7469339pde.41 for ; Tue, 17 Dec 2013 17:04:38 -0800 (PST) X-Received: by 10.66.231.6 with SMTP id tc6mr31019687pac.68.1387328677911; Tue, 17 Dec 2013 17:04:37 -0800 (PST) Received: from localhost.localdomain (c-67-170-153-23.hsd1.or.comcast.net. [67.170.153.23]) by mx.google.com with ESMTPSA id jk16sm36805552pbb.34.2013.12.17.17.04.36 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 17 Dec 2013 17:04:36 -0800 (PST) From: John Stultz To: LKML Cc: John Stultz , Colin Cross , Android Kernel Team , Greg KH Subject: [PATCH v3] staging: ion: Avoid using rt_mutexes directly Date: Tue, 17 Dec 2013 17:04:29 -0800 Message-Id: <1387328669-26785-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: References: X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: john.stultz@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.53 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , RT_MUTEXES can be configured out of the kernel, causing compile problems with ION. To quote Colin: "rt_mutexes were added with the deferred freeing feature. Heaps need to return zeroed memory to userspace, but zeroing the memory on every allocation was causing performance issues. We added a SCHED_IDLE thread to zero memory in the background after freeing, but locking the heap from the SCHED_IDLE thread might block a high priority allocation thread for a long time. The lock is only used to protect the heap's free_list and free_list_size members, and is not held for any long or sleeping operations. Converting to a spinlock should prevent priority inversion without using the rt_mutex. I'd also rename it to free_lock to so it doesn't get used as a general heap lock." Thus this patch converts the rt_mutex usage to a spinlock and renames the lock free_lock to be more clear as to its use. I also had to change a bit of logic in ion_heap_freelist_drain() to safely avoid list corruption. Cc: Colin Cross Cc: Android Kernel Team Cc: Greg KH Reported-by: Jim Davis Signed-off-by: John Stultz --- Changes from v2: - Modified while loop to be more clear, per Colin's suggestion drivers/staging/android/ion/ion_heap.c | 28 ++++++++++++++++------------ drivers/staging/android/ion/ion_priv.h | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/drivers/staging/android/ion/ion_heap.c b/drivers/staging/android/ion/ion_heap.c index 9cf5622..296c74f 100644 --- a/drivers/staging/android/ion/ion_heap.c +++ b/drivers/staging/android/ion/ion_heap.c @@ -160,10 +160,10 @@ int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot) void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer) { - rt_mutex_lock(&heap->lock); + spin_lock(&heap->free_lock); list_add(&buffer->list, &heap->free_list); heap->free_list_size += buffer->size; - rt_mutex_unlock(&heap->lock); + spin_unlock(&heap->free_lock); wake_up(&heap->waitqueue); } @@ -171,34 +171,38 @@ size_t ion_heap_freelist_size(struct ion_heap *heap) { size_t size; - rt_mutex_lock(&heap->lock); + spin_lock(&heap->free_lock); size = heap->free_list_size; - rt_mutex_unlock(&heap->lock); + spin_unlock(&heap->free_lock); return size; } size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size) { - struct ion_buffer *buffer, *tmp; + struct ion_buffer *buffer; size_t total_drained = 0; if (ion_heap_freelist_size(heap) == 0) return 0; - rt_mutex_lock(&heap->lock); + spin_lock(&heap->free_lock); if (size == 0) size = heap->free_list_size; - list_for_each_entry_safe(buffer, tmp, &heap->free_list, list) { + while (!list_empty(&heap->free_list)) { if (total_drained >= size) break; + buffer = list_first_entry(&heap->free_list, struct ion_buffer, + list); list_del(&buffer->list); heap->free_list_size -= buffer->size; total_drained += buffer->size; + spin_unlock(&heap->free_lock); ion_buffer_destroy(buffer); + spin_lock(&heap->free_lock); } - rt_mutex_unlock(&heap->lock); + spin_unlock(&heap->free_lock); return total_drained; } @@ -213,16 +217,16 @@ static int ion_heap_deferred_free(void *data) wait_event_freezable(heap->waitqueue, ion_heap_freelist_size(heap) > 0); - rt_mutex_lock(&heap->lock); + spin_lock(&heap->free_lock); if (list_empty(&heap->free_list)) { - rt_mutex_unlock(&heap->lock); + spin_unlock(&heap->free_lock); continue; } buffer = list_first_entry(&heap->free_list, struct ion_buffer, list); list_del(&buffer->list); heap->free_list_size -= buffer->size; - rt_mutex_unlock(&heap->lock); + spin_unlock(&heap->free_lock); ion_buffer_destroy(buffer); } @@ -235,7 +239,7 @@ int ion_heap_init_deferred_free(struct ion_heap *heap) INIT_LIST_HEAD(&heap->free_list); heap->free_list_size = 0; - rt_mutex_init(&heap->lock); + spin_lock_init(&heap->free_lock); init_waitqueue_head(&heap->waitqueue); heap->task = kthread_run(ion_heap_deferred_free, heap, "%s", heap->name); diff --git a/drivers/staging/android/ion/ion_priv.h b/drivers/staging/android/ion/ion_priv.h index fc8a4c3..d986739 100644 --- a/drivers/staging/android/ion/ion_priv.h +++ b/drivers/staging/android/ion/ion_priv.h @@ -159,7 +159,7 @@ struct ion_heap { struct shrinker shrinker; struct list_head free_list; size_t free_list_size; - struct rt_mutex lock; + spinlock_t free_lock; wait_queue_head_t waitqueue; struct task_struct *task; int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);