linux-staging.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Xiaoke Wang <xkernel.wang@foxmail.com>
Cc: "larry.finger" <Larry.Finger@lwfinger.net>,
	phil <phil@philpotter.co.uk>, gregkh <gregkh@linuxfoundation.org>,
	linux-staging <linux-staging@lists.linux.dev>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: Re: [PATCH 2/2] staging: r8188eu: fix potential memory leak in _rtw_init_xmit_priv()
Date: Thu, 31 Mar 2022 12:20:13 +0300	[thread overview]
Message-ID: <20220331092013.GC12805@kadam> (raw)
In-Reply-To: <tencent_7E870E856738AA52C5FF04C81D735AEE1206@qq.com>

On Thu, Mar 31, 2022 at 04:33:37PM +0800, Xiaoke Wang wrote:
> 
> On Thu, 31 Mar 2022 16:21:43 +0800, xkernel.wang@foxmail.com wrote:
> >In fact, this is considering that we do not know where is the failure
> >from. In rtw_os_xmit_resource_alloc(), the failure can from 
> >
> >> pxmitbuf->pallocated_buf = kzalloc(alloc_sz, GFP_KERNEL);
> >
> > , but also can from 
> >
> >> 		pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
> >
> > So if we do not handle the current failed item and just skip it, then some
> > memory may be ignored.
> 
> 
> 
> Maybe this should be the problem of rtw_os_xmit_resource_alloc() as it
> does properly handle the error from it.
> Anyway, I will attempt to fix them.
> 

It would be a lot easier to fix this code, if it were cleaner.

It goes to a lot of work to ensure that the pointers are aligned at the
4 byte mark, but memory from kmalloc() or vmalloc() is already aligned
at 8 bytes so there is no need.

	pxmitpriv->pallocated_frame_buf = vzalloc(NR_XMITFRAME * sizeof(struct xmit_frame) + 4);

The + 4 is for alignment.

	pxmitpriv->pxmit_frame_buf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitpriv->pallocated_frame_buf), 4);

So then it stores pxmitpriv->pxmit_frame_buf which is the aligned
version of the pointer and the ->pallocated_frame_buf pointer which it
uses to free the memory.  Delete pxmitpriv->pallocated_frame_buf.  Just
allocate it directly.  Use vcalloc().

	pxmitpriv->pxmit_frame_buf = vzalloc(NR_XMITFRAME * sizeof(struct xmit_frame);

That stuff can all be done in one patch because it is:

Patch 1: Remove unnecessary alignment hacks.

This code goes to great lengths to ensure that the buffers are 4 bytes
aligned.  However that is not necessary as the vmalloc() and kmalloc()
functions return memory that is already aligned at 8 bytes.
1) No need for the temporary pxmitpriv->pallocated_frame_buf pointer to
   store unaligned memory.
2) No need to allocate "+ 4" extra bytes for alignment.
3) No need to call N_BYTE_ALIGMENT().

Patch 2: Use vcalloc() instead of vzalloc()
Patch 3: Declare the pxmitpriv->pxmit_frame_buf pointer as an array of
structs instead of an array of u8.  This allows us to remove some
casting.

Once the code is cleaner then the error handling will also be easier.

regards,
dan carpenter

  reply	other threads:[~2022-03-31  9:20 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-30 15:16 [PATCH 1/2] staging: r8188eu: properly handle the kzalloc() xkernel.wang
2022-03-30 15:29 ` [PATCH 2/2] staging: r8188eu: fix potential memory leak in _rtw_init_xmit_priv() xkernel.wang
2022-03-31  6:04   ` Greg KH
2022-03-31  7:35   ` Dan Carpenter
2022-03-31  8:21     ` Xiaoke Wang
2022-03-31  8:49       ` Dan Carpenter
     [not found]     ` <2022033116214474301568@foxmail.com>
2022-03-31  8:33       ` Xiaoke Wang
2022-03-31  9:20         ` Dan Carpenter [this message]
2022-03-31  6:03 ` [PATCH 1/2] staging: r8188eu: properly handle the kzalloc() Greg KH
2022-03-31  6:37 ` Dan Carpenter

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=20220331092013.GC12805@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=Larry.Finger@lwfinger.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=phil@philpotter.co.uk \
    --cc=xkernel.wang@foxmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).