Skip to contentSkip to conversation

security: mark kmem caches as __ro_after_init

PATCH New Alexey Dobriyan 4 participants 5 messages Feb 26, 2018 – Feb 28, 2018

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 security: mark kmem caches as __ro_after_init

Kmem caches are never reallocated once set up.

---

 security/integrity/iint.c     |    3 ++-
 security/selinux/avc.c        |    9 +++++----
 security/selinux/hooks.c      |    5 +++--
 security/selinux/ss/avtab.c   |    5 +++--
 security/selinux/ss/ebitmap.c |    3 ++-
 security/selinux/ss/hashtab.c |    3 ++-
 security/smack/smack_lsm.c    |    3 ++-
 7 files changed, 19 insertions(+), 12 deletions(-)
security/integrity/iint.c +2 -1
@@ -15,6 +15,7 @@
  *	- cache integrity information associated with an inode
  *	  using a rbtree tree.
  */
+#include <linux/cache.h>
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/spinlock.h>
@@ -25,7 +26,7 @@
 
 static struct rb_root integrity_iint_tree = RB_ROOT;
 static DEFINE_RWLOCK(integrity_iint_lock);
-static struct kmem_cache *iint_cache __read_mostly;
+static struct kmem_cache *iint_cache __ro_after_init;
 
 /*
  * __integrity_iint_find - return the iint associated with an inode
security/selinux/avc.c +5 -4
@@ -13,6 +13,7 @@
  *	it under the terms of the GNU General Public License version 2,
  *	as published by the Free Software Foundation.
  */
+#include <linux/cache.h>
 #include <linux/types.h>
 #include <linux/stddef.h>
 #include <linux/kernel.h>
@@ -91,10 +92,10 @@ DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
 
 static struct avc_cache avc_cache;
 static struct avc_callback_node *avc_callbacks;
