Skip to contentSkip to conversation

sunrpc: clean-up cache downcall

PATCH Superseded Roberto Bergantinos Corpas 2 participants 3 messages Nov 27, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sunrpc: clean-up cache downcall

We can simplifly code around cache_downcall unifying memory allocations using kvmalloc, this have the benefit of getting rid of cache_slow_downcall (and queue_io_mutex), and also matches userland allocation size and limits

---
 net/sunrpc/cache.c | 41 +++++++++++------------------------------
 1 file changed, 11 insertions(+), 30 deletions(-)
net/sunrpc/cache.c +11 -30
index baef5ee43dbb..1347ecae9c84 100644
@@ -777,7 +777,6 @@ void cache_clean_deferred(void *owner)
  */
 
 static DEFINE_SPINLOCK(queue_lock);
-static DEFINE_MUTEX(queue_io_mutex);
 
 struct cache_queue {
 	struct list_head	list;
@@ -905,44 +904,26 @@ static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
 	return ret;
 }
 
-static ssize_t cache_slow_downcall(const char __user *buf,
-				   size_t count, struct cache_detail *cd)
-{
-	static char write_buf[8192]; /* protected by queue_io_mutex */
-	ssize_t ret = -EINVAL;
-
-	if (count >= sizeof(write_buf))
-		goto out;
-	mutex_lock(&queue_io_mutex);
-	ret = cache_do_downcall(write_buf, buf, count, cd);
-	mutex_unlock(&queue_io_mutex);
-out:
-	return ret;
-}
-
 static ssize_t cache_downcall(struct address_space *mapping,
 			      const char __user *buf,
 			      size_t count, struct cache_detail *cd)
 {
-	struct page *page;
-	char *kaddr;
+	char *write_buf;
 	ssize_t ret = -ENOMEM;
 
-	if (count >= PAGE_SIZE)
-		goto out_slow;
+	if (count >= 32768) { /* 32k is max userland buffer, lets check anyway */
+		ret = -EINVAL;
+		goto out;
+	}
 
-	page = find_or_create_page(mapping, 0, GFP_KERNEL);
-	if (!page)
-		goto out_slow;
+	write_buf = kvmalloc(count + 1, GFP_KERNEL);
+	if (!write_buf)
+		goto out;
 
-	kaddr = kmap(page);
-	ret = cache_do_downcall(kaddr, buf, count, cd);
-	kunmap(page);
-	unlock_page(page);
-	put_page(page);
+	ret = cache_do_downcall(write_buf, buf, count, cd);
+	kvfree(write_buf);
+out:
 	return ret;
-out_slow:
-	return cache_slow_downcall(buf, count, cd);
 }
 
 static ssize_t cache_write(struct file *filp, const char __user *buf,
Signature · 1 line
2.21.0
#1 lore raw

Hi Roberto-

I spotted some mechanical problems.

Quoted 3 lines · Roberto Bergantinos Corpas

We can simplifly code around cache_downcall unifying memory

^simplifly^simplify

Quoted 1 line

allocations using kvmalloc, this have the benefit of getting rid of

^, this have^. This has

Quoted 4 lines

cache_slow_downcall (and queue_io_mutex), and also matches userland allocation size and limits

Assuming Bruce is copacetic with this patch, the change looks appropriate for the v5.11 merge window. However, this patch doesn't appear to apply to v5.10-rc5. Might be because 27a1e8a0f79e ("sunrpc: raise kernel RPC channel buffer size") was already merged?

Quoted 75 lines

---
net/sunrpc/cache.c | 41 +++++++++++------------------------------

1 file changed, 11 insertions(+), 30 deletions(-)
net/sunrpc/cache.c +11 -30
index baef5ee43dbb..1347ecae9c84 100644
@@ -777,7 +777,6 @@ void cache_clean_deferred(void *owner)
 */
 
 static DEFINE_SPINLOCK(queue_lock);
-static DEFINE_MUTEX(queue_io_mutex);
 
 struct cache_queue {
 	struct list_head	list;
@@ -905,44 +904,26 @@ static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
 	return ret;
 }
 
-static ssize_t cache_slow_downcall(const char __user *buf,
-				   size_t count, struct cache_detail *cd)
-{
-	static char write_buf[8192]; /* protected by queue_io_mutex */
-	ssize_t ret = -EINVAL;
-
-	if (count >= sizeof(write_buf))
-		goto out;
-	mutex_lock(&queue_io_mutex);
-	ret = cache_do_downcall(write_buf, buf, count, cd);
-	mutex_unlock(&queue_io_mutex);
-out:
-	return ret;
-}
-
 static ssize_t cache_downcall(struct address_space *mapping,
 			      const char __user *buf,
 			      size_t count, struct cache_detail *cd)
 {
-	struct page *page;
-	char *kaddr;
+	char *write_buf;
 	ssize_t ret = -ENOMEM;
 
-	if (count >= PAGE_SIZE)
-		goto out_slow;
+	if (count >= 32768) { /* 32k is max userland buffer, lets check anyway */
+		ret = -EINVAL;
+		goto out;
+	}
 
-	page = find_or_create_page(mapping, 0, GFP_KERNEL);
-	if (!page)
-		goto out_slow;
+	write_buf = kvmalloc(count + 1, GFP_KERNEL);
+	if (!write_buf)
+		goto out;
 
-	kaddr = kmap(page);
-	ret = cache_do_downcall(kaddr, buf, count, cd);
-	kunmap(page);
-	unlock_page(page);
-	put_page(page);
+	ret = cache_do_downcall(write_buf, buf, count, cd);
+	kvfree(write_buf);
+out:
 	return ret;
-out_slow:
-	return cache_slow_downcall(buf, count, cd);
 }
 
 static ssize_t cache_write(struct file *filp, const char __user *buf,
Signature · 1 line
2.21.0

--
Chuck Lever

#2 lore raw

Hi Bruce!

Thanks for comments!, i'll send a v2 with mechanical errors fixed
based on v5.10-rc5.

rgds
roberto

Quoted 109 lines · Chuck Lever

Hi Roberto-

I spotted some mechanical problems.

Quoted 3 lines · Roberto Bergantinos Corpas

We can simplifly code around cache_downcall unifying memory

^simplifly^simplify

Quoted 1 line

allocations using kvmalloc, this have the benefit of getting rid of

^, this have^. This has

Quoted 4 lines

cache_slow_downcall (and queue_io_mutex), and also matches userland allocation size and limits

Assuming Bruce is copacetic with this patch, the change looks appropriate for the v5.11 merge window. However, this patch doesn't appear to apply to v5.10-rc5. Might be because 27a1e8a0f79e ("sunrpc: raise kernel RPC channel buffer size") was already merged?

Quoted 73 lines

---
net/sunrpc/cache.c | 41 +++++++++++------------------------------

1 file changed, 11 insertions(+), 30 deletions(-)
net/sunrpc/cache.c +11 -31
index baef5ee43dbb..1347ecae9c84 100644
@@ -777,7 +777,6 @@ void cache_clean_deferred(void *owner)
 */
 
 static DEFINE_SPINLOCK(queue_lock);
-static DEFINE_MUTEX(queue_io_mutex);
 
 struct cache_queue {
      struct list_head        list;
@@ -905,44 +904,26 @@ static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
      return ret;
 }
 
-static ssize_t cache_slow_downcall(const char __user *buf,
-                                size_t count, struct cache_detail *cd)
-{
-     static char write_buf[8192]; /* protected by queue_io_mutex */
-     ssize_t ret = -EINVAL;
-
-     if (count >= sizeof(write_buf))
-             goto out;
-     mutex_lock(&queue_io_mutex);
-     ret = cache_do_downcall(write_buf, buf, count, cd);
-     mutex_unlock(&queue_io_mutex);
-out:
-     return ret;
-}
-
 static ssize_t cache_downcall(struct address_space *mapping,
                            const char __user *buf,
                            size_t count, struct cache_detail *cd)
 {
-     struct page *page;
-     char *kaddr;
+     char *write_buf;
      ssize_t ret = -ENOMEM;
 
-     if (count >= PAGE_SIZE)
-             goto out_slow;
+     if (count >= 32768) { /* 32k is max userland buffer, lets check anyway */
+             ret = -EINVAL;
+             goto out;
+     }
 
-     page = find_or_create_page(mapping, 0, GFP_KERNEL);
-     if (!page)
-             goto out_slow;
+     write_buf = kvmalloc(count + 1, GFP_KERNEL);
+     if (!write_buf)
+             goto out;
 
-     kaddr = kmap(page);
-     ret = cache_do_downcall(kaddr, buf, count, cd);
-     kunmap(page);
-     unlock_page(page);
-     put_page(page);
+     ret = cache_do_downcall(write_buf, buf, count, cd);
+     kvfree(write_buf);
+out:
      return ret;
-out_slow:
-     return cache_slow_downcall(buf, count, cd);
 }
 
 static ssize_t cache_write(struct file *filp, const char __user *buf,
--
Quoted 2 lines

2.21.0

--
Chuck Lever