Skip to contentSkip to conversation

Use conditional

PATCH In review Kurt Garloff 4 participants 10 messages Jul 3, 2005 – Jul 4, 2005 Security (LSM)

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 Use conditional

Hi,

this optimizes the case where no LSM is loaded and the (new) default capablities is used. These are not called via indirect calls but called as hardcoded calls and might thus be inlined; the price for this is a conditional -- benchmarks done by hp showed this to be beneficial (on ia64).

Enjoy,

Signature · 1 line
Kurt Garloff, Director SUSE Labs, Novell Inc.

From: Kurt Garloff <[email protected]>
Subject: Replace indirect calls by a branch
References: SUSE40217, SUSE39439

In the LSM stub collection, rather do a branch than an indirect call. Many of the functions called do only return 0 or do nothing for the default (capability) case.
This is a fast-path optimization; a branch is faster than an indirect call, even more so if correctly predicted.
This shows a >3% perf. increase in netperf -t TCP_RR benchmark on IA64. (More exactly: The benchmark was taken with the next two patches
applied as well, but I attribute the main effect to this patch.)

This is patch 3/5 of the LSM overhaul.

 include/linux/security.h |    6 +++++-
 security/security.c      |    2 --
 2 files changed, 5 insertions(+), 3 deletions(-)
include/linux/security.h +5 -1
Index: linux-2.6.12/include/linux/security.h
===================================================================
@@ -1246,17 +1246,21 @@ struct security_operations {
 };
 
 /* global variables */
 extern struct security_operations *security_ops;
+/* default security ops */
+extern struct security_operations capability_security_ops;
 
 /* prototypes */
 extern int security_init	(void);
 extern int register_security	(struct security_operations *ops);
 extern int unregister_security	(struct security_operations *ops);
 extern int mod_reg_security	(const char *name, struct security_operations *ops);
 extern int mod_unreg_security	(const char *name, struct security_operations *ops);
 
-#  define COND_SECURITY(seop, def) security_ops->seop
+/* Condition for invocation of non-default security_op */
+#  define COND_SECURITY(seop, def) 	\
+	(security_ops == &capability_security_ops)? def: security_ops->seop
 
 # else /* CONFIG_SECURITY */
 static inline int security_init(void)
 {
security/security.c +0 -2
Index: linux-2.6.12/security/security.c
===================================================================
@@ -21,10 +21,8 @@
 #define SECURITY_FRAMEWORK_VERSION	"1.0.0"
 
 /* things that live in dummy.c */
 extern void security_fixup_ops (struct security_operations *ops);
-/* default security ops */
-extern struct security_operations capability_security_ops;
 
 struct security_operations *security_ops;	/* Initialized to NULL */
 
 static inline int verify(struct security_operations *ops)
#1 lore raw
Quoted 4 lines · Kurt Garloff

capablities is used. These are not called via indirect calls but called as hardcoded calls and might thus be inlined; the price for this is a conditional -- benchmarks done by hp showed this to be beneficial (on ia64).

What about on i386, x86_64 or ppc64?

- James

Signature · 2 lines
James Morris
<[email protected]>
#2 lore raw

Hi James,

[added [email protected] to Cc]

Quoted 8 lines · James Morris
Quoted 4 lines · Kurt Garloff

capablities is used. These are not called via indirect calls but called as hardcoded calls and might thus be inlined; the price for this is a conditional -- benchmarks done by hp showed this to be beneficial (on ia64).

What about on i386, x86_64 or ppc64?

We tested on i386 as well at the time, and it looked like a tiny improvement. But doing the statistics, it was in the noise. I have no numbers for x86_64 or ppc64.

If you have reason to believe that there could be regressions, we should indeed do the benchmarks.

Sidenote: The patches 1 -- 2b alone still make sense, so I would vote not for delaying their inclusion until we can collect numbers for all arches we care about to take a decision on patch 3.

Best,

Signature · 1 line
Kurt Garloff, Director SUSE Labs, Novell Inc.
#3 lore raw
In reply to Kurt Garloff

On Sun, Jul 03, 2005 at 05:44:05PM +0200, Kurt Garloff wrote:

Agree with James, pls resend to [email protected].

The topic of replacing dummy (with capability) was discussed there last week, in the context of stacker, but a common solution for both cases would be needed.

Also, I was going to ask where 4/5 and 5/5 were :-)

If you are claiming a perf increase it would be nice to get an idea what these patches were even though you believe most of the gain was in patch #3.

Thanks

Quoted 26 lines

Hi,

this optimizes the case where no LSM is loaded and the (new) default capablities is used. These are not called via indirect calls but called as hardcoded calls and might thus be inlined; the price for this is a conditional -- benchmarks done by hp showed this to be beneficial (on ia64).

Enjoy,

Signature · 16 lines
Kurt Garloff, Director SUSE Labs, Novell Inc.

From: Kurt Garloff <[email protected]>
Subject: Replace indirect calls by a branch
References: SUSE40217, SUSE39439

In the LSM stub collection, rather do a branch than an indirect
call. Many of the functions called do only return 0 or do nothing
for the default (capability) case.
This is a fast-path optimization; a branch is faster than an
indirect call, even more so if correctly predicted.
This shows a >3% perf. increase in netperf -t TCP_RR benchmark on IA64.
(More exactly: The benchmark was taken with the next two patches
 applied as well, but I attribute the main effect to this patch.)

This is patch 3/5 of the LSM overhaul.
#4 lore raw

Hi Tony,

Quoted 1 line · Tony Jones

Agree with James, pls resend to [email protected].

Done.

Quoted 3 lines

The topic of replacing dummy (with capability) was discussed there last week, in the context of stacker, but a common solution for both cases would be needed.

Both cases?

Quoted 1 line

Also, I was going to ask where 4/5 and 5/5 were :-)

They were part of my previous submission attempts as indicated in the first message [Patch 0/3]. Last time I sent them people were discussing whether or not we should have this likely/unlikely, so I left this out this time, as I'd rather prefer discussing the other patches.

Still, the 3% perf. increase measurement that has been done was with patches 4 and 5, so claiming otherwise would not be right.

Quoted 3 lines

If you are claiming a perf increase it would be nice to get an idea what these patches were even though you believe most of the gain was in patch #3.

For completeness, find both attached.

Best,

Signature · 1 line
Kurt Garloff, Director SUSE Labs, Novell Inc.

From: Kurt Garloff <[email protected]>
Subject: Consider the capability case the likely one
References: SUSE40217, SUSE39439

The case that security_ops points to the default capability_ security_ops is the fast path and arguably the more likely one on most systems. So mark it likely to tell the compiler to optimize accordingly and increase the chances of having this predicted correctly by the CPU.

This is patch 4/5 of the LSM overhaul.

 security.h |    2 +-
 1 files changed, 1 insertion(+), 1 deletion(-)
