hc
2023-11-06 e3e12f52b214121840b44c91de5b3e5af5d3eb84
kernel/mm/zsmalloc.c
....@@ -56,6 +56,7 @@
5656 #include <linux/wait.h>
5757 #include <linux/pagemap.h>
5858 #include <linux/fs.h>
59
+#include <linux/locallock.h>
5960
6061 #define ZSPAGE_MAGIC 0x58
6162
....@@ -73,8 +74,21 @@
7374 */
7475 #define ZS_MAX_ZSPAGE_ORDER 2
7576 #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
76
-
7777 #define ZS_HANDLE_SIZE (sizeof(unsigned long))
78
+
79
+#ifdef CONFIG_PREEMPT_RT_FULL
80
+
81
+struct zsmalloc_handle {
82
+ unsigned long addr;
83
+ struct mutex lock;
84
+};
85
+
86
+#define ZS_HANDLE_ALLOC_SIZE (sizeof(struct zsmalloc_handle))
87
+
88
+#else
89
+
90
+#define ZS_HANDLE_ALLOC_SIZE (sizeof(unsigned long))
91
+#endif
7892
7993 /*
8094 * Object location (<PFN>, <obj_idx>) is encoded as
....@@ -325,7 +339,7 @@
325339
326340 static int create_cache(struct zs_pool *pool)
327341 {
328
- pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
342
+ pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_ALLOC_SIZE,
329343 0, 0, NULL);
330344 if (!pool->handle_cachep)
331345 return 1;
....@@ -349,9 +363,26 @@
349363
350364 static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
351365 {
352
- return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
353
- gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
366
+ void *p;
367
+
368
+ p = kmem_cache_alloc(pool->handle_cachep,
369
+ gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
370
+#ifdef CONFIG_PREEMPT_RT_FULL
371
+ if (p) {
372
+ struct zsmalloc_handle *zh = p;
373
+
374
+ mutex_init(&zh->lock);
375
+ }
376
+#endif
377
+ return (unsigned long)p;
354378 }
379
+
380
+#ifdef CONFIG_PREEMPT_RT_FULL
381
+static struct zsmalloc_handle *zs_get_pure_handle(unsigned long handle)
382
+{
383
+ return (void *)(handle &~((1 << OBJ_TAG_BITS) - 1));
384
+}
385
+#endif
355386
356387 static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
357388 {
....@@ -371,12 +402,18 @@
371402
372403 static void record_obj(unsigned long handle, unsigned long obj)
373404 {
405
+#ifdef CONFIG_PREEMPT_RT_FULL
406
+ struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
407
+
408
+ WRITE_ONCE(zh->addr, obj);
409
+#else
374410 /*
375411 * lsb of @obj represents handle lock while other bits
376412 * represent object value the handle is pointing so
377413 * updating shouldn't do store tearing.
378414 */
379415 WRITE_ONCE(*(unsigned long *)handle, obj);
416
+#endif
380417 }
381418
382419 /* zpool driver */
....@@ -458,6 +495,7 @@
458495
459496 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
460497 static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
498
+static DEFINE_LOCAL_IRQ_LOCK(zs_map_area_lock);
461499
462500 static bool is_zspage_isolated(struct zspage *zspage)
463501 {
....@@ -887,7 +925,13 @@
887925
888926 static unsigned long handle_to_obj(unsigned long handle)
889927 {
928
+#ifdef CONFIG_PREEMPT_RT_FULL
929
+ struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
930
+
931
+ return zh->addr;
932
+#else
890933 return *(unsigned long *)handle;
934
+#endif
891935 }
892936
893937 static unsigned long obj_to_head(struct page *page, void *obj)
....@@ -901,22 +945,46 @@
901945
902946 static inline int testpin_tag(unsigned long handle)
903947 {
948
+#ifdef CONFIG_PREEMPT_RT_FULL
949
+ struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
950
+
951
+ return mutex_is_locked(&zh->lock);
952
+#else
904953 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
954
+#endif
905955 }
906956
907957 static inline int trypin_tag(unsigned long handle)
908958 {
959
+#ifdef CONFIG_PREEMPT_RT_FULL
960
+ struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
961
+
962
+ return mutex_trylock(&zh->lock);
963
+#else
909964 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
965
+#endif
910966 }
911967
912968 static void pin_tag(unsigned long handle)
913969 {
970
+#ifdef CONFIG_PREEMPT_RT_FULL
971
+ struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
972
+
973
+ return mutex_lock(&zh->lock);
974
+#else
914975 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
976
+#endif
915977 }
916978
917979 static void unpin_tag(unsigned long handle)
918980 {
981
+#ifdef CONFIG_PREEMPT_RT_FULL
982
+ struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
983
+
984
+ return mutex_unlock(&zh->lock);
985
+#else
919986 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
987
+#endif
920988 }
921989
922990 static void reset_page(struct page *page)
....@@ -1342,7 +1410,7 @@
13421410 class = pool->size_class[class_idx];
13431411 off = (class->size * obj_idx) & ~PAGE_MASK;
13441412
1345
- area = &get_cpu_var(zs_map_area);
1413
+ area = &get_locked_var(zs_map_area_lock, zs_map_area);
13461414 area->vm_mm = mm;
13471415 if (off + class->size <= PAGE_SIZE) {
13481416 /* this object is contained entirely within a page */
....@@ -1396,7 +1464,7 @@
13961464
13971465 __zs_unmap_object(area, pages, off, class->size);
13981466 }
1399
- put_cpu_var(zs_map_area);
1467
+ put_locked_var(zs_map_area_lock, zs_map_area);
14001468
14011469 migrate_read_unlock(zspage);
14021470 unpin_tag(handle);