[patch V2 4/7] futex: Add sysctl knobs for process private hash

From: Thomas Gleixner
Date: Thu May 05 2016 - 16:47:11 EST


From: Sebastian Siewior <bigeasy@xxxxxxxxxxxxx>

To adjust the default hash size and the maximum hash size for process private
futexes we add the following sysctls:

futex_private_default_hash_bits:

Adjusts the default hash size (in bits) which is used for automatic hash
allocations on the first futex operation

futex_private_max_hash_bits:

Adjusts the maximum hash size (in bits). This limits the hash size which
can be preallocated by applications with the FUTEX_PREALLOC_HASH op.

Signed-off-by: Sebastian Siewior <bigeasy@xxxxxxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
---
Documentation/sysctl/kernel.txt | 17 +++++++++++++++++
include/linux/futex.h | 1 +
kernel/futex.c | 5 +++--
kernel/sysctl.c | 21 +++++++++++++++++++++
4 files changed, 42 insertions(+), 2 deletions(-)

--- a/Documentation/sysctl/kernel.txt
+++ b/Documentation/sysctl/kernel.txt
@@ -29,6 +29,8 @@ Currently, these files might (depending
- core_pipe_limit
- core_uses_pid
- ctrl-alt-del
+- futex_private_default_hash_bits
+- futex_private_max_hash_bits
- dmesg_restrict
- domainname
- hostname
@@ -265,6 +267,21 @@ to decide what to do with it.

==============================================================

+futex_private_default_hash_bits:
+
+Adjusts the default hash size (in bits) which is used for
+automatic hash allocations on the first futex operation
+
+==============================================================
+
+futex_private_max_hash_bits:
+
+Adjusts the maximum hash size (in bits). This limits the hash
+size which can be preallocated by applications with the
+FUTEX_PREALLOC_HASH op.
+
+==============================================================
+
dmesg_restrict:

This toggle indicates whether unprivileged users are prevented
--- a/include/linux/futex.h
+++ b/include/linux/futex.h
@@ -75,6 +75,7 @@ static inline void exit_pi_state_list(st

extern unsigned int futex_default_hash_bits;
extern unsigned int futex_max_hash_bits;
+extern unsigned int futex_sysmax_hash_bits;

extern void futex_mm_hash_exit(struct mm_struct *mm);

--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -297,8 +297,9 @@ struct futex_hash_bucket {
#define FUTEX_DEF_HASH_BITS order_base_2(8UL)
#define FUTEX_MAX_HASH_BITS order_base_2(256UL)

-unsigned int futex_default_hash_bits = FUTEX_DEF_HASH_BITS;
-unsigned int futex_max_hash_bits = FUTEX_MAX_HASH_BITS;
+unsigned int futex_default_hash_bits = FUTEX_DEF_HASH_BITS;
+unsigned int futex_max_hash_bits = FUTEX_MAX_HASH_BITS;
+unsigned int __read_mostly futex_sysmax_hash_bits = FUTEX_MAX_HASH_BITS;
#else
static const unsigned int futex_default_hash_bits = 0;
#endif
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -65,6 +65,7 @@
#include <linux/sched/sysctl.h>
#include <linux/kexec.h>
#include <linux/bpf.h>
+#include <linux/futex.h>

#include <asm/uaccess.h>
#include <asm/processor.h>
@@ -593,6 +594,26 @@ static struct ctl_table kern_table[] = {
.mode = 0644,
.proc_handler = proc_dointvec,
},
+#ifdef CONFIG_FUTEX_PRIVATE_HASH
+ {
+ .procname = "futex_private_default_hash_bits",
+ .data = &futex_default_hash_bits,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = &two,
+ .extra2 = &futex_max_hash_bits,
+ },
+ {
+ .procname = "futex_private_max_hash_bits",
+ .data = &futex_max_hash_bits,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = &futex_default_hash_bits,
+ .extra2 = &futex_sysmax_hash_bits,
+ },
+#endif
#ifdef CONFIG_FUNCTION_TRACER
{
.procname = "ftrace_enabled",