Kernel Newbies archive on lore.kernel.org
 help / color / Atom feed
* Netlink socket returns NULL in vmx.c kernel file
@ 2019-10-23  2:43 Irfan Ullah (울라 이르판)
  2019-10-23  5:38 ` Valdis Klētnieks
  0 siblings, 1 reply; 7+ messages in thread
From: Irfan Ullah (울라 이르판) @ 2019-10-23  2:43 UTC (permalink / raw)
  To: kernelnewbies

[-- Attachment #1.1: Type: text/plain, Size: 1029 bytes --]

Dear All,
I am posting again the same problem that is "Netlink socket creation
returns NULL" when I try to create it in handle_ept_violation() in the
vmx.c file. Actually, I am trying to use netlink to send information (i.e.,
guest physical address) from kernel space to user space.
The netlink socket creation is not working in the context of vmx.c, but it
works perfectly when I test my code in a kernel module and load it. Code is
in the attachement.
I have tested many things such as shared memory, kernel thread, sleep
function, etc. but nothing worked out.
Can you please what's the problem?

*What's the best too/way that can provide full duplex (two-way)
communication between kernel and user spaces in any context (from any
kernel source file)*.
Thank you.
-- 
*Best Regards,*


*Mr. Irfan Ullah*
PhD Candidate
Data and Knowledge Engineering(DKE) Lab
Department of Computer Science and Engineering
Kyung Hee University, South Korea.
 +82-010-591-51651 <+82%2010-3877-8867>
  sahibzada.iu@gmail.com
 sahibzada_irfanullah

[-- Attachment #1.2: Type: text/html, Size: 2830 bytes --]

<div dir="ltr">Dear All,<div>I am posting again the same problem that is &quot;Netlink socket creation returns NULL&quot; when I try to create it in handle_ept_violation() in the vmx.c file. Actually, I am trying to use netlink to send information (i.e., guest physical address) from kernel space to user space. <br clear="all"><div>The netlink socket creation is not working in the context of vmx.c, but it works perfectly when I test my code in a kernel module and load it. Code is in the attachement.  </div><div>I have tested many things such as shared memory, kernel thread, sleep function, etc. but nothing worked out. </div><div>Can you please what&#39;s the problem? </div><div><br></div><div><b>What&#39;s the best too/way that can provide full duplex (two-way) communication between kernel and user spaces in any context (from any kernel source file)</b>.</div><div>Thank you.</div>-- <br><div dir="ltr" data-smartmail="gmail_signature"><div dir="ltr"><div><b style="color:rgb(39,78,19)">Best Regards,</b></div><div><b style="color:rgb(39,78,19)"><br></b></div><div><b style="color:rgb(39,78,19)"><br></b></div><div dir="ltr"><b style="color:rgb(39,78,19)">Mr. Irfan Ullah</b><br></div><div dir="ltr"><font color="#666666">PhD Candidate<br></font><div><font color="#666666">Data and Knowledge Engineering(DKE) Lab</font></div><div><font color="#666666">Department of Computer Science and Engineering</font></div><div><font color="#666666">Kyung Hee University, South Korea.</font></div><div><img src="http://teedayusa.com/media/wysiwyg/telephone_icon.gif" style="font-family:arial,sans-serif;font-size:12.8px"><span style="font-family:arial,sans-serif;font-size:12.8px"> </span><span style="font-family:arial,sans-serif;font-size:12.8px"><font color="#000000"><a href="tel:+82%2010-3877-8867" value="+821038778867" style="color:rgb(17,85,204)" target="_blank">+82-010-591-51651</a></font></span></div><div><div style="font-family:arial,sans-serif;font-size:12.8px"><img src="http://www.letsplaydodgeball.com/ndo/templates/dj-sport01/images/system/emailButton.png" style="font-size:12.8px"><span style="font-size:12.8px"><font color="#000000">  </font></span><span style="font-size:12.8px"><font color="#000000"><a href="mailto:sahibzada.iu@gmail.com" style="color:rgb(17,85,204)" target="_blank">sahibzada.iu@gmail.com</a></font></span></div><div><font face="arial, sans-serif"><span style="font-size:12.8px"><img src="https://docs.google.com/uc?export=download&amp;id=0B1-dY3m3XHQnOFBOM0NSODNoUUU&amp;revid=0B1-dY3m3XHQnUmp1c0ZWTGovR2pYbGZCenlOU29DY3ByQU5RPQ" style="font-size:12.8px"></span></font><span style="font-family:arial,sans-serif;font-size:12.8px"> </span><font face="arial, sans-serif"><span style="font-size:12.8px">sahibzada_irfanullah</span></font></div></div></div></div></div></div></div>

[-- Attachment #2: Kernel_User_comm.zip --]
[-- Type: application/zip, Size: 7514 bytes --]

[-- Attachment #3: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Netlink socket returns NULL in vmx.c kernel file
  2019-10-23  2:43 Netlink socket returns NULL in vmx.c kernel file Irfan Ullah (울라 이르판)
@ 2019-10-23  5:38 ` Valdis Klētnieks
  2019-11-05  3:29   ` Irfan Ullah (울라 이르판)
  0 siblings, 1 reply; 7+ messages in thread
From: Valdis Klētnieks @ 2019-10-23  5:38 UTC (permalink / raw)
  To: Irfan Ullah (울라
	이르판)
  Cc: kernelnewbies

[-- Attachment #1.1: Type: text/plain, Size: 375 bytes --]

On Wed, 23 Oct 2019 11:43:22 +0900, Irfan Ullah said:

> Can you please what's the problem?

To be brutally honest, your code is too unreadable to figure out
what the problem is.

First off, fix your code formatting to match Linux kernel indenting.

Second, there's a lot of code in your .h file that should be in a .c file.

Third, functions should be static when feasible.

[-- Attachment #1.2: Type: application/pgp-signature, Size: 832 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Netlink socket returns NULL in vmx.c kernel file
  2019-10-23  5:38 ` Valdis Klētnieks
