Skip to contentSkip to conversation

exofs: confusion between kmap() and kmap_atomic() api

PATCH In review Dan Carpenter 3 participants 5 messages May 7, 2010 – May 11, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 exofs: confusion between kmap() and kmap_atomic() api

For kmap_atomic() we call kunmap_atomic() on the returned pointer. That's different from kmap() and kunmap() and so it's easy to get them backwards.

fs/exofs/dir.c +1 -1
index 4cfab1c..d91e9d8 100644
@@ -608,7 +608,7 @@ int exofs_make_empty(struct inode *inode, struct inode *parent)
 	de->inode_no = cpu_to_le64(parent->i_ino);
 	memcpy(de->name, PARENT_DIR, sizeof(PARENT_DIR));
 	exofs_set_de_type(de, inode);
-	kunmap_atomic(page, KM_USER0);
+	kunmap_atomic(kaddr, KM_USER0);
 	err = exofs_commit_chunk(page, 0, chunk_size);
 fail:
 	page_cache_release(page);
#1 lore raw
Quoted 6 lines · Dan Carpenter

For kmap_atomic() we call kunmap_atomic() on the returned pointer. That's different from kmap() and kunmap() and so it's easy to get them backwards.

Thank you Dan, I'll push it ASAP.

Looks like a bad bug. So this is actually a leak, right? kunmap_atomic would detect the bad pointer and do nothing?

Thanks again
Boaz

Quoted 13 lines
fs/exofs/dir.c +1 -1
index 4cfab1c..d91e9d8 100644
@@ -608,7 +608,7 @@ int exofs_make_empty(struct inode *inode, struct inode *parent)
 	de->inode_no = cpu_to_le64(parent->i_ino);
 	memcpy(de->name, PARENT_DIR, sizeof(PARENT_DIR));
 	exofs_set_de_type(de, inode);
-	kunmap_atomic(page, KM_USER0);
+	kunmap_atomic(kaddr, KM_USER0);
 	err = exofs_commit_chunk(page, 0, chunk_size);
 fail:
 	page_cache_release(page);
#2 lore raw
Quoted 5 lines · Boaz Harrosh
Quoted 4 lines · Dan Carpenter

For kmap_atomic() we call kunmap_atomic() on the returned pointer. That's different from kmap() and kunmap() and so it's easy to get them backwards.

Quoted 8 lines
Quoted 2 lines

Thank you Dan, I'll push it ASAP.

Looks like a bad bug. So this is actually a leak, right? kunmap_atomic would detect the bad pointer and do nothing?

I've looked at it, and I think you're right but I'm not the guy to ask...

I saw somenoe else fixing these and wrote a smatch check is all. I'm at the "Monkey see, monkey do" level of kernel hacking.

regards,
dan carpenter

#3 lore raw
In reply to Boaz Harrosh
Quoted 5 lines · On Sun, 09 May 2010 13:16:38 +0300 Boaz Harrosh <[email protected]> wrote:
Quoted 4 lines · Dan Carpenter

For kmap_atomic() we call kunmap_atomic() on the returned pointer. That's different from kmap() and kunmap() and so it's easy to get them backwards.

Quoted 7 lines
Quoted 2 lines

Thank you Dan, I'll push it ASAP.

Looks like a bad bug. So this is actually a leak, right? kunmap_atomic would detect the bad pointer and do nothing?

void kunmap_atomic(void *kvaddr, enum km_type type)

{
	unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
	enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id();

	/*
	 * Force other mappings to Oops if they'll try to access this pte
	 * without first remap it.  Keeping stale mappings around is a bad idea
	 * also, in case the page changes cacheability attributes or becomes
	 * a protected page in a hypervisor.
	 */
	if (vaddr == __fix_to_virt(FIX_KMAP_BEGIN+idx))
		kpte_clear_flush(kmap_pte-idx, vaddr);
	else {
#ifdef CONFIG_DEBUG_HIGHMEM
		BUG_ON(vaddr < PAGE_OFFSET);
		BUG_ON(vaddr >= (unsigned long)high_memory);
#endif
	}

	pagefault_enable();
}

if CONFIG_DEBUG_HIGHMEM=y, kunmap_atomic() will go BUG.

if CONFIG_DEBUG_HIGHMEM=n, kunmap_atomic() will do nothing, leaving the pte pointing at the old page. Next time someone tries to use that kmap_atomic() slot,

void *kmap_atomic_prot(struct page *page, enum km_type type, pgprot_t prot)

{
	enum fixed_addresses idx;
	unsigned long vaddr;

	/* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
	pagefault_disable();

	if (!PageHighMem(page))
		return page_address(page);

	debug_kmap_atomic(type);

	idx = type + KM_TYPE_NR*smp_processor_id();
	vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
	BUG_ON(!pte_none(*(kmap_pte-idx)));
	set_pte(kmap_pte-idx, mk_pte(page, prot));

	return (void *)vaddr;
}

kmap_atomic_prot() will go BUG because the pte wasn't cleared.

I can only assume that this code has never been run on i386. I'd suggest adding a "Cc: <[email protected]>" to the changelog if you have expectations that anyone will try to run it on i386.

#4 lore raw
Quoted 66 lines · Andrew Morton
Quoted 5 lines · On Sun, 09 May 2010 13:16:38 +0300 Boaz Harrosh <[email protected]> wrote:
Quoted 4 lines · Dan Carpenter

For kmap_atomic() we call kunmap_atomic() on the returned pointer. That's different from kmap() and kunmap() and so it's easy to get them backwards.

Quoted 7 lines
Quoted 2 lines

Thank you Dan, I'll push it ASAP.

Looks like a bad bug. So this is actually a leak, right? kunmap_atomic would detect the bad pointer and do nothing?

void kunmap_atomic(void *kvaddr, enum km_type type)

{
	unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
	enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id();

	/*
	 * Force other mappings to Oops if they'll try to access this pte
	 * without first remap it.  Keeping stale mappings around is a bad idea
	 * also, in case the page changes cacheability attributes or becomes
	 * a protected page in a hypervisor.
	 */
	if (vaddr == __fix_to_virt(FIX_KMAP_BEGIN+idx))
		kpte_clear_flush(kmap_pte-idx, vaddr);
	else {
#ifdef CONFIG_DEBUG_HIGHMEM
		BUG_ON(vaddr < PAGE_OFFSET);
		BUG_ON(vaddr >= (unsigned long)high_memory);
#endif
	}

	pagefault_enable();
}

if CONFIG_DEBUG_HIGHMEM=y, kunmap_atomic() will go BUG.

if CONFIG_DEBUG_HIGHMEM=n, kunmap_atomic() will do nothing, leaving the pte pointing at the old page. Next time someone tries to use that kmap_atomic() slot,

void *kmap_atomic_prot(struct page *page, enum km_type type, pgprot_t prot)

{
	enum fixed_addresses idx;
	unsigned long vaddr;

	/* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
	pagefault_disable();

	if (!PageHighMem(page))
		return page_address(page);

	debug_kmap_atomic(type);

	idx = type + KM_TYPE_NR*smp_processor_id();
	vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
	BUG_ON(!pte_none(*(kmap_pte-idx)));
	set_pte(kmap_pte-idx, mk_pte(page, prot));

	return (void *)vaddr;
}
Quoted 7 lines

kmap_atomic_prot() will go BUG because the pte wasn't cleared.

I can only assume that this code has never been run on i386. I'd suggest adding a "Cc: <[email protected]>" to the changelog if you have expectations that anyone will try to run it on i386.

Right! Everyone I know runs 64bit. I will add the Cc: <[email protected]> to the patch. Thanks.

Boaz