From: "Serge E. Hallyn" <serge@hallyn.com>
To: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Cc: linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/5] Move sleeping operations to outside the semaphore.
Date: Mon, 1 Jun 2009 21:40:31 -0500 [thread overview]
Message-ID: <20090602024031.GA21564@hallyn.com> (raw)
In-Reply-To: <200906020141.n521faqb003256@www262.sakura.ne.jp>
Quoting Tetsuo Handa (penguin-kernel@i-love.sakura.ne.jp):
> TOMOYO is using rw_semaphore for protecting list elements.
> But TOMOYO is doing operations which might sleep inside down_write().
> This patch makes TOMOYO's sleeping operations go outside down_write().
>
> Signed-off-by: Kentaro Takeda <takedakn@nttdata.co.jp>
> Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
> Signed-off-by: Toshiharu Harada <haradats@nttdata.co.jp>
> ---
> security/tomoyo/common.c | 80 ++++++---------------
> security/tomoyo/common.h | 2
> security/tomoyo/domain.c | 107 ++++++++++++++--------------
> security/tomoyo/file.c | 133 ++++++++++++++++++-----------------
> security/tomoyo/realpath.c | 169 ++++++++++++++-------------------------------
> security/tomoyo/realpath.h | 7 -
> 6 files changed, 205 insertions(+), 293 deletions(-)
>
> --- security-testing-2.6.git.orig/security/tomoyo/common.c
> +++ security-testing-2.6.git/security/tomoyo/common.c
> @@ -861,26 +861,27 @@ static struct tomoyo_profile *tomoyo_fin
> int profile)
> {
> static DEFINE_MUTEX(lock);
> - struct tomoyo_profile *ptr = NULL;
> - int i;
> + struct tomoyo_profile *new_ptr = NULL;
> + struct tomoyo_profile *ptr;
>
> if (profile >= TOMOYO_MAX_PROFILES)
> return NULL;
> + new_ptr = kmalloc(sizeof(*new_ptr), GFP_KERNEL);
> /***** EXCLUSIVE SECTION START *****/
> mutex_lock(&lock);
> ptr = tomoyo_profile_ptr[profile];
> - if (ptr)
> - goto ok;
> - ptr = tomoyo_alloc_element(sizeof(*ptr));
> - if (!ptr)
> - goto ok;
> - for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++)
> - ptr->value[i] = tomoyo_control_array[i].current_value;
> - mb(); /* Avoid out-of-order execution. */
> - tomoyo_profile_ptr[profile] = ptr;
> - ok:
> + if (!ptr && tomoyo_memory_ok(new_ptr)) {
> + int i;
> + ptr = new_ptr;
> + new_ptr = NULL;
> + for (i = 0; i < TOMOYO_MAX_CONTROL_INDEX; i++)
> + ptr->value[i] = tomoyo_control_array[i].current_value;
> + mb(); /* Avoid out-of-order execution. */
> + tomoyo_profile_ptr[profile] = ptr;
> + }
> mutex_unlock(&lock);
> /***** EXCLUSIVE SECTION END *****/
> + kfree(new_ptr);
> return ptr;
> }
Again I feel (no offense) like I'm reading Ada code here...
Focusing just on this one function,
1. the mutex lock belonging to this function really is just protecting
writes to elements of tomoyo_profile_ptr. It should be defined,
with a descriptive name and comment, next to tomoyo_profile_ptr
at common.c:46.
2. I see no reason for this not to be a fast spinlock at this
point.
3. Once it's a fast checkpoint, you can change the flow a bit
(unless there is good reason not to) to do:
spin_lock(&profile_lock);
ptr = tomoyo_profile_ptr[profile];
spin_unlock(&profile_lock);
if (ptr)
return ptr;
new_ptr = kmalloc(sizeof(*new_ptr), GFP_KERNEL);
if (!new_ptr)
return ERR_PTR(-ENOMEM);
spin_lock(&profile_lock);
if (!tomoyo_profile_ptr[profile]) {
/* do your initialization as above */
ptr = tomoyo_profile_ptr[profile] = new_ptr;
new_ptr = NULL;
}
spin_unlock(&profile_lock);
kfree(newptr);
return ptr;
which avoids the kmalloc in the common case.
-serge
next prev parent reply other threads:[~2009-06-02 2:34 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-06-02 1:41 [PATCH 1/5] Move sleeping operations to outside the semaphore Tetsuo Handa
2009-06-02 2:40 ` Serge E. Hallyn [this message]
2009-06-02 4:05 ` Serge E. Hallyn
2009-06-02 4:36 ` Tetsuo Handa
2009-06-02 13:59 ` Serge E. Hallyn
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=20090602024031.GA21564@hallyn.com \
--to=serge@hallyn.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=penguin-kernel@i-love.sakura.ne.jp \
/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