kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: j.neuschaefer@gmx.net (Jonathan Neuschäfer)
To: kernelnewbies@lists.kernelnewbies.org
Subject: [RFC]Something wrong with my module
Date: Thu, 12 Apr 2012 15:03:25 +0200	[thread overview]
Message-ID: <20120412130325.GA1874@debian.debian> (raw)
In-Reply-To: <CAD+1EGO--aBWqehzmm+W1gX6wMD-d67P_Rpy97JTtbQ4U0VX=Q@mail.gmail.com>

On Thu, Apr 12, 2012 at 06:16:56PM +0800, harryxiyou wrote:
> Hi greg,
> 
>     I write a module for inserting a PCB or delete a PCB to kernel's
> PCB tree, but when i run it something wrong happens to me like following.
> My environment is "Linux 10 2.6.35-22-generic #33-Ubuntu SMP Sun Sep
> 19 20:34:50 UTC 2010 i686 GNU/Linux"
> 
> hw2.c
> 
> #include <linux/module.h>
> #include <linux/kernel.h>
> #include <linux/init.h>
> #include <linux/sched.h>
> #include <linux/list.h>
> #include <linux/slab.h>
> 
> struct pcb {
> 	int pid;
> 	int state;
> 	int flag;
> 	char *comm;
> 	struct list_head tasks;
> };
> 
> static int insert_task(struct task_struct *p) {
> 	struct pcb *pcb1 = NULL;
> 	pcb1 = (struct pcb *)kmalloc(sizeof(struct pcb), GFP_KERNEL);
> 	if (NULL == pcb1) {
> 		printk("<0> kmalloc failed!\n");

If you don't return, you'll do an invalid memory access the next line.

> 	}
> 	pcb1->state = 8;
> 	pcb1->flag = 8;
> 	pcb1->pid= 2;
> 	pcb1->comm = "jiawei";
> 	list_add(&pcb1->tasks, &p->tasks);

You add your pcb structure to a list of struct task_structs, this looks
somewhat bogus.

> 	return 0;
> }
> 
> static int rm_task(struct task_struct *p){
> 	struct task_struct *del = p;
> 	list_del(&p->tasks);
> //	kfree(del);
> 	return 0;
> }
> #if 1
> static int print_pid(void) {

You do possibly destructive operations here, "print" doesn't quite imply
that.

> 	struct task_struct *task = NULL;
> 	struct task_struct *p = NULL;
> 	struct list_head *pos = NULL;
> 	int count = 0;
> 	
> 	printk("Search for insert task-------->\n");
> 	task = &init_task;
> 	list_for_each(pos, &task->tasks) {
> 		p = list_entry(pos, struct task_struct, tasks);
> 		count++;
> 		if (0 == p->pid) {
> 			rm_task(p);
> 		}
> 		printk("pid: %d, state: %ld, comm: %s\n", p->pid, p->state, p->comm);
> 	}
> 	insert_task(p);

Why do you want to insert your bogus struct after the last task?

> 	printk("<1> Hello World\n");

The KERN_* constants are a good replacement for a manual "<n>".

> 
> 
> Dmesg logs:
> 
> [ 1174.738305] Search for insert task-------->
[...]
> [ 1174.738819] pid: 2481, state: 1, comm: bash
> [ 1174.738822] pid: 0, state: 1, comm:
> [ 1174.738840] BUG: unable to handle kernel paging request at 00100100

This is probably in insert_task.
list_del sets tasks->next to LIST_POISON1 (which is 0x00100100), list_add
tries to access it and segfaults.

> 
> Cloud you please give me some help?

Hope This Helps,
	Jonathan Neusch?fer

  parent reply	other threads:[~2012-04-12 13:03 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-12 10:16 [RFC]Something wrong with my module harryxiyou
2012-04-12 11:18 ` Kristof Provost
2012-04-12 13:40   ` harryxiyou
2012-04-12 13:59   ` Frank Ch. Eigler
2012-04-12 14:04     ` harryxiyou
2012-04-12 14:08       ` harryxiyou
2012-04-12 14:45         ` Kristof Provost
2012-04-12 13:03 ` Jonathan Neuschäfer [this message]
2012-04-12 13:52   ` harryxiyou
2012-04-12 14:33     ` Jonathan Neuschäfer
2012-04-13 15:00       ` harryxiyou

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=20120412130325.GA1874@debian.debian \
    --to=j.neuschaefer@gmx.net \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /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;
as well as URLs for NNTP newsgroup(s).