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);
144 #ifndef HAVE_XARRAY_SUPPORT
145 struct kmem_cache *xarray_cachep;
147 static void xarray_node_ctor(void *arg)
149 struct xa_node *node = arg;
151 memset(node, 0, sizeof(*node));
152 INIT_LIST_HEAD(&node->private_list);
157 * This is opencoding of vfree_atomic from Linux kernel added in 4.10 with
158 * minimum changes needed to work on older kernels too.
161 #ifndef llist_for_each_safe
162 #define llist_for_each_safe(pos, n, node) \
163 for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n))
166 struct vfree_deferred {
167 struct llist_head list;
168 struct work_struct wq;
170 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
172 static void free_work(struct work_struct *w)
174 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
175 struct llist_node *t, *llnode;
177 llist_for_each_safe(llnode, t, llist_del_all(&p->list))
178 vfree((void *)llnode);
181 void libcfs_vfree_atomic(const void *addr)
183 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
188 if (llist_add((struct llist_node *)addr, &p->list))
189 schedule_work(&p->wq);
191 EXPORT_SYMBOL(libcfs_vfree_atomic);
193 void __init init_libcfs_vfree_atomic(void)
197 for_each_possible_cpu(i) {
198 struct vfree_deferred *p;
200 p = &per_cpu(vfree_deferred, i);
201 init_llist_head(&p->list);
202 INIT_WORK(&p->wq, free_work);
206 int __init cfs_arch_init(void)
208 init_libcfs_vfree_atomic();
210 #ifndef HAVE_WAIT_VAR_EVENT
213 #ifdef CONFIG_ARCH_STACKWALK
215 (void *)cfs_kallsyms_lookup_name("stack_trace_save_tsk");
217 cfs_apply_workqueue_attrs_t =
218 (void *)cfs_kallsyms_lookup_name("apply_workqueue_attrs");
219 #ifndef HAVE_XARRAY_SUPPORT
220 xarray_cachep = kmem_cache_create("xarray_cache",
221 sizeof(struct xa_node), 0,
222 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
225 return llcrypt_init();
228 void __exit cfs_arch_exit(void)
230 /* exit_libcfs_vfree_atomic */
231 flush_scheduled_work();
236 int cfs_kernel_write(struct file *filp, const void *buf, size_t count,
239 #ifdef HAVE_NEW_KERNEL_WRITE
240 return kernel_write(filp, buf, count, pos);
242 mm_segment_t __old_fs = get_fs();
246 rc = vfs_write(filp, (__force const char __user *)buf, count, pos);
252 EXPORT_SYMBOL(cfs_kernel_write);
254 ssize_t cfs_kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
256 #ifdef HAVE_KERNEL_READ_LAST_POSP
257 return kernel_read(file, buf, count, pos);
259 ssize_t size = kernel_read(file, *pos, buf, count);
266 EXPORT_SYMBOL(cfs_kernel_read);
268 #ifndef HAVE_KSET_FIND_OBJ
269 struct kobject *kset_find_obj(struct kset *kset, const char *name)
271 struct kobject *ret = NULL;
274 spin_lock(&kset->list_lock);
276 list_for_each_entry(k, &kset->list, entry) {
277 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
278 if (kref_get_unless_zero(&k->kref))
284 spin_unlock(&kset->list_lock);
287 EXPORT_SYMBOL_GPL(kset_find_obj);
290 #ifndef HAVE_MATCH_WILDCARD
292 * match_wildcard: - parse if a string matches given wildcard pattern
293 * @pattern: wildcard pattern
294 * @str: the string to be parsed
296 * Description: Parse the string @str to check if matches wildcard
297 * pattern @pattern. The pattern may contain two type wildcardes:
298 * '*' - matches zero or more characters
299 * '?' - matches one character
300 * If it's matched, return true, else return false.
302 bool match_wildcard(const char *pattern, const char *str)
305 const char *p = pattern;
340 EXPORT_SYMBOL(match_wildcard);
341 #endif /* !HAVE_MATCH_WILDCARD */
343 #ifndef HAVE_BITMAP_TO_ARR32
345 * bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits
346 * @buf: array of u32 (in host byte order), the dest bitmap
347 * @bitmap: array of unsigned longs, the source bitmap
348 * @nbits: number of bits in @bitmap
350 void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
352 unsigned int i, halfwords;
354 halfwords = DIV_ROUND_UP(nbits, 32);
355 for (i = 0; i < halfwords; i++) {
356 buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
358 buf[i] = (u32) (bitmap[i/2] >> 32);
361 /* Clear tail bits in last element of array beyond nbits. */
362 if (nbits % BITS_PER_LONG)
363 buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
365 EXPORT_SYMBOL(bitmap_to_arr32);
366 #endif /* !HAVE_BITMAP_TO_ARR32 */
368 #ifndef HAVE_KSTRTOBOOL_FROM_USER
369 int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
371 /* Longest string needed to differentiate, newline, terminator */
374 count = min(count, sizeof(buf) - 1);
375 if (copy_from_user(buf, s, count))
378 return strtobool(buf, res);
380 EXPORT_SYMBOL(kstrtobool_from_user);
381 #endif /* !HAVE_KSTRTOBOOL_FROM_USER */
383 #ifndef HAVE_NLA_STRDUP
384 char *nla_strdup(const struct nlattr *nla, gfp_t flags)
386 size_t srclen = nla_len(nla);
387 char *src = nla_data(nla), *dst;
389 if (srclen > 0 && src[srclen - 1] == '\0')
392 dst = kmalloc(srclen + 1, flags);
394 memcpy(dst, src, srclen);
399 EXPORT_SYMBOL(nla_strdup);
400 #endif /* !HAVE_NLA_STRDUP */