Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8947281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:43:04+00:00 2026-06-15T12:43:04+00:00

i write a module about link list with pri as the head node. It’s

  • 0

i write a module about link list with pri as the head node. It’s empty initially.

In pri_init(), I insert 3 nodes into the list and assign value to each of them. Finally, i want to use list_for_each_entry to iterate the list and print out the nodes’ value.

But actually, it just printed out only 1 random value in list_for_each_entry{…} in pri_init().

Instead, if I use container_of within a for() loop to iterate the list, the result is correct.

Can anyone tell me why? Thanks a lot.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <linux/list.h>  
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>

MODULE_LICENSE("GPL");

struct pri_listitem
{
    int v;
    struct list_head list;   
};

LIST_HEAD(pri);

void pri_exit(void)
{
    struct pri_listitem *list_p, *next;
    list_for_each_entry_safe(list_p, next, &pri, list)
    {
        list_del(&list_p->list);
        kfree(list_p);
    }
    printk(KERN_ALERT "***** device pri exit() ***** \n");
}

int pri_init(void)
{
    int i;
    struct pri_listitem *list_p;

    printk(KERN_ALERT "***** device pri init() ***** \n");
    // Inserting 3 nodes
    for(i=1; i<=3; ++i)
    {
            list_p = kmalloc(sizeof(struct pri_listitem),GFP_KERNEL);
            if(!list_p)
            {
                printk(KERN_DEBUG "Error: alloction memory for list_p\n");
                return -ENOMEM;
            }
            memset(list_p, 0, sizeof(struct pri_listitem));
            list_p->v = i;
            list_add(&(list_p->list), &pri);
    }

   printk(KERN_DEBUG "value of the 3 nodes:\n");
   list_for_each_entry(list_p, &pri, list);
   {
           printk(KERN_DEBUG "%d\n",list_p->v);  // print nodes' value
   } 
   return 0;
}

module_init(pri_init);
module_exit(pri_exit);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:43:06+00:00Added an answer on June 15, 2026 at 12:43 pm

    You simply had a semi colon at the end of your list for each entry, this meant your position was set to a garbage value in memory (not really garbage just an offset behind your allocated list_head pri structure in memory) hence you getting random values for the dereference (you unluckily landed in memory you could access so it’s hard to spot the error sometimes.) The single run through the loop should have tipped you off as the following section on the list_for_each_entry simply executed as a scoped block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I write 2 GWT module and compile it. I want locate ***.nocache.js files into
when i was about to write a sub module for my app, i'd like
I'm trying to write a python (2.7) matrix module. (I know about numpy, this
I'm about to dive into the source code for the cgi.py module again because
I'm looking to write a NPM module that builds on top of node-http-proxy but
On Server Fault, How to list symbolic link chains? (not my question) talks about
I'm trying to write a kernel module, which prints some information about the objects
I need to write a module that gives me a page will all possible
I need to write a module that sends order data to an epayment service,
I write a chat module and i need to load previous messages to chat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.