Bug 243532 - kern.ipc.maxsockets wrong init value
Summary: kern.ipc.maxsockets wrong init value
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Allan Jude
Depends on:
Reported: 2020-01-23 00:10 UTC by Ivan Rozhuk
Modified: 2021-12-04 01:23 UTC (History)
3 users (show)

See Also:

sysctl.conf (14.34 KB, text/plain)
2021-02-14 13:08 UTC, Ivan Rozhuk
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ivan Rozhuk 2020-01-23 00:10:22 UTC
I got:
kern.ipc.maxsockets: 517552
kern.maxfiles: 262144

static void
init_maxsockets(void *ignored)

	TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
	maxsockets = imax(maxsockets, maxfiles);
SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);

looks like imin() should be used instead of imax():
	int error, newmaxsockets;

	newmaxsockets = maxsockets;
	error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
	if (error == 0 && req->newptr) {
		if (newmaxsockets > maxsockets &&
		    newmaxsockets <= maxfiles) {
			maxsockets = newmaxsockets;
		} else
			error = EINVAL;

Also, IMHO sysctl_maxsockets() (and some other sysctl val handlers) should not return EINVAL in case: oldval == newval.
This will avoid multiple error generation on system boot in case sysctl.conf contains kern.ipc.maxsockets, kern.maxfiles and other things that could not be decreased.
Comment 1 Vladislav V. Prodan 2021-02-14 08:40:13 UTC
Can this variable be changed at all? 
I was unable to do this ... 

FreeBSD 12.2-STABLE #0 r369260M: Sat Feb 13 11:28:24 EET 2021     root@vb-12.2.0.domain.com:/usr/obj/usr/src/amd64.amd64/sys/vb-12.2.0.domain.com.1  amd64

[10:35]vb-12:root-> /root# sysctl kern.ipc.maxsockets=400600
kern.ipc.maxsockets: 204800
sysctl: kern.ipc.maxsockets=400600: Invalid argument
Comment 2 Vladislav V. Prodan 2021-02-14 08:51:02 UTC
(In reply to Vladislav V. Prodan from comment #1)

kern.ipc.maxsockets <= kern.maxfiles

You can increase kern.maxfiles, and only then can you increase the kern.ipc.maxsockets value, but not more than the kern.maxfiles value.

In any case, you want a more informative answer than "Invalid argument".
Comment 3 Ivan Rozhuk 2021-02-14 13:08:44 UTC
Created attachment 222432 [details]
Comment 4 Ivan Rozhuk 2021-02-14 13:15:25 UTC
This file applies twice on boot, second time I got "Invalid argument" in console.
This is wrong, because value already set to required number.

You can simple reproduce this, even without apply mine sysctl.conf and reboot, just:

1. Get actual value on your system:
sysctl kern.ipc.maxsockets

2. Try to set this value back
sysctl kern.ipc.maxsockets='value that you got on step 1'
Comment 5 Allan Jude freebsd_committer 2021-11-01 13:42:31 UTC
Comment 6 commit-hook freebsd_committer 2021-11-04 12:57:38 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=c441592a0e1591591665cd037a8a5e9b54675f99

commit c441592a0e1591591665cd037a8a5e9b54675f99
Author:     Allan Jude <allanjude@FreeBSD.org>
AuthorDate: 2021-11-04 12:55:33 +0000
Commit:     Allan Jude <allanjude@FreeBSD.org>
CommitDate: 2021-11-04 12:56:09 +0000

    Allow kern.ipc.maxsockets to be set to current value without error

    Normally setting kern.ipc.maxsockets returns EINVAL if the new value
    is not greater than the previous value. This can cause spurious
    error messages when sysctl.conf is processed multiple times, or when
    automation systems try to ensure the sysctl is set to the correct
    value. If the value is unchanged, then just do nothing.

    PR:     243532
    Reviewed by:    markj
    MFC after:      3 days
    Sponsored by:   Modirum MDPay
    Sponsored by:   Klara Inc.
    Differential Revision:  https://reviews.freebsd.org/D32775

 sys/kern/uipc_socket.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)