From owner-freebsd-bugs@FreeBSD.ORG Wed Nov 9 14:10:10 2011 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1FB9E106566C for ; Wed, 9 Nov 2011 14:10:10 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id EC9068FC1E for ; Wed, 9 Nov 2011 14:10:09 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id pA9EA91H084090 for ; Wed, 9 Nov 2011 14:10:09 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.4/8.14.4/Submit) id pA9EA9f1084089; Wed, 9 Nov 2011 14:10:09 GMT (envelope-from gnats) Resent-Date: Wed, 9 Nov 2011 14:10:09 GMT Resent-Message-Id: <201111091410.pA9EA9f1084089@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Armin Gruner Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 05937106567C for ; Wed, 9 Nov 2011 14:09:10 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22]) by mx1.freebsd.org (Postfix) with ESMTP id E00298FC21 for ; Wed, 9 Nov 2011 14:09:09 +0000 (UTC) Received: from red.freebsd.org (localhost [127.0.0.1]) by red.freebsd.org (8.14.4/8.14.4) with ESMTP id pA9E99UJ077146 for ; Wed, 9 Nov 2011 14:09:09 GMT (envelope-from nobody@red.freebsd.org) Received: (from nobody@localhost) by red.freebsd.org (8.14.4/8.14.4/Submit) id pA9E99BK077145; Wed, 9 Nov 2011 14:09:09 GMT (envelope-from nobody) Message-Id: <201111091409.pA9E99BK077145@red.freebsd.org> Date: Wed, 9 Nov 2011 14:09:09 GMT From: Armin Gruner To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: misc/162403: regression in FreeBSD/9 regarding pthread timeouts in kernel context X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Nov 2011 14:10:10 -0000 >Number: 162403 >Category: misc >Synopsis: regression in FreeBSD/9 regarding pthread timeouts in kernel context >Confidential: no >Severity: serious >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Wed Nov 09 14:10:09 UTC 2011 >Closed-Date: >Last-Modified: >Originator: Armin Gruner >Release: FreeBSD 9.0-RC1 >Organization: muc.de >Environment: FreeBSD ag.techsat.com 9.0-RC1 FreeBSD 9.0-RC1 #1 r226966M: Mon Oct 31 15:11:15 CET 2011 ag@ag.techsat.com:/usr/obj/usr/src/sys/GENERIC i386 >Description: pthread operations with a given timeout don't work under FreeBSD 9, if the process is running in realtime priority context. The timeout is not honoured at all. >How-To-Repeat: Compile the given code snippet with cc -pthread. It has two threads, one waiting for a condition to fire up. The producer delays initial signaling of the condition when started; thus the consumer should timeout after five seconds. Afterwards, it will be signaled every three seconds. The call to pthread_cond_timedwait() properly times out if the process is not running with RT prio: [ag@ag 1024]$ uname -a FreeBSD ag.techsat.com 9.0-RC1 FreeBSD 9.0-RC1 #1 r226966M: Mon Oct 31 15:11:15 CET 2011 ag@ag:/usr/obj/usr/src/sys/GENERIC i386 [ag@ag 1017]$ ./tt tt: pthread_cond_timedwait: Operation timed out WOKE UP WOKE UP It does not honour the timeout if running with realtime priority: [ag@ag 1020]$ SU rtprio 31 ./tt ~/work/c WOKE UP WOKE UP Under FreeBSD/8, both user and kernel pthread contexts works as expected: $ uname -a FreeBSD release-pc-freebsd 8.2-RELEASE FreeBSD 8.2-RELEASE #0: Fri Feb 18 02:24:46 UTC 2011 root@almeida.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC i386 $ ./tt tt: pthread_cond_timedwait: Operation timed out WOKE UP WOKE UP [ag@release-pc-freebsd 369]$ SU rtprio 31 ./tt tt: pthread_cond_timedwait: Operation timed out WOKE UP WOKE UP >Fix: Patch attached with submission follows: #include #include #include pthread_mutex_t mtx; pthread_cond_t cnd; void * loop(void *dummy) { int rc; struct timespec abstime; while (1) { clock_gettime(CLOCK_REALTIME, &abstime); abstime.tv_sec += 5; rc = pthread_mutex_lock(&mtx); if (rc != 0) warnc(rc, "pthread_mutex_lock"); rc = pthread_cond_timedwait(&cnd, &mtx, &abstime); if (rc != 0) warnc(rc, "pthread_cond_timedwait"); else fprintf(stderr, "WOKE UP\n"); rc = pthread_mutex_unlock(&mtx); if (rc != 0) warnc(rc, "pthread_mutex_unlock"); } return NULL; } main() { int rc; pthread_t t1; struct sched_param sp; rc = pthread_mutex_init(&mtx, NULL); if (rc != 0) warnc(rc, "pthread_mutex_init"); rc = pthread_cond_init(&cnd, NULL); if (rc != 0) warnc(rc, "pthread_cond_init"); pthread_create(&t1, NULL, loop, NULL); sleep(8); while (1) { rc = pthread_mutex_lock(&mtx); if (rc != 0) warnc(rc, "pthread_mutex_lock"); rc = pthread_cond_signal(&cnd); if (rc != 0) warnc(rc, "pthread_cond_signal"); rc = pthread_mutex_unlock(&mtx); if (rc != 0) warnc(rc, "pthread_mutex_unlock"); sleep(3); } } >Release-Note: >Audit-Trail: >Unformatted: