Path: blob/main/sys/contrib/openzfs/module/os/linux/spl/spl-thread.c
48775 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.3* Copyright (C) 2007 The Regents of the University of California.4* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).5* Written by Brian Behlendorf <[email protected]>.6* UCRL-CODE-2351977*8* This file is part of the SPL, Solaris Porting Layer.9*10* The SPL is free software; you can redistribute it and/or modify it11* under the terms of the GNU General Public License as published by the12* Free Software Foundation; either version 2 of the License, or (at your13* option) any later version.14*15* The SPL is distributed in the hope that it will be useful, but WITHOUT16* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or17* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License18* for more details.19*20* You should have received a copy of the GNU General Public License along21* with the SPL. If not, see <http://www.gnu.org/licenses/>.22*23* Solaris Porting Layer (SPL) Thread Implementation.24*/2526#include <sys/thread.h>27#include <sys/kmem.h>28#include <sys/tsd.h>29#include <sys/string.h>30#include <sys/misc.h>3132/*33* Thread interfaces34*/35typedef struct thread_priv_s {36unsigned long tp_magic; /* Magic */37int tp_name_size; /* Name size */38char *tp_name; /* Name (without _thread suffix) */39void (*tp_func)(void *); /* Registered function */40void *tp_args; /* Args to be passed to function */41size_t tp_len; /* Len to be passed to function */42int tp_state; /* State to start thread at */43pri_t tp_pri; /* Priority to start threat at */44} thread_priv_t;4546static int47thread_generic_wrapper(void *arg)48{49thread_priv_t *tp = (thread_priv_t *)arg;50void (*func)(void *);51void *args;5253ASSERT(tp->tp_magic == TP_MAGIC);54func = tp->tp_func;55args = tp->tp_args;56set_current_state(tp->tp_state);57set_user_nice((kthread_t *)current, PRIO_TO_NICE(tp->tp_pri));58kmem_free(tp->tp_name, tp->tp_name_size);59kmem_free(tp, sizeof (thread_priv_t));6061if (func)62func(args);6364return (0);65}6667/*68* thread_create() may block forever if it cannot create a thread or69* allocate memory. This is preferable to returning a NULL which Solaris70* style callers likely never check for... since it can't fail.71*/72kthread_t *73__thread_create(caddr_t stk, size_t stksize, thread_func_t func,74const char *name, void *args, size_t len, proc_t *pp, int state, pri_t pri)75{76thread_priv_t *tp;77struct task_struct *tsk;78char *p;7980/* Option pp is simply ignored */81/* Variable stack size unsupported */82ASSERT0P(stk);8384tp = kmem_alloc(sizeof (thread_priv_t), KM_PUSHPAGE);85if (tp == NULL)86return (NULL);8788tp->tp_magic = TP_MAGIC;89tp->tp_name_size = strlen(name) + 1;9091tp->tp_name = kmem_alloc(tp->tp_name_size, KM_PUSHPAGE);92if (tp->tp_name == NULL) {93kmem_free(tp, sizeof (thread_priv_t));94return (NULL);95}9697strlcpy(tp->tp_name, name, tp->tp_name_size);9899/*100* Strip trailing "_thread" from passed name which will be the func101* name since the exposed API has no parameter for passing a name.102*/103p = strstr(tp->tp_name, "_thread");104if (p)105p[0] = '\0';106107tp->tp_func = func;108tp->tp_args = args;109tp->tp_len = len;110tp->tp_state = state;111tp->tp_pri = pri;112113tsk = spl_kthread_create(thread_generic_wrapper, (void *)tp,114"%s", tp->tp_name);115if (IS_ERR(tsk))116return (NULL);117118wake_up_process(tsk);119return ((kthread_t *)tsk);120}121EXPORT_SYMBOL(__thread_create);122123/*124* spl_kthread_create - Wrapper providing pre-3.13 semantics for125* kthread_create() in which it is not killable and less likely126* to return -ENOMEM.127*/128struct task_struct *129spl_kthread_create(int (*func)(void *), void *data, const char namefmt[], ...)130{131struct task_struct *tsk;132va_list args;133char name[TASK_COMM_LEN];134135va_start(args, namefmt);136vsnprintf(name, sizeof (name), namefmt, args);137va_end(args);138do {139tsk = kthread_create(func, data, "%s", name);140if (IS_ERR(tsk)) {141if (signal_pending(current)) {142clear_thread_flag(TIF_SIGPENDING);143continue;144}145if (PTR_ERR(tsk) == -ENOMEM)146continue;147return (NULL);148} else {149return (tsk);150}151} while (1);152}153EXPORT_SYMBOL(spl_kthread_create);154155/*156* Extract the next pending signal from p_sig into p_cursig; stop the process157* if a stop has been requested or if a traced signal is pending.158*/159int160issig(void)161{162163if (!signal_pending(current))164return (0);165166spl_kernel_siginfo_t __info;167sigset_t set;168siginitsetinv(&set, 1ULL << (SIGSTOP - 1) | 1ULL << (SIGTSTP - 1));169sigorsets(&set, ¤t->blocked, &set);170171spin_lock_irq(¤t->sighand->siglock);172#if defined(HAVE_DEQUEUE_SIGNAL_4ARG)173enum pid_type __type;174if (dequeue_signal(current, &set, &__info, &__type) != 0) {175#elif defined(HAVE_DEQUEUE_SIGNAL_3ARG_TYPE)176enum pid_type __type;177if (dequeue_signal(&set, &__info, &__type) != 0) {178#else179if (dequeue_signal(current, &set, &__info) != 0) {180#endif181spin_unlock_irq(¤t->sighand->siglock);182kernel_signal_stop();183184/*185* Dequeued SIGSTOP/SIGTSTP.186* Check if process has other singal pending.187*/188if (signal_pending(current))189return (1);190191return (0);192}193194spin_unlock_irq(¤t->sighand->siglock);195196return (1);197}198199EXPORT_SYMBOL(issig);200201/*202* Check if the current thread is a memory reclaim thread.203* Returns true if current thread is kswapd.204*/205int206current_is_reclaim_thread(void)207{208return (current_is_kswapd());209}210EXPORT_SYMBOL(current_is_reclaim_thread);211212213