ocfs2-devel.oss.oracle.com archive mirror
 help / color / mirror / Atom feed
From: Joseph Qi <jiangqi903@gmail.com>
To: ocfs2-devel@oss.oracle.com
Subject: [Ocfs2-devel] [PATCH] ocfs2: don't evaluate buffer head to NULL managed by caller
Date: Fri, 30 Mar 2018 10:03:53 +0800	[thread overview]
Message-ID: <f08b4265-6c49-489f-6168-1ac966f96bbf@gmail.com> (raw)
In-Reply-To: <63ADC13FD55D6546B7DECE290D39E373F2958D95@H3CMLB12-EX.srv.huawei-3com.com>



On 18/3/30 09:31, Changwei Ge wrote:
> Hi Joseph,
> 
> On 2018/3/30 9:27, Joseph Qi wrote:
>>
>>
>> On 18/3/29 10:06, Changwei Ge wrote:
>>> ocfs2_read_blocks() is used to read several blocks from disk.
>>> Currently, the input argument *bhs* can be NULL or NOT. It depends on
>>> the caller's behavior. If the function fails in reading blocks from
>>> disk, the corresponding bh will be assigned to NULL and put.
>>>
>>> Obviously, above process for non-NULL input bh is not appropriate.
>>> Because the caller doesn't even know its bhs are put and re-assigned.
>>>
>>> If buffer head is managed by caller, ocfs2_read_blocks should not
>>> evaluate it to NULL. It will cause caller accessing illegal memory,
>>> thus crash.
>>>
>>> Signed-off-by: Changwei Ge <ge.changwei@h3c.com>
>>> ---
>>>   fs/ocfs2/buffer_head_io.c | 31 +++++++++++++++++++++++++------
>>>   1 file changed, 25 insertions(+), 6 deletions(-)
>>>
>>> diff --git a/fs/ocfs2/buffer_head_io.c b/fs/ocfs2/buffer_head_io.c
>>> index d9ebe11..17329b6 100644
>>> --- a/fs/ocfs2/buffer_head_io.c
>>> +++ b/fs/ocfs2/buffer_head_io.c
>>> @@ -188,6 +188,7 @@ int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,
>>>   	int i, ignore_cache = 0;
>>>   	struct buffer_head *bh;
>>>   	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
>>> +	int new_bh = 0;
>>>   
>>>   	trace_ocfs2_read_blocks_begin(ci, (unsigned long long)block, nr, flags);
>>>   
>>> @@ -213,6 +214,18 @@ int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,
>>>   		goto bail;
>>>   	}
>>>   
>>> +	/* Use below trick to check if all bhs are NULL or assigned.
>>> +	 * Basically, we hope all bhs are consistent so that we can
>>> +	 * handle exception easily.
>>> +	 */
>>> +	new_bh = (bhs[0] == NULL);
>>> +	for (i = 1 ; i < nr ; i++) {
>>> +		if ((new_bh && bhs[i]) || (!new_bh && !bhs[i])) {
>>> +			WARN(1, "Not all bhs are consistent\n");
>>> +			break;
>>> +		}
>>> +	}
>>> +
>>>   	ocfs2_metadata_cache_io_lock(ci);
>>>   	for (i = 0 ; i < nr ; i++) {
>>>   		if (bhs[i] == NULL) {
>>> @@ -324,8 +337,10 @@ int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,
>>>   		if (!(flags & OCFS2_BH_READAHEAD)) {
>>>   			if (status) {
>>>   				/* Clear the rest of the buffers on error */
>>> -				put_bh(bh);
>>> -				bhs[i] = NULL;
>>> +				if (new_bh) {
>>> +					put_bh(bh);
>>> +					bhs[i] = NULL;
>>> +				}
>>
>> Since we assume caller has to pass either all NULL or all non-NULL,
>> here we will only put bh internal allocated. Am I missing something?
> 
> Thanks for your review.
> Yes, we will only put bh internally allocated.
> If bh is reserved in advance, we will not put it and re-assign it to NULL.
> 

So this branch won't have risk, right?

Thanks,
Joseph

  reply	other threads:[~2018-03-30  2:03 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-29  2:06 [Ocfs2-devel] [PATCH] ocfs2: don't evaluate buffer head to NULL managed by caller Changwei Ge
2018-03-29  2:35 ` Gang He
2018-03-29  3:01   ` Changwei Ge
2018-03-29  3:21     ` Gang He
2018-03-29  6:25       ` Changwei Ge
2018-03-29  3:36 ` Larry Chen
2018-03-29  6:27   ` Changwei Ge
2018-03-29  9:50 ` piaojun
2018-03-29 10:32   ` Larry Chen
2018-03-29 12:04     ` Changwei Ge
2018-03-29 12:03   ` Changwei Ge
2018-03-29 21:45 ` Andrew Morton
2018-03-30  0:50   ` Changwei Ge
2018-03-30  1:26 ` Joseph Qi
2018-03-30  1:31   ` Changwei Ge
2018-03-30  2:03     ` Joseph Qi [this message]
2018-03-30  2:17       ` Changwei Ge
2018-03-30  2:37         ` Joseph Qi
2018-03-31  2:06           ` Changwei Ge
2018-03-30  2:16   ` piaojun

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=f08b4265-6c49-489f-6168-1ac966f96bbf@gmail.com \
    --to=jiangqi903@gmail.com \
    --cc=ocfs2-devel@oss.oracle.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).