mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
To: <john.johansen@canonical.com>, <paul@paul-moore.com>,
	<jmorris@namei.org>, <serge@hallyn.com>
Cc: <linux-security-module@vger.kernel.org>,
	<apparmor@lists.ubuntu.com>, <linux-kernel@vger.kernel.org>,
	<gautham.shenoy@amd.com>, <Santosh.Shukla@amd.com>,
	<Ananth.Narayan@amd.com>, <raghavendra.kodsarathimmappa@amd.com>,
	<paulmck@kernel.org>, <boqun.feng@gmail.com>,
	<vinicius.gomes@intel.com>, <mjguzik@gmail.com>,
	Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
Subject: [RFC 3/9] apparmor: Switch unconfined namespaces refcount to percpu mode
Date: Wed, 10 Jan 2024 16:48:50 +0530	[thread overview]
Message-ID: <20240110111856.87370-3-Neeraj.Upadhyay@amd.com> (raw)
In-Reply-To: <f184a2d6-7892-4e43-a0cd-cab638c3d5c2@amd.com>

Switch unconfined labels to percpu mode, to avoid high
memory contention on refcount get and put operations,
when multiple cpus try to perform these operations
at the same time. Unconfined label for root and sub
namespaces are killed at the point of aa_free_root_ns().
Though labels/profiles in various namespaces could
potentially still be active after this point, aa_free_root_ns()
is not typically called when apparmor enforcement is enabled.

Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
---
 security/apparmor/include/policy.h    | 24 ++++++++++++++++++++++++
 security/apparmor/include/policy_ns.h | 24 ++++++++++++++++++++++++
 security/apparmor/policy_ns.c         |  6 ++++--
 3 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/security/apparmor/include/policy.h b/security/apparmor/include/policy.h
index 5849b6b94cea..1e3b29ba6c03 100644
--- a/security/apparmor/include/policy.h
+++ b/security/apparmor/include/policy.h
@@ -379,6 +379,30 @@ static inline void aa_put_profile(struct aa_profile *p)
 		percpu_ref_put(&p->label.count);
 }
 
+/**
+ * aa_switch_ref_profile - switch percpu-ref mode for profile @p
+ * @p: profile  (MAYBE NULL)
+ */
+static inline void aa_switch_ref_profile(struct aa_profile *p, bool percpu)
+{
+	if (p) {
+		if (percpu)
+			percpu_ref_switch_to_percpu(&p->label.count);
+		else
+			percpu_ref_switch_to_atomic_sync(&p->label.count);
+	}
+}
+
+/**
+ * aa_kill_ref_profile - percpu-ref kill for profile @p
+ * @p: profile  (MAYBE NULL)
+ */
+static inline void aa_kill_ref_profile(struct aa_profile *p)
+{
+	if (p)
+		percpu_ref_kill(&p->label.count);
+}
+
 static inline int AUDIT_MODE(struct aa_profile *profile)
 {
 	if (aa_g_audit != AUDIT_NORMAL)
diff --git a/security/apparmor/include/policy_ns.h b/security/apparmor/include/policy_ns.h
index d646070fd966..f3db01c5e193 100644
--- a/security/apparmor/include/policy_ns.h
+++ b/security/apparmor/include/policy_ns.h
@@ -127,6 +127,30 @@ static inline void aa_put_ns(struct aa_ns *ns)
 		aa_put_profile(ns->unconfined);
 }
 
+/**
+ * aa_switch_ref_ns - switch percpu-ref mode for @ns
+ * @ns: namespace to switch percpu-ref mode of
+ *
+ * Switch percpu-ref mode of @ns between percpu and atomic
+ */
+static inline void aa_switch_ref_ns(struct aa_ns *ns, bool percpu)
+{
+	if (ns)
+		aa_switch_ref_profile(ns->unconfined, percpu);
+}
+
+/**
+ * aa_kill_ref_ns - do percpu-ref kill for @ns
+ * @ns: namespace to perform percpu-ref kill for
+ *
+ * Do percpu-ref kill of @ns refcount
+ */
+static inline void aa_kill_ref_ns(struct aa_ns *ns)
+{
+	if (ns)
+		aa_kill_ref_profile(ns->unconfined);
+}
+
 /**
  * __aa_findn_ns - find a namespace on a list by @name
  * @head: list to search for namespace on  (NOT NULL)
diff --git a/security/apparmor/policy_ns.c b/security/apparmor/policy_ns.c
index 1f02cfe1d974..ca633cfbd936 100644
--- a/security/apparmor/policy_ns.c
+++ b/security/apparmor/policy_ns.c
@@ -124,6 +124,7 @@ static struct aa_ns *alloc_ns(const char *prefix, const char *name)
 		goto fail_unconfined;
 	/* ns and ns->unconfined share ns->unconfined refcount */
 	ns->unconfined->ns = ns;
+	aa_switch_ref_ns(ns, true);
 
 	atomic_set(&ns->uniq_null, 0);
 
@@ -336,7 +337,7 @@ void __aa_remove_ns(struct aa_ns *ns)
 	/* remove ns from namespace list */
 	list_del_rcu(&ns->base.list);
 	destroy_ns(ns);
-	aa_put_ns(ns);
+	aa_kill_ref_ns(ns);
 }
 
 /**
@@ -377,6 +378,7 @@ int __init aa_alloc_root_ns(void)
 	}
 	kernel_t = &kernel_p->label;
 	root_ns->unconfined->ns = aa_get_ns(root_ns);
+	aa_switch_ref_ns(root_ns, true);
 
 	return 0;
 }
@@ -392,5 +394,5 @@ void __init aa_free_root_ns(void)
 
 	 aa_label_free(kernel_t);
 	 destroy_ns(ns);
-	 aa_put_ns(ns);
+	 aa_kill_ref_ns(ns);
 }
-- 
2.34.1


  parent reply	other threads:[~2024-01-10 11:20 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-10 11:11 [RFC 0/9] Nginx refcount scalability issue with Apparmor enabled and potential solutions Neeraj Upadhyay
2024-01-10 11:18 ` [RFC 1/9] doc: Add document for apparmor refcount management Neeraj Upadhyay
2024-01-10 11:18 ` [RFC 2/9] apparmor: Switch labels to percpu refcount in atomic mode Neeraj Upadhyay
2024-01-10 11:18 ` Neeraj Upadhyay [this message]
2024-01-10 11:18 ` [RFC 4/9] apparmor: Add infrastructure to reclaim percpu labels Neeraj Upadhyay
2024-01-10 11:18 ` [RFC 5/9] apparmor: Switch intree labels to percpu mode Neeraj Upadhyay
2024-01-10 11:18 ` [RFC 6/9] apparmor: Initial prototype for optimizing ref switch Neeraj Upadhyay
2024-01-10 11:18 ` [RFC 7/9] percpu-rcuref: Add basic infrastructure Neeraj Upadhyay
2024-01-10 11:18 ` [RFC 8/9] apparmor: Switch labels to percpu rcurefcount in unmanaged mode Neeraj Upadhyay
2024-01-10 11:18 ` [RFC 9/9] apparmor: Switch unconfined and in tree labels to managed ref mode Neeraj Upadhyay
2024-02-07  4:40 ` [RFC 0/9] Nginx refcount scalability issue with Apparmor enabled and potential solutions Neeraj Upadhyay
2024-02-09 17:33   ` John Johansen
2024-03-02 10:23     ` Mateusz Guzik
2024-03-08 20:09       ` John Johansen
2024-05-24 21:10         ` Mateusz Guzik
2024-05-24 21:51           ` John Johansen
2024-05-28 13:29             ` Mateusz Guzik
2024-05-29  0:37               ` John Johansen
2024-05-29  9:38                 ` Mateusz Guzik
2024-05-30  4:19                 ` Neeraj Upadhyay
2024-05-30  5:59                   ` John Johansen
2024-05-30  9:47                     ` Mateusz Guzik
2024-05-31  0:17                       ` John Johansen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240110111856.87370-3-Neeraj.Upadhyay@amd.com \
    --to=neeraj.upadhyay@amd.com \
    --cc=Ananth.Narayan@amd.com \
    --cc=Santosh.Shukla@amd.com \
    --cc=apparmor@lists.ubuntu.com \
    --cc=boqun.feng@gmail.com \
    --cc=gautham.shenoy@amd.com \
    --cc=jmorris@namei.org \
    --cc=john.johansen@canonical.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mjguzik@gmail.com \
    --cc=paul@paul-moore.com \
    --cc=paulmck@kernel.org \
    --cc=raghavendra.kodsarathimmappa@amd.com \
    --cc=serge@hallyn.com \
    --cc=vinicius.gomes@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®