-static struct kmem_cache *avc_node_cachep;
-static struct kmem_cache *avc_xperms_data_cachep;
-static struct kmem_cache *avc_xperms_decision_cachep;
-static struct kmem_cache *avc_xperms_cachep;
+static struct kmem_cache *avc_node_cachep __ro_after_init;
+static struct kmem_cache *avc_xperms_data_cachep __ro_after_init;
+static struct kmem_cache *avc_xperms_decision_cachep __ro_after_init;
+static struct kmem_cache *avc_xperms_cachep __ro_after_init;
 
 static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
 {
security/selinux/hooks.c +3 -2
@@ -24,6 +24,7 @@
  *	as published by the Free Software Foundation.
  */
 
+#include <linux/cache.h>
 #include <linux/init.h>
 #include <linux/kd.h>
 #include <linux/kernel.h>
@@ -129,8 +130,8 @@ __setup("selinux=", selinux_enabled_setup);
 int selinux_enabled = 1;
 #endif
 
-static struct kmem_cache *sel_inode_cache;
-static struct kmem_cache *file_security_cache;
+static struct kmem_cache *sel_inode_cache __ro_after_init;
+static struct kmem_cache *file_security_cache __ro_after_init;
 
 /**
  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
security/selinux/ss/avtab.c +3 -2
@@ -17,14 +17,15 @@
  *	Tuned number of hash slots for avtab to reduce memory usage
  */
 
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include "avtab.h"
 #include "policydb.h"
 
-static struct kmem_cache *avtab_node_cachep;
-static struct kmem_cache *avtab_xperms_cachep;
+static struct kmem_cache *avtab_node_cachep __ro_after_init;
+static struct kmem_cache *avtab_xperms_cachep __ro_after_init;
 
 /* Based on MurmurHash3, written by Austin Appleby and placed in the
  * public domain.
security/selinux/ss/ebitmap.c +2 -1
@@ -16,6 +16,7 @@
  *      Applied standard bit operations to improve bitmap scanning.
  */
 
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
@@ -25,7 +26,7 @@
 
 #define BITS_PER_U64	(sizeof(u64) * 8)
 
-static struct kmem_cache *ebitmap_node_cachep;
+static struct kmem_cache *ebitmap_node_cachep __ro_after_init;
 
 int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
 {
security/selinux/ss/hashtab.c +2 -1
@@ -4,13 +4,14 @@
  *
  * Author : Stephen Smalley, <[email protected]>
  */
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include <linux/sched.h>
 #include "hashtab.h"
 
-static struct kmem_cache *hashtab_node_cachep;
+static struct kmem_cache *hashtab_node_cachep __ro_after_init;
 
 struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
 			       int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
security/smack/smack_lsm.c +2 -1
@@ -18,6 +18,7 @@
  *      as published by the Free Software Foundation.
  */
 
+#include <linux/cache.h>
 #include <linux/xattr.h>
 #include <linux/pagemap.h>
 #include <linux/mount.h>
@@ -55,7 +56,7 @@
 DEFINE_MUTEX(smack_ipv6_lock);
 static LIST_HEAD(smk_ipv6_port_list);
 #endif
-static struct kmem_cache *smack_inode_cache;
+static struct kmem_cache *smack_inode_cache __ro_after_init;
 int smack_enabled;
 
 static const match_table_t smk_mount_tokens = {

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#1 lore raw

PATCH 1/1 security: mark kmem caches as __ro_after_init

Quoted 5 lines · Alexey Dobriyan

Kmem caches are never reallocated once set up.

---

Quoted 8 lines
 security/integrity/iint.c     |    3 ++-
 security/selinux/avc.c        |    9 +++++----
 security/selinux/hooks.c      |    5 +++--
 security/selinux/ss/avtab.c   |    5 +++--
 security/selinux/ss/ebitmap.c |    3 ++-
 security/selinux/ss/hashtab.c |    3 ++-
 security/smack/smack_lsm.c    |    3 ++-
 7 files changed, 19 insertions(+), 12 deletions(-)

In the future you're probably better off separating the SELinux, Smack, and IMA pieces into separate patches. SELinux comments inline
...

Quoted 7 lines
security/selinux/ss/avtab.c +1 -0
@@ -17,14 +17,15 @@
  *     Tuned number of hash slots for avtab to reduce memory usage
  */
 
+#include <linux/cache.h>

security/selinux/ss/avtab.c line 28

Quoted 10 lines
security/selinux/ss/avtab.c +2 -2
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include "avtab.h"
 #include "policydb.h"
 
-static struct kmem_cache *avtab_node_cachep;
-static struct kmem_cache *avtab_xperms_cachep;
+static struct kmem_cache *avtab_node_cachep __ro_after_init;
+static struct kmem_cache *avtab_xperms_cachep __ro_after_init;

Both avtab_node_cachep and avtab_xperms_cachep get allocated in avtab_cache_init() which is called during the first policy load (from userspace) which happens after we are past __init, yes?

This is why we don't mark avtab_cache_init() with the __init macro.

Quoted 11 lines
security/selinux/ss/ebitmap.c +1 -0
@@ -16,6 +16,7 @@
  *      Applied standard bit operations to improve bitmap scanning.
  */
 
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>

security/selinux/ss/ebitmap.c line 29

Quoted 5 lines
security/selinux/ss/ebitmap.c +1 -1
@@ -25,7 +26,7 @@
 
 #define BITS_PER_U64   (sizeof(u64) * 8)
 
-static struct kmem_cache *ebitmap_node_cachep;
+static struct kmem_cache *ebitmap_node_cachep __ro_after_init;

Same.

Quoted 5 lines
security/selinux/ss/hashtab.c +0 -0
@@ -4,13 +4,14 @@
  *
  * Author : Stephen Smalley, <[email protected]>

security/selinux/ss/hashtab.c line 14

Quoted 10 lines
security/selinux/ss/hashtab.c +2 -1
  */
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include <linux/sched.h>
 #include "hashtab.h"
 
-static struct kmem_cache *hashtab_node_cachep;
+static struct kmem_cache *hashtab_node_cachep __ro_after_init;

Same.

Signature · 6 lines
paul moore
www.paul-moore.com
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

PATCH 1/1 security: mark kmem caches as __ro_after_init

Quoted 41 lines · Paul Moore
Quoted 5 lines · Alexey Dobriyan

Kmem caches are never reallocated once set up.

---

Quoted 8 lines
 security/integrity/iint.c     |    3 ++-
 security/selinux/avc.c        |    9 +++++----
 security/selinux/hooks.c      |    5 +++--
 security/selinux/ss/avtab.c   |    5 +++--
 security/selinux/ss/ebitmap.c |    3 ++-
 security/selinux/ss/hashtab.c |    3 ++-
 security/smack/smack_lsm.c    |    3 ++-
 7 files changed, 19 insertions(+), 12 deletions(-)

In the future you're probably better off separating the SELinux, Smack, and IMA pieces into separate patches. SELinux comments inline
...

Quoted 7 lines
security/selinux/ss/avtab.c +1 -0
@@ -17,14 +17,15 @@
  *     Tuned number of hash slots for avtab to reduce memory usage
  */
 
+#include <linux/cache.h>

security/selinux/ss/avtab.c line 28

Quoted 10 lines
security/selinux/ss/avtab.c +2 -2
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include "avtab.h"
 #include "policydb.h"
 
-static struct kmem_cache *avtab_node_cachep;
-static struct kmem_cache *avtab_xperms_cachep;
+static struct kmem_cache *avtab_node_cachep __ro_after_init;
+static struct kmem_cache *avtab_xperms_cachep __ro_after_init;

Both avtab_node_cachep and avtab_xperms_cachep get allocated in avtab_cache_init() which is called during the first policy load (from userspace) which happens after we are past __init, yes?

Quoted 1 line

This is why we don't mark avtab_cache_init() with the __init macro.

NB My "selinux: wrap global selinux state" patch moves this initialization to selinux_init(), at which point we can in fact mark these caches this way. I think that is more correct anyway, but it was specifically motivated by the fact that we only want to perform this initialization once and first policy load becomes a per-state/namespace operation.

Quoted 33 lines
Quoted 12 lines
security/selinux/ss/ebitmap.c +1 -0
@@ -16,6 +16,7 @@
  *      Applied standard bit operations to improve bitmap scanning.
  */
 
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>

security/selinux/ss/ebitmap.c line 29

Quoted 5 lines
security/selinux/ss/ebitmap.c +1 -1
@@ -25,7 +26,7 @@
 
 #define BITS_PER_U64   (sizeof(u64) * 8)
 
-static struct kmem_cache *ebitmap_node_cachep;
+static struct kmem_cache *ebitmap_node_cachep __ro_after_init;

Same.

Quoted 13 lines
security/selinux/ss/hashtab.c +1 -0
@@ -4,13 +4,14 @@
  *
  * Author : Stephen Smalley, <[email protected]>
  */
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include <linux/sched.h>
 #include "hashtab.h"
 
Quoted 5 lines

security/selinux/ss/hashtab.c line 14

Quoted 2 lines
security/selinux/ss/hashtab.c +1 -1
-static struct kmem_cache *hashtab_node_cachep;
+static struct kmem_cache *hashtab_node_cachep __ro_after_init;

Same.

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

PATCH 1/1 security: mark kmem caches as __ro_after_init

Quoted 9 lines · Stephen Smalley
Quoted 1 line · Paul Moore

This is why we don't mark avtab_cache_init() with the __init macro.

NB My "selinux: wrap global selinux state" patch moves this initialization to selinux_init(), at which point we can in fact mark these caches this way. I think that is more correct anyway, but it was specifically motivated by the fact that we only want to perform this initialization once and first policy load becomes a per-state/namespace operation.

I got confused by SLAB_PANIC markings.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#4 lore raw

PATCH 1/1 security: mark kmem caches as __ro_after_init

In reply to Alexey Dobriyan
Quoted 147 lines · Alexey Dobriyan

Kmem caches are never reallocated once set up.

---

 security/integrity/iint.c     |    3 ++-
 security/selinux/avc.c        |    9 +++++----
 security/selinux/hooks.c      |    5 +++--
 security/selinux/ss/avtab.c   |    5 +++--
 security/selinux/ss/ebitmap.c |    3 ++-
 security/selinux/ss/hashtab.c |    3 ++-
 security/smack/smack_lsm.c    |    3 ++-
 7 files changed, 19 insertions(+), 12 deletions(-)
security/integrity/iint.c +2 -1
@@ -15,6 +15,7 @@
  *	- cache integrity information associated with an inode
  *	  using a rbtree tree.
  */
+#include <linux/cache.h>
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/spinlock.h>
@@ -25,7 +26,7 @@
 
 static struct rb_root integrity_iint_tree = RB_ROOT;
 static DEFINE_RWLOCK(integrity_iint_lock);
-static struct kmem_cache *iint_cache __read_mostly;
+static struct kmem_cache *iint_cache __ro_after_init;
 
 /*
  * __integrity_iint_find - return the iint associated with an inode
security/selinux/avc.c +5 -4
@@ -13,6 +13,7 @@
  *	it under the terms of the GNU General Public License version 2,
  *	as published by the Free Software Foundation.
  */
+#include <linux/cache.h>
 #include <linux/types.h>
 #include <linux/stddef.h>
 #include <linux/kernel.h>
@@ -91,10 +92,10 @@ DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
 
 static struct avc_cache avc_cache;
 static struct avc_callback_node *avc_callbacks;
-static struct kmem_cache *avc_node_cachep;
-static struct kmem_cache *avc_xperms_data_cachep;
-static struct kmem_cache *avc_xperms_decision_cachep;
-static struct kmem_cache *avc_xperms_cachep;
+static struct kmem_cache *avc_node_cachep __ro_after_init;
+static struct kmem_cache *avc_xperms_data_cachep __ro_after_init;
+static struct kmem_cache *avc_xperms_decision_cachep __ro_after_init;
+static struct kmem_cache *avc_xperms_cachep __ro_after_init;
 
 static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
 {
security/selinux/hooks.c +3 -2
@@ -24,6 +24,7 @@
  *	as published by the Free Software Foundation.
  */
 
+#include <linux/cache.h>
 #include <linux/init.h>
 #include <linux/kd.h>
 #include <linux/kernel.h>
@@ -129,8 +130,8 @@ __setup("selinux=", selinux_enabled_setup);
 int selinux_enabled = 1;
 #endif
 
-static struct kmem_cache *sel_inode_cache;
-static struct kmem_cache *file_security_cache;
+static struct kmem_cache *sel_inode_cache __ro_after_init;
+static struct kmem_cache *file_security_cache __ro_after_init;
 
 /**
  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
security/selinux/ss/avtab.c +3 -2
@@ -17,14 +17,15 @@
  *	Tuned number of hash slots for avtab to reduce memory usage
  */
 
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include "avtab.h"
 #include "policydb.h"
 
-static struct kmem_cache *avtab_node_cachep;
-static struct kmem_cache *avtab_xperms_cachep;
+static struct kmem_cache *avtab_node_cachep __ro_after_init;
+static struct kmem_cache *avtab_xperms_cachep __ro_after_init;
 
 /* Based on MurmurHash3, written by Austin Appleby and placed in the
  * public domain.
security/selinux/ss/ebitmap.c +2 -1
@@ -16,6 +16,7 @@
  *      Applied standard bit operations to improve bitmap scanning.
  */
 
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
@@ -25,7 +26,7 @@
 
 #define BITS_PER_U64	(sizeof(u64) * 8)
 
-static struct kmem_cache *ebitmap_node_cachep;
+static struct kmem_cache *ebitmap_node_cachep __ro_after_init;
 
 int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
 {
security/selinux/ss/hashtab.c +2 -1
@@ -4,13 +4,14 @@
  *
  * Author : Stephen Smalley, <[email protected]>
  */
+#include <linux/cache.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include <linux/sched.h>
 #include "hashtab.h"
 
-static struct kmem_cache *hashtab_node_cachep;
+static struct kmem_cache *hashtab_node_cachep __ro_after_init;
 
 struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
 			       int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
security/smack/smack_lsm.c +1 -0
@@ -18,6 +18,7 @@
  *      as published by the Free Software Foundation.
  */
 
+#include <linux/cache.h>
 #include <linux/xattr.h>
 #include <linux/pagemap.h>
 #include <linux/mount.h>

security/smack/smack_lsm.c line 59

Quoted 5 lines
security/smack/smack_lsm.c +1 -1
@@ -55,7 +56,7 @@
 DEFINE_MUTEX(smack_ipv6_lock);
 static LIST_HEAD(smk_ipv6_port_list);
 #endif
-static struct kmem_cache *smack_inode_cache;
+static struct kmem_cache *smack_inode_cache __ro_after_init;

Have you tried this to see if it works?

security/smack/smack_lsm.c line 62

Quoted 4 lines
security/smack/smack_lsm.c +0 -0
 int smack_enabled;
 
 static const match_table_t smk_mount_tokens = {

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html