From owner-svn-src-head@FreeBSD.ORG Sun Jun 16 09:30:36 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 376CC1D7; Sun, 16 Jun 2013 09:30:36 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 104E8112C; Sun, 16 Jun 2013 09:30:36 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r5G9UZep059295; Sun, 16 Jun 2013 09:30:35 GMT (envelope-from ed@svn.freebsd.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r5G9UZfE059294; Sun, 16 Jun 2013 09:30:35 GMT (envelope-from ed@svn.freebsd.org) Message-Id: <201306160930.r5G9UZfE059294@svn.freebsd.org> From: Ed Schouten Date: Sun, 16 Jun 2013 09:30:35 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r251803 - head/sys/kern X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 16 Jun 2013 09:30:36 -0000 Author: ed Date: Sun Jun 16 09:30:35 2013 New Revision: 251803 URL: http://svnweb.freebsd.org/changeset/base/251803 Log: Change callout use counter to use C11 atomics. In order to get some coverage of C11 atomics in kernelspace, switch at least one piece of code in kernelspace to use C11 atomics instead of . While there, slightly improve the code by adding an assertion to prevent the use count from going negative. Modified: head/sys/kern/kern_event.c Modified: head/sys/kern/kern_event.c ============================================================================== --- head/sys/kern/kern_event.c Sun Jun 16 07:18:07 2013 (r251802) +++ head/sys/kern/kern_event.c Sun Jun 16 09:30:35 2013 (r251803) @@ -47,6 +47,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -182,9 +183,9 @@ static struct filterops user_filtops = { }; static uma_zone_t knote_zone; -static int kq_ncallouts = 0; -static int kq_calloutmax = (4 * 1024); -SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW, +static atomic_uint kq_ncallouts = ATOMIC_VAR_INIT(0); +static unsigned int kq_calloutmax = 4 * 1024; +SYSCTL_UINT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW, &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue"); /* XXX - ensure not KN_INFLUX?? */ @@ -549,13 +550,15 @@ static int filt_timerattach(struct knote *kn) { struct callout *calloutp; + unsigned int ncallouts; - atomic_add_int(&kq_ncallouts, 1); - - if (kq_ncallouts >= kq_calloutmax) { - atomic_add_int(&kq_ncallouts, -1); - return (ENOMEM); - } + ncallouts = atomic_load_explicit(&kq_ncallouts, memory_order_relaxed); + do { + if (ncallouts >= kq_calloutmax) + return (ENOMEM); + } while (!atomic_compare_exchange_weak_explicit(&kq_ncallouts, + &ncallouts, ncallouts + 1, memory_order_relaxed, + memory_order_relaxed)); kn->kn_flags |= EV_CLEAR; /* automatically set */ kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */ @@ -573,11 +576,13 @@ static void filt_timerdetach(struct knote *kn) { struct callout *calloutp; + unsigned int old; calloutp = (struct callout *)kn->kn_hook; callout_drain(calloutp); free(calloutp, M_KQUEUE); - atomic_add_int(&kq_ncallouts, -1); + old = atomic_fetch_sub_explicit(&kq_ncallouts, 1, memory_order_relaxed); + KASSERT(old > 0, ("Number of callouts cannot become negative")); kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */ }