From patchwork Fri Dec 13 22:24:16 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 22407 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pb0-f69.google.com (mail-pb0-f69.google.com [209.85.160.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 3294223FBA for ; Fri, 13 Dec 2013 22:27:17 +0000 (UTC) Received: by mail-pb0-f69.google.com with SMTP id md12sf7434357pbc.8 for ; Fri, 13 Dec 2013 14:27:16 -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=cEtB3QMfkmkBkpaRomCRaJ1SZSjseImKDMXj/0bX9Vg=; b=TRSheaeVLw405GVwBb9/BkcFqwbzhod1Lkg5xJoqVhQklX0XU4NqrGzgzHKRS6ITN5 Qd8uIdGiACy7/YWv9LW9a79aZWl1rQpzDruMBxiyZ3xGQNB2FTnDt0Kldj6q+uz32873 UTnKVn028GtYxEO3bRlRiWlbWGrhzn2S0RJxZZrurPoquErTPNzE+vVhvZjxj+wHIwFx 2PxFJ9cTZsnq1QGqPp1gx61erkBULSb/2jspYHG1oP/TPlG8MOvaYlX1z4YI13pjYTmR R/1OsZSwfKFRiN3EVzP+s4ZN9K3eHh3KJB5HD5AQJsRvcGVEXL7nDKVNl4IQig0+OFEv 5gPA== X-Gm-Message-State: ALoCoQmihfJOMlRAs4O15fTM2AHjZ7w9QSYqsnllQDupKSKpTSHzllrvk4NXqa2aQneg51Wf8TmW X-Received: by 10.68.91.226 with SMTP id ch2mr2971076pbb.1.1386973636499; Fri, 13 Dec 2013 14:27:16 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.2.69 with SMTP id 5ls1209751qes.98.gmail; Fri, 13 Dec 2013 14:27:16 -0800 (PST) X-Received: by 10.58.233.98 with SMTP id tv2mr2191034vec.11.1386973636375; Fri, 13 Dec 2013 14:27:16 -0800 (PST) Received: from mail-vc0-f174.google.com (mail-vc0-f174.google.com [209.85.220.174]) by mx.google.com with ESMTPS id ti3si1202368vdc.2.2013.12.13.14.27.16 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 13 Dec 2013 14:27:16 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.174 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.174; Received: by mail-vc0-f174.google.com with SMTP id id10so1730135vcb.19 for ; Fri, 13 Dec 2013 14:27:16 -0800 (PST) X-Received: by 10.220.186.202 with SMTP id ct10mr2256691vcb.14.1386973636301; Fri, 13 Dec 2013 14:27:16 -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.220.174.196 with SMTP id u4csp73524vcz; Fri, 13 Dec 2013 14:27:15 -0800 (PST) X-Received: by 10.68.209.133 with SMTP id mm5mr5876403pbc.157.1386973635332; Fri, 13 Dec 2013 14:27:15 -0800 (PST) Received: from mail-pb0-f54.google.com (mail-pb0-f54.google.com [209.85.160.54]) by mx.google.com with ESMTPS id za5si2483748pbc.279.2013.12.13.14.27.15 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 13 Dec 2013 14:27:15 -0800 (PST) Received-SPF: neutral (google.com: 209.85.160.54 is neither permitted nor denied by best guess record for domain of john.stultz@linaro.org) client-ip=209.85.160.54; Received: by mail-pb0-f54.google.com with SMTP id un15so3163928pbc.13 for ; Fri, 13 Dec 2013 14:27:15 -0800 (PST) X-Received: by 10.67.30.70 with SMTP id kc6mr6191614pad.32.1386973634934; Fri, 13 Dec 2013 14:27:14 -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 qz9sm7457908pbc.3.2013.12.13.14.27.13 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 13 Dec 2013 14:27:14 -0800 (PST) From: John Stultz To: LKML Cc: Greg KH , Android Kernel Team , Sumit Semwal , Jesse Barker , Colin Cross , Rebecca Schultz Zavin , John Stultz Subject: [PATCH 042/115] gpu: ion: Refactor locking Date: Fri, 13 Dec 2013 14:24:16 -0800 Message-Id: <1386973529-4884-43-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1386973529-4884-1-git-send-email-john.stultz@linaro.org> References: <1386973529-4884-1-git-send-email-john.stultz@linaro.org> 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.220.174 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: , From: Rebecca Schultz Zavin Removes contention for lock between allocate and free by reducing the length of time the lock is held for. Split out a seperate lock to protect the list of heaps and replace it with a rwsem since the list will most likely only be updated during initialization. Signed-off-by: Rebecca Schultz Zavin [jstultz: modified patch to apply to staging directory] Signed-off-by: John Stultz --- drivers/staging/android/ion/ion.c | 45 ++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c index 0070c9b..ef8f132 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -38,15 +38,17 @@ /** * struct ion_device - the metadata of the ion device node * @dev: the actual misc device - * @buffers: an rb tree of all the existing buffers - * @lock: lock protecting the buffers & heaps trees + * @buffers: an rb tree of all the existing buffers + * @buffer_lock: lock protecting the tree of buffers + * @lock: rwsem protecting the tree of heaps and clients * @heaps: list of all the heaps in the system * @user_clients: list of all the clients created from userspace */ struct ion_device { struct miscdevice dev; struct rb_root buffers; - struct mutex lock; + struct mutex buffer_lock; + struct rw_semaphore lock; struct rb_root heaps; long (*custom_ioctl) (struct ion_client *client, unsigned int cmd, unsigned long arg); @@ -205,7 +207,9 @@ static struct ion_buffer *ion_buffer_create(struct ion_heap *heap, cached mapping that mapping has been invalidated */ for_each_sg(buffer->sg_table->sgl, sg, buffer->sg_table->nents, i) sg_dma_address(sg) = sg_phys(sg); + mutex_lock(&dev->buffer_lock); ion_buffer_add(dev, buffer); + mutex_unlock(&dev->buffer_lock); return buffer; err: @@ -224,9 +228,9 @@ static void ion_buffer_destroy(struct kref *kref) buffer->heap->ops->unmap_kernel(buffer->heap, buffer); buffer->heap->ops->unmap_dma(buffer->heap, buffer); buffer->heap->ops->free(buffer); - mutex_lock(&dev->lock); + mutex_lock(&dev->buffer_lock); rb_erase(&buffer->node, &dev->buffers); - mutex_unlock(&dev->lock); + mutex_unlock(&dev->buffer_lock); if (buffer->flags & ION_FLAG_CACHED) kfree(buffer->dirty); kfree(buffer); @@ -244,9 +248,9 @@ static int ion_buffer_put(struct ion_buffer *buffer) static void ion_buffer_add_to_handle(struct ion_buffer *buffer) { - mutex_lock(&buffer->dev->lock); + mutex_lock(&buffer->lock); buffer->handle_count++; - mutex_unlock(&buffer->dev->lock); + mutex_unlock(&buffer->lock); } static void ion_buffer_remove_from_handle(struct ion_buffer *buffer) @@ -260,7 +264,7 @@ static void ion_buffer_remove_from_handle(struct ion_buffer *buffer) * The taskcomm and pid can provide a debug hint as to where this fd * is in the system */ - mutex_lock(&buffer->dev->lock); + mutex_lock(&buffer->lock); buffer->handle_count--; BUG_ON(buffer->handle_count < 0); if (!buffer->handle_count) { @@ -270,7 +274,7 @@ static void ion_buffer_remove_from_handle(struct ion_buffer *buffer) get_task_comm(buffer->task_comm, task); buffer->pid = task_pid_nr(task); } - mutex_unlock(&buffer->dev->lock); + mutex_unlock(&buffer->lock); } static struct ion_handle *ion_handle_create(struct ion_client *client, @@ -403,7 +407,7 @@ struct ion_handle *ion_alloc(struct ion_client *client, size_t len, len = PAGE_ALIGN(len); - mutex_lock(&dev->lock); + down_read(&dev->lock); for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { struct ion_heap *heap = rb_entry(n, struct ion_heap, node); /* if the client doesn't support this heap type */ @@ -416,7 +420,7 @@ struct ion_handle *ion_alloc(struct ion_client *client, size_t len, if (!IS_ERR_OR_NULL(buffer)) break; } - mutex_unlock(&dev->lock); + up_read(&dev->lock); if (buffer == NULL) return ERR_PTR(-ENODEV); @@ -662,7 +666,7 @@ struct ion_client *ion_client_create(struct ion_device *dev, client->task = task; client->pid = pid; - mutex_lock(&dev->lock); + down_write(&dev->lock); p = &dev->clients.rb_node; while (*p) { parent = *p; @@ -680,7 +684,7 @@ struct ion_client *ion_client_create(struct ion_device *dev, client->debug_root = debugfs_create_file(debug_name, 0664, dev->debug_root, client, &debug_client_fops); - mutex_unlock(&dev->lock); + up_write(&dev->lock); return client; } @@ -696,12 +700,12 @@ void ion_client_destroy(struct ion_client *client) node); ion_handle_destroy(&handle->ref); } - mutex_lock(&dev->lock); + down_write(&dev->lock); if (client->task) put_task_struct(client->task); rb_erase(&client->node, &dev->clients); debugfs_remove_recursive(client->debug_root); - mutex_unlock(&dev->lock); + up_write(&dev->lock); kfree(client); } @@ -1220,7 +1224,7 @@ static int ion_debug_heap_show(struct seq_file *s, void *unused) seq_printf(s, "----------------------------------------------------\n"); seq_printf(s, "orphaned allocations (info is from last known client):" "\n"); - mutex_lock(&dev->lock); + mutex_lock(&dev->buffer_lock); for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { struct ion_buffer *buffer = rb_entry(n, struct ion_buffer, node); @@ -1233,7 +1237,7 @@ static int ion_debug_heap_show(struct seq_file *s, void *unused) total_orphaned_size += buffer->size; } } - mutex_unlock(&dev->lock); + mutex_unlock(&dev->buffer_lock); seq_printf(s, "----------------------------------------------------\n"); seq_printf(s, "%16.s %16u\n", "total orphaned", total_orphaned_size); @@ -1270,7 +1274,7 @@ void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap) __func__); heap->dev = dev; - mutex_lock(&dev->lock); + down_write(&dev->lock); while (*p) { parent = *p; entry = rb_entry(parent, struct ion_heap, node); @@ -1291,7 +1295,7 @@ void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap) debugfs_create_file(heap->name, 0664, dev->debug_root, heap, &debug_heap_fops); end: - mutex_unlock(&dev->lock); + up_write(&dev->lock); } struct ion_device *ion_device_create(long (*custom_ioctl) @@ -1322,7 +1326,8 @@ struct ion_device *ion_device_create(long (*custom_ioctl) idev->custom_ioctl = custom_ioctl; idev->buffers = RB_ROOT; - mutex_init(&idev->lock); + mutex_init(&idev->buffer_lock); + init_rwsem(&idev->lock); idev->heaps = RB_ROOT; idev->clients = RB_ROOT; return idev;