Kernel Newbies archive on lore.kernel.org
 help / color / Atom feed
From: "Valdis Klētnieks" <valdis.kletnieks@vt.edu>
To: Irfan Ullah (울라 이르판) <irfan@dke.khu.ac.kr>
Cc: Greg KH <greg@kroah.com>,
	Linux Kernel List <kernelnewbies@kernelnewbies.org>
Subject: Re: Netlink socket returns NULL in vmx.c kernel file
Date: Tue, 05 Nov 2019 05:43:26 -0500
Message-ID: <219753.1572950606@turing-police> (raw)
In-Reply-To: <CA+mB8Oyh-y6WYTOt0YEXkXxg0ZY+c8FuBBmGwaQkgEeuEodrGg@mail.gmail.com>

[-- 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

  reply index

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-23  2:43 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 [this message]
2019-11-08  4:54           ` Irfan Ullah (울라 이르판)

Reply instructions:

You may reply publically 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=219753.1572950606@turing-police \
    --to=valdis.kletnieks@vt.edu \
    --cc=greg@kroah.com \
    --cc=irfan@dke.khu.ac.kr \
    --cc=kernelnewbies@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

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