anolis: uio: Replace mutex info_lock with percpu_ref to improve performance

ANBZ: #3289

The mutex info_lock was introduced to fix crash after the device is
unregistered in
commit 57c5f4df0a ("uio: fix crash after the device is unregistered"),
we can replace it with more powerful percpu-ref to improve performance.

Use tcm_loop and tcmu(backstore is file) to evaluate performance,
fio job: fio -filename=/dev/sdb  -ioengine=libaio -direct=1 -size=2G
-name=1 -thread -runtime=60 -time_based  -rw=randread -numjobs=16
-iodepth=16 -bs=128k

Without this patch:
    READ: bw=2965MiB/s (3110MB/s), 183MiB/s-188MiB/s (192MB/s-197MB/s),
io=174GiB (187GB), run=60005-60007msec

With this patch:
    READ: bw=5823MiB/s (6106MB/s), 338MiB/s-379MiB/s (354MB/s-397MB/s),
io=341GiB (366GB), run=60002-60005msec

There is about a 100% performance improvement in this case.

Reviewed-by: Joseph Qi <joseph.qi@linux.alibaba.com>
Reviewed-by: Xiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
Link: https://gitee.com/anolis/cloud-kernel/pulls/1255
This commit is contained in:
Guixin Liu 2022-02-14 17:15:07 +08:00 committed by 小龙
parent af0054ed40
commit ed32aa5dc7
2 changed files with 80 additions and 29 deletions

View File

@ -12,6 +12,8 @@
* Base Functions
*/
#include <linux/completion.h>
#include <linux/percpu-refcount.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/poll.h>
@ -216,7 +218,9 @@ static ssize_t name_show(struct device *dev,
struct uio_device *idev = dev_get_drvdata(dev);
int ret;
mutex_lock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref))
return -EINVAL;
if (!idev->info) {
ret = -EINVAL;
dev_err(dev, "the device has been unregistered\n");
@ -226,7 +230,7 @@ static ssize_t name_show(struct device *dev,
ret = sprintf(buf, "%s\n", idev->info->name);
out:
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
return ret;
}
static DEVICE_ATTR_RO(name);
@ -237,7 +241,9 @@ static ssize_t version_show(struct device *dev,
struct uio_device *idev = dev_get_drvdata(dev);
int ret;
mutex_lock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref))
return -EINVAL;
if (!idev->info) {
ret = -EINVAL;
dev_err(dev, "the device has been unregistered\n");
@ -247,7 +253,7 @@ static ssize_t version_show(struct device *dev,
ret = sprintf(buf, "%s\n", idev->info->version);
out:
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
return ret;
}
static DEVICE_ATTR_RO(version);
@ -487,16 +493,20 @@ static int uio_open(struct inode *inode, struct file *filep)
listener->event_count = atomic_read(&idev->event);
filep->private_data = listener;
mutex_lock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref)) {
ret = -EINVAL;
goto err_infoopen;
}
if (!idev->info) {
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
ret = -EINVAL;
goto err_infoopen;
}
if (idev->info->open)
ret = idev->info->open(idev->info, inode);
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
if (ret)
goto err_infoopen;
@ -529,10 +539,12 @@ static int uio_release(struct inode *inode, struct file *filep)
struct uio_listener *listener = filep->private_data;
struct uio_device *idev = listener->dev;
mutex_lock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref))
return -EINVAL;
if (idev->info && idev->info->release)
ret = idev->info->release(idev->info, inode);
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
module_put(idev->owner);
kfree(listener);
@ -546,10 +558,12 @@ static __poll_t uio_poll(struct file *filep, poll_table *wait)
struct uio_device *idev = listener->dev;
__poll_t ret = 0;
mutex_lock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref))
return -EINVAL;
if (!idev->info || !idev->info->irq)
ret = -EIO;
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
if (ret)
return ret;
@ -575,13 +589,17 @@ static ssize_t uio_read(struct file *filep, char __user *buf,
add_wait_queue(&idev->wait, &wait);
do {
mutex_lock(&idev->info_lock);
if (!idev->info || !idev->info->irq) {
retval = -EIO;
mutex_unlock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref)) {
retval = -EINVAL;
break;
}
mutex_unlock(&idev->info_lock);
if (!idev->info || !idev->info->irq) {
retval = -EIO;
percpu_ref_put(&idev->info_ref);
break;
}
percpu_ref_put(&idev->info_ref);
set_current_state(TASK_INTERRUPTIBLE);
@ -629,7 +647,9 @@ static ssize_t uio_write(struct file *filep, const char __user *buf,
if (copy_from_user(&irq_on, buf, count))
return -EFAULT;
mutex_lock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref))
return -EINVAL;
if (!idev->info) {
retval = -EINVAL;
goto out;
@ -648,7 +668,7 @@ static ssize_t uio_write(struct file *filep, const char __user *buf,
retval = idev->info->irqcontrol(idev->info, irq_on);
out:
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
return retval ? retval : sizeof(s32);
}
@ -673,7 +693,9 @@ static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
vm_fault_t ret = 0;
int mi;
mutex_lock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref))
return VM_FAULT_SIGBUS;
if (!idev->info) {
ret = VM_FAULT_SIGBUS;
goto out;
@ -700,8 +722,7 @@ static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
vmf->page = page;
out:
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
return ret;
}
@ -770,7 +791,9 @@ static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
vma->vm_private_data = idev;
mutex_lock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref))
return -EINVAL;
if (!idev->info) {
ret = -EINVAL;
goto out;
@ -809,7 +832,7 @@ static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
}
out:
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
return ret;
}
@ -819,7 +842,8 @@ static long uio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
struct uio_device *idev = listener->dev;
long retval = 0;
mutex_lock(&idev->info_lock);
if (!percpu_ref_tryget_live(&idev->info_ref))
return -EINVAL;
if (!idev->info || !idev->info->ioctl) {
retval = -EINVAL;
@ -828,7 +852,7 @@ static long uio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
retval = idev->info->ioctl(idev->info, cmd, arg);
out:
mutex_unlock(&idev->info_lock);
percpu_ref_put(&idev->info_ref);
return retval;
}
@ -926,6 +950,13 @@ static void uio_device_release(struct device *dev)
kfree(idev);
}
static void uio_info_free(struct percpu_ref *ref)
{
struct uio_device *idev = container_of(ref, struct uio_device, info_ref);
complete(&idev->free_done);
}
/**
* uio_register_device - register a new userspace IO device
* @owner: module that creates the new device
@ -956,10 +987,18 @@ int __uio_register_device(struct module *owner,
idev->owner = owner;
idev->info = info;
mutex_init(&idev->info_lock);
init_waitqueue_head(&idev->wait);
atomic_set(&idev->event, 0);
ret = percpu_ref_init(&idev->info_ref, uio_info_free, 0, GFP_KERNEL);
if (ret) {
pr_err("percpu_ref init failed!\n");
kfree(idev);
return ret;
}
init_completion(&idev->confirm_done);
init_completion(&idev->free_done);
ret = uio_get_minor(idev);
if (ret) {
kfree(idev);
@ -1055,6 +1094,13 @@ int __devm_uio_register_device(struct module *owner,
}
EXPORT_SYMBOL_GPL(__devm_uio_register_device);
static void uio_confirm_info(struct percpu_ref *ref)
{
struct uio_device *idev = container_of(ref, struct uio_device, info_ref);
complete(&idev->confirm_done);
}
/**
* uio_unregister_device - unregister a industrial IO device
* @info: UIO device capabilities
@ -1071,14 +1117,16 @@ void uio_unregister_device(struct uio_info *info)
idev = info->uio_dev;
minor = idev->minor;
mutex_lock(&idev->info_lock);
percpu_ref_kill_and_confirm(&idev->info_ref, uio_confirm_info);
wait_for_completion(&idev->confirm_done);
wait_for_completion(&idev->free_done);
/* now, we can set info to NULL */
uio_dev_del_attributes(idev);
if (info->irq && info->irq != UIO_IRQ_CUSTOM)
free_irq(info->irq, idev);
idev->info = NULL;
mutex_unlock(&idev->info_lock);
wake_up_interruptible(&idev->wait);
kill_fasync(&idev->async_queue, SIGIO, POLL_HUP);

View File

@ -16,6 +16,7 @@
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/percpu-refcount.h>
struct module;
struct uio_map;
@ -74,9 +75,11 @@ struct uio_device {
struct fasync_struct *async_queue;
wait_queue_head_t wait;
struct uio_info *info;
struct mutex info_lock;
struct kobject *map_dir;
struct kobject *portio_dir;
struct percpu_ref info_ref;
struct completion confirm_done;
struct completion free_done;
};
/**