Re: VMA management in device driver

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 18 Aug 2004, Jim Bauer wrote:

> On Wednesday 18 August 2004 13:56, Mikael Levernes Valen-Sendstad wrote:
> >
> > Is this code safe?:
> >
> > [code deleted]
>
> I don't see you grabbing the current->mm->mmap_sem sepaphore.  Or are
> you doing that in the caller to this posted routine?
>

Thanks for a good advice.

Seems a little bit funny though to protect the vm with a spinlock and a
semaphore at the same time. Can/should I remove one of them?

Do you mean that the code should be like this:


/** Removes pages given by the userspace address "page_addr".
        Used to free pages within a mmaped area. (via ioctl)
        Better to use this than frequent calls to "mremap()"
*/
int clear_pages(unsigned long page_addr,unsigned int page_numbers)
{
        struct page *page;
        struct page *page_test;
        pgd_t *pgd; pmd_t *pmd; pte_t *pte;
        int i,page_nr_done=0;

        MSG("clear_pages(%lx,%d)",page_addr,page_numbers);
	down_write(&current->mm->mmap_sem);
	spin_lock(current->mm->page_table_lock);
        for(i=0; i<page_numbers; i++)
        {

                pgd = pgd_offset(current->mm, page_addr);
                pmd = pmd_offset(pgd, page_addr);
                pte = pte_offset(pmd, page_addr);
                page = pte_page(*pte);

                if (VALID_PAGE(page))
                {
                        pte_clear(pte);
                        put_page(page);
                        page_nr_done++;
                }
                else MSG("Clear pages: Page not found.");

                page_addr+= PAGE_SIZE;
        }
        spin_unlock(current->mm->page_table_lock);
	up_write(&current->mm->mmap_sem);
        __flush_tlb();

        return page_nr_done;
}


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux