Whamcloud - gitweb
LU-13783 libcfs: support __vmalloc with only 2 args.
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-mem.h
index 1d0f03d..69d027f 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, Whamcloud, Inc.
+ * Copyright (c) 2011, 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 #ifndef __LIBCFS_LINUX_CFS_MEM_H__
 #define __LIBCFS_LINUX_CFS_MEM_H__
 
-#ifndef __LIBCFS_LIBCFS_H__
-#error Do not #include this file directly. #include <libcfs/libcfs.h> instead
-#endif
-
-#ifndef __KERNEL__
-#error This include is only for kernel use.
-#endif
-
 #include <linux/mm.h>
 #include <linux/vmalloc.h>
 #include <linux/pagemap.h>
 #ifdef HAVE_MM_INLINE
 # include <linux/mm_inline.h>
 #endif
+#include <linux/sched.h>
+#ifdef HAVE_SCHED_HEADERS
+#include <linux/sched/mm.h>
+#endif
 
-typedef struct page                     cfs_page_t;
-#define CFS_PAGE_SIZE                   PAGE_CACHE_SIZE
-#define CFS_PAGE_SHIFT                  PAGE_CACHE_SHIFT
-#define CFS_PAGE_MASK                   (~((__u64)CFS_PAGE_SIZE-1))
-
-#define cfs_num_physpages               num_physpages
-
-#define cfs_copy_from_user(to, from, n) copy_from_user(to, from, n)
-#define cfs_copy_to_user(to, from, n)   copy_to_user(to, from, n)
+#ifdef HAVE_TOTALRAM_PAGES_AS_FUNC
+ #ifndef cfs_totalram_pages
+  #define cfs_totalram_pages() totalram_pages()
+ #endif
+#else
+ #ifndef cfs_totalram_pages
+  #define cfs_totalram_pages() totalram_pages
+ #endif
+#endif
 
-static inline void *cfs_page_address(cfs_page_t *page)
+#ifndef HAVE_MEMALLOC_RECLAIM
+static inline unsigned int memalloc_noreclaim_save(void)
 {
-        /*
-         * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
-         * from here: this will lead to infinite recursion.
-         */
-        return page_address(page);
+       unsigned int flags = current->flags & PF_MEMALLOC;
+
+       current->flags |= PF_MEMALLOC;
+       return flags;
 }
 
-static inline void *cfs_kmap(cfs_page_t *page)
+static inline void memalloc_noreclaim_restore(unsigned int flags)
 {
-        return kmap(page);
+       current->flags = (current->flags & ~PF_MEMALLOC) | flags;
 }
+#endif /* !HAVE_MEMALLOC_RECLAIM */
 
-static inline void cfs_kunmap(cfs_page_t *page)
+#ifndef HAVE_BITMAP_ALLOC
+static inline unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
 {
-        kunmap(page);
+       return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
+                            flags);
 }
 
-static inline void cfs_get_page(cfs_page_t *page)
+static inline unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
 {
-        get_page(page);
+       return bitmap_alloc(nbits, flags | __GFP_ZERO);
 }
 
-static inline int cfs_page_count(cfs_page_t *page)
+static inline void bitmap_free(const unsigned long *bitmap)
 {
-        return page_count(page);
+       kfree(bitmap);
 }
