From: Amit Choudhary <amit2030@yahoo.com>
To: Vadim Lobanov <vlobanov@speakeasy.net>
Cc: Christoph Hellwig <hch@infradead.org>,
Linux Kernel <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] include/linux/slab.h: new KFREE() macro.
Date: Sun, 7 Jan 2007 20:09:58 -0800 (PST) [thread overview]
Message-ID: <60237.99836.qm@web55612.mail.re4.yahoo.com> (raw)
In-Reply-To: <1168223734.5259.12.camel@dsl081-166-245.sea1.dsl.speakeasy.net>
--- Vadim Lobanov <vlobanov@speakeasy.net> wrote:
>
> struct type1 {
> /* something */
> };
>
> struct type2 {
> /* something */
> };
>
> #define COUNT 10
>
> void function1(struct type1 **a1, struct type2 **a2, unsigned int sz);
>
> void function2(void)
> {
> struct type1 *arr1[COUNT];
> struct type2 *arr2[COUNT];
> int i;
>
> /* init */
> for (i = 0; i < COUNT; i++) {
> arr1[i] = kmalloc(sizeof(struct type1), ...);
> if (!arr1[i])
> goto free_1;
> }
> for (i = 0; i < COUNT; i++) {
> arr2[i] = kmalloc(sizeof(struct type2), ...);
> if (!arr2[i])
> goto free_2;
> }
>
> /* work */
> function1(arr1, arr2, COUNT);
>
> /* fini */
> i = COUNT;
> free_2:
> for (i--; i >= 0; i--) {
> kfree(arr2[i]);
> }
> i = COUNT;
> free_1:
> for (i--; i >= 0; i--) {
> kfree(arr1[i]);
> }
> }
>
> In most cases, though, the above code design would be brain-damaged from
> the start: unless the sizes involved are prohibitively large, the
> function should be allocating all the memory in a single pass.
>
> So, where's the demonstrated need for KFREE()?
I have already explained it earlier. I will try again. You will not need free_2: and free_1: with
KFREE(). You will only need one free: with KFREE.
Also, let's say that count is different for each array? Then how do you propose that memory be
allocated in one pass?
>In most cases, though, the above code design would be brain-damaged from
>the start: unless the sizes involved are prohibitively large, the
>function should be allocating all the memory in a single pass.
Well, only if everything would be fine and correct, we would not be needing anything. If you think
this kind of code is brain-damaged then the linux kernel has couple of these.
I have scanned the whole kernel to check whether people are checking for return values of kmalloc,
I found that at many places they don't and have sent patches for them. Now, this too is brain
damaged code. And during the scan I saw examples of what I described earlier.
KFREE() can fix some of those cases.
Consider this as the proof of what I explained earlier. This fucntion was broken but I fixed it
and then realized why KFREE() is needed. 2 kmallocs and 1 usb_alloc_urb(). Well, I can only give
examples why KFREE() is needed. If you do not agree, I cannot force you to agree with me. And if
you really do not want to agree then even my examples will fail. Also, if you think that people
are not doing KFREE() kind of stuff then you should scan the kernel and you will see it for
yourself.
Below are some examples where people are doing KFREE() kind of stuff:
--
arch/ppc/kernel/smp-tbsync.c: kfree( tbsync );
arch/ppc/kernel/smp-tbsync.c- tbsync = NULL;
--
arch/ppc/8260_io/fcc_enet.c: dev_kfree_skb(fep->tx_skbuff[i]);
arch/ppc/8260_io/fcc_enet.c- fep->tx_skbuff[i] = NULL;
--
arch/ppc/8xx_io/cs4218_tdm.c: kfree (sound_buffers);
arch/ppc/8xx_io/cs4218_tdm.c- sound_buffers = 0;
--
arch/ia64/kernel/topology.c: kfree(all_cpu_cache_info[cpu].cache_leaves);
arch/ia64/kernel/topology.c- all_cpu_cache_info[cpu].cache_leaves = NULL;
--
arch/ia64/kernel/acpi.c: kfree(buffer.pointer);
arch/ia64/kernel/acpi.c- buffer.pointer = NULL;
--
arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c: kfree(acpi_perf_data[j]);
arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c- acpi_perf_data[j] = NULL;
--
There are many more and you can scan the kernel yourself.
Below is where memory is allocated in different arrays with different counts:
static int stv680_start_stream (struct usb_stv *stv680)
{
struct urb *urb;
int err = 0, i;
stv680->streaming = 1;
/* Do some memory allocation */
for (i = 0; i < STV680_NUMFRAMES; i++) {
stv680->frame[i].data = stv680->fbuf + i * stv680->maxframesize;
stv680->frame[i].curpix = 0;
}
/* packet size = 4096 */
for (i = 0; i < STV680_NUMSBUF; i++) {
stv680->sbuf[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
if (stv680->sbuf[i].data == NULL) {
PDEBUG (0, "STV(e): Could not kmalloc raw data buffer %i", i);
goto nomem_err;
}
}
stv680->scratch_next = 0;
stv680->scratch_use = 0;
stv680->scratch_overflow = 0;
for (i = 0; i < STV680_NUMSCRATCH; i++) {
stv680->scratch[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
if (stv680->scratch[i].data == NULL) {
PDEBUG (0, "STV(e): Could not kmalloc raw scratch buffer %i", i);
goto nomem_err;
}
stv680->scratch[i].state = BUFFER_UNUSED;
}
for (i = 0; i < STV680_NUMSBUF; i++) {
urb = usb_alloc_urb (0, GFP_KERNEL);
if (!urb)
goto nomem_err;
/* sbuf is urb->transfer_buffer, later gets memcpyed to scratch */
usb_fill_bulk_urb (urb, stv680->udev,
usb_rcvbulkpipe (stv680->udev, stv680->bulk_in_endpointAddr),
stv680->sbuf[i].data, stv680->rawbufsize,
stv680_video_irq, stv680);
stv680->urb[i] = urb;
err = usb_submit_urb (stv680->urb[i], GFP_KERNEL);
if (err)
PDEBUG (0, "STV(e): urb burned down in start stream");
} /* i STV680_NUMSBUF */
stv680->framecount = 0;
return 0;
nomem_err:
for (i = 0; i < STV680_NUMSCRATCH; i++) {
kfree(stv680->scratch[i].data);
stv680->scratch[i].data = NULL;
}
for (i = 0; i < STV680_NUMSBUF; i++) {
usb_kill_urb(stv680->urb[i]);
usb_free_urb(stv680->urb[i]);
stv680->urb[i] = NULL;
kfree(stv680->sbuf[i].data);
stv680->sbuf[i].data = NULL;
}
return -ENOMEM;
}
static int stv680_stop_stream (struct usb_stv *stv680)
{
int i;
if (!stv680->streaming || !stv680->udev)
return 1;
stv680->streaming = 0;
for (i = 0; i < STV680_NUMSBUF; i++)
if (stv680->urb[i]) {
usb_kill_urb (stv680->urb[i]);
usb_free_urb (stv680->urb[i]);
stv680->urb[i] = NULL;
kfree(stv680->sbuf[i].data);
}
for (i = 0; i < STV680_NUMSCRATCH; i++) {
kfree(stv680->scratch[i].data);
stv680->scratch[i].data = NULL;
}
return 0;
}
-Amit
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
next prev parent reply other threads:[~2007-01-08 4:10 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-01-07 8:46 Amit Choudhary
2007-01-07 10:24 ` Christoph Hellwig
2007-01-07 22:43 ` Amit Choudhary
2007-01-07 23:22 ` Vadim Lobanov
2007-01-08 0:02 ` Amit Choudhary
2007-01-08 2:35 ` Vadim Lobanov
2007-01-08 4:09 ` Amit Choudhary [this message]
2007-01-08 7:04 ` Vadim Lobanov
2007-01-08 7:29 ` Amit Choudhary
2007-01-08 8:15 ` Vadim Lobanov
2007-01-08 8:47 ` Amit Choudhary
2007-01-08 9:09 ` Al Viro
2007-01-08 7:49 ` Hua Zhong
2007-01-08 8:00 ` Pekka Enberg
2007-01-08 8:31 ` Amit Choudhary
2007-01-08 8:37 ` Al Viro
2007-01-08 8:39 ` Sumit Narayan
2007-01-08 8:44 ` Robert P. J. Day
2007-01-08 8:56 ` Amit Choudhary
2007-01-08 8:45 ` Pekka Enberg
2007-01-08 9:06 ` Amit Choudhary
2007-01-08 9:26 ` Pekka Enberg
2007-01-08 22:43 ` Valdis.Kletnieks
2007-01-09 19:02 ` Amit Choudhary
2007-01-09 19:19 ` Randy Dunlap
2007-01-10 4:57 ` Amit Choudhary
2007-01-09 22:57 ` Valdis.Kletnieks
2007-01-10 0:00 ` Amit Choudhary
2007-01-10 2:43 ` Valdis.Kletnieks
2007-01-08 11:10 ` Jesper Juhl
2007-01-08 8:05 ` Amit Choudhary
2007-01-08 8:12 ` Al Viro
2007-01-08 8:57 ` Hua Zhong
-- strict thread matches above, loose matches on Subject: below --
2007-07-23 17:55 Amit Choudhary
[not found] <7ADs5-25a-11@gated-at.bofh.it>
[not found] ` <7AP02-3l3-13@gated-at.bofh.it>
2007-01-08 18:29 ` Bodo Eggert
2007-01-01 0:17 Amit Choudhary
2007-01-01 3:01 ` Segher Boessenkool
2007-01-01 21:23 ` Pekka Enberg
2007-01-02 9:21 ` Christoph Hellwig
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=60237.99836.qm@web55612.mail.re4.yahoo.com \
--to=amit2030@yahoo.com \
--cc=hch@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=vlobanov@speakeasy.net \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome