4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2013, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
32 #define DEBUG_SUBSYSTEM S_LNET
34 #include <linux/module.h>
35 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #ifdef HAVE_SCHED_HEADERS
39 #include <linux/sched/mm.h>
41 #include <linux/slab.h>
42 #include <linux/uaccess.h>
43 #include <net/netlink.h>
45 #if defined(CONFIG_KGDB)
49 #include <lustre_compat.h>
50 #include <libcfs/crypto/llcrypt.h>
51 #include <libcfs/linux/linux-time.h>
52 #include <libcfs/linux/linux-wait.h>
53 #include <libcfs/linux/linux-misc.h>
54 #include <libcfs/linux/linux-mem.h>
55 #ifndef HAVE_XARRAY_SUPPORT
56 #include <libcfs/linux/xarray.h>
59 #ifndef HAVE_KTIME_GET_TS64
60 void ktime_get_ts64(struct timespec64 *ts)
65 *ts = timespec_to_timespec64(now);
67 EXPORT_SYMBOL(ktime_get_ts64);
68 #endif /* HAVE_KTIME_GET_TS64 */
70 #ifndef HAVE_KTIME_GET_REAL_TS64
71 void ktime_get_real_ts64(struct timespec64 *ts)
76 *ts = timespec_to_timespec64(now);
78 EXPORT_SYMBOL(ktime_get_real_ts64);
79 #endif /* HAVE_KTIME_GET_REAL_TS64 */
81 #ifndef HAVE_KTIME_GET_REAL_SECONDS
83 * Get the seconds portion of CLOCK_REALTIME (wall clock).
84 * This is the clock that can be altered by NTP and is
85 * independent of a reboot.
87 time64_t ktime_get_real_seconds(void)
89 return (time64_t)get_seconds();
91 EXPORT_SYMBOL(ktime_get_real_seconds);
92 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
94 #ifndef HAVE_KTIME_GET_SECONDS
96 * Get the seconds portion of CLOCK_MONOTONIC
97 * This clock is immutable and is reset across
98 * reboots. For older platforms this is a
99 * wrapper around get_seconds which is valid
100 * until 2038. By that time this will be gone
103 time64_t ktime_get_seconds(void)
105 struct timespec64 now;
107 ktime_get_ts64(&now);
110 EXPORT_SYMBOL(ktime_get_seconds);
111 #endif /* HAVE_KTIME_GET_SECONDS */
113 static int (*cfs_apply_workqueue_attrs_t)(struct workqueue_struct *wq,
114 const struct workqueue_attrs *attrs);
116 int cfs_apply_workqueue_attrs(struct workqueue_struct *wq,
117 const struct workqueue_attrs *attrs)
119 if (cfs_apply_workqueue_attrs_t)
120 return cfs_apply_workqueue_attrs_t(wq, attrs);
123 EXPORT_SYMBOL_GPL(cfs_apply_workqueue_attrs);
125 /* Linux v5.1-rc5 214d8ca6ee ("stacktrace: Provide common infrastructure")
126 * CONFIG_ARCH_STACKWALK indicates that save_stack_trace_tsk symbol is not
127 * exported. Use symbol_get() to find if save_stack_trace_tsk is available.
129 #ifdef CONFIG_ARCH_STACKWALK
130 static unsigned int (*task_dump_stack_t)(struct task_struct *task,
131 unsigned long *store,
133 unsigned int skipnr);
135 int cfs_stack_trace_save_tsk(struct task_struct *task, unsigned long *store,
136 unsigned int size, unsigned int skipnr)
138 if (task_dump_stack_t)
139 return task_dump_stack_t(task, store, size, skipnr);
141 pr_info("No stack, save_stack_trace_tsk() could not be found\n");
147 #ifndef HAVE_XARRAY_SUPPORT
148 struct kmem_cache *xarray_cachep;
150 static void xarray_node_ctor(void *arg)
152 struct xa_node *node = arg;
154 memset(node, 0, sizeof(*node));
155 INIT_LIST_HEAD(&node->private_list);
160 * This is opencoding of vfree_atomic from Linux kernel added in 4.10 with
161 * minimum changes needed to work on older kernels too.
164 #ifndef llist_for_each_safe
165 #define llist_for_each_safe(pos, n, node) \
166 for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n))
169 struct vfree_deferred {
170 struct llist_head list;
171 struct work_struct wq;
173 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
175 static void free_work(struct work_struct *w)
177 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
178 struct llist_node *t, *llnode;
180 llist_for_each_safe(llnode, t, llist_del_all(&p->list))
181 vfree((void *)llnode);
184 void libcfs_vfree_atomic(const void *addr)
186 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
191 if (llist_add((struct llist_node *)addr, &p->list))
192 schedule_work(&p->wq);
194 EXPORT_SYMBOL(libcfs_vfree_atomic);
196 void __init init_libcfs_vfree_atomic(void)
200 for_each_possible_cpu(i) {
201 struct vfree_deferred *p;
203 p = &per_cpu(vfree_deferred, i);
204 init_llist_head(&p->list);
205 INIT_WORK(&p->wq, free_work);
209 int __init cfs_arch_init(void)
211 init_libcfs_vfree_atomic();
213 #ifndef HAVE_WAIT_VAR_EVENT
216 #ifdef CONFIG_ARCH_STACKWALK
218 (void *)cfs_kallsyms_lookup_name("stack_trace_save_tsk");
220 cfs_apply_workqueue_attrs_t =
221 (void *)cfs_kallsyms_lookup_name("apply_workqueue_attrs");
222 #ifndef HAVE_XARRAY_SUPPORT
223 xarray_cachep = kmem_cache_create("xarray_cache",
224 sizeof(struct xa_node), 0,
225 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
228 return llcrypt_init();
231 void __exit cfs_arch_exit(void)
233 /* exit_libcfs_vfree_atomic */
234 __flush_workqueue(system_wq);
239 int cfs_kernel_write(struct file *filp, const void *buf, size_t count,
242 #ifdef HAVE_NEW_KERNEL_WRITE
243 return kernel_write(filp, buf, count, pos);
245 mm_segment_t __old_fs = get_fs();
249 rc = vfs_write(filp, (__force const char __user *)buf, count, pos);
255 EXPORT_SYMBOL(cfs_kernel_write);
257 ssize_t cfs_kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
259 #ifdef HAVE_KERNEL_READ_LAST_POSP
260 return kernel_read(file, buf, count, pos);
262 ssize_t size = kernel_read(file, *pos, buf, count);
269 EXPORT_SYMBOL(cfs_kernel_read);
271 #ifndef HAVE_KSET_FIND_OBJ
272 struct kobject *kset_find_obj(struct kset *kset, const char *name)
274 struct kobject *ret = NULL;
277 spin_lock(&kset->list_lock);
279 list_for_each_entry(k, &kset->list, entry) {
280 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
281 if (kref_get_unless_zero(&k->kref))
287 spin_unlock(&kset->list_lock);
290 EXPORT_SYMBOL_GPL(kset_find_obj);
293 #ifndef HAVE_MATCH_WILDCARD
295 * match_wildcard: - parse if a string matches given wildcard pattern
296 * @pattern: wildcard pattern
297 * @str: the string to be parsed
299 * Description: Parse the string @str to check if matches wildcard
300 * pattern @pattern. The pattern may contain two type wildcardes:
301 * '*' - matches zero or more characters
302 * '?' - matches one character
303 * If it's matched, return true, else return false.
305 bool match_wildcard(const char *pattern, const char *str)
308 const char *p = pattern;
343 EXPORT_SYMBOL(match_wildcard);
344 #endif /* !HAVE_MATCH_WILDCARD */
346 #ifndef HAVE_BITMAP_TO_ARR32
348 * bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits
349 * @buf: array of u32 (in host byte order), the dest bitmap
350 * @bitmap: array of unsigned longs, the source bitmap
351 * @nbits: number of bits in @bitmap
353 void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
355 unsigned int i, halfwords;
357 halfwords = DIV_ROUND_UP(nbits, 32);
358 for (i = 0; i < halfwords; i++) {
359 buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
361 buf[i] = (u32) (bitmap[i/2] >> 32);
364 /* Clear tail bits in last element of array beyond nbits. */
365 if (nbits % BITS_PER_LONG)
366 buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
368 EXPORT_SYMBOL(bitmap_to_arr32);
369 #endif /* !HAVE_BITMAP_TO_ARR32 */
371 #ifndef HAVE_KSTRTOBOOL_FROM_USER
372 int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
374 /* Longest string needed to differentiate, newline, terminator */
377 count = min(count, sizeof(buf) - 1);
378 if (copy_from_user(buf, s, count))
381 return strtobool(buf, res);
383 EXPORT_SYMBOL(kstrtobool_from_user);
384 #endif /* !HAVE_KSTRTOBOOL_FROM_USER */
386 #ifndef HAVE_NLA_STRDUP
387 char *nla_strdup(const struct nlattr *nla, gfp_t flags)
389 size_t srclen = nla_len(nla);
390 char *src = nla_data(nla), *dst;
392 if (srclen > 0 && src[srclen - 1] == '\0')
395 dst = kmalloc(srclen + 1, flags);
397 memcpy(dst, src, srclen);
402 EXPORT_SYMBOL(nla_strdup);
403 #endif /* !HAVE_NLA_STRDUP */