From owner-svn-src-all@freebsd.org Tue Nov 6 20:46:02 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 296271130EAF; Tue, 6 Nov 2018 20:46:02 +0000 (UTC) (envelope-from shurd@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id A5BEB6D7A6; Tue, 6 Nov 2018 20:46:01 +0000 (UTC) (envelope-from shurd@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 87D8A40EE; Tue, 6 Nov 2018 20:46:01 +0000 (UTC) (envelope-from shurd@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id wA6Kk16T069100; Tue, 6 Nov 2018 20:46:01 GMT (envelope-from shurd@FreeBSD.org) Received: (from shurd@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id wA6Kk1Lo069099; Tue, 6 Nov 2018 20:46:01 GMT (envelope-from shurd@FreeBSD.org) Message-Id: <201811062046.wA6Kk1Lo069099@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: shurd set sender to shurd@FreeBSD.org using -f From: Stephen Hurd Date: Tue, 6 Nov 2018 20:46:01 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r340202 - in stable/12/sys: kern sys X-SVN-Group: stable-12 X-SVN-Commit-Author: shurd X-SVN-Commit-Paths: in stable/12/sys: kern sys X-SVN-Commit-Revision: 340202 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: A5BEB6D7A6 X-Spamd-Result: default: False [-103.09 / 200.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; ALLOW_DOMAIN_WHITELIST(-100.00)[FreeBSD.org]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-0.99)[-0.992,0]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; HAS_XAW(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all]; DMARC_NA(0.00)[FreeBSD.org]; RCVD_COUNT_THREE(0.00)[4]; MX_GOOD(-0.01)[cached: mx1.FreeBSD.org]; NEURAL_HAM_SHORT(-0.99)[-0.988,0]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; RCVD_TLS_LAST(0.00)[] X-Rspamd-Server: mx1.freebsd.org X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Nov 2018 20:46:02 -0000 Author: shurd Date: Tue Nov 6 20:46:00 2018 New Revision: 340202 URL: https://svnweb.freebsd.org/changeset/base/340202 Log: MFC r339861: Drain grouptaskqueue of the gtask before detaching it. taskqgroup_detach() would remove the task even if it was running or enqueued, which could lead to panics (see D17404). With this change, taskqgroup_detach() drains the task and sets a new flag which prevents the task from being scheduled again. I've added grouptask_block() and grouptask_unblock() to allow control over the flag from other locations as well. Reviewed by: Jeffrey Pieper Approved by: re (kib) Sponsored by: Limelight Networks Differential Revision: https://reviews.freebsd.org/D17674 Modified: stable/12/sys/kern/subr_gtaskqueue.c stable/12/sys/sys/gtaskqueue.h Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/kern/subr_gtaskqueue.c ============================================================================== --- stable/12/sys/kern/subr_gtaskqueue.c Tue Nov 6 20:45:15 2018 (r340201) +++ stable/12/sys/kern/subr_gtaskqueue.c Tue Nov 6 20:46:00 2018 (r340202) @@ -51,6 +51,8 @@ __FBSDID("$FreeBSD$"); static MALLOC_DEFINE(M_GTASKQUEUE, "gtaskqueue", "Group Task Queues"); static void gtaskqueue_thread_enqueue(void *); static void gtaskqueue_thread_loop(void *arg); +static int task_is_running(struct gtaskqueue *queue, struct gtask *gtask); +static void gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask); TASKQGROUP_DEFINE(softirq, mp_ncpus, 1); TASKQGROUP_DEFINE(config, 1, 1); @@ -183,6 +185,44 @@ gtaskqueue_free(struct gtaskqueue *queue) free(queue, M_GTASKQUEUE); } +/* + * Wait for all to complete, then prevent it from being enqueued + */ +void +grouptask_block(struct grouptask *grouptask) +{ + struct gtaskqueue *queue = grouptask->gt_taskqueue; + struct gtask *gtask = &grouptask->gt_task; + +#ifdef INVARIANTS + if (queue == NULL) { + gtask_dump(gtask); + panic("queue == NULL"); + } +#endif + TQ_LOCK(queue); + gtask->ta_flags |= TASK_NOENQUEUE; + gtaskqueue_drain_locked(queue, gtask); + TQ_UNLOCK(queue); +} + +void +grouptask_unblock(struct grouptask *grouptask) +{ + struct gtaskqueue *queue = grouptask->gt_taskqueue; + struct gtask *gtask = &grouptask->gt_task; + +#ifdef INVARIANTS + if (queue == NULL) { + gtask_dump(gtask); + panic("queue == NULL"); + } +#endif + TQ_LOCK(queue); + gtask->ta_flags &= ~TASK_NOENQUEUE; + TQ_UNLOCK(queue); +} + int grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *gtask) { @@ -197,6 +237,10 @@ grouptaskqueue_enqueue(struct gtaskqueue *queue, struc TQ_UNLOCK(queue); return (0); } + if (gtask->ta_flags & TASK_NOENQUEUE) { + TQ_UNLOCK(queue); + return (EAGAIN); + } STAILQ_INSERT_TAIL(&queue->tq_queue, gtask, ta_link); gtask->ta_flags |= TASK_ENQUEUED; TQ_UNLOCK(queue); @@ -378,6 +422,13 @@ gtaskqueue_cancel(struct gtaskqueue *queue, struct gta return (error); } +static void +gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask) +{ + while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask)) + TQ_SLEEP(queue, gtask, &queue->tq_mutex, PWAIT, "-", 0); +} + void gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *gtask) { @@ -386,8 +437,7 @@ gtaskqueue_drain(struct gtaskqueue *queue, struct gtas WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); TQ_LOCK(queue); - while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask)) - TQ_SLEEP(queue, gtask, &queue->tq_mutex, PWAIT, "-", 0); + gtaskqueue_drain_locked(queue, gtask); TQ_UNLOCK(queue); } @@ -803,6 +853,7 @@ taskqgroup_detach(struct taskqgroup *qgroup, struct gr { int i; + grouptask_block(gtask); mtx_lock(&qgroup->tqg_lock); for (i = 0; i < qgroup->tqg_cnt; i++) if (qgroup->tqg_queue[i].tgc_taskq == gtask->gt_taskqueue) @@ -813,6 +864,7 @@ taskqgroup_detach(struct taskqgroup *qgroup, struct gr LIST_REMOVE(gtask, gt_list); mtx_unlock(&qgroup->tqg_lock); gtask->gt_taskqueue = NULL; + gtask->gt_task.ta_flags &= ~TASK_NOENQUEUE; } static void Modified: stable/12/sys/sys/gtaskqueue.h ============================================================================== --- stable/12/sys/sys/gtaskqueue.h Tue Nov 6 20:45:15 2018 (r340201) +++ stable/12/sys/sys/gtaskqueue.h Tue Nov 6 20:46:00 2018 (r340202) @@ -52,7 +52,9 @@ int gtaskqueue_cancel(struct gtaskqueue *queue, struct void gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *task); void gtaskqueue_drain_all(struct gtaskqueue *queue); -int grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *task); +void grouptask_block(struct grouptask *grouptask); +void grouptask_unblock(struct grouptask *grouptask); +int grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *task); void taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *grptask, void *uniq, int irq, const char *name); int taskqgroup_attach_cpu(struct taskqgroup *qgroup, struct grouptask *grptask, @@ -67,6 +69,7 @@ void taskqgroup_config_gtask_deinit(struct grouptask * #define TASK_ENQUEUED 0x1 #define TASK_SKIP_WAKEUP 0x2 +#define TASK_NOENQUEUE 0x4 #define GTASK_INIT(task, flags, priority, func, context) do { \