Bug 243532

Summary: kern.ipc.maxsockets wrong init value
Product: Base System Reporter: Ivan Rozhuk <rozhuk.im>
Component: kernAssignee: Allan Jude <allanjude>
Status: Closed FIXED    
Severity: Affects Some People CC: admin, allanjude, rozhuk.im
Priority: --- Flags: koobs: mfc-stable13?
koobs: mfc-stable12-
Version: CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
sysctl.conf none

Description Ivan Rozhuk 2020-01-23 00:10:22 UTC
I got:
kern.ipc.maxsockets: 517552
kern.maxfiles: 262144

/sys/kern/uipc_socket.c
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():
sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
{
	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;
			EVENTHANDLER_INVOKE(maxsockets_change);
		} 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 Vladyslav 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 Vladyslav 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]
sysctl.conf
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 freebsd_triage 2021-11-01 13:42:31 UTC
https://reviews.freebsd.org/D32775
Comment 6 commit-hook freebsd_committer freebsd_triage 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(-)
Comment 7 Ivan Rozhuk 2022-03-17 19:06:46 UTC
Please, commit this fix to 13.
Comment 8 commit-hook freebsd_committer freebsd_triage 2022-03-24 17:11:14 UTC
A commit in branch stable/13 references this bug:

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

commit 4f69c575996e069bfee62af9db2faa3ffa65db71
Author:     Allan Jude <allanjude@FreeBSD.org>
AuthorDate: 2021-11-04 12:55:33 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-03-24 17:09:29 +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
    Sponsored by:   Modirum MDPay
    Sponsored by:   Klara Inc.

    (cherry picked from commit c441592a0e1591591665cd037a8a5e9b54675f99)

 sys/kern/uipc_socket.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 9 commit-hook freebsd_committer freebsd_triage 2022-03-24 17:25:19 UTC
A commit in branch releng/13.1 references this bug:

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

commit 26714a5fa2c650f530b4977f1a005bad5f875567
Author:     Allan Jude <allanjude@FreeBSD.org>
AuthorDate: 2021-11-04 12:55:33 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-03-24 17:24:07 +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.

    Approved by:    re (gjb)
    PR:     243532
    Reviewed by:    markj
    Sponsored by:   Modirum MDPay
    Sponsored by:   Klara Inc.

    (cherry picked from commit c441592a0e1591591665cd037a8a5e9b54675f99)
    (cherry picked from commit 4f69c575996e069bfee62af9db2faa3ffa65db71)

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