sem2mutex: infiniband, #2
Subsystems (2)
PATCH 1/1 sem2mutex: infiniband, #2
From: Ingo Molnar <[email protected]>
semaphore to mutex conversion.
the conversion was generated via scripts, and the result was validated automatically via a script as well.
build tested.
----
drivers/infiniband/core/ucm.c | 32 ++++++++++++------------ drivers/infiniband/core/user_mad.c | 2 - drivers/infiniband/hw/mthca/mthca_cmd.c | 6 ++-- drivers/infiniband/hw/mthca/mthca_dev.h | 10 ++++--- drivers/infiniband/hw/mthca/mthca_mcg.c | 10 +++---- drivers/infiniband/hw/mthca/mthca_memfree.c | 36 +++++++++++++-------------- drivers/infiniband/hw/mthca/mthca_memfree.h | 8 +++--- drivers/infiniband/hw/mthca/mthca_provider.c | 6 ++-- drivers/infiniband/ulp/srp/ib_srp.c | 14 +++++----- drivers/infiniband/ulp/srp/ib_srp.h | 5 +-- 10 files changed, 66 insertions(+), 63 deletions(-)
drivers/infiniband/core/ucm.c +16 -16
@@ -61,7 +61,7 @@ struct ib_ucm_device { };struct ib_ucm_file { -struct semaphore mutex; +struct mutex mutex;struct file *filp;struct ib_ucm_device *device;
@@ -150,7 +150,7 @@ static void ib_ucm_cleanup_events(struct {struct ib_ucm_event *uevent; -down (&ctx->file->mutex); +mutex_lock (&ctx->file->mutex);list_del (&ctx->file_list);while (!list_empty (&ctx->events)) {
@@ -165,7 +165,7 @@ static void ib_ucm_cleanup_events(structkfree (uevent); } -up (&ctx->file->mutex); +mutex_unlock (&ctx->file->mutex); }static struct ib_ucm_context *ib_ucm_ctx_alloc (struct ib_ucm_file *file)
@@ -400,11 +400,11 @@ static int ib_ucm_event_handler(struct iif (result) goto err2; -down (&ctx->file->mutex); +mutex_lock (&ctx->file->mutex);list_add_tail (&uevent->file_list, &ctx->file->events);list_add_tail (&uevent->ctx_list, &ctx->events);wake_up_interruptible (&ctx->file->poll_wait); -up (&ctx->file->mutex); +mutex_unlock (&ctx->file->mutex);return 0 ; err2:
@@ -430,7 +430,7 @@ static ssize_t ib_ucm_event(struct ib_ucif (copy_from_user (&cmd, inbuf,sizeof (cmd)))return -EFAULT ; -down (&file->mutex); +mutex_lock (&file->mutex);while (list_empty (&file->events)) {if (file->filp->f_flags &O_NONBLOCK ) {
@@ -445,9 +445,9 @@ static ssize_t ib_ucm_event(struct ib_ucprepare_to_wait (&file->poll_wait, &wait,TASK_INTERRUPTIBLE ); -up (&file->mutex); +mutex_unlock (&file->mutex);schedule (); -down (&file->mutex); +mutex_lock (&file->mutex);finish_wait (&file->poll_wait, &wait); }
@@ -507,7 +507,7 @@ static ssize_t ib_ucm_event(struct ib_uckfree (uevent->info);kfree (uevent); done: -up (&file->mutex); +mutex_unlock (&file->mutex);return result; }
@@ -526,9 +526,9 @@ static ssize_t ib_ucm_create_id(struct iif (copy_from_user (&cmd, inbuf,sizeof (cmd)))return -EFAULT ; -down (&file->mutex); +mutex_lock (&file->mutex); ctx =ib_ucm_ctx_alloc (file); -up (&file->mutex); +mutex_unlock (&file->mutex);if (!ctx)return -ENOMEM ;
@@ -1261,7 +1261,7 @@ static int ib_ucm_open(struct inode *inoINIT_LIST_HEAD (&file->ctxs);init_waitqueue_head (&file->poll_wait); -init_MUTEX (&file->mutex); +mutex_init (&file->mutex); filp->private_data = file; file->filp = filp;
@@ -1275,11 +1275,11 @@ static int ib_ucm_close(struct inode *instruct ib_ucm_file *file = filp->private_data;struct ib_ucm_context *ctx; -down (&file->mutex); +mutex_lock (&file->mutex);while (!list_empty (&file->ctxs)) { ctx =list_entry (file->ctxs.next, struct ib_ucm_context, file_list); -up (&file->mutex); +mutex_unlock (&file->mutex);mutex_lock (&ctx_id_mutex);idr_remove (&ctx_id_table, ctx->id);
@@ -1289,9 +1289,9 @@ static int ib_ucm_close(struct inode *inib_ucm_cleanup_events (ctx);kfree (ctx); -down (&file->mutex); +mutex_lock (&file->mutex); } -up (&file->mutex); +mutex_unlock (&file->mutex);kfree (file);return 0 ; }
drivers/infiniband/core/user_mad.c +1 -1
@@ -47,7 +47,7 @@#include <linux/kref.h> #include <asm/uaccess.h> -#include <asm/semaphore.h> +#include <linux/mutex.h> #include <rdma/ib_mad.h> #include <rdma/ib_user_mad.h>
drivers/infiniband/hw/mthca/mthca_cmd.c +3 -3
@@ -199,7 +199,7 @@ static int mthca_cmd_post(struct mthca_d {int err =0 ; -if (down_interruptible (&dev->cmd.hcr_sem)) +if (mutex_lock_interruptible (&dev->cmd.hcr_mutex))return -EINTR ;if (event) {
@@ -238,7 +238,7 @@ static int mthca_cmd_post(struct mthca_d op), dev->hcr + 6 * 4); out: -up (&dev->cmd.hcr_sem); +mutex_unlock (&dev->cmd.hcr_mutex);return err; }
@@ -438,7 +438,7 @@ static int mthca_cmd_imm(struct mthca_deint mthca_cmd_init (struct mthca_dev *dev) { -sema_init (&dev->cmd.hcr_sem,1 ); +mutex_init (&dev->cmd.hcr_mutex);sema_init (&dev->cmd.poll_sem,1 ); dev->cmd.use_events =0 ;
drivers/infiniband/hw/mthca/mthca_dev.h +6 -4
@@ -44,7 +44,9 @@#include <linux/pci.h> #include <linux/dma-mapping.h> #include <linux/timer.h> -#include <asm/semaphore.h> +#include <linux/mutex.h> +#include <linux/mutex.h> +#include "mthca_provider.h" #include "mthca_doorbell.h"
@@ -111,7 +113,7 @@ enum {struct mthca_cmd {struct pci_pool *pool;int use_events; -struct semaphore hcr_sem; +struct mutex hcr_mutex;struct semaphore poll_sem;struct semaphore event_sem;int max_cmds;
@@ -256,7 +258,7 @@ struct mthca_av_table { };struct mthca_mcg_table { -struct semaphore sem; +struct mutex mutex;struct mthca_alloc alloc;struct mthca_icm_table *table; };
@@ -301,7 +303,7 @@ struct mthca_dev {u64 ddr_end;MTHCA_DECLARE_DOORBELL_LOCK (doorbell_lock) - struct semaphore cap_mask_mutex; + struct mutex cap_mask_mutex;void __iomem *hcr;void __iomem *kar;
drivers/infiniband/hw/mthca/mthca_mcg.c +5 -5
@@ -154,7 +154,7 @@ int mthca_multicast_attach(struct ib_qpreturn PTR_ERR (mailbox); mgm = mailbox->buf; -if (down_interruptible (&dev->mcg_table.sem)) { +if (mutex_lock_interruptible (&dev->mcg_table.mutex)) { err = -EINTR ; goto err_sem; }
@@ -241,7 +241,7 @@ int mthca_multicast_attach(struct ib_qpBUG_ON (index < dev->limits.num_mgms);mthca_free (&dev->mcg_table.alloc, index); } -up (&dev->mcg_table.sem); +mutex_unlock (&dev->mcg_table.mutex); err_sem:mthca_free_mailbox (dev, mailbox);return err;
@@ -263,7 +263,7 @@ int mthca_multicast_detach(struct ib_qpreturn PTR_ERR (mailbox); mgm = mailbox->buf; -if (down_interruptible (&dev->mcg_table.sem)) { +if (mutex_lock_interruptible (&dev->mcg_table.mutex)) { err = -EINTR ; goto err_sem; }
@@ -371,7 +371,7 @@ int mthca_multicast_detach(struct ib_qp } out: -up (&dev->mcg_table.sem); +mutex_unlock (&dev->mcg_table.mutex); err_sem:mthca_free_mailbox (dev, mailbox);return err;
@@ -389,7 +389,7 @@ int __devinit mthca_init_mcg_table(strucif (err)return err; -init_MUTEX (&dev->mcg_table.sem); +mutex_init (&dev->mcg_table.mutex);return 0 ; }
drivers/infiniband/hw/mthca/mthca_memfree.c +18 -18
@@ -50,7 +50,7 @@ enum { };struct mthca_user_db_table { -struct semaphore mutex; +struct mutex mutex;struct {u64 uvirt;struct scatterlist mem;
@@ -158,7 +158,7 @@ int mthca_table_get(struct mthca_dev *deint ret =0 ;u8 status; -down (&table->mutex); +mutex_lock (&table->mutex);if (table->icm[i]) { ++table->icm[i]->refcount;
@@ -184,7 +184,7 @@ int mthca_table_get(struct mthca_dev *de ++table->icm[i]->refcount; out: -up (&table->mutex); +mutex_unlock (&table->mutex);return ret; }
@@ -198,7 +198,7 @@ void mthca_table_put(struct mthca_dev *d i = (obj & (table->num_obj -1 )) * table->obj_size /MTHCA_TABLE_CHUNK_SIZE ; -down (&table->mutex); +mutex_lock (&table->mutex);if (--table->icm[i]->refcount ==0 ) { mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
@@ -207,7 +207,7 @@ void mthca_table_put(struct mthca_dev *d table->icm[i] =NULL ; } -up (&table->mutex); +mutex_unlock (&table->mutex); }void *mthca_table_find (struct mthca_icm_table *table,int obj)
@@ -220,7 +220,7 @@ void *mthca_table_find(struct mthca_icm_if (!table->lowmem)return NULL ; -down (&table->mutex); +mutex_lock (&table->mutex); idx = (obj & (table->num_obj -1 )) * table->obj_size; icm = table->icm[idx /MTHCA_TABLE_CHUNK_SIZE ];
@@ -240,7 +240,7 @@ void *mthca_table_find(struct mthca_icm_ } out: -up (&table->mutex); +mutex_unlock (&table->mutex);return page ?lowmem_page_address (page) + offset :NULL ; }
@@ -301,7 +301,7 @@ struct mthca_icm_table *mthca_alloc_icm_ table->num_obj = nobj; table->obj_size = obj_size; table->lowmem = use_lowmem; -init_MUTEX (&table->mutex); +mutex_init (&table->mutex);for (i =0 ; i < num_icm; ++i) table->icm[i] =NULL ;
@@ -380,7 +380,7 @@ int mthca_map_user_db(struct mthca_dev *if (index <0 || index > dev->uar_table.uarc_size /8 )return -EINVAL ; -down (&db_tab->mutex); +mutex_lock (&db_tab->mutex); i = index /MTHCA_DB_REC_PER_PAGE ;
@@ -424,7 +424,7 @@ int mthca_map_user_db(struct mthca_dev * db_tab->page[i].refcount =1 ; out: -up (&db_tab->mutex); +mutex_unlock (&db_tab->mutex);return ret; }
@@ -439,11 +439,11 @@ void mthca_unmap_user_db(struct mthca_de* pages until we clean up the whole db table. */ -down (&db_tab->mutex); +mutex_lock (&db_tab->mutex); --db_tab->page[index /MTHCA_DB_REC_PER_PAGE ].refcount; -up (&db_tab->mutex); +mutex_unlock (&db_tab->mutex); }struct mthca_user_db_table *mthca_init_user_db_tab (struct mthca_dev *dev)
@@ -460,7 +460,7 @@ struct mthca_user_db_table *mthca_init_uif (!db_tab)return ERR_PTR (-ENOMEM ); -init_MUTEX (&db_tab->mutex); +mutex_init (&db_tab->mutex);for (i =0 ; i < npages; ++i) { db_tab->page[i].refcount =0 ; db_tab->page[i].uvirt =0 ;
@@ -499,7 +499,7 @@ int mthca_alloc_db(struct mthca_dev *devint ret =0 ;u8 status; -down (&dev->db_tab->mutex); +mutex_lock (&dev->db_tab->mutex);switch (type) {case MTHCA_DB_TYPE_CQ_ARM :
@@ -585,7 +585,7 @@ found: *db = (__be32 *) &page->db_rec[j]; out: -up (&dev->db_tab->mutex); +mutex_unlock (&dev->db_tab->mutex);return ret; }
@@ -601,7 +601,7 @@ void mthca_free_db(struct mthca_dev *dev page = dev->db_tab->page + i; -down (&dev->db_tab->mutex); +mutex_lock (&dev->db_tab->mutex); page->db_rec[j] =0 ;if (i >= dev->db_tab->min_group2)
@@ -624,7 +624,7 @@ void mthca_free_db(struct mthca_dev *dev ++dev->db_tab->min_group2; } -up (&dev->db_tab->mutex); +mutex_unlock (&dev->db_tab->mutex); }int mthca_init_db_tab (struct mthca_dev *dev)
@@ -638,7 +638,7 @@ int mthca_init_db_tab(struct mthca_dev *if (!dev->db_tab)return -ENOMEM ; -init_MUTEX (&dev->db_tab->mutex); +mutex_init (&dev->db_tab->mutex); dev->db_tab->npages = dev->uar_table.uarc_size /4096 ; dev->db_tab->max_group1 =0 ;
drivers/infiniband/hw/mthca/mthca_memfree.h +5 -3
@@ -40,7 +40,9 @@#include <linux/list.h> #include <linux/pci.h> -#include <asm/semaphore.h> +#include <linux/mutex.h> +#include <linux/mutex.h> +#define MTHCA_ICM_CHUNK_LEN \ ((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \
@@ -64,7 +66,7 @@ struct mthca_icm_table {int num_obj;int obj_size;int lowmem; -struct semaphore mutex; +struct mutex mutex;struct mthca_icm *icm[0 ]; };
@@ -147,7 +149,7 @@ struct mthca_db_table {int max_group1;int min_group2;struct mthca_db_page *page; -struct semaphore mutex; +struct mutex mutex; };enum mthca_db_type {
drivers/infiniband/hw/mthca/mthca_provider.c +3 -3
@@ -185,7 +185,7 @@ static int mthca_modify_port(struct ib_dint err;u8 status; -if (down_interruptible (&to_mdev (ibdev)->cap_mask_mutex)) +if (mutex_lock_interruptible (&to_mdev (ibdev)->cap_mask_mutex))return -ERESTARTSYS ; err =mthca_query_port (ibdev, port, &attr);
@@ -207,7 +207,7 @@ static int mthca_modify_port(struct ib_d } out: -up (&to_mdev (ibdev)->cap_mask_mutex); +mutex_unlock (&to_mdev (ibdev)->cap_mask_mutex);return err; }
@@ -1185,7 +1185,7 @@ int mthca_register_device(struct mthca_d dev->ib_dev.post_recv = mthca_tavor_post_receive; } -init_MUTEX (&dev->cap_mask_mutex); +mutex_init (&dev->cap_mask_mutex); ret =ib_register_device (&dev->ib_dev);if (ret)
drivers/infiniband/ulp/srp/ib_srp.c +7 -7
@@ -357,9 +357,9 @@ static void srp_remove_work(void *target target->state =SRP_TARGET_REMOVED ;spin_unlock_irq (target->scsi_host->host_lock); -down (&target->srp_host->target_mutex); +mutex_lock (&target->srp_host->target_mutex);list_del (&target->list); -up (&target->srp_host->target_mutex); +mutex_unlock (&target->srp_host->target_mutex);scsi_remove_host (target->scsi_host);ib_destroy_cm_id (target->cm_id);
@@ -1254,9 +1254,9 @@ static int srp_add_target(struct srp_hosif (scsi_add_host (target->scsi_host, host->dev->dma_device))return -ENODEV ; -down (&host->target_mutex); +mutex_lock (&host->target_mutex);list_add_tail (&target->list, &host->target_list); -up (&host->target_mutex); +mutex_unlock (&host->target_mutex); target->state =SRP_TARGET_LIVE ;
@@ -1525,7 +1525,7 @@ static struct srp_host *srp_add_port(strreturn NULL ;INIT_LIST_HEAD (&host->target_list); -init_MUTEX (&host->target_mutex); +mutex_init (&host->target_mutex);init_completion (&host->released); host->dev = device; host->port = port;
@@ -1626,7 +1626,7 @@ static void srp_remove_one(struct ib_dev* Mark all target ports as removed, so we stop queueing * commands and don't try to reconnect. */ -down (&host->target_mutex); +mutex_lock (&host->target_mutex);list_for_each_entry_safe (target, tmp_target, &host->target_list, list) {spin_lock_irqsave (target->scsi_host->host_lock, flags);
@@ -1634,7 +1634,7 @@ static void srp_remove_one(struct ib_dev target->state =SRP_TARGET_REMOVED ;spin_unlock_irqrestore (target->scsi_host->host_lock, flags); } -up (&host->target_mutex); +mutex_unlock (&host->target_mutex);/* * Wait for any reconnection tasks that may have
drivers/infiniband/ulp/srp/ib_srp.h +2 -3
@@ -37,8 +37,7 @@#include <linux/types.h> #include <linux/list.h> - -#include <asm/semaphore.h> +#include <linux/mutex.h> #include <scsi/scsi_host.h> #include <scsi/scsi_cmnd.h>
@@ -85,7 +84,7 @@ struct srp_host {struct ib_mr *mr;struct class_device class_dev;struct list_head target_list; -struct semaphore target_mutex; +struct mutex target_mutex;struct completion released;struct list_head list; };
Hi Ingo,
Just a small thing, it looks like the script is doing a double include of linux/mutex.h a few times:
Quoted 15 lines
Index:
linux/drivers/infiniband/hw/mthca/mthca_dev.h
===================================================================drivers/infiniband/hw/mthca/mthca_dev.h +3
-1@@ -44,7 +44,9 @@#include <linux/pci.h> #include <linux/dma-mapping.h> #include <linux/timer.h> -#include <asm/semaphore.h> +#include <linux/mutex.h> +#include <linux/mutex.h> +#include "mthca_provider.h" #include "mthca_doorbell.h"
...
Quoted 5 lines
Index:
linux/drivers/infiniband/hw/mthca/mthca_memfree.h
===================================================================
drivers/infiniband/hw/mthca/mthca_memfree.h line 48
Quoted 10 lines
drivers/infiniband/hw/mthca/mthca_memfree.h +3
-1@@ -40,7 +40,9 @@#include <linux/list.h> #include <linux/pci.h> -#include <asm/semaphore.h> +#include <linux/mutex.h> +#include <linux/mutex.h> +#define MTHCA_ICM_CHUNK_LEN \ ((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \
Anton
Quoted 8 lines · Anton Blanchard
Hi Ingo,
Just a small thing, it looks like the script is doing a double include of
linux/mutex.ha few times:drivers/infiniband/hw/mthca/mthca_dev.h line 48
Quoted 3 lines
drivers/infiniband/hw/mthca/mthca_dev.h +2
-1-#include <asm/semaphore.h> +#include <linux/mutex.h> +#include <linux/mutex.h>
yeah. I fixed this up in later patches (and in the queue to Andrew). I double-checked 2.6.15-mm4, it doesnt seem to be affected by this problem.
Ingo
Quoted 4 lines · Ingo Molnar
semaphore to mutex conversion.
the conversion was generated via scripts, and the result was validated automatically via a script as well.
Note that the number of inserted/deleted lines don't match, sometimes the line
#include <linux/mutex.h>
is duplicated.
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
Thanks, I finally got around to looking at this, and I applied the infiniband/ulp/srp part of this to my tree. However, I noticed some fishy things in other parts of the patch. First of all, the entire user_mad.c diff you sent is:
drivers/infiniband/core/user_mad.c line 53
Quoted 11 lines
drivers/infiniband/core/user_mad.c +1
-1@@ -47,7 +47,7 @@#include <linux/kref.h> #include <asm/uaccess.h> -#include <asm/semaphore.h> +#include <linux/mutex.h> #include <rdma/ib_mad.h> #include <rdma/ib_user_mad.h>
it seems that you're just getting lucky that struct semaphore is getting defined implicitly, since you didn't remove the actual semaphore use.
The semaphore usage in user_mad.c seems a little tricky to convert to a mutex, if I understand things correctly. The idea is that we create a special file that turns on the "IsSM" bit on the InfiniBand port when it's open (don't worry about what that means, really). We use a semaphore to prevent anyone else from opening the file and trying to be a SM until the first file is closed -- but of course the last reference to a file might be dropped by a different process than the one that opened it, so the up() might be called from a different process than the original down().
Second, in the mthca changes, you have
drivers/infiniband/hw/mthca/mthca_dev.h line 47
Quoted 8 lines
drivers/infiniband/hw/mthca/mthca_dev.h +1
-1@@ -44,7 +44,9 @@#include <linux/pci.h> #include <linux/dma-mapping.h> #include <linux/timer.h> -#include <asm/semaphore.h> +#include <linux/mutex.h>
but again not all semaphore usages have been removed.
For example, a few lines later in the same file, we have:
drivers/infiniband/hw/mthca/mthca_dev.h enum { · line 118
Quoted 8 lines
drivers/infiniband/hw/mthca/mthca_dev.h +1
-1@@ -111,7 +113,7 @@ enum {struct mthca_cmd {struct pci_pool *pool;int use_events; -struct semaphore hcr_sem; +struct mutex hcr_mutex;struct semaphore poll_sem;struct semaphore event_sem;
so poll_sem and event_sem remain.
Again, these seem hard to convert to mutexes, especially event_sem. The device firmware allows some number of commands to be queued up (64 is a typical number) and event_sem is used to keep track of when a command slot is available. I guess I could rewrite things so that I use something like wait_event() to block until there is a free command slot, but I'm not convinced that's an improvement.
Thanks,
Roland