Skip to content

Commit

Permalink
Merge branch 'use-standard-sysctl-macro'
Browse files Browse the repository at this point in the history
Tonghao Zhang says:

====================
use standard sysctl macro

From: Tonghao Zhang <xiangxia.m.yue@gmail.com>

This patchset introduce sysctl macro or replace var
with macro.
====================

Link: https://lore.kernel.org/r/20220501035524.91205-1-xiangxia.m.yue@gmail.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Paolo Abeni committed May 3, 2022
2 parents 0530a68 + 57b1946 commit cb636b3
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 30 deletions.
2 changes: 1 addition & 1 deletion fs/proc/proc_sysctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static const struct file_operations proc_sys_dir_file_operations;
static const struct inode_operations proc_sys_dir_operations;

/* shared constants to be used in various sysctls */
const int sysctl_vals[] = { -1, 0, 1, 2, 4, 100, 200, 1000, 3000, INT_MAX, 65535 };
const int sysctl_vals[] = { 0, 1, 2, 3, 4, 100, 200, 1000, 3000, INT_MAX, 65535, -1 };
EXPORT_SYMBOL(sysctl_vals);

const unsigned long sysctl_long_vals[] = { 0, 1, LONG_MAX };
Expand Down
9 changes: 5 additions & 4 deletions include/linux/sysctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ struct ctl_table_header;
struct ctl_dir;

/* Keep the same order as in fs/proc/proc_sysctl.c */
#define SYSCTL_NEG_ONE ((void *)&sysctl_vals[0])
#define SYSCTL_ZERO ((void *)&sysctl_vals[1])
#define SYSCTL_ONE ((void *)&sysctl_vals[2])
#define SYSCTL_TWO ((void *)&sysctl_vals[3])
#define SYSCTL_ZERO ((void *)&sysctl_vals[0])
#define SYSCTL_ONE ((void *)&sysctl_vals[1])
#define SYSCTL_TWO ((void *)&sysctl_vals[2])
#define SYSCTL_THREE ((void *)&sysctl_vals[3])
#define SYSCTL_FOUR ((void *)&sysctl_vals[4])
#define SYSCTL_ONE_HUNDRED ((void *)&sysctl_vals[5])
#define SYSCTL_TWO_HUNDRED ((void *)&sysctl_vals[6])
Expand All @@ -51,6 +51,7 @@ struct ctl_dir;

/* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
#define SYSCTL_MAXOLDUID ((void *)&sysctl_vals[10])
#define SYSCTL_NEG_ONE ((void *)&sysctl_vals[11])

extern const int sysctl_vals[];

Expand Down
32 changes: 32 additions & 0 deletions lib/test_sysctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

static int i_zero;
static int i_one_hundred = 100;
static int match_int_ok = 1;

struct test_sysctl_data {
int int_0001;
Expand Down Expand Up @@ -95,6 +96,13 @@ static struct ctl_table test_table[] = {
.mode = 0644,
.proc_handler = proc_dointvec,
},
{
.procname = "match_int",
.data = &match_int_ok,
.maxlen = sizeof(match_int_ok),
.mode = 0444,
.proc_handler = proc_dointvec,
},
{
.procname = "boot_int",
.data = &test_data.boot_int,
Expand Down Expand Up @@ -132,6 +140,30 @@ static struct ctl_table_header *test_sysctl_header;

static int __init test_sysctl_init(void)
{
int i;

struct {
int defined;
int wanted;
} match_int[] = {
{.defined = *(int *)SYSCTL_ZERO, .wanted = 0},
{.defined = *(int *)SYSCTL_ONE, .wanted = 1},
{.defined = *(int *)SYSCTL_TWO, .wanted = 2},
{.defined = *(int *)SYSCTL_THREE, .wanted = 3},
{.defined = *(int *)SYSCTL_FOUR, .wanted = 4},
{.defined = *(int *)SYSCTL_ONE_HUNDRED, .wanted = 100},
{.defined = *(int *)SYSCTL_TWO_HUNDRED, .wanted = 200},
{.defined = *(int *)SYSCTL_ONE_THOUSAND, .wanted = 1000},
{.defined = *(int *)SYSCTL_THREE_THOUSAND, .wanted = 3000},
{.defined = *(int *)SYSCTL_INT_MAX, .wanted = INT_MAX},
{.defined = *(int *)SYSCTL_MAXOLDUID, .wanted = 65535},
{.defined = *(int *)SYSCTL_NEG_ONE, .wanted = -1},
};

for (i = 0; i < ARRAY_SIZE(match_int); i++)
if (match_int[i].defined != match_int[i].wanted)
match_int_ok = 0;

test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL);
if (!test_data.bitmap_0001)
return -ENOMEM;
Expand Down
13 changes: 5 additions & 8 deletions net/core/sysctl_net_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@

#include "dev.h"

static int two = 2;
static int three = 3;
static int int_3600 = 3600;
static int min_sndbuf = SOCK_MIN_SNDBUF;
static int min_rcvbuf = SOCK_MIN_RCVBUF;
static int max_skb_frags = MAX_SKB_FRAGS;
static long long_one __maybe_unused = 1;
static long long_max __maybe_unused = LONG_MAX;

static int net_msg_warn; /* Unused, but still a sysctl */
Expand Down Expand Up @@ -390,7 +387,7 @@ static struct ctl_table net_core_table[] = {
.extra2 = SYSCTL_ONE,
# else
.extra1 = SYSCTL_ZERO,
.extra2 = &two,
.extra2 = SYSCTL_TWO,
# endif
},
# ifdef CONFIG_HAVE_EBPF_JIT
Expand All @@ -401,7 +398,7 @@ static struct ctl_table net_core_table[] = {
.mode = 0600,
.proc_handler = proc_dointvec_minmax_bpf_restricted,
.extra1 = SYSCTL_ZERO,
.extra2 = &two,
.extra2 = SYSCTL_TWO,
},
{
.procname = "bpf_jit_kallsyms",
Expand All @@ -419,7 +416,7 @@ static struct ctl_table net_core_table[] = {
.maxlen = sizeof(long),
.mode = 0600,
.proc_handler = proc_dolongvec_minmax_bpf_restricted,
.extra1 = &long_one,
.extra1 = SYSCTL_LONG_ONE,
.extra2 = &bpf_jit_limit_max,
},
#endif
Expand Down Expand Up @@ -546,7 +543,7 @@ static struct ctl_table net_core_table[] = {
.mode = 0644,
.proc_handler = proc_dointvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = &two,
.extra2 = SYSCTL_TWO,
},
{
.procname = "devconf_inherit_init_net",
Expand All @@ -555,7 +552,7 @@ static struct ctl_table net_core_table[] = {
.mode = 0644,
.proc_handler = proc_dointvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = &three,
.extra2 = SYSCTL_THREE,
},
{
.procname = "high_order_alloc_disable",
Expand Down
16 changes: 6 additions & 10 deletions net/ipv4/sysctl_net_ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
#include <net/protocol.h>
#include <net/netevent.h>

static int two = 2;
static int three __maybe_unused = 3;
static int four = 4;
static int thousand = 1000;
static int tcp_retr1_max = 255;
static int ip_local_port_range_min[] = { 1, 1 };
static int ip_local_port_range_max[] = { 65535, 65535 };
Expand Down Expand Up @@ -1006,7 +1002,7 @@ static struct ctl_table ipv4_net_table[] = {
.mode = 0644,
.proc_handler = proc_dou8vec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = &two,
.extra2 = SYSCTL_TWO,
},
{
.procname = "tcp_max_syn_backlog",
Expand Down Expand Up @@ -1059,7 +1055,7 @@ static struct ctl_table ipv4_net_table[] = {
.mode = 0644,
.proc_handler = proc_fib_multipath_hash_policy,
.extra1 = SYSCTL_ZERO,
.extra2 = &three,
.extra2 = SYSCTL_THREE,
},
{
.procname = "fib_multipath_hash_fields",
Expand Down Expand Up @@ -1117,7 +1113,7 @@ static struct ctl_table ipv4_net_table[] = {
.mode = 0644,
.proc_handler = proc_dou8vec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = &four,
.extra2 = SYSCTL_FOUR,
},
{
.procname = "tcp_recovery",
Expand Down Expand Up @@ -1310,7 +1306,7 @@ static struct ctl_table ipv4_net_table[] = {
.mode = 0644,
.proc_handler = proc_dointvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = &thousand,
.extra2 = SYSCTL_ONE_THOUSAND,
},
{
.procname = "tcp_pacing_ca_ratio",
Expand All @@ -1319,7 +1315,7 @@ static struct ctl_table ipv4_net_table[] = {
.mode = 0644,
.proc_handler = proc_dointvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = &thousand,
.extra2 = SYSCTL_ONE_THOUSAND,
},
{
.procname = "tcp_wmem",
Expand Down Expand Up @@ -1391,7 +1387,7 @@ static struct ctl_table ipv4_net_table[] = {
.mode = 0644,
.proc_handler = proc_dou8vec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = &two,
.extra2 = SYSCTL_TWO,
},
{ }
};
Expand Down
6 changes: 2 additions & 4 deletions net/ipv6/sysctl_net_ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#endif
#include <linux/ioam6.h>

static int two = 2;
static int three = 3;
static int flowlabel_reflect_max = 0x7;
static int auto_flowlabels_max = IP6_AUTO_FLOW_LABEL_MAX;
static u32 rt6_multipath_hash_fields_all_mask =
Expand Down Expand Up @@ -172,7 +170,7 @@ static struct ctl_table ipv6_table_template[] = {
.mode = 0644,
.proc_handler = proc_rt6_multipath_hash_policy,
.extra1 = SYSCTL_ZERO,
.extra2 = &three,
.extra2 = SYSCTL_THREE,
},
{
.procname = "fib_multipath_hash_fields",
Expand All @@ -197,7 +195,7 @@ static struct ctl_table ipv6_table_template[] = {
.mode = 0644,
.proc_handler = proc_dou8vec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = &two,
.extra2 = SYSCTL_TWO,
},
{
.procname = "ioam6_id",
Expand Down
4 changes: 1 addition & 3 deletions net/netfilter/ipvs/ip_vs_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1767,8 +1767,6 @@ static int ip_vs_zero_all(struct netns_ipvs *ipvs)

#ifdef CONFIG_SYSCTL

static int three = 3;

static int
proc_do_defense_mode(struct ctl_table *table, int write,
void *buffer, size_t *lenp, loff_t *ppos)
Expand Down Expand Up @@ -1977,7 +1975,7 @@ static struct ctl_table vs_vars[] = {
.mode = 0644,
.proc_handler = proc_dointvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = &three,
.extra2 = SYSCTL_THREE,
},
{
.procname = "nat_icmp_send",
Expand Down
23 changes: 23 additions & 0 deletions tools/testing/selftests/sysctl/sysctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ALL_TESTS="$ALL_TESTS 0004:1:1:uint_0001"
ALL_TESTS="$ALL_TESTS 0005:3:1:int_0003"
ALL_TESTS="$ALL_TESTS 0006:50:1:bitmap_0001"
ALL_TESTS="$ALL_TESTS 0007:1:1:boot_int"
ALL_TESTS="$ALL_TESTS 0008:1:1:match_int"

function allow_user_defaults()
{
Expand Down Expand Up @@ -785,6 +786,27 @@ sysctl_test_0007()
return $ksft_skip
}

sysctl_test_0008()
{
TARGET="${SYSCTL}/match_int"
if [ ! -f $TARGET ]; then
echo "Skipping test for $TARGET as it is not present ..."
return $ksft_skip
fi

echo -n "Testing if $TARGET is matched in kernel"
ORIG_VALUE=$(cat "${TARGET}")

if [ $ORIG_VALUE -ne 1 ]; then
echo "TEST FAILED"
rc=1
test_rc
fi

echo "ok"
return 0
}

list_tests()
{
echo "Test ID list:"
Expand All @@ -800,6 +822,7 @@ list_tests()
echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
echo "0006 x $(get_test_count 0006) - tests proc_do_large_bitmap()"
echo "0007 x $(get_test_count 0007) - tests setting sysctl from kernel boot param"
echo "0008 x $(get_test_count 0008) - tests sysctl macro values match"
}

usage()
Expand Down

0 comments on commit cb636b3

Please sign in to comment.