Re: [PATCH 2/3][mmotm] showing size of kcore v2

From: AmÃrico Wang
Date: Thu Sep 17 2009 - 22:20:45 EST


On Thu, Sep 17, 2009 at 2:10 PM, KAMEZAWA Hiroyuki
<kamezawa.hiroyu@xxxxxxxxxxxxxx> wrote:
> On Thu, 17 Sep 2009 14:02:39 +0800
> AmÃrico Wang <xiyou.wangcong@xxxxxxxxx> wrote:
>> > @@ -124,6 +126,7 @@ static void __kcore_update_ram(struct li
>> > Â Â Â Âwrite_unlock(&kclist_lock);
>> >
>> > Â Â Â Âfree_kclist_ents(&garbage);
>> > + Â Â Â proc_root_kcore->size = get_kcore_size(&nphdr, &size);
>>
>>
>> This makes me to think if we will have some race condition here?
>> Two processes can open kcore at the same time...
>>
> Finally,
> ==
> static void __kcore_update_ram(struct list_head *list)
> {
> Âwrite_lock(&kclist_lock);
> Â Â Â Âif (kcore_need_update) {
> Â Â Â Â Â Â Â Âlist_for_each_entry_safe(pos, tmp, &kclist_head, list) {
> Â Â Â Â Â Â Â Â Â Â Â Âif (pos->type == KCORE_RAM
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â|| pos->type == KCORE_VMEMMAP)
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âlist_move(&pos->list, &garbage);
> Â Â Â Â Â Â Â Â}
> Â Â Â Â Â Â Â Âlist_splice_tail(list, &kclist_head);
> Â Â Â Â} else
> Â Â Â Â Â Â Â Âlist_splice(list, &garbage);
> Â Â Â Âkcore_need_update = 0;
> Â Â Â Âwrite_unlock(&kclist_lock);
> }
>
> kclist itself is double checked under write_lock.
> And, once updated, get_kcore_size()'s return vaule is static.


Imagine one process does get_kcore_size(), then another process
is scheduled, who also does get_kcore_size() but at this time,
memory size is changed, so it gets a different value. If then the
second process writes to proc_root_kcore->size before the first one
does, the proc_root_kcore->size is wrong.

Am I missing something here?

> So, I think there are no race. But..Hmm...is this clearer ?
>
> ==
> From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>


Yes, this version should be OK.

Acked-by: WANG Cong <xiyou.wangcong@xxxxxxxxx>

>
> Now, size of /proc/kcore which can be read by 'ls -l' is 0.
> But it's not correct value.
>
> This is a patch for showing size of /proc/kcore as following.
>
> On x86-64, ls -l shows
> Â... root root 140737486266368 2009-09-17 10:29 /proc/kcore
> Then, 7FFFFFFE02000. This comes from vmalloc area's size.
> This shows "core" size, not Âmemory size.
>
> This patch shows the size by updating "size" field in struct proc_dir_entry.
> Later, lookup routine will create inode and fill inode->i_size based
> on this value. Then, this has a problem.
>
> Â- Once inode is cached, inode->i_size will never be updated.
>
> Then, this patch is not memory-hotplug-aware.
>
> To update inode->i_size, we have to know dentry or inode.
> But there is no way to lookup them by inside kernel. Hmmm....
> Next patch will try it.
>
> Changelog:
> Â-moved upadting ->size under lock.
>
> Cc: WANG Cong <xiyou.wangcong@xxxxxxxxx>
> Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>
> ---
> Âfs/proc/kcore.c | Â Â6 +++++-
> Â1 file changed, 5 insertions(+), 1 deletion(-)
>
> Index: mmotm-2.6.31-Sep14/fs/proc/kcore.c
> ===================================================================
> --- mmotm-2.6.31-Sep14.orig/fs/proc/kcore.c
> +++ mmotm-2.6.31-Sep14/fs/proc/kcore.c
> @@ -107,6 +107,8 @@ static void free_kclist_ents(struct list
> Â*/
> Âstatic void __kcore_update_ram(struct list_head *list)
> Â{
> + Â Â Â int nphdr;
> + Â Â Â size_t size;
> Â Â Â Âstruct kcore_list *tmp, *pos;
> Â Â Â ÂLIST_HEAD(garbage);
>
> @@ -121,6 +123,7 @@ static void __kcore_update_ram(struct li
> Â Â Â Â} else
> Â Â Â Â Â Â Â Âlist_splice(list, &garbage);
> Â Â Â Âkcore_need_update = 0;
> + Â Â Â proc_root_kcore->size = get_kcore_size(&nphdr, &size);
> Â Â Â Âwrite_unlock(&kclist_lock);
>
> Â Â Â Âfree_kclist_ents(&garbage);
> @@ -429,7 +432,8 @@ read_kcore(struct file *file, char __use
> Â Â Â Âunsigned long start;
>
> Â Â Â Âread_lock(&kclist_lock);
> - Â Â Â proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen);
> + Â Â Â size = get_kcore_size(&nphdr, &elf_buflen);
> +
> Â Â Â Âif (buflen == 0 || *fpos >= size) {
> Â Â Â Â Â Â Â Âread_unlock(&kclist_lock);
> Â Â Â Â Â Â Â Âreturn 0;
>
>
>
>
>
>
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/