From: Valentine Fatiev <valentinef@nvidia.com>
Subject: [PATCH] BACKPORT: drivers/infiniband/core/ucma.c

Change-Id: If8d258c0a0d18190c8eca954baf1d8d620391877
---
 drivers/infiniband/core/ucma.c | 38 +++++++++++++++++++++++++++++++---
 1 file changed, 35 insertions(+), 3 deletions(-)

--- a/drivers/infiniband/core/ucma.c
+++ b/drivers/infiniband/core/ucma.c
@@ -43,9 +43,10 @@
 #include <linux/sysctl.h>
 #include <linux/module.h>
 #include <linux/nsproxy.h>
-
 #include <linux/nospec.h>
-
+#ifdef HAVE_BASECODE_EXTRAS
+#include <linux/vfio.h>
+#endif
 #include <rdma/rdma_user_cm.h>
 #include <rdma/ib_marshall.h>
 #include <rdma/rdma_cm.h>
@@ -59,9 +60,13 @@
 MODULE_AUTHOR("Sean Hefty");
 MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
 MODULE_LICENSE("Dual BSD/GPL");
+#ifdef RETPOLINE_MLNX
+MODULE_INFO(retpoline, "Y");
+#endif
 
 static unsigned int max_backlog = 1024;
 
+#ifndef CONFIG_SYSCTL_SYSCALL_CHECK
 static struct ctl_table_header *ucma_ctl_table_hdr;
 static struct ctl_table ucma_ctl_table[] = {
 	{
@@ -71,7 +76,11 @@ static struct ctl_table ucma_ctl_table[]
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec,
 	},
+#ifndef HAVE_REMOVE_SENTINEL_FROM_CTL_TABLE
+	{ }
+#endif
 };
+#endif
 
 struct ucma_file {
 	struct mutex		mut;
@@ -1624,13 +1633,25 @@ static ssize_t ucma_migrate_id(struct uc
 
 	/* Get current fd to protect against it being closed */
 	f = fdget(cmd.fd);
+#if defined (HAVE_FD_FILE)
+	if (!fd_file(f))
+#else
 	if (!f.file)
+#endif
 		return -ENOENT;
+#if defined (HAVE_FD_FILE)
+	if (fd_file(f)->f_op != &ucma_fops) {
+#else
 	if (f.file->f_op != &ucma_fops) {
+#endif
 		ret = -EINVAL;
 		goto file_put;
 	}
+#if defined (HAVE_FD_FILE)
+	cur_file = fd_file(f)->private_data;
+#else
 	cur_file = f.file->private_data;
+#endif
 
 	/* Validate current fd and prevent destruction of id. */
 	ctx = ucma_get_ctx(cur_file, cmd.id);
@@ -1733,7 +1754,6 @@ static ssize_t ucma_write(struct file *f
 	if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
 		return -EINVAL;
 	hdr.cmd = array_index_nospec(hdr.cmd, ARRAY_SIZE(ucma_cmd_table));
-
 	if (hdr.in + sizeof(hdr) > len)
 		return -EINVAL;
 
@@ -1784,7 +1804,11 @@ static int ucma_open(struct inode *inode
 	filp->private_data = file;
 	file->filp = filp;
 
+#ifdef HAVE_STREAM_OPEN
 	return stream_open(inode, filp);
+#else
+	return nonseekable_open(inode, filp);
+#endif
 }
 
 static int ucma_close(struct inode *inode, struct file *filp)
@@ -1817,7 +1841,9 @@ static const struct file_operations ucma
 	.release = ucma_close,
 	.write	 = ucma_write,
 	.poll    = ucma_poll,
+#ifdef no_llseek
 	.llseek	 = no_llseek,
+#endif
 };
 
 static struct miscdevice ucma_misc = {
@@ -1862,12 +1888,14 @@ static int __init ucma_init(void)
 		goto err1;
 	}
 
+#ifndef CONFIG_SYSCTL_SYSCALL_CHECK
 	ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table);
 	if (!ucma_ctl_table_hdr) {
 		pr_err("rdma_ucm: couldn't register sysctl paths\n");
 		ret = -ENOMEM;
 		goto err2;
 	}
+#endif
 
 	ret = ib_register_client(&rdma_cma_client);
 	if (ret)
@@ -1876,8 +1904,10 @@ static int __init ucma_init(void)
 	return 0;
 err3:
 	unregister_net_sysctl_table(ucma_ctl_table_hdr);
+#ifndef CONFIG_SYSCTL_SYSCALL_CHECK
 err2:
 	device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
+#endif
 err1:
 	misc_deregister(&ucma_misc);
 	return ret;
@@ -1886,7 +1916,9 @@ err1:
 static void __exit ucma_cleanup(void)
 {
 	ib_unregister_client(&rdma_cma_client);
+#ifndef CONFIG_SYSCTL_SYSCALL_CHECK
 	unregister_net_sysctl_table(ucma_ctl_table_hdr);
+#endif
 	device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
 	misc_deregister(&ucma_misc);
 }