@ 2019-11-05  3:29   ` Irfan Ullah (울라 이르판)
  2019-11-05  7:22     ` Greg KH
  0 siblings, 1 reply; 7+ messages in thread
From: Irfan Ullah (울라 이르판) @ 2019-11-05  3:29 UTC (permalink / raw)
  To: Valdis Klētnieks; +Cc: Linux Kernel List

[-- Attachment #1.1: Type: text/plain, Size: 1017 bytes --]

I have tested code from different aspects. I have searched a lot in two
weeks, but still I am facing the same problem. Can you please check out
what is the problem with my code. Code is in the attached zipped file.
Thank you very much.

On Wed, Oct 23, 2019 at 2:38 PM Valdis Klētnieks <valdis.kletnieks@vt.edu>
wrote:

> On Wed, 23 Oct 2019 11:43:22 +0900, Irfan Ullah said:
>
> > Can you please what's the problem?
>
> To be brutally honest, your code is too unreadable to figure out
> what the problem is.
>
> First off, fix your code formatting to match Linux kernel indenting.
>
> Second, there's a lot of code in your .h file that should be in a .c file.
>
> Third, functions should be static when feasible.
>


-- 
*Best Regards,*


*Mr. Irfan Ullah*
PhD Candidate
Data and Knowledge Engineering(DKE) Lab
Department of Computer Science and Engineering
Kyung Hee University, South Korea.
 +82-010-591-51651 <+82%2010-3877-8867>
  sahibzada.iu@gmail.com
 sahibzada_irfanullah

[-- Attachment #1.2: Type: text/html, Size: 3003 bytes --]

<div dir="ltr">I have tested code from different aspects. I have searched a lot in two weeks, but still I am facing the same problem. Can you please check out what is the problem with my code. Code is in the attached zipped file. <div>Thank you very much.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Oct 23, 2019 at 2:38 PM Valdis Klētnieks &lt;<a href="mailto:valdis.kletnieks@vt.edu" target="_blank">valdis.kletnieks@vt.edu</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wed, 23 Oct 2019 11:43:22 +0900, Irfan Ullah said:<br>
<br>
&gt; Can you please what&#39;s the problem?<br>
<br>
To be brutally honest, your code is too unreadable to figure out<br>
what the problem is.<br>
<br>
First off, fix your code formatting to match Linux kernel indenting.<br>
<br>
Second, there&#39;s a lot of code in your .h file that should be in a .c file.<br>
<br>
Third, functions should be static when feasible.<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><b style="color:rgb(39,78,19)">Best Regards,</b></div><div><b style="color:rgb(39,78,19)"><br></b></div><div><b style="color:rgb(39,78,19)"><br></b></div><div dir="ltr"><b style="color:rgb(39,78,19)">Mr. Irfan Ullah</b><br></div><div dir="ltr"><font color="#666666">PhD Candidate<br></font><div><font color="#666666">Data and Knowledge Engineering(DKE) Lab</font></div><div><font color="#666666">Department of Computer Science and Engineering</font></div><div><font color="#666666">Kyung Hee University, South Korea.</font></div><div><img src="http://teedayusa.com/media/wysiwyg/telephone_icon.gif" style="font-family: arial, sans-serif; font-size: 12.8px;"><span style="font-family:arial,sans-serif;font-size:12.8px"> </span><span style="font-family:arial,sans-serif;font-size:12.8px"><font color="#000000"><a href="tel:+82%2010-3877-8867" value="+821038778867" style="color:rgb(17,85,204)" target="_blank">+82-010-591-51651</a></font></span></div><div><div style="font-family:arial,sans-serif;font-size:12.8px"><img src="http://www.letsplaydodgeball.com/ndo/templates/dj-sport01/images/system/emailButton.png" style="font-size: 12.8px;"><span style="font-size:12.8px"><font color="#000000">  </font></span><span style="font-size:12.8px"><font color="#000000"><a href="mailto:sahibzada.iu@gmail.com" style="color:rgb(17,85,204)" target="_blank">sahibzada.iu@gmail.com</a></font></span></div><div><font face="arial, sans-serif"><span style="font-size:12.8px"><img src="https://docs.google.com/uc?export=download&amp;id=0B1-dY3m3XHQnOFBOM0NSODNoUUU&amp;revid=0B1-dY3m3XHQnUmp1c0ZWTGovR2pYbGZCenlOU29DY3ByQU5RPQ" style="font-size: 12.8px;"></span></font><span style="font-family:arial,sans-serif;font-size:12.8px"> </span><font face="arial, sans-serif"><span style="font-size:12.8px">sahibzada_irfanullah</span></font></div></div></div></div></div>

[-- Attachment #2: Kernel_User_comm.zip --]
[-- Type: application/zip, Size: 7855 bytes --]

[-- Attachment #3: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Netlink socket returns NULL in vmx.c kernel file
  2019-11-05  3:29   ` Irfan Ullah (울라 이르판)
@ 2019-11-05  7:22     ` Greg KH
  2019-11-05  8:59       ` Irfan Ullah (울라 이르판)
  0 siblings, 1 reply; 7+ messages in thread
From: Greg KH @ 2019-11-05  7:22 UTC (permalink / raw)
  To: Irfan Ullah (울라 이르판)
  Cc: Valdis Klētnieks, Linux Kernel List

On Tue, Nov 05, 2019 at 12:29:56PM +0900, Irfan Ullah (울라 이르판) wrote:
> I have tested code from different aspects. I have searched a lot in two
> weeks, but still I am facing the same problem. Can you please check out
> what is the problem with my code. Code is in the attached zipped file.

Random compressed files are not the easiest way to review code.  Just
attach the files inline if you wish people to be able to review them
easily, like all kernel development happens.

thanks,

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Netlink socket returns NULL in vmx.c kernel file
  2019-11-05  7:22     ` Greg KH
@ 2019-11-05  8:59       ` Irfan Ullah (울라 이르판)
  2019-11-05 10:43         ` Valdis Klētnieks
  0 siblings, 1 reply; 7+ messages in thread
From: Irfan Ullah (울라 이르판) @ 2019-11-05  8:59 UTC (permalink / raw)
  To: Greg KH; +Cc: Valdis Klētnieks, Linux Kernel List

[-- Attachment #1.1: Type: text/plain, Size: 981 bytes --]

Thank you for the response.
Attached are the files for kernel-user spaces communication.


On Tue, Nov 5, 2019 at 4:22 PM Greg KH <greg@kroah.com> wrote:

> On Tue, Nov 05, 2019 at 12:29:56PM +0900, Irfan Ullah (울라 이르판) wrote:
> > I have tested code from different aspects. I have searched a lot in two
> > weeks, but still I am facing the same problem. Can you please check out
> > what is the problem with my code. Code is in the attached zipped file.
>
> Random compressed files are not the easiest way to review code.  Just
> attach the files inline if you wish people to be able to review them
> easily, like all kernel development happens.
>
> thanks,
>
> greg k-h
>


-- 
*Best Regards,*


*Mr. Irfan Ullah*
PhD Candidate
Data and Knowledge Engineering(DKE) Lab
Department of Computer Science and Engineering
Kyung Hee University, South Korea.
 +82-010-591-51651 <+82%2010-3877-8867>
  sahibzada.iu@gmail.com
 sahibzada_irfanullah

[-- Attachment #1.2: Type: text/html, Size: 2969 bytes --]

<div dir="ltr">Thank you for the response. <div>Attached are the files for kernel-user spaces communication.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Nov 5, 2019 at 4:22 PM Greg KH &lt;<a href="mailto:greg@kroah.com" target="_blank">greg@kroah.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Tue, Nov 05, 2019 at 12:29:56PM +0900, Irfan Ullah (울라 이르판) wrote:<br>
&gt; I have tested code from different aspects. I have searched a lot in two<br>
&gt; weeks, but still I am facing the same problem. Can you please check out<br>
&gt; what is the problem with my code. Code is in the attached zipped file.<br>
<br>
Random compressed files are not the easiest way to review code.  Just<br>
attach the files inline if you wish people to be able to review them<br>
easily, like all kernel development happens.<br>
<br>
thanks,<br>
<br>
greg k-h<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><b style="color:rgb(39,78,19)">Best Regards,</b></div><div><b style="color:rgb(39,78,19)"><br></b></div><div><b style="color:rgb(39,78,19)"><br></b></div><div dir="ltr"><b style="color:rgb(39,78,19)">Mr. Irfan Ullah</b><br></div><div dir="ltr"><font color="#666666">PhD Candidate<br></font><div><font color="#666666">Data and Knowledge Engineering(DKE) Lab</font></div><div><font color="#666666">Department of Computer Science and Engineering</font></div><div><font color="#666666">Kyung Hee University, South Korea.</font></div><div><img src="http://teedayusa.com/media/wysiwyg/telephone_icon.gif" style="font-family: arial, sans-serif; font-size: 12.8px;"><span style="font-family:arial,sans-serif;font-size:12.8px"> </span><span style="font-family:arial,sans-serif;font-size:12.8px"><font color="#000000"><a href="tel:+82%2010-3877-8867" value="+821038778867" style="color:rgb(17,85,204)" target="_blank">+82-010-591-51651</a></font></span></div><div><div style="font-family:arial,sans-serif;font-size:12.8px"><img src="http://www.letsplaydodgeball.com/ndo/templates/dj-sport01/images/system/emailButton.png" style="font-size: 12.8px;"><span style="font-size:12.8px"><font color="#000000">  </font></span><span style="font-size:12.8px"><font color="#000000"><a href="mailto:sahibzada.iu@gmail.com" style="color:rgb(17,85,204)" target="_blank">sahibzada.iu@gmail.com</a></font></span></div><div><font face="arial, sans-serif"><span style="font-size:12.8px"><img src="https://docs.google.com/uc?export=download&amp;id=0B1-dY3m3XHQnOFBOM0NSODNoUUU&amp;revid=0B1-dY3m3XHQnUmp1c0ZWTGovR2pYbGZCenlOU29DY3ByQU5RPQ" style="font-size: 12.8px;"></span></font><span style="font-family:arial,sans-serif;font-size:12.8px"> </span><font face="arial, sans-serif"><span style="font-size:12.8px">sahibzada_irfanullah</span></font></div></div></div></div></div>

[-- Attachment #2: Makefile --]
[-- Type: application/octet-stream, Size: 229 bytes --]

[-- Attachment #3: netlink_kernel_space.h --]
[-- Type: text/x-chdr, Size: 487 bytes --]

#ifndef NETLINK_KERNEL_SPACE_H
#define NETLINK_KERNEL_SPACE_H
MODULE_LICENSE("GPL");

//static void data_update(unsigned long long int, char[100]);
void kernel_space_sender(unsigned long long);
void kernel_space_receiver(struct sk_buff*); 
void create_socket(unsigned long long int);



void prepare_buffer_msg(void);
void user_space_sender(unsigned long long);
void user_space_receiver(void);
/* This function update the data, ie, s_data which is transferred to the user space.*/
#endif

[-- Attachment #4: netlink_kernel_space.c --]
[-- Type: text/x-csrc, Size: 2306 bytes --]

#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include "netlink_kernel_space.h"
MODULE_LICENSE("GPL");
#define NETLINK_USER 31

struct sock *nl_sk = NULL;
struct nlmsghdr *nlh;
struct sk_buff *skb_out;

typedef struct data_pack{
    unsigned long long int int_address;
    char str_data[100];
} struct_data_pack;
struct_data_pack s_data; 
int msg_size = sizeof(s_data);


int pid  = -1;
int res = -1;


void data_update(unsigned long long int addr, char text[100])
{       /* 
         * updating s_data
        */

        strcpy(s_data.str_data, text);
        s_data.int_address = addr;

}


		/* 
		 * This function sends message to the user-space
        */
void kernel_space_sender(unsigned long long int addr)
{
		/* 
		 * updating s_data
        */
		data_update(addr, "KERNER SPACE MESSAGE"); 
		skb_out = nlmsg_new(msg_size,0);
		if(!skb_out) {
				printk(KERN_ERR "Failed to allocate new skb\n");
    			return;
		} 
		nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0);  
		/* not in mcast group */
		NETLINK_CB(skb_out).dst_group = 0; 
		memcpy(nlmsg_data(nlh), &s_data, msg_size);
		pid = 1;
		res=nlmsg_unicast(nl_sk,skb_out,pid);
		if(res<0)
    		printk(KERN_INFO "Error while sending to user\n");

}


		/* 
		 * This function receives message from the user-space.
        */
void kernel_space_receiver(struct sk_buff *skb) 
{
		/* 
		 * updating s_data
        */
		struct_data_pack *xy;
		nlh=(struct nlmsghdr*)skb->data;
 		xy = (struct_data_pack *)NLMSG_DATA(nlh);
		printk(KERN_INFO "Received message payload in Kernel  from USER: %lld - %s\n",  xy->int_address, xy->str_data);
		//msleep(1000);
		//kernel_space_sender(xy->int_address*2);
}

		/* 
		 * This function creates the socket for kernel-user spaces and communication.
        */
void create_socket(unsigned long long int addr)
{
		struct netlink_kernel_cfg cfg = {
    	.input = kernel_space_receiver,
		};
		nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
		if(!nl_sk) {
				printk(KERN_ALERT "Error creating socket.\n");
		}
		//when I remove this wait the code does not work
		msleep(3000);
		/* 
		 * sending the guest physical address to the user space by calling the following function
        */
		kernel_space_sender(addr);
		netlink_kernel_release(nl_sk);
		
		return;

}

[-- Attachment #5: netlink_kernel_module.c --]
[-- Type: text/x-csrc, Size: 484 bytes --]

#include <linux/module.h>
#include "netlink_kernel_space.h"
#include <linux/kallsyms.h>


MODULE_LICENSE("GPL");




int hello_init(void) 
{
		printk(KERN_INFO "entering module hello module\n");

		create_socket(10);
		return 0;
}

void hello_exit(void) 
{
	//netlink_kernel_release(nl_sk);
	printk(KERN_INFO "exiting hello module\n");


}
module_init(hello_init); 
module_exit(hello_exit);
MODULE_DESCRIPTION("Kernel-User Communication using Netlink");
MODULE_AUTHOR("Irfan Ullah");

[-- Attachment #6: netlink_user_module.c --]
[-- Type: text/x-csrc, Size: 2156 bytes --]

#include <sys/socket.h>
#include <linux/netlink.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define NETLINK_USER 31
#define MAX_PAYLOAD 1024 /* maximum payload size*/ 


struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
struct msghdr msg;

typedef struct data_pack{
    unsigned long long int int_address;
    char str_data[100];
} struct_data_pack;
struct_data_pack s_data;

int msg_size = sizeof(s_data);

void data_update(unsigned long long int addr, char text[100])
{       /* 
         * updating s_data
        */

        strcpy(s_data.str_data, text);
        s_data.int_address = addr;

}


void prepare_buffer_msg()
{

		bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));
		memset(&dest_addr, 0, sizeof(dest_addr));
		dest_addr.nl_family = AF_NETLINK;
		dest_addr.nl_pid = 0; /* For Linux Kernel */
		dest_addr.nl_groups = 0; /* unicast */
		nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));	
		memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
		nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
		//nlh->nlmsg_pid = getpid();
		nlh->nlmsg_pid = 1;
		nlh->nlmsg_flags = 0;
		memcpy(NLMSG_DATA(nlh), &s_data, msg_size);
		iov.iov_base = (void *)nlh;
		iov.iov_len = nlh->nlmsg_len;
		msg.msg_name = (void *)&dest_addr;
		msg.msg_namelen = sizeof(dest_addr);
		msg.msg_iov = &iov;
		msg.msg_iovlen = 1;


}

void user_space_sender(unsigned long long int addr)
{
		data_update(addr, "USER SPACE MESSAGE");
		sendmsg(sock_fd,&msg,0);
}


void user_space_receiver()
{
		while(1) {
				recvmsg(sock_fd, &msg, 0);
 				struct_data_pack *xy = (struct_data_pack *)NLMSG_DATA(nlh);
				printf("Received message payload from KERNEL: %lld - %s\n", xy->int_address, xy->str_data);
				//user_space_sender(xy->int_address*2);
		}

		close(sock_fd);

}





void main()
{
		sock_fd=socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
		while(sock_fd<0) {
				sock_fd=socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
		}

		src_addr.nl_family = AF_NETLINK;
		//src_addr.nl_pid = getpid(); /* self pid */
		src_addr.nl_pid = 1; /* self pid */
		prepare_buffer_msg();
		user_space_receiver(sock_fd);

}

[-- Attachment #7: ReadMe --]
[-- Type: application/octet-stream, Size: 421 bytes --]

[-- Attachment #8: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Netlink socket returns NULL in vmx.c kernel file
  2019-11-05  8:59       ` Irfan Ullah (울라 이르판)
@ 2019-11-05 10:43         ` Valdis Klētnieks
  2019-11-08  4:54           ` Irfan Ullah (울라 이르판)
  0 siblings, 1 reply; 7+ messages in thread
From: Valdis Klētnieks @ 2019-11-05 10:43 UTC (permalink / raw)
  To: Irfan Ullah (울라
	이르판)
  Cc: Greg KH, Linux Kernel List

[-- Attachment #1.1: Type: text/plain, Size: 2405 bytes --]

On Tue, 05 Nov 2019 17:59:43 +0900, Irfan Ullah said:

> Thank you for the response.
> Attached are the files for kernel-user spaces communication.

> 		//when I remove this wait the code does not work
> 		msleep(3000);

If your code doesn't work, but sticking in random delays makes
it start working, you almost certainly have a race condition or
synchronization issue that should be fixed using proper locking.

> void hello_exit(void)
> {
>	//netlink_kernel_release(nl_sk);

Congratulations. You just leaked a socket here, which is going to
make it difficult to use that socket until you either reboot or find a
way to close it properly before trying to create it again.

> (code generates some warnings, but it is not severe and could be ignored for the time being).

You should do the following:
1) Understand your code well enough so you understand *why* the compiler
issued the warning.
2) Correct your code so the compiler doesn't complain. It almost certainly
understands C better than you do.

gcc 9.2.1 emits one warning on the kernel module code at default warning
levels.  And it's one you *really* need to fix, because it indicates that you
and the compiler are not on the same page about what types your variables are.
Since it's going to go ahead and generate code based on what types *it* thinks
your variables are, you will have nothing but pain and anguish debugging if
you thought they were some other type....

In fact, you may want to compile the kernel module with 'make W=1' to get more
warnings.  If your system has sparse, you should use 'make W=1 C=1'.

And all the warnings this generates are things that shouldn't be seen in clean
kernel code.

I didn't bother looking closely at your userspace.  I gave up
when I saw this:

     14 int sock_fd;
(...)
     68 void user_space_receiver()
     69 {
(...)
     96                 user_space_receiver(sock_fd);

There's 2 basic ways to pass a variable to a function. You're trying
to use both of them here.  Pick one and use it properly.

Oh - and there's no possible way to reach the close(sock_fd); on line
77, because the rest of the function infinite loops without a break.  At the
very least, you should be checking the return code from recvmsg() and
exiting the while(1) loop if there's an issue.

Bottom line:  You need to get a *lot* more experience writing proper
C code in userspace before you try to write kernel code.

[-- Attachment #1.2: Type: application/pgp-signature, Size: 832 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Netlink socket returns NULL in vmx.c kernel file
  2019-11-05 10:43         ` Valdis Klētnieks
@ 2019-11-08  4:54           ` Irfan Ullah (울라 이르판)
  0 siblings, 0 replies; 7+ messages in thread
From: Irfan Ullah (울라 이르판) @ 2019-11-08  4:54 UTC (permalink / raw)
  To: Valdis Klētnieks; +Cc: Greg KH, Linux Kernel List

[-- Attachment #1.1: Type: text/plain, Size: 3071 bytes --]

Thank you very much. I am trying to rewrite the code. and fix these issues.

On Tue, Nov 5, 2019 at 7:43 PM Valdis Klētnieks <valdis.kletnieks@vt.edu>
wrote:

> On Tue, 05 Nov 2019 17:59:43 +0900, Irfan Ullah said:
>
> > Thank you for the response.
> > Attached are the files for kernel-user spaces communication.
>
> >               //when I remove this wait the code does not work
> >               msleep(3000);
>
> If your code doesn't work, but sticking in random delays makes
> it start working, you almost certainly have a race condition or
> synchronization issue that should be fixed using proper locking.
>
> > void hello_exit(void)
> > {
> >       //netlink_kernel_release(nl_sk);
>
> Congratulations. You just leaked a socket here, which is going to
> make it difficult to use that socket until you either reboot or find a
> way to close it properly before trying to create it again.
>
> > (code generates some warnings, but it is not severe and could be ignored
> for the time being).
>
> You should do the following:
> 1) Understand your code well enough so you understand *why* the compiler
> issued the warning.
> 2) Correct your code so the compiler doesn't complain. It almost certainly
> understands C better than you do.
>
> gcc 9.2.1 emits one warning on the kernel module code at default warning
> levels.  And it's one you *really* need to fix, because it indicates that
> you
> and the compiler are not on the same page about what types your variables
> are.
> Since it's going to go ahead and generate code based on what types *it*
> thinks
> your variables are, you will have nothing but pain and anguish debugging if
> you thought they were some other type....
>
> In fact, you may want to compile the kernel module with 'make W=1' to get
> more
> warnings.  If your system has sparse, you should use 'make W=1 C=1'.
>
> And all the warnings this generates are things that shouldn't be seen in
> clean
> kernel code.
>
> I didn't bother looking closely at your userspace.  I gave up
> when I saw this:
>
>      14 int sock_fd;
> (...)
>      68 void user_space_receiver()
>      69 {
> (...)
>      96                 user_space_receiver(sock_fd);
>
> There's 2 basic ways to pass a variable to a function. You're trying
> to use both of them here.  Pick one and use it properly.
>
> Oh - and there's no possible way to reach the close(sock_fd); on line
> 77, because the rest of the function infinite loops without a break.  At
> the
> very least, you should be checking the return code from recvmsg() and
> exiting the while(1) loop if there's an issue.
>
> Bottom line:  You need to get a *lot* more experience writing proper
> C code in userspace before you try to write kernel code.
>


-- 
*Best Regards,*


*Mr. Irfan Ullah*
PhD Candidate
Data and Knowledge Engineering(DKE) Lab
Department of Computer Science and Engineering
Kyung Hee University, South Korea.
 +82-010-591-51651 <+82%2010-3877-8867>
  sahibzada.iu@gmail.com
 sahibzada_irfanullah

[-- Attachment #1.2: Type: text/html, Size: 5264 bytes --]

<div dir="ltr">Thank you very much. I am trying to rewrite the code. and fix these issues. <br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Nov 5, 2019 at 7:43 PM Valdis Klētnieks &lt;<a href="mailto:valdis.kletnieks@vt.edu">valdis.kletnieks@vt.edu</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Tue, 05 Nov 2019 17:59:43 +0900, Irfan Ullah said:<br>
<br>
&gt; Thank you for the response.<br>
&gt; Attached are the files for kernel-user spaces communication.<br>
<br>
&gt;               //when I remove this wait the code does not work<br>
&gt;               msleep(3000);<br>
<br>
If your code doesn&#39;t work, but sticking in random delays makes<br>
it start working, you almost certainly have a race condition or<br>
synchronization issue that should be fixed using proper locking.<br>
<br>
&gt; void hello_exit(void)<br>
&gt; {<br>
&gt;       //netlink_kernel_release(nl_sk);<br>
<br>
Congratulations. You just leaked a socket here, which is going to<br>
make it difficult to use that socket until you either reboot or find a<br>
way to close it properly before trying to create it again.<br>
<br>
&gt; (code generates some warnings, but it is not severe and could be ignored for the time being).<br>
<br>
You should do the following:<br>
1) Understand your code well enough so you understand *why* the compiler<br>
issued the warning.<br>
2) Correct your code so the compiler doesn&#39;t complain. It almost certainly<br>
understands C better than you do.<br>
<br>
gcc 9.2.1 emits one warning on the kernel module code at default warning<br>
levels.  And it&#39;s one you *really* need to fix, because it indicates that you<br>
and the compiler are not on the same page about what types your variables are.<br>
Since it&#39;s going to go ahead and generate code based on what types *it* thinks<br>
your variables are, you will have nothing but pain and anguish debugging if<br>
you thought they were some other type....<br>
<br>
In fact, you may want to compile the kernel module with &#39;make W=1&#39; to get more<br>
warnings.  If your system has sparse, you should use &#39;make W=1 C=1&#39;.<br>
<br>
And all the warnings this generates are things that shouldn&#39;t be seen in clean<br>
kernel code.<br>
<br>
I didn&#39;t bother looking closely at your userspace.  I gave up<br>
when I saw this:<br>
<br>
     14 int sock_fd;<br>
(...)<br>
     68 void user_space_receiver()<br>
     69 {<br>
(...)<br>
     96                 user_space_receiver(sock_fd);<br>
<br>
There&#39;s 2 basic ways to pass a variable to a function. You&#39;re trying<br>
to use both of them here.  Pick one and use it properly.<br>
<br>
Oh - and there&#39;s no possible way to reach the close(sock_fd); on line<br>
77, because the rest of the function infinite loops without a break.  At the<br>
very least, you should be checking the return code from recvmsg() and<br>
exiting the while(1) loop if there&#39;s an issue.<br>
<br>
Bottom line:  You need to get a *lot* more experience writing proper<br>
C code in userspace before you try to write kernel code.<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><b style="color:rgb(39,78,19)">Best Regards,</b></div><div><b style="color:rgb(39,78,19)"><br></b></div><div><b style="color:rgb(39,78,19)"><br></b></div><div dir="ltr"><b style="color:rgb(39,78,19)">Mr. Irfan Ullah</b><br></div><div dir="ltr"><font color="#666666">PhD Candidate<br></font><div><font color="#666666">Data and Knowledge Engineering(DKE) Lab</font></div><div><font color="#666666">Department of Computer Science and Engineering</font></div><div><font color="#666666">Kyung Hee University, South Korea.</font></div><div><img src="http://teedayusa.com/media/wysiwyg/telephone_icon.gif" style="font-family: arial, sans-serif; font-size: 12.8px;"><span style="font-family:arial,sans-serif;font-size:12.8px"> </span><span style="font-family:arial,sans-serif;font-size:12.8px"><font color="#000000"><a href="tel:+82%2010-3877-8867" value="+821038778867" style="color:rgb(17,85,204)" target="_blank">+82-010-591-51651</a></font></span></div><div><div style="font-family:arial,sans-serif;font-size:12.8px"><img src="http://www.letsplaydodgeball.com/ndo/templates/dj-sport01/images/system/emailButton.png" style="font-size: 12.8px;"><span style="font-size:12.8px"><font color="#000000">  </font></span><span style="font-size:12.8px"><font color="#000000"><a href="mailto:sahibzada.iu@gmail.com" style="color:rgb(17,85,204)" target="_blank">sahibzada.iu@gmail.com</a></font></span></div><div><font face="arial, sans-serif"><span style="font-size:12.8px"><img src="https://docs.google.com/uc?export=download&amp;id=0B1-dY3m3XHQnOFBOM0NSODNoUUU&amp;revid=0B1-dY3m3XHQnUmp1c0ZWTGovR2pYbGZCenlOU29DY3ByQU5RPQ" style="font-size: 12.8px;"></span></font><span style="font-family:arial,sans-serif;font-size:12.8px"> </span><font face="arial, sans-serif"><span style="font-size:12.8px">sahibzada_irfanullah</span></font></div></div></div></div></div>

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, back to index

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-23  2:43 Netlink socket returns NULL in vmx.c kernel file Irfan Ullah (울라 이르판)
2019-10-23  5:38 ` Valdis Klētnieks
2019-11-05  3:29   ` Irfan Ullah (울라 이르판)
2019-11-05  7:22     ` Greg KH
2019-11-05  8:59       ` Irfan Ullah (울라 이르판)
2019-11-05 10:43         ` Valdis Klētnieks
2019-11-08  4:54           ` Irfan Ullah (울라 이르판)

Kernel Newbies archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org
	public-inbox-index kernelnewbies

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git