LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   Error while saving memory pages of a process- Sleeping in memory from invalid context (https://www.linuxquestions.org/questions/linux-kernel-70/error-while-saving-memory-pages-of-a-process-sleeping-in-memory-from-invalid-context-854100/)

Deep Narayan Dubey 01-04-2011 05:17 AM

Error while saving memory pages of a process- Sleeping in memory from invalid context
 
Hi,
I am trying to checkpoint process state in a file so that I can restore it later. I am able to save the registers, open files, signals of a process. I am also able to save the task->mm information and the vma start, end and offset but when I am trying to save pages I am getting following error -
Sleeping function is called from invalid context

My code for saving page is following -

Code:

/*
 * This function saves the memory pages in the region of memory specified by vma
 */
int save_pages(struct task_struct *task, struct vm_area_struct *vma, struct segments *seg) {

    int ret = 0;
    int len = PAGE_SIZE;

    struct mm_struct *mm;

    unsigned long addr;
    struct page *page;
    pte_t * pte;

    if(!(vma->vm_flags & VM_WRITE) || get_fd(task,vma->vm_file) != -1) {
        return(0);
    }

    mm = task->mm;

    if(!mm) {
        return -1;
    }

    for(addr = vma->vm_start ; addr < vma->vm_end ; addr = addr + PAGE_SIZE) {
               
    seg->num_pages++;

    pte = chkpt_get_pte(vma,addr);
               
    if(pte && pte_present(*pte)) {

      page = pfn_to_page(pte_pfn(*pte));

      if(page_mapped(page)) {

          if(addr + PAGE_SIZE > vma->vm_end) {
              len = vma->vm_end - addr;
          }
          else {
              len = PAGE_SIZE;
          }
          if(vma->vm_flags & VM_MAYREAD) {
              if(copy_from_user(buffer, (void *)addr, len) != 0) {
                  printk(KERN_ALERT "Error while copying pages from used addr %lu to buffer\n", addr);
                  return -1;
              }
              ret = chkpt_write(filp,&addr,sizeof(unsigned long));
              if(ret < 0) {
                  printk(KERN_ALERT "Error while saving the address = %lu\n", addr);
                  return(ret);
              }
              ret = chkpt_write(filp, buffer, PAGE_SIZE);
              if(ret < 0) {
                  printk(KERN_ALERT "Error while saving the page\n");
                      return(ret);
                  }
                }
            }
        }
    }
    return(ret);
}

I have created a lock before saving memory using down_read(&task->mm->mmap_sem);

The stack trace printed after the above error gives that the above error has occurred when copy_from_user() function is called. It internally invokes the might_sleep() function and this function prints the above error.

Please, help so that I can remove the above error and save the memory pages.

Thanks in advance for any help.

Bibek1602 04-05-2012 02:58 PM

Quote:

Originally Posted by Deep Narayan Dubey (Post 4212465)
Hi,
I am trying to checkpoint process state in a file so that I can restore it later. I am able to save the registers, open files, signals of a process. I am also able to save the task->mm information and the vma start, end and offset but when I am trying to save pages I am getting following error -
Sleeping function is called from invalid context

My code for saving page is following -

Code:

/*
 * This function saves the memory pages in the region of memory specified by vma
 */
int save_pages(struct task_struct *task, struct vm_area_struct *vma, struct segments *seg) {

    int ret = 0;
    int len = PAGE_SIZE;

    struct mm_struct *mm;

    unsigned long addr;
    struct page *page;
    pte_t * pte;

    if(!(vma->vm_flags & VM_WRITE) || get_fd(task,vma->vm_file) != -1) {
        return(0);
    }

    mm = task->mm;

    if(!mm) {
        return -1;
    }

    for(addr = vma->vm_start ; addr < vma->vm_end ; addr = addr + PAGE_SIZE) {
               
    seg->num_pages++;

    pte = chkpt_get_pte(vma,addr);
               
    if(pte && pte_present(*pte)) {

      page = pfn_to_page(pte_pfn(*pte));

      if(page_mapped(page)) {

          if(addr + PAGE_SIZE > vma->vm_end) {
              len = vma->vm_end - addr;
          }
          else {
              len = PAGE_SIZE;
          }
          if(vma->vm_flags & VM_MAYREAD) {
              if(copy_from_user(buffer, (void *)addr, len) != 0) {
                  printk(KERN_ALERT "Error while copying pages from used addr %lu to buffer\n", addr);
                  return -1;
              }
              ret = chkpt_write(filp,&addr,sizeof(unsigned long));
              if(ret < 0) {
                  printk(KERN_ALERT "Error while saving the address = %lu\n", addr);
                  return(ret);
              }
              ret = chkpt_write(filp, buffer, PAGE_SIZE);
              if(ret < 0) {
                  printk(KERN_ALERT "Error while saving the page\n");
                      return(ret);
                  }
                }
            }
        }
    }
    return(ret);
}

I have created a lock before saving memory using down_read(&task->mm->mmap_sem);

The stack trace printed after the above error gives that the above error has occurred when copy_from_user() function is called. It internally invokes the might_sleep() function and this function prints the above error.

Please, help so that I can remove the above error and save the memory pages.

Thanks in advance for any help.

Hi,we are new to Linux and the concept of checkpointing. As you have mentioned that you are able to save the contents of the process but facing problem in saving the pages, can you help us in doing what you have already done. We are not able to save the context of the process also. It will be grateful of you if you help us anyhow...

Thanks in advance for any help....


All times are GMT -5. The time now is 11:17 AM.