Whamcloud - gitweb
LU-3570 libcfs: accelerate crc32c with pclmulqdq
[fs/lustre-release.git] / libcfs / libcfs / module.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_crypto.h>
41 #include <lnet/lib-lnet.h>
42 #include <lnet/lnet.h>
43 #include "tracefile.h"
44
45 void
46 kportal_memhog_free (struct libcfs_device_userstate *ldu)
47 {
48         struct page **level0p = &ldu->ldu_memhog_root_page;
49         struct page **level1p;
50         struct page **level2p;
51         int           count1;
52         int           count2;
53
54         if (*level0p != NULL) {
55                 level1p = (struct page **)page_address(*level0p);
56                 count1 = 0;
57
58                 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
59                        *level1p != NULL) {
60
61                         level2p = (struct page **)page_address(*level1p);
62                         count2 = 0;
63
64                         while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
65                                *level2p != NULL) {
66
67                                 __free_page(*level2p);
68                                 ldu->ldu_memhog_pages--;
69                                 level2p++;
70                                 count2++;
71                         }
72
73                         __free_page(*level1p);
74                         ldu->ldu_memhog_pages--;
75                         level1p++;
76                         count1++;
77                 }
78
79                 __free_page(*level0p);
80                 ldu->ldu_memhog_pages--;
81
82                 *level0p = NULL;
83         }
84
85         LASSERT(ldu->ldu_memhog_pages == 0);
86 }
87
88 int
89 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
90 {
91         struct page **level0p;
92         struct page **level1p;
93         struct page **level2p;
94         int           count1;
95         int           count2;
96
97         LASSERT(ldu->ldu_memhog_pages == 0);
98         LASSERT(ldu->ldu_memhog_root_page == NULL);
99
100         if (npages < 0)
101                 return -EINVAL;
102
103         if (npages == 0)
104                 return 0;
105
106         level0p = &ldu->ldu_memhog_root_page;
107         *level0p = alloc_page(flags);
108         if (*level0p == NULL)
109                 return -ENOMEM;
110         ldu->ldu_memhog_pages++;
111
112         level1p = (struct page **)page_address(*level0p);
113         count1 = 0;
114         memset(level1p, 0, PAGE_CACHE_SIZE);
115
116         while (ldu->ldu_memhog_pages < npages &&
117                count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
118
119                 if (cfs_signal_pending())
120                         return -EINTR;
121
122                 *level1p = alloc_page(flags);
123                 if (*level1p == NULL)
124                         return -ENOMEM;
125                 ldu->ldu_memhog_pages++;
126
127                 level2p = (struct page **)page_address(*level1p);
128                 count2 = 0;
129                 memset(level2p, 0, PAGE_CACHE_SIZE);
130
131                 while (ldu->ldu_memhog_pages < npages &&
132                        count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
133
134                         if (cfs_signal_pending())
135                                 return -EINTR;
136
137                         *level2p = alloc_page(flags);
138                         if (*level2p == NULL)
139                                 return -ENOMEM;
140                         ldu->ldu_memhog_pages++;
141
142                         level2p++;
143                         count2++;
144                 }
145
146                 level1p++;
147                 count1++;
148         }
149
150         return 0;
151 }
152
153 /* called when opening /dev/device */
154 static int libcfs_psdev_open(unsigned long flags, void *args)
155 {
156         struct libcfs_device_userstate *ldu;
157         ENTRY;
158
159         try_module_get(THIS_MODULE);
160
161         LIBCFS_ALLOC(ldu, sizeof(*ldu));
162         if (ldu != NULL) {
163                 ldu->ldu_memhog_pages = 0;
164                 ldu->ldu_memhog_root_page = NULL;
165         }
166         *(struct libcfs_device_userstate **)args = ldu;
167
168         RETURN(0);
169 }
170
171 /* called when closing /dev/device */
172 static int libcfs_psdev_release(unsigned long flags, void *args)
173 {
174         struct libcfs_device_userstate *ldu;
175         ENTRY;
176
177         ldu = (struct libcfs_device_userstate *)args;
178         if (ldu != NULL) {
179                 kportal_memhog_free(ldu);
180                 LIBCFS_FREE(ldu, sizeof(*ldu));
181         }
182
183         module_put(THIS_MODULE);
184         RETURN(0);
185 }
186
187 static struct rw_semaphore ioctl_list_sem;
188 static cfs_list_t ioctl_list;
189
190 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
191 {
192         int rc = 0;
193
194         down_write(&ioctl_list_sem);
195         if (!cfs_list_empty(&hand->item))
196                 rc = -EBUSY;
197         else
198                 cfs_list_add_tail(&hand->item, &ioctl_list);
199         up_write(&ioctl_list_sem);
200
201         return rc;
202 }
203 EXPORT_SYMBOL(libcfs_register_ioctl);
204
205 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
206 {
207         int rc = 0;
208
209         down_write(&ioctl_list_sem);
210         if (cfs_list_empty(&hand->item))
211                 rc = -ENOENT;
212         else
213                 cfs_list_del_init(&hand->item);
214         up_write(&ioctl_list_sem);
215
216         return rc;
217 }
218 EXPORT_SYMBOL(libcfs_deregister_ioctl);
219
220 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile,unsigned long cmd,
221                             void *arg, struct libcfs_ioctl_data *data)
222 {
223         int err = -EINVAL;
224         ENTRY;
225
226         switch (cmd) {
227         case IOC_LIBCFS_CLEAR_DEBUG:
228                 libcfs_debug_clear_buffer();
229                 RETURN(0);
230         /*
231          * case IOC_LIBCFS_PANIC:
232          * Handled in arch/cfs_module.c
233          */
234         case IOC_LIBCFS_MARK_DEBUG:
235                 if (data->ioc_inlbuf1 == NULL ||
236                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
237                         RETURN(-EINVAL);
238                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
239                 RETURN(0);
240 #if LWT_SUPPORT
241         case IOC_LIBCFS_LWT_CONTROL:
242                 err = lwt_control ((data->ioc_flags & 1) != 0, 
243                                    (data->ioc_flags & 2) != 0);
244                 break;
245
246         case IOC_LIBCFS_LWT_SNAPSHOT: {
247                 cfs_cycles_t   now;
248                 int            ncpu;
249                 int            total_size;
250
251                 err = lwt_snapshot (&now, &ncpu, &total_size,
252                                     data->ioc_pbuf1, data->ioc_plen1);
253                 data->ioc_u64[0] = now;
254                 data->ioc_u32[0] = ncpu;
255                 data->ioc_u32[1] = total_size;
256
257                 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
258                 data->ioc_u32[2] = sizeof(lwt_event_t);
259                 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
260
261                 if (err == 0 &&
262                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
263                         err = -EFAULT;
264                 break;
265         }
266
267         case IOC_LIBCFS_LWT_LOOKUP_STRING:
268                 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
269                                          data->ioc_pbuf2, data->ioc_plen2);
270                 if (err == 0 &&
271                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
272                         err = -EFAULT;
273                 break;
274 #endif
275         case IOC_LIBCFS_MEMHOG:
276                 if (pfile->private_data == NULL) {
277                         err = -EINVAL;
278                 } else {
279                         kportal_memhog_free(pfile->private_data);
280                         /* XXX The ioc_flags is not GFP flags now, need to be fixed */
281                         err = kportal_memhog_alloc(pfile->private_data,
282                                                    data->ioc_count,
283                                                    data->ioc_flags);
284                         if (err != 0)
285                                 kportal_memhog_free(pfile->private_data);
286                 }
287                 break;
288
289         case IOC_LIBCFS_PING_TEST: {
290                 extern void (kping_client)(struct libcfs_ioctl_data *);
291                 void (*ping)(struct libcfs_ioctl_data *);
292
293                 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
294                        data->ioc_count, libcfs_nid2str(data->ioc_nid),
295                        libcfs_nid2str(data->ioc_nid));
296                 ping = symbol_get(kping_client);
297                 if (!ping) {
298                         CERROR("symbol_get failed\n");
299                 } else {
300                         ping(data);
301                         symbol_put(kping_client);
302                 }
303                 RETURN(0);
304         }
305
306         default: {
307                 struct libcfs_ioctl_handler *hand;
308                 err = -EINVAL;
309                 down_read(&ioctl_list_sem);
310                 cfs_list_for_each_entry_typed(hand, &ioctl_list,
311                         struct libcfs_ioctl_handler, item) {
312                         err = hand->handle_ioctl(cmd, data);
313                         if (err != -EINVAL) {
314                                 if (err == 0)
315                                         err = libcfs_ioctl_popdata(arg, 
316                                                         data, sizeof (*data));
317                                 break;
318                         }
319                 }
320                 up_read(&ioctl_list_sem);
321                 break;
322         }
323         }
324
325         RETURN(err);
326 }
327
328 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
329                         unsigned long cmd, void *arg)
330 {
331         char    *buf;
332         struct libcfs_ioctl_data *data;
333         int err = 0;
334         ENTRY;
335
336         LIBCFS_ALLOC_GFP(buf, 1024, GFP_IOFS);
337         if (buf == NULL)
338                 RETURN(-ENOMEM);
339
340         /* 'cmd' and permissions get checked in our arch-specific caller */
341         if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
342                 CERROR("PORTALS ioctl: data error\n");
343                 GOTO(out, err = -EINVAL);
344         }
345         data = (struct libcfs_ioctl_data *)buf;
346
347         err = libcfs_ioctl_int(pfile, cmd, arg, data);
348
349 out:
350         LIBCFS_FREE(buf, 1024);
351         RETURN(err);
352 }
353
354
355 struct cfs_psdev_ops libcfs_psdev_ops = {
356         libcfs_psdev_open,
357         libcfs_psdev_release,
358         NULL,
359         NULL,
360         libcfs_ioctl
361 };
362
363 extern int insert_proc(void);
364 extern void remove_proc(void);
365 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
366 MODULE_DESCRIPTION("Portals v3.1");
367 MODULE_LICENSE("GPL");
368
369 extern struct miscdevice libcfs_dev;
370 extern struct rw_semaphore cfs_tracefile_sem;
371 extern struct mutex cfs_trace_thread_mutex;
372 extern struct cfs_wi_sched *cfs_sched_rehash;
373
374 extern void libcfs_init_nidstrings(void);
375 extern int libcfs_arch_init(void);
376 extern void libcfs_arch_cleanup(void);
377
378 static int init_libcfs_module(void)
379 {
380         int rc;
381
382         libcfs_arch_init();
383         libcfs_init_nidstrings();
384         init_rwsem(&cfs_tracefile_sem);
385         mutex_init(&cfs_trace_thread_mutex);
386         init_rwsem(&ioctl_list_sem);
387         CFS_INIT_LIST_HEAD(&ioctl_list);
388         init_waitqueue_head(&cfs_race_waitq);
389
390         rc = libcfs_debug_init(5 * 1024 * 1024);
391         if (rc < 0) {
392                 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
393                 return (rc);
394         }
395
396         rc = cfs_cpu_init();
397         if (rc != 0)
398                 goto cleanup_debug;
399
400 #if LWT_SUPPORT
401         rc = lwt_init();
402         if (rc != 0) {
403                 CERROR("lwt_init: error %d\n", rc);
404                 goto cleanup_debug;
405         }
406 #endif
407         rc = misc_register(&libcfs_dev);
408         if (rc) {
409                 CERROR("misc_register: error %d\n", rc);
410                 goto cleanup_lwt;
411         }
412
413         rc = cfs_wi_startup();
414         if (rc) {
415                 CERROR("initialize workitem: error %d\n", rc);
416                 goto cleanup_deregister;
417         }
418
419         /* max to 4 threads, should be enough for rehash */
420         rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
421         rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
422                                  rc, &cfs_sched_rehash);
423         if (rc != 0) {
424                 CERROR("Startup workitem scheduler: error: %d\n", rc);
425                 goto cleanup_deregister;
426         }
427
428         rc = cfs_crypto_register();
429         if (rc) {
430                 CERROR("cfs_crypto_regster: error %d\n", rc);
431                 goto cleanup_wi;
432         }
433
434
435         rc = insert_proc();
436         if (rc) {
437                 CERROR("insert_proc: error %d\n", rc);
438                 goto cleanup_crypto;
439         }
440
441         CDEBUG (D_OTHER, "portals setup OK\n");
442         return 0;
443  cleanup_crypto:
444         cfs_crypto_unregister();
445  cleanup_wi:
446         cfs_wi_shutdown();
447  cleanup_deregister:
448         misc_deregister(&libcfs_dev);
449  cleanup_lwt:
450 #if LWT_SUPPORT
451         lwt_fini();
452 #endif
453  cleanup_debug:
454         libcfs_debug_cleanup();
455         return rc;
456 }
457
458 static void exit_libcfs_module(void)
459 {
460         int rc;
461
462         remove_proc();
463
464         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
465                atomic_read(&libcfs_kmemory));
466
467         if (cfs_sched_rehash != NULL) {
468                 cfs_wi_sched_destroy(cfs_sched_rehash);
469                 cfs_sched_rehash = NULL;
470         }
471
472         cfs_crypto_unregister();
473         cfs_wi_shutdown();
474
475         rc = misc_deregister(&libcfs_dev);
476         if (rc)
477                 CERROR("misc_deregister error %d\n", rc);
478
479 #if LWT_SUPPORT
480         lwt_fini();
481 #endif
482         cfs_cpu_fini();
483
484         if (atomic_read(&libcfs_kmemory) != 0)
485                 CERROR("Portals memory leaked: %d bytes\n",
486                        atomic_read(&libcfs_kmemory));
487
488         rc = libcfs_debug_cleanup();
489         if (rc)
490                 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
491                        rc);
492
493         fini_rwsem(&ioctl_list_sem);
494         fini_rwsem(&cfs_tracefile_sem);
495
496         libcfs_arch_cleanup();
497 }
498
499 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);