From: John Johansen <john.johansen@canonical.com>
To: Pekka Enberg <penberg@cs.helsinki.fi>
Cc: linux-kernel@vger.kernel.org,
linux-security-module@vger.kernel.org,
Nick Piggin <npiggin@suse.de>,
Peter Zijlstra <a.p.zijlstra@chello.nl>,
Andrew Morton <akpm@linux-foundation.org>,
xiaosuo@gmail.com, laijs@cn.fujitsu.com
Subject: Re: [PATCH 01/13] AppArmor: misc. base functions and defines
Date: Fri, 30 Jul 2010 03:01:43 -0700 [thread overview]
Message-ID: <4C52A307.7080002@canonical.com> (raw)
In-Reply-To: <AANLkTikk3tNM-fsXuS_SeAWgaYYCFrFAToRXOBkPxwy9@mail.gmail.com>
On 07/30/2010 02:20 AM, Pekka Enberg wrote:
> On Fri, Jul 30, 2010 at 12:47 AM, John Johansen
> <john.johansen@canonical.com> wrote:
>> +/**
>> + * kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
>> + * @size: size of allocation
>> + *
>> + * Return: allocated buffer or NULL if failed
>> + *
>> + * It is possible that policy being loaded from the user is larger than
>> + * what can be allocated by kmalloc, in those cases fall back to vmalloc.
>> + */
>> +void *kvmalloc(size_t size)
>> +{
>> + void *buffer = NULL;
>> +
>> + if (size == 0)
>> + return NULL;
>> +
>> + /* do not attempt kmalloc if we need more than 16 pages at once */
>> + if (size <= (16*PAGE_SIZE))
>> + buffer = kmalloc(size, GFP_NOIO | __GFP_NOWARN);
>
> 16 pages is a lot of memory for 64 K pages. What's the purpose of
yes it is, and I don't expect it will every allocate that much, though it
will occassionally with large policies do allocations larger than 16*4K.
The figure here is some what arbitrary, and I would certainly be willing
to shrink it. Basically it is there to put a clamp on allocating precious
physically contiguous memory.
> GFP_NOIO here? vmalloc() will do GFP_KERNEL allocations anyway.
>
yep, and it used to be GFP_KERNEL too, looking back GFP_NOIO happend when
poking at a bug where apparmor was trigger a IO when it was allocating its
memory. Turned out the bug wasn't apparmor related just being triggered
while apparmor was loading policy, but the GFP_NOIO flag stuck here.
I am more than willing to flip it back.
>> + if (!buffer) {
>> + /* see kvfree for why size must be at least work_struct size
>> + * when allocated via vmalloc
>> + */
>> + if (size < sizeof(struct work_struct))
>> + size = sizeof(struct work_struct);
>> + buffer = vmalloc(size);
>> + }
>> + return buffer;
>> +}
>
> Please don't hide this into apparmor internals. People have invented
> this function in the past so maybe it's time to put it in mm/util.c?
>
sure, I would be more than willing to replace this with a generic
system fn. The last attempt I saw at adding generic routines of this
nature was here
http://www.spinics.net/lists/linux-fsdevel/msg31407.html
>> +
>> +/**
>> + * do_vfree - workqueue routine for freeing vmalloced memory
>> + * @work: data to be freed
>> + *
>> + * The work_struct is overlaid to the data being freed, as at the point
>> + * the work is scheduled the data is no longer valid, be its freeing
>> + * needs to be delayed until safe.
>> + */
>> +static void do_vfree(struct work_struct *work)
>> +{
>> + vfree(work);
>> +}
>> +
>> +/**
>> + * kvfree - free an allocation do by kvmalloc
>> + * @buffer: buffer to free (MAYBE_NULL)
>> + *
>> + * Free a buffer allocated by kvmalloc
>> + */
>> +void kvfree(void *buffer)
>> +{
>> + if (is_vmalloc_addr(buffer)) {
>> + /* Data is no longer valid so just use the allocated space
>> + * as the work_struct
>> + */
>> + struct work_struct *work = (struct work_struct *) buffer;
>> + INIT_WORK(work, do_vfree);
>> + schedule_work(work);
>
> I don't understand this part here. Is it needed for interrupt contexts
> or does vfree() sleep somewhere? If it's for the former, I think we
> can just add a comment saying that kvmalloc/kvfree is not safe from
> interrupt context and remove the schedule_work() parts here.
>
vfree can sleep, and skipping the schedule_work parts won't work for
apparmor as many of these allocations are being freed via rcu callbacks
as most of our object life cycles are dependent on cred refcounting.
next prev parent reply other threads:[~2010-07-30 10:01 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-29 21:47 [AppArmor #7 0/13] AppArmor security module John Johansen
2010-07-29 21:47 ` [PATCH 01/13] AppArmor: misc. base functions and defines John Johansen
2010-07-30 9:20 ` Pekka Enberg
2010-07-30 10:01 ` John Johansen [this message]
2010-07-30 10:53 ` Pekka Enberg
2010-07-30 14:24 ` Changli Gao
2010-07-30 15:01 ` Pekka Enberg
2010-07-29 21:47 ` [PATCH 02/13] AppArmor: basic auditing infrastructure John Johansen
2010-07-29 21:47 ` [PATCH 03/13] AppArmor: contexts used in attaching policy to system objects John Johansen
2010-07-29 21:48 ` [PATCH 04/13] AppArmor: core policy routines John Johansen
2010-07-29 21:48 ` [PATCH 05/13] AppArmor: dfa match engine John Johansen
2010-07-29 21:48 ` [PATCH 06/13] AppArmor: policy routines for loading and unpacking policy John Johansen
2010-07-29 21:48 ` [PATCH 07/13] AppArmor: userspace interfaces John Johansen
2010-07-29 21:48 ` [PATCH 08/13] AppArmor: file enforcement routines John Johansen
2010-07-29 21:48 ` [PATCH 09/13] AppArmor: mediation of non file objects John Johansen
2010-07-29 21:48 ` [PATCH 10/13] AppArmor: functions for domain transitions John Johansen
2010-07-29 21:48 ` [PATCH 11/13] AppArmor: LSM interface, and security module initialization John Johansen
2010-07-29 21:48 ` [PATCH 12/13] AppArmor: Enable configuring and building of the AppArmor security module John Johansen
2010-07-29 21:48 ` [PATCH 13/13] AppArmor: update Maintainer and Documentation John Johansen
2010-07-29 23:05 ` [AppArmor #7 0/13] AppArmor security module James Morris
2010-07-30 1:45 ` Tetsuo Handa
2010-07-30 2:04 ` John Johansen
2010-07-30 2:26 ` Tetsuo Handa
2010-07-30 3:50 ` James Morris
2010-07-30 5:39 ` Tetsuo Handa
2010-07-30 4:48 ` Casey Schaufler
2010-08-05 6:24 ` Pavel Machek
2010-08-05 9:58 ` Jan III Sobieski
2010-08-05 10:27 ` James Morris
2010-08-26 7:01 ` Pavel Machek
-- strict thread matches above, loose matches on Subject: below --
2010-07-27 2:57 [AppArmor #6 " John Johansen
2010-07-27 2:57 ` [PATCH 01/13] AppArmor: misc. base functions and defines John Johansen
2010-07-15 0:43 [AppArmor #5 0/13] AppArmor security module John Johansen
2010-07-15 0:43 ` [PATCH 01/13] AppArmor: misc. base functions and defines 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=4C52A307.7080002@canonical.com \
--to=john.johansen@canonical.com \
--cc=a.p.zijlstra@chello.nl \
--cc=akpm@linux-foundation.org \
--cc=laijs@cn.fujitsu.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=npiggin@suse.de \
--cc=penberg@cs.helsinki.fi \
--cc=xiaosuo@gmail.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