include/linux/security.h +1 -1
Index: linux-2.6.12/include/linux/security.h
===================================================================
@@ -1258,9 +1258,9 @@ extern int mod_reg_security	(const char 
 extern int mod_unreg_security	(const char *name, struct security_operations *ops);
 
 /* Condition for invocation of non-default security_op */
 #  define COND_SECURITY(seop, def) 	\
-	(security_ops == &capability_security_ops)? def: security_ops->seop
+	(likely(security_ops == &capability_security_ops))? def: security_ops->seop
 
 # else /* CONFIG_SECURITY */
 static inline int security_init(void)
 {

From: Kurt Garloff <[email protected]>
Subject: Test security_enabled var rather than security_ops pointer
References: SUSE40217, SUSE39439

Rather than doing a pointer comparison, test an integer var for being null. Should be slightly faster.
I consider this patch as optional.

Note that it does not introduce a (new) race, as we still set the security_ops pointer to the capability_security_ops. So no wmb() is needed for the module unload case.

Sidenote: A wmb() in the module load and unload cases might actually be useful to ensure that the other CPUs start using the new LSM pointer ASAP. It's still racy, but that's by design of LSM. Introducing locks would hurt performance tremendously. One could do RCU ... but that's not for this patchset.

This is patch 5/5 of the LSM overhaul.

 include/linux/security.h |    7 ++++---
 security/security.c      |    7 +++++++
 2 files changed, 11 insertions(+), 3 deletions(-)
include/linux/security.h +4 -3
Index: linux-2.6.12/include/linux/security.h
===================================================================
@@ -1246,10 +1246,10 @@ struct security_operations {
 };
 
 /* global variables */
 extern struct security_operations *security_ops;
-/* default security ops */
-extern struct security_operations capability_security_ops;
+/* Security enabled? */
+extern int security_enabled;
 
 /* prototypes */
 extern int security_init	(void);
 extern int register_security	(struct security_operations *ops);
@@ -1258,16 +1258,17 @@ extern int mod_reg_security	(const char 
 extern int mod_unreg_security	(const char *name, struct security_operations *ops);
 
 /* Condition for invocation of non-default security_op */
 #  define COND_SECURITY(seop, def) 	\
-	(likely(security_ops == &capability_security_ops))? def: security_ops->seop
+	(unlikely(security_enabled))? security_ops->seop: def
 
 # else /* CONFIG_SECURITY */
 static inline int security_init(void)
 {
 	return 0;
 }
 
+#  define security_enabled 0
 #  define COND_SECURITY(seop, def) def
 # endif
 
 # ifdef CONFIG_SECURITY_NETWORK
security/security.c +7 -0
Index: linux-2.6.12/security/security.c
===================================================================
@@ -21,10 +21,14 @@
 #define SECURITY_FRAMEWORK_VERSION	"1.0.0"
 
 /* things that live in dummy.c */
 extern void security_fixup_ops (struct security_operations *ops);
+/* default security ops */
+extern struct security_operations capability_security_ops;
 
 struct security_operations *security_ops;	/* Initialized to NULL */
+int security_enabled;				/* ditto */
+EXPORT_SYMBOL(security_enabled);
 
 static inline int verify(struct security_operations *ops)
 {
 	/* verify the security_operations structure exists */
@@ -59,8 +63,9 @@ int __init security_init(void)
 		       "capability_security_ops structure.\n", __FUNCTION__);
 		return -EIO;
 	}
 
+	security_enabled = 0;
 	security_ops = &capability_security_ops;
 
 	/* Initialize compiled-in security modules */
 	do_security_initcalls();
@@ -91,8 +96,9 @@ int register_security(struct security_op
 	if (security_ops != &capability_security_ops)
 		return -EAGAIN;
 
 	security_ops = ops;
+	security_enabled = 1;
 
 	return 0;
 }
 
@@ -115,8 +121,9 @@ int unregister_security(struct security_
 		       "registered, failing.\n", __FUNCTION__);
 		return -EINVAL;
 	}
 
+	security_enabled = 0;
 	security_ops = &capability_security_ops;
 
 	return 0;
 }
#5 lore raw
Quoted 5 lines · Kurt Garloff
Quoted 3 lines

The topic of replacing dummy (with capability) was discussed there last week, in the context of stacker, but a common solution for both cases would be needed.

Both cases?

CONFIG_SECURITY_STACKER and !CONFIG_SECURITY_STACKER ;-)

http://mail.wirex.com/pipermail/linux-security-module/2005-June/6200.html

I was assuming (bad of me I know) that Serge's patch would nail both cases with one stone.

Tony

#6 lore raw
Quoted 6 lines · Tony Jones
Quoted 4 lines · Kurt Garloff
Quoted 3 lines

The topic of replacing dummy (with capability) was discussed there last week, in the context of stacker, but a common solution for both cases would be needed.

Quoted 8 lines
Quoted 1 line

Both cases?

CONFIG_SECURITY_STACKER and !CONFIG_SECURITY_STACKER ;-)

http://mail.wirex.com/pipermail/linux-security-module/2005-June/6200.html

I was assuming (bad of me I know) that Serge's patch would nail both cases with one stone.

Yes, sorry, I never got around to the replace-dummy-with-capability patch. There wasn't a single cry when Chris asked for anyone who'd care about dummy being removed, so I do plan on switching that.

thanks,
-serge

#7 lore raw

Hi Serge,

Quoted 11 lines · On Mon, Jul 04, 2005 at 07:01:05AM -0500, [email protected] wrote:
Quoted 10 lines · Tony Jones
Quoted 5 lines · Kurt Garloff
Quoted 3 lines

The topic of replacing dummy (with capability) was discussed there last week, in the context of stacker, but a common solution for both cases would be needed.

Both cases?

CONFIG_SECURITY_STACKER and !CONFIG_SECURITY_STACKER ;-)

Quoted 8 lines
Quoted 4 lines

http://mail.wirex.com/pipermail/linux-security-module/2005-June/6200.html

I was assuming (bad of me I know) that Serge's patch would nail both cases with one stone.

Yes, sorry, I never got around to the replace-dummy-with-capability patch. There wasn't a single cry when Chris asked for anyone who'd care about dummy being removed, so I do plan on switching that.

I was a bit careful: My patch did switch the default, but LSMs that don't fill in all security_ops would still fall back to dummy, just to make sure there is no possibly unintended change in behaviour.

Getting rid of dummy entirely would be better, I agree, but someone needs to review that this won't break anything.

So how should we proceed?
You want to do the dummy removal first, then have stacker merged and then what remains of my patches? Or should I start ... ?

Regards,

Signature · 1 line
Kurt Garloff, Director SUSE Labs, Novell Inc.
#8 lore raw

Hey,

Quoted 2 lines · Kurt Garloff

Getting rid of dummy entirely would be better, I agree, but someone needs to review that this won't break anything.

Unfortunately I think it's way too soon for that. Even if stacker is accepted, it is still a module (for now at least) which can be compiled out. So we'll need dummy hooks for modules (like seclvl) to use. I just don't think it's possible to get rid of that yet.

Quoted 3 lines

So how should we proceed?
You want to do the dummy removal first, then have stacker merged and then what remains of my patches? Or should I start ... ?

I think your patches to make capability the default are the best place to start. Doing the same under stacker will be trivial, and I'll do that in the next set I send out.

thanks,
-serge

#9 lore raw

Hi Serge,

Quoted 8 lines · On Mon, Jul 04, 2005 at 07:37:21AM -0500, [email protected] wrote:
Quoted 2 lines · Kurt Garloff

Getting rid of dummy entirely would be better, I agree, but someone needs to review that this won't break anything.

Unfortunately I think it's way too soon for that. Even if stacker is accepted, it is still a module (for now at least) which can be compiled out. So we'll need dummy hooks for modules (like seclvl) to use. I just don't think it's possible to get rid of that yet.

Hmmmm, getting rid of dummy would mean replacing it with capability.
- The differences between cap and dummy affect a relatively small subset of hooks
- If all of these hooks are implemented by all LSMs, we're done and can just remove dummy and replace it by capability.
- If not, we'd need to review for all of these LSMs, whether defaulting to capability rather than dummy could create a problem and whether that can be addressed easily.

seclvl would probably need some changes, indeed.

root_plug could become shorter :-)

Quoted 7 lines
Quoted 3 lines

So how should we proceed?
You want to do the dummy removal first, then have stacker merged and then what remains of my patches? Or should I start ... ?

I think your patches to make capability the default are the best place to start. Doing the same under stacker will be trivial, and I'll do that in the next set I send out.

Sounds good!

Signature · 1 line
Kurt Garloff, Director SUSE Labs, Novell Inc.