From c12717b6eed6f6bfd1c6feb63a8e70f36d5555b5 Mon Sep 17 00:00:00 2001 From: Swapnil Ingle Date: Mon, 4 Nov 2019 08:52:50 -0500 Subject: muser.h: fix the ioctl defination _IOW means userland is writing and kernel is reading _IOR means userland is reading and kernel is writing MUSER_DEV_CMD_WAIT should be _IOR as userland waits to read cmd from kernel. Signed-off-by: Swapnil Ingle --- kmod/muser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kmod/muser.h b/kmod/muser.h index 34a68bc..58deb4c 100644 --- a/kmod/muser.h +++ b/kmod/muser.h @@ -65,7 +65,7 @@ struct muser_cmd { }; /* ioctl cmds valid for /dev/muser/ */ -#define MUSER_DEV_CMD_WAIT _IOW('M', 1, struct muser_cmd) +#define MUSER_DEV_CMD_WAIT _IOR('M', 1, struct muser_cmd) #define MUSER_DEV_CMD_DONE _IOW('M', 2, struct muser_cmd) #endif /* _UAPI_LINUX_MUSER_H */ -- cgit v1.1 From 308f038f0a9566214adfc7e7297d1339c1e391c1 Mon Sep 17 00:00:00 2001 From: Swapnil Ingle Date: Mon, 4 Nov 2019 09:27:43 -0500 Subject: muser.h: remove unused variable struct muser_cmd_rw.buf Signed-off-by: Swapnil Ingle --- kmod/muser.h | 1 - lib/libmuser.c | 28 ++++++++++++++-------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/kmod/muser.h b/kmod/muser.h index 58deb4c..2f6a095 100644 --- a/kmod/muser.h +++ b/kmod/muser.h @@ -32,7 +32,6 @@ enum muser_cmd_type { struct muser_cmd_rw { size_t count; loff_t pos; - char *buf; /* only used for write */ }; struct muser_cmd_ioctl { diff --git a/lib/libmuser.c b/lib/libmuser.c index d747c1b..fcfdfd2 100644 --- a/lib/libmuser.c +++ b/lib/libmuser.c @@ -636,10 +636,11 @@ out: * ret), or a negative number on error. */ static int -post_read(lm_ctx_t *lm_ctx, struct muser_cmd *cmd, ssize_t count) +post_read(lm_ctx_t *lm_ctx, char *rwbuf, ssize_t count) { - ssize_t ret = write(lm_ctx->fd, cmd->rw.buf, count); + ssize_t ret; + ret = write(lm_ctx->fd, rwbuf, count); if (ret != count) { lm_log(lm_ctx, LM_ERR, "%s: bad muser write: %lu/%lu, %s\n", __func__, ret, count, strerror(errno)); @@ -792,15 +793,15 @@ lm_access(lm_ctx_t *lm_ctx, char *buf, size_t count, loff_t *ppos, static inline int muser_access(lm_ctx_t *lm_ctx, struct muser_cmd *cmd, bool is_write) { - char *data; + char *rwbuf; int err; unsigned int i; size_t count = 0, _count; ssize_t ret; /* TODO how big do we expect count to be? Can we use alloca(3) instead? */ - data = calloc(1, cmd->rw.count); - if (data == NULL) { + rwbuf = calloc(1, cmd->rw.count); + if (rwbuf == NULL) { lm_log(lm_ctx, LM_ERR, "failed to allocate memory\n"); return -1; } @@ -812,7 +813,7 @@ muser_access(lm_ctx_t *lm_ctx, struct muser_cmd *cmd, bool is_write) /* copy data to be written from kernel to user space */ if (is_write) { - err = read(lm_ctx->fd, data, cmd->rw.count); + err = read(lm_ctx->fd, rwbuf, cmd->rw.count); /* * FIXME this is wrong, we should be checking for * err != cmd->rw.count @@ -824,17 +825,17 @@ muser_access(lm_ctx_t *lm_ctx, struct muser_cmd *cmd, bool is_write) } err = 0; #ifndef LM_TERSE_LOGGING - dump_buffer(lm_ctx, "buffer write", data, cmd->rw.count); + dump_buffer(lm_ctx, "buffer write", rwbuf, cmd->rw.count); #endif } count = _count = cmd->rw.count; cmd->err = muser_pci_hdr_access(lm_ctx, &_count, &cmd->rw.pos, - is_write, data); + is_write, rwbuf); if (cmd->err) { lm_log(lm_ctx, LM_ERR, "failed to access PCI header: %d\n", cmd->err); #ifndef LM_TERSE_LOGGING - dump_buffer(lm_ctx, "buffer write", data, _count); + dump_buffer(lm_ctx, "buffer write", rwbuf, _count); #endif } @@ -843,19 +844,18 @@ muser_access(lm_ctx_t *lm_ctx, struct muser_cmd *cmd, bool is_write) * _count is how much there's left to be processed by lm_access */ count -= _count; - ret = lm_access(lm_ctx, data + count, _count, &cmd->rw.pos, + ret = lm_access(lm_ctx, rwbuf + count, _count, &cmd->rw.pos, is_write); if (!is_write && ret >= 0) { ret += count; - cmd->rw.buf = data; - err = post_read(lm_ctx, cmd, ret); + err = post_read(lm_ctx, rwbuf, ret); if (!LM_TERSE_LOGGING && err == ret) { - dump_buffer(lm_ctx, "buffer read", cmd->rw.buf, ret); + dump_buffer(lm_ctx, "buffer read", rwbuf, ret); } } out: - free(data); + free(rwbuf); return err; } -- cgit v1.1 From 6828a0f601d57cae12d45837b5fcec71f21d1a27 Mon Sep 17 00:00:00 2001 From: Swapnil Ingle Date: Mon, 4 Nov 2019 10:23:29 -0500 Subject: muser.c: code cleanup and coding style fixups Signed-off-by: Swapnil Ingle --- kmod/muser.c | 98 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/kmod/muser.c b/kmod/muser.c index 2102c09..72ce427 100644 --- a/kmod/muser.c +++ b/kmod/muser.c @@ -38,13 +38,13 @@ #define MIN(a, b) ((a) < (b) ? (a):(b)) static struct muser { - struct class *class; - struct list_head dev_list; - struct idr dev_idr; - struct cdev muser_cdev; - dev_t muser_devt; - struct device dev; - struct mutex muser_lock; + struct class *class; + struct list_head dev_list; + struct idr dev_idr; + struct cdev muser_cdev; + dev_t muser_devt; + struct device dev; + struct mutex muser_lock; } muser; #define muser_log(func, fmt, ...) \ @@ -62,10 +62,10 @@ static struct muser { * they seem to serve the same purpose, fix. */ struct page_map { - struct page **pages; - int nr_pages; - size_t len; - int offset; + struct page **pages; + int nr_pages; + size_t len; + int offset; }; struct vfio_dma_mapping { @@ -80,42 +80,42 @@ struct vfio_dma_mapping { * of them in a union? */ struct mudev_cmd { - enum muser_cmd_type type; /* copy of muser_cmd.type */ - struct muser_cmd muser_cmd; - struct page_map pg_map; - struct file **fds; - int *data_fds; + enum muser_cmd_type type; /* copy of muser_cmd.type */ + struct muser_cmd muser_cmd; + struct page_map pg_map; + struct file **fds; + int *data_fds; /* * When libmuser completes an mmap call, we need to know the length * in order to pass it to do_pin_pages. */ - unsigned long mmap_len; - struct list_head entry; + unsigned long mmap_len; + struct list_head entry; }; -// FIXME: Reorganise the members of this struct. +/* + * TODO: + * Reorganise the members of this struct muser_dev + * mucmd_pending should be per filep context + * muser_dev should have a list of filep contexts instead of srv_opened + */ struct muser_dev { - guid_t uuid; - int minor; - struct device *dev; - struct list_head dlist_entry; - struct list_head cmd_list; - // FIXME: mucmd_pending should be per filep context. - struct mudev_cmd *mucmd_pending; - // FIXME: muser_dev should have a list of filep contexts instead of - // srv_opened - atomic_t srv_opened; - atomic_t mdev_opened; - struct mutex dev_lock; - struct mdev_device *mdev; - wait_queue_head_t user_wait_q; - struct semaphore sem; - struct notifier_block iommu_notifier; - + guid_t uuid; + int minor; + struct device *dev; + struct list_head dlist_entry; + struct list_head cmd_list; + struct mudev_cmd *mucmd_pending; + atomic_t srv_opened; + atomic_t mdev_opened; + struct mutex dev_lock; + struct mdev_device *mdev; + wait_queue_head_t user_wait_q; + struct semaphore sem; + struct notifier_block iommu_notifier; struct vfio_dma_mapping *dma_map; /* Current DMA operation */ - struct list_head dma_list; /* list of dma mappings */ - - struct radix_tree_root devmem_tree; /* Device memory */ + struct list_head dma_list; /* list of dma mappings */ + struct radix_tree_root devmem_tree; /* Device memory */ }; /* function prototypes */ @@ -302,7 +302,6 @@ static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf) { return sprintf(buf, "muser\n"); } - MDEV_TYPE_ATTR_RO(name); static ssize_t device_api_show(struct kobject *kobj, struct device *dev, @@ -310,7 +309,6 @@ static ssize_t device_api_show(struct kobject *kobj, struct device *dev, { return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING); } - MDEV_TYPE_ATTR_RO(device_api); static struct attribute *mdev_types_attrs[] = { @@ -348,9 +346,8 @@ static int muser_process_cmd(struct muser_dev *mudev, struct mudev_cmd *mucmd) * Timeouts can happen if: * 1. No server has attached to mudev * 2. Processing of cmd takes more time than timeout - */ - /* - * TODO: Maybe use a while loop instead of goto + * + * Maybe use a while loop instead of goto */ retry: err = down_timeout(&mudev->sem, msecs_to_jiffies(5000)); @@ -964,7 +961,7 @@ pin_pages(struct mudev_cmd *mucmd, char __user *buf, size_t count, void dump_buffer(unsigned char const *const buf, uint32_t count) { #if defined(DEBUG) - /* + /* * TODO would be nice to add an option to print_hex_dump to hide * repeated lines, e.g. like od(1) */ @@ -999,7 +996,9 @@ static ssize_t muser_read(struct mdev_device *mdev, char __user *buf, muser_dbg("R %lx@%llx", mucmd.muser_cmd.rw.count, mucmd.muser_cmd.rw.pos); - /* Process mudev_cmd in libmuser context. TODO move into function */ + /* TODO: move following into function */ + + /* Process mudev_cmd in libmuser context */ err = muser_process_cmd(mudev, &mucmd); if (unlikely(err != 0)) _count = err; @@ -1056,13 +1055,13 @@ ssize_t muser_write(struct mdev_device *mdev, const char __user *buf, if (mucmd.muser_cmd.err) muser_info("PCI config write %ld@0x%llx not handled: %d", - _count, _pos, mucmd.muser_cmd.err); + _count, _pos, mucmd.muser_cmd.err); return count; } -static int -bounce_fds(struct mudev_cmd *mucmd, void __user *data, int user_data_size) +static int bounce_fds(struct mudev_cmd *mucmd, void __user *data, + int user_data_size) { int count = mucmd->muser_cmd.ioctl.data.irq_set.count; int data_size = count * sizeof(int32_t); @@ -1135,6 +1134,7 @@ static unsigned int get_argsz(unsigned int cmd, struct mudev_cmd *mucmd) case VFIO_DEVICE_SET_IRQS: return mucmd->muser_cmd.ioctl.data.irq_set.argsz; } + return -1; } -- cgit v1.1