-
-#define cfs_page_index(p)       ((p)->index)
-
-#define cfs_page_pin(page) page_cache_get(page)
-#define cfs_page_unpin(page) page_cache_release(page)
-
-/*
- * Memory allocator
- * XXX Liang: move these declare to public file
- */
-extern void *cfs_alloc(size_t nr_bytes, u_int32_t flags);
-extern void  cfs_free(void *addr);
-
-extern void *cfs_alloc_large(size_t nr_bytes);
-extern void  cfs_free_large(void *addr);
-
-extern cfs_page_t *cfs_alloc_page(unsigned int flags);
-extern void cfs_free_page(cfs_page_t *page);
-
-#define cfs_memory_pressure_get() (current->flags & PF_MEMALLOC)
-#define cfs_memory_pressure_set() do { current->flags |= PF_MEMALLOC; } while (0)
-#define cfs_memory_pressure_clr() do { current->flags &= ~PF_MEMALLOC; } while (0)
-
-#if BITS_PER_LONG == 32
-/* limit to lowmem on 32-bit systems */
-#define CFS_NUM_CACHEPAGES \
-        min(cfs_num_physpages, 1UL << (30 - CFS_PAGE_SHIFT) * 3 / 4)
-#else
-#define CFS_NUM_CACHEPAGES cfs_num_physpages
-#endif
-
-/*
- * In Linux there is no way to determine whether current execution context is
- * blockable.
- */
-#define CFS_ALLOC_ATOMIC_TRY   CFS_ALLOC_ATOMIC
-
-/*
- * SLAB allocator
- * XXX Liang: move these declare to public file
- */
-typedef struct kmem_cache cfs_mem_cache_t;
-extern cfs_mem_cache_t * cfs_mem_cache_create (const char *, size_t, size_t, unsigned long);
-extern int cfs_mem_cache_destroy ( cfs_mem_cache_t * );
-extern void *cfs_mem_cache_alloc ( cfs_mem_cache_t *, int);
-extern void cfs_mem_cache_free ( cfs_mem_cache_t *, void *);
-extern int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem);
-
-#define CFS_DECL_MMSPACE                mm_segment_t __oldfs
-#define CFS_MMSPACE_OPEN \
-        do { __oldfs = get_fs(); set_fs(get_ds());} while(0)
-#define CFS_MMSPACE_CLOSE               set_fs(__oldfs)
-
-#define CFS_SLAB_HWCACHE_ALIGN          SLAB_HWCACHE_ALIGN
-#define CFS_SLAB_KERNEL                 SLAB_KERNEL
-#define CFS_SLAB_NOFS                   SLAB_NOFS
-
-/*
- * NUMA allocators
- *
- * NB: we will rename these functions in a separate patch:
- * - rename cfs_alloc to cfs_malloc
- * - rename cfs_alloc/free_page to cfs_page_alloc/free
- * - rename cfs_alloc/free_large to cfs_vmalloc/vfree
- */
-extern void *cfs_cpt_malloc(struct cfs_cpt_table *cptab, int cpt,
-                           size_t nr_bytes, unsigned int flags);
-extern void *cfs_cpt_vmalloc(struct cfs_cpt_table *cptab, int cpt,
-                            size_t nr_bytes);
-extern cfs_page_t *cfs_page_cpt_alloc(struct cfs_cpt_table *cptab,
-                                     int cpt, unsigned int flags);
-extern void *cfs_mem_cache_cpt_alloc(cfs_mem_cache_t *cachep,
-                                    struct cfs_cpt_table *cptab,
-                                    int cpt, unsigned int flags);
+#endif /* !HAVE_BITMAP_ALLOC */
 
 /*
  * Shrinker
  */
-#define cfs_shrinker    shrinker
-
-#ifdef HAVE_SHRINK_CONTROL
-# define SHRINKER_ARGS(sc, nr_to_scan, gfp_mask)  \
-                       struct shrinker *shrinker, \
-                       struct shrink_control *sc
-# define shrink_param(sc, var) ((sc)->var)
-#else
-# ifdef HAVE_SHRINKER_WANT_SHRINK_PTR
-#  define SHRINKER_ARGS(sc, nr_to_scan, gfp_mask)  \
-                        struct shrinker *shrinker, \
-                        int nr_to_scan, gfp_t gfp_mask
-# else
-#  define SHRINKER_ARGS(sc, nr_to_scan, gfp_mask)  \
-                        int nr_to_scan, gfp_t gfp_mask
-# endif
-# define shrink_param(sc, var) (var)
+#ifndef SHRINK_STOP
+# define SHRINK_STOP (~0UL)
 #endif
 
-#ifdef HAVE_REGISTER_SHRINKER
-typedef int (*cfs_shrinker_t)(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask));
-
-static inline
-struct cfs_shrinker *cfs_set_shrinker(int seek, cfs_shrinker_t func)
+#ifndef HAVE_MMAP_LOCK
+static inline void mmap_write_lock(struct mm_struct *mm)
 {
-        struct shrinker *s;
-
-        s = kmalloc(sizeof(*s), GFP_KERNEL);
-        if (s == NULL)
-                return (NULL);
+       down_write(&mm->mmap_sem);
+}
 
-        s->shrink = func;
-        s->seeks = seek;
+static inline bool mmap_write_trylock(struct mm_struct *mm)
+{
+       return down_write_trylock(&mm->mmap_sem) != 0;
+}
 
-        register_shrinker(s);
+static inline void mmap_write_unlock(struct mm_struct *mm)
+{
+       up_write(&mm->mmap_sem);
+}
 
-        return s;
+static inline void mmap_read_lock(struct mm_struct *mm)
+{
+       down_read(&mm->mmap_sem);
 }
 
-static inline
-void cfs_remove_shrinker(struct cfs_shrinker *shrinker)
+static inline bool mmap_read_trylock(struct mm_struct *mm)
 {
-        if (shrinker == NULL)
-                return;
+       return down_read_trylock(&mm->mmap_sem) != 0;
+}
 
-        unregister_shrinker(shrinker);
-        kfree(shrinker);
+static inline void mmap_read_unlock(struct mm_struct *mm)
+{
+       up_read(&mm->mmap_sem);
 }
+#endif
+
+#ifdef HAVE_VMALLOC_2ARGS
+#define __ll_vmalloc(size, flags) __vmalloc(size, flags)
 #else
-typedef shrinker_t              cfs_shrinker_t;
-#define cfs_set_shrinker(s, f)  set_shrinker(s, f)
-#define cfs_remove_shrinker(s)  remove_shrinker(s)
+#define __ll_vmalloc(size, flags) __vmalloc(size, flags, PAGE_KERNEL)
 #endif
 
-#define CFS_DEFAULT_SEEKS                 DEFAULT_SEEKS
 #endif /* __LINUX_CFS_MEM_H__ */