From nobody Thu Sep 28 22:28:14 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RxSmZ6y7Sz4vhbf; Thu, 28 Sep 2023 22:28:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RxSmZ3y2Lz3QVT; Thu, 28 Sep 2023 22:28:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695940094; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Ikg07/ix3sfJF4zwdQTMrfnakYe0F2e71pxgds96ehk=; b=eZnWU3tYYTFRwnhoIpY6Yfu0Vvk+WY0JZN5W1NDIxLE/aPIvtO8H1QaTSd9Rjes1GQLPhR 57WBJMwVMqXplxA7fBPBArgbq9p98kTpBnrae18IRCQaYkci5Yu32lWMKrFL/F1z9KH+Im rCT9tU0BeRGPyqaL5XVz/0MoSXcc5N4GnLUYKqr6509dDU19ocJPRsiI+9bCAUHg8XcE0U OxGQBz4YoG8yIU5BKvaiMamTZg4sQLoOML8IDg7lSczB6dj55DnVcQXCF/BNH+rFXexz3F VLsFqAZe4hUOZHKLyrplWmcYje3AGSUOK8Ky7QSClb2yf9GjY6Jf/77e+J+gew== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695940094; a=rsa-sha256; cv=none; b=VHDHvouYBvnNkJfXDnZcdpICOjnwJgjIzdjGJcMbXOieVe1QXgfX4Uale6LPTxCuObyyNH DqaUiK0tMBII99BobQ4TN0i4hozLsT+4wzOqdCl2iudFOdZa3HLdcIhE+bittcaaxDFqvU Q2x9ZUkfsPGPJjVRbtawEiZ9k6gAZEgZBPX3IlBkCHsxN0sGWLVYUH0HasQhR8tNAQF/Lk sAmb5jfxKlAdpFjmJndT/v9ZEGO8C2pdH3xbIQOEewENw+aTxrrHsOFBvK9ku/xXk6nE6I pQk4a+XtdvKYSsLQIk86Yy6egXw0F99Xb5HEvtRAhSynd4L16sIvCDJyDsgI9A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695940094; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Ikg07/ix3sfJF4zwdQTMrfnakYe0F2e71pxgds96ehk=; b=rRdUbvZfimHBciaSy2WHQBCc+1HnK/eToB411v5JZPZhufN9xqiiS8xJPEjNsGnTW66y5t dfVVRxdhW4tvO/CJy5v6DIZBwwKBUi4RpARVg+aV9JWVQDBzEXRV5LzplqJ/QAftZssMHE mp2CaUtFZrPmTz2GvNIkbhgg0yuVre7HHPQKVXWHWTzwiNz2a6ggdBBsyW9TwFFfH5PA37 5gRlcrCFgzUS4+7zorPzMOYGCTkXZIQrhOw7VNWbTxlEP6M5lnP0+ejHPdJKkbxQXxGICr vQvP9kvaoiPNKc41pahOvVzZjtFnnrtcP6la3wrgn3NGis78pqgSabqS4EEtVQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RxSmZ349Pzfx5; Thu, 28 Sep 2023 22:28:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38SMSErq073899; Thu, 28 Sep 2023 22:28:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38SMSE62073896; Thu, 28 Sep 2023 22:28:14 GMT (envelope-from git) Date: Thu, 28 Sep 2023 22:28:14 GMT Message-Id: <202309282228.38SMSE62073896@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Warner Losh Subject: git: fcafa2453102 - releng/14.0 - timerfd: fix up a memory leak and missing locking List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/releng/14.0 X-Git-Reftype: branch X-Git-Commit: fcafa24531021555976b4945d9c58667c0f75ca2 Auto-Submitted: auto-generated The branch releng/14.0 has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=fcafa24531021555976b4945d9c58667c0f75ca2 commit fcafa24531021555976b4945d9c58667c0f75ca2 Author: Mateusz Guzik AuthorDate: 2023-08-25 14:21:39 +0000 Commit: Warner Losh CommitDate: 2023-09-28 22:26:19 +0000 timerfd: fix up a memory leak and missing locking timerfd01 from ltp passes (and some other don't), but none of the tests crash the kernel. This is a bare minimum patch to fix up the immediate regression. Reported by: yasu (cherry picked from commit 02f534b57f84d6f4f97c337b05b383c8b3aaf18c) Approved by: re (cperciva@) (cherry picked from commit 8c496b26641e3d86b0c9db0ed2b4598093df6d5a) Approved-by: re (cperciva) --- sys/kern/sys_timerfd.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/sys/kern/sys_timerfd.c b/sys/kern/sys_timerfd.c index 6948fa059b8c..2bf2a05c443c 100644 --- a/sys/kern/sys_timerfd.c +++ b/sys/kern/sys_timerfd.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -59,7 +60,11 @@ #endif static MALLOC_DEFINE(M_TIMERFD, "timerfd", "timerfd structures"); -static LIST_HEAD(, timerfd) timerfd_head; + +static struct sx timerfd_list_lock; +static LIST_HEAD(, timerfd) timerfd_list; +SX_SYSINIT(timerfd, &timerfd_list_lock, "timerfd_list_lock"); + static struct unrhdr64 tfdino_unr; #define TFD_NOJUMP 0 /* Realtime clock has not jumped. */ @@ -125,7 +130,8 @@ timerfd_jumped(void) struct timespec boottime, diff; timerfd_getboottime(&boottime); - LIST_FOREACH(tfd, &timerfd_head, entry) { + sx_xlock(&timerfd_list_lock); + LIST_FOREACH(tfd, &timerfd_list, entry) { mtx_lock(&tfd->tfd_lock); if (tfd->tfd_clockid != CLOCK_REALTIME || (tfd->tfd_timflags & TFD_TIMER_ABSTIME) == 0 || @@ -160,6 +166,7 @@ timerfd_jumped(void) tfd->tfd_boottim = boottime; mtx_unlock(&tfd->tfd_lock); } + sx_xunlock(&timerfd_list_lock); } static int @@ -314,11 +321,14 @@ timerfd_close(struct file *fp, struct thread *td) { struct timerfd *tfd = fp->f_data; + sx_xlock(&timerfd_list_lock); + LIST_REMOVE(tfd, entry); + sx_xunlock(&timerfd_list_lock); + callout_drain(&tfd->tfd_callout); seldrain(&tfd->tfd_sel); knlist_destroy(&tfd->tfd_sel.si_note); mtx_destroy(&tfd->tfd_lock); - LIST_REMOVE(tfd, entry); free(tfd, M_TIMERFD); fp->f_ops = &badfileops; @@ -420,9 +430,11 @@ kern_timerfd_create(struct thread *td, int clockid, int flags) if ((flags & TFD_CLOEXEC) != 0) fflags |= O_CLOEXEC; + error = falloc(td, &fp, &fd, fflags); + if (error != 0) + return (error); + tfd = malloc(sizeof(*tfd), M_TIMERFD, M_WAITOK | M_ZERO); - if (tfd == NULL) - return (ENOMEM); tfd->tfd_clockid = (clockid_t)clockid; tfd->tfd_flags = flags; tfd->tfd_ino = alloc_unr64(&tfdino_unr); @@ -431,16 +443,15 @@ kern_timerfd_create(struct thread *td, int clockid, int flags) knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock); timerfd_getboottime(&tfd->tfd_boottim); getnanotime(&tfd->tfd_birthtim); - LIST_INSERT_HEAD(&timerfd_head, tfd, entry); + sx_xlock(&timerfd_list_lock); + LIST_INSERT_HEAD(&timerfd_list, tfd, entry); + sx_xunlock(&timerfd_list_lock); - error = falloc(td, &fp, &fd, fflags); - if (error != 0) - return (error); fflags = FREAD; if ((flags & TFD_NONBLOCK) != 0) fflags |= FNONBLOCK; - finit(fp, fflags, DTYPE_TIMERFD, tfd, &timerfdops); + fdrop(fp, td); td->td_retval[0] = fd;