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 16:33:55 +0200	[thread overview]
Message-ID: <20120412143355.GB1874@debian.debian> (raw)
In-Reply-To: <CAD+1EGPWs1SzzUR0X-T4mv9FQteo8yyJtcZaE2eet6k7FiHEBg@mail.gmail.com>

On Thu, Apr 12, 2012 at 09:52:02PM +0800, harryxiyou wrote:
> On Thu, Apr 12, 2012 at 9:03 PM, Jonathan Neusch?fer
> <j.neuschaefer@gmx.net> wrote:
> 
> Hi Jonathan,
> 
> > On Thu, Apr 12, 2012 at 06:16:56PM +0800, harryxiyou wrote:
> >> Hi greg,
> >>
> ...
> >>
> >> 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;
> >> };
[...]

(from print_pid:)
> >> ? ? ? struct task_struct *p = NULL;
[...]
> >> ? ? ? ? ? ? ? printk("pid: %d, state: %ld, comm: %s\n", p->pid, p->state, p->comm);

> 
> Hmmm.., i just want to give a simplest task_struct, which is my pcb structure.
> Of course, it is bogus but it is now wrong for inserting. It can not
> print my fields
> correctly. (I run this module after i take away the rm_task function)
> 
> Some wrong logs like this:
> 
[...]
> [ 1515.055481] pid: 0, state: 1, comm:
> [ 1515.055483] the number of process is 145
> 
> I give the pid 8, state 8, and comm "jiawei" in my module. But it can
> not print correctly. Maybe kernel can tell my bogus one,right?

This has to do with the way accessing struct fields works in C:
For each struct each field name is translated by the compiler into an
offset which is used to compute the address of a field given the struct's
address. When you access the pid field of a struct task_struct the offset
will be at least around 20 * sizeof(int), which is an invalid offset to
your struct pcb, where the offsets are (most of the time):
	pid: 0
	state: sizeof(int)
	flag: 2 * sizeof(int)
	comm: 3 * sizeof(int)
	tasks: 3 * sizeof(int) + sizeof(char *)
(You get (an approximation of) the offset of a field by adding the size
 of the previous field (the compiler also adds some padding - see
 Documentation/unaligned-memory-access.txt in the kernel tree and
 http://en.wikipedia.org/wiki/Data_padding#Data_structure_padding))

Thanks,
	Jonathan Neusch?fer

  reply	other threads:[~2012-04-12 14:33 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
2012-04-12 13:52   ` harryxiyou
2012-04-12 14:33     ` Jonathan Neuschäfer [this message]
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=20120412143355.GB1874@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).