All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Németh Márton" <nm127@freemail.hu>
To: LKML <linux-kernel@vger.kernel.org>
Subject: Re: pair of alloc_disk()? + OOPS
Date: Thu, 08 Jan 2009 06:56:37 +0100	[thread overview]
Message-ID: <49659595.8020805@freemail.hu> (raw)
In-Reply-To: <496444F9.2020202@freemail.hu>

[-- Attachment #1: Type: text/plain, Size: 4325 bytes --]

Németh Márton wrote:
> Hi,
> 
> I cannot find free_disk() or similar function which takes "struct gendisk"
> as a parameter and can do the reverse thing what alloc_disk() do.
> 
> Could you please give me a hint what would be the correct function call
> in the following example in test_exit_module() so the module do not leak?
> 
> 
> #include <linux/module.h>
> #include <linux/genhd.h>
> 
> MODULE_AUTHOR("Márton Németh <nm127@freemail.hu>");
> MODULE_DESCRIPTION("Test alloc_disk");
> MODULE_LICENSE("GPL");
> 
> static struct gendisk *gd_ptr;
> 
> static int test_init_module(void)
> {
>         printk(KERN_DEBUG "starting module\n");
> 
>         gd_ptr = alloc_disk(1);
>         if (!gd_ptr) {
>                 return -ENOMEM;
>         }
> 
>         printk(KERN_DEBUG "gd_ptr after alloc=%p\n", gd_ptr);
> 
>         return 0;
> }
> 
> 
> static void test_exit_module(void)
> {
>         printk(KERN_DEBUG "unloading module\n");
> 
>         /* FIXME: no free_disk(gd_ptr) is available */
> }
> 
> module_init(test_init_module);
> module_exit(test_exit_module);

I found a solution in "Linux Device Drivers, Third Edition", in chapter 16
"Block drivers" (page 468) where del_gendisk() is mentioned, so the
test_exit_module() should look like:

static void test_exit_module(void)
{
        printk(KERN_DEBUG "unloading module\n");

        del_gendisk(gd_ptr);

}

However, when I unload this module (also attached), I get the following
oops message in dmesg with kernel 2.6.28:

[ 2164.859107] starting module
[ 2164.859132] gd_ptr after alloc=f60f2a30
[ 2246.611974] unloading module
[ 2246.612648] BUG: unable to handle kernel NULL pointer dereference at 00000164
[ 2246.612666] IP: [<c0172266>] bdi_unregister+0x6/0x30
[ 2246.612684] *pde = 00000000
[ 2246.612694] Oops: 0000 [#1] PREEMPT
[ 2246.612703] last sysfs file: /sys/class/power_supply/BAT0/charge_full
[ 2246.612711] Modules linked in: test_alloc_disk(-) nls_iso8859_2 nls_cp437 vfat fat nls_base sd_mod usb_storage scsi_mod ppdev lp cpufreq_ondemand
cpufreq_conservative ipv6 xt_tcpudp iptable_filter ip_tables x_tables leds_clevo_mail led_class via via_agp drm agpgart eeprom snd_pcm_oss snd_mixer_oss
cpufreq_userspace cpufreq_powersave powernow_k8 fan snd_via82xx snd_via82xx_modem snd_mpu401_uart snd_seq_midi snd_seq_midi_event pcmcia firmware_class
snd_ac97_codec ac97_bus mousedev snd_rawmidi snd_pcm snd_seq snd_timer snd_seq_device ide_cd_mod snd i2c_viapro yenta_socket rsrc_nonstatic 8139too psmouse
serio_raw k8temp hwmon pcspkr cdrom snd_page_alloc soundcore 8250_pnp i2c_core ehci_hcd uhci_hcd pcmcia_core usbcore mii bitrev crc32 video backlight output
8250 serial_core parport_pc parport battery ac thermal button processor evdev
[ 2246.612866]
[ 2246.612874] Pid: 5121, comm: rmmod Not tainted (2.6.28 #3) K8N800
[ 2246.612881] EIP: 0060:[<c0172266>] EFLAGS: 00210206 CPU: 0
[ 2246.612889] EIP is at bdi_unregister+0x6/0x30
[ 2246.612895] EAX: 000000fc EBX: 000000fc ECX: f60f2b48 EDX: c03cb301
[ 2246.612901] ESI: f60f2a30 EDI: f60f2c78 EBP: d348df10 ESP: d348df0c
[ 2246.612908]  DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 0068
[ 2246.612915] Process rmmod (pid: 5121, ti=d348c000 task=d372c860 task.ti=d348c000)
[ 2246.612920] Stack:
[ 2246.612924]  f60f2a30 d348df1c c02347f6 00000000 d348df40 c01cd344 f60f2a30 00000000
[ 2246.612941]  00000000 00000003 00000000 f821d400 00000000 d348df4c f821d01c f821d088
[ 2246.612959]  d348dfb0 c014f1e8 f821d40c 74736574 6c6c615f 645f636f 006b7369 ffffffff
[ 2246.612979] Call Trace:
[ 2246.612983]  [<c02347f6>] ? unlink_gendisk+0x26/0x50
[ 2246.612995]  [<c01cd344>] ? del_gendisk+0x84/0xd0
[ 2246.613009]  [<f821d01c>] ? test_exit_module+0x1c/0x20 [test_alloc_disk]
[ 2246.613025]  [<c014f1e8>] ? sys_delete_module+0x158/0x220
[ 2246.613028]  [<c0103407>] ? sysenter_exit+0xf/0x16
[ 2246.613028]  [<c01033d9>] ? sysenter_do_call+0x12/0x31
[ 2246.613028] Code: 00 00 c7 40 24 00 00 00 00 c7 40 28 00 00 00 00 c7 40 58 00 00 00 00 83 c0 2c e8 c6 9b 0c 00 5d c3 8d 74 26 00 55 89 e5 53 89 c3 <8b> 40 68
85 c0 74 1f 8b 43 70 e8 3b 8d 0a 00 8b 43 6c e8 33 8d
[ 2246.613028] EIP: [<c0172266>] bdi_unregister+0x6/0x30 SS:ESP 0068:d348df0c
[ 2246.613224] ---[ end trace 8f06b3a64120ba58 ]---

Do anybody know what is wrong with the attached simple example module?

Regards,

	Márton Németh

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: test_alloc_disk.c --]
[-- Type: text/x-csrc; name="test_alloc_disk.c", Size: 582 bytes --]


#include <linux/module.h>
#include <linux/genhd.h>

MODULE_AUTHOR("Márton Németh <nm127@freemail.hu>");
MODULE_DESCRIPTION("Test alloc_disk");
MODULE_LICENSE("GPL");

static struct gendisk *gd_ptr;

static int test_init_module(void)
{
	printk(KERN_DEBUG "starting module\n");

	gd_ptr = alloc_disk(1);
	if (!gd_ptr) {
		return -ENOMEM;
	}

	printk(KERN_DEBUG "gd_ptr after alloc=%p\n", gd_ptr);

	return 0;
}


static void test_exit_module(void)
{
	printk(KERN_DEBUG "unloading module\n");

	del_gendisk(gd_ptr);

}

module_init(test_init_module);
module_exit(test_exit_module);

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


ifneq ($(KERNELRELEASE),)

obj-m	:= test_alloc_disk.o
else
KDIR	:= /lib/modules/$(shell uname -r)/build
PWD	:= $(shell pwd)

default:
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
endif

  reply	other threads:[~2009-01-08  5:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-07  6:00 pair of alloc_disk()? Németh Márton
2009-01-08  5:56 ` Németh Márton [this message]
2009-01-08  6:27   ` pair of alloc_disk()? + OOPS Németh Márton

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=49659595.8020805@freemail.hu \
    --to=nm127@freemail.hu \
    --cc=linux-kernel@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.