Hi i'm programming kernel (2.6) for the first time and i have a problem using spinlocks.
I'm trying to add this system call (inserisci_nodo) that externalizes a structure (an ibrid list-hashtable) and try to add a node (Persona) in this structure, if the node already exists it will be updated.
I have a problem on the first "update", in fact, if I try to insert all new nodes it works! But if I try to insert an already existent node this seems already locked. I cannot understand why.
This is the code.
asmlinkage long sys_inserisci_nodo(key_t id, char* nome, int eta){
persona *p;
spin_lock(&htable.lock);
printk("<3> PRESO SPIN_LOCK TABELLA\n");
if((p=lookup(id))!=NULL){ //Update an already existent node
printk("<3> NODO %d, AGGIORNAMENTO IN CORSO....\n",id); <--- system locked.
spin_lock(&(p->lock));
printk("<3> PRESO SPIN_LOCK NODO %d\n",id);
p->eta=eta;
strcpy(p->nome, nome);
printk("<3> NODO %d, AGGIORNAMENTO... OK\n",id);
spin_unlock(&p->lock);
printk("<3> RILASCIATO SPIN_LOCK NODO %d\n",id);
spin_unlock(&htable.lock);
printk("<3> RILASCIATO SPIN_LOCK TABELLA\n");
return p->id;
}
else{ //Insert new node.
p = (persona *) kmalloc(sizeof(persona),GFP_KERNEL);
if(p==NULL){
printk("<3> ERRORE NELL'ALLOCARE MEMORIA PER PERSONA CON ID: %d\n",id);
spin_unlock(&htable.lock);
printk("<3> RILASCIATO SPIN_LOCK TABELLA\n");
return -1;
}
p->id = id;
p->eta=eta;
p->nome = (char*) kmalloc(sizeof(nome),GFP_KERNEL);
strcpy(p->nome, nome);
printk("<3> NODO %d, AGGIUNTO\n",id);
int h = hashfunc(id);
p->next=htable.persone[h];
htable.persone[h] = p;
spin_unlock(&htable.lock);
printk("<3> RILASCIATO SPIN_LOCK TABELLA\n");
return h;
}
}
Sorry for the italian language on the code, I add some english comments on the code.
These are the two structures (hashtable and "persona" (node)).
typedef struct _persona{
key_t id;
char *nome;
int eta;
spinlock_t lock;
struct _persona *next;
}persona;
typedef struct _hashtable{
spinlock_t lock;
int occupati;
persona* persone[MAX_NUM];
}hashtable;
"lookup(key)" and "hash(key)" are two simple functions to get nodes from the struct.
I hope you have an idea about it :)
bye!
You need to initialize the spinlock:
p->id = id;
p->eta=eta;
p->nome = (char*) kmalloc(sizeof(nome),GFP_KERNEL);
spin_lock_init(&p->lock); // <- don't forget to initialize the locks!