[PATCH v2] Ucount: Optimize the ucount code

Xavier posted 1 patch 1 year, 5 months ago
kernel/ucount.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
[PATCH v2] Ucount: Optimize the ucount code
Posted by Xavier 1 year, 5 months ago
Optimize the memory release operation by placing it outside of the
spin lock in alloc_ucounts.

Signed-off-by: Xavier <xavier_qy@163.com>
---
 kernel/ucount.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/kernel/ucount.c b/kernel/ucount.c
index 8c07714ff27d..87a773e6ff15 100644
--- a/kernel/ucount.c
+++ b/kernel/ucount.c
@@ -164,7 +164,7 @@ struct ucounts *get_ucounts(struct ucounts *ucounts)
 struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
 {
 	struct hlist_head *hashent = ucounts_hashentry(ns, uid);
-	struct ucounts *ucounts, *new;
+	struct ucounts *ucounts, *new = NULL;
 	bool wrapped;
 
 	spin_lock_irq(&ucounts_lock);
@@ -182,9 +182,7 @@ struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
 
 		spin_lock_irq(&ucounts_lock);
 		ucounts = find_ucounts(ns, uid, hashent);
-		if (ucounts) {
-			kfree(new);
-		} else {
+		if (!ucounts) {
 			hlist_add_head(&new->node, hashent);
 			get_user_ns(new->ns);
 			spin_unlock_irq(&ucounts_lock);
@@ -193,6 +191,9 @@ struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
 	}
 	wrapped = !get_ucounts_or_wrap(ucounts);
 	spin_unlock_irq(&ucounts_lock);
+	if (new)
+		kfree(new);
+
 	if (wrapped) {
 		put_ucounts(ucounts);
 		return NULL;
-- 
2.45.2
Re:[PATCH v2] Ucount: Optimize the ucount code
Posted by Xavier 1 year, 5 months ago
Hi,

Just a reminder, does anyone have any comments or feedback on the patch?

--
Thanks,
Xavier




At 2024-08-12 16:48:23, "Xavier" <xavier_qy@163.com> wrote:
>Optimize the memory release operation by placing it outside of the
>spin lock in alloc_ucounts.
>
>Signed-off-by: Xavier <xavier_qy@163.com>
>---
> kernel/ucount.c | 9 +++++----
> 1 file changed, 5 insertions(+), 4 deletions(-)
>
>diff --git a/kernel/ucount.c b/kernel/ucount.c
>index 8c07714ff27d..87a773e6ff15 100644
>--- a/kernel/ucount.c
>+++ b/kernel/ucount.c
>@@ -164,7 +164,7 @@ struct ucounts *get_ucounts(struct ucounts *ucounts)
> struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
> {
> 	struct hlist_head *hashent = ucounts_hashentry(ns, uid);
>-	struct ucounts *ucounts, *new;
>+	struct ucounts *ucounts, *new = NULL;
> 	bool wrapped;
> 
> 	spin_lock_irq(&ucounts_lock);
>@@ -182,9 +182,7 @@ struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
> 
> 		spin_lock_irq(&ucounts_lock);
> 		ucounts = find_ucounts(ns, uid, hashent);
>-		if (ucounts) {
>-			kfree(new);
>-		} else {
>+		if (!ucounts) {
> 			hlist_add_head(&new->node, hashent);
> 			get_user_ns(new->ns);
> 			spin_unlock_irq(&ucounts_lock);
>@@ -193,6 +191,9 @@ struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
> 	}
> 	wrapped = !get_ucounts_or_wrap(ucounts);
> 	spin_unlock_irq(&ucounts_lock);
>+	if (new)
>+		kfree(new);
>+
> 	if (wrapped) {
> 		put_ucounts(ucounts);
> 		return NULL;
>-- 
>2.45.2