New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bugfix and refactor device increate count #8782
Conversation
Apokleos
commented
Jan 8, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! A few comments here.
return Err(anyhow!("attach count increased failed as some reason.")); | ||
warn!( | ||
sl!(), | ||
"It's not allowed that device {:?} is attached many times,", self.device_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is more concise.
"It's not allowed that device {:?} is attached many times,", self.device_id | |
"The device {:?} is not allowed to be attached multiple times.", self.device_id |
0 => { | ||
// When ref_count is 0, it indicates that the device is new here, In this case, we know that the device | ||
// has not been attached into the Guest. | ||
*ref_count += 1; | ||
Ok(false) | ||
} | ||
// While in practice, the number of times a device is attempted to be inserted into the Guest cannot reach | ||
// this maximum value, the device is theoretically allowed to do so. | ||
// However, we will not allow the device to actually be inserted into the Guest more than once. | ||
std::u64::MAX => Err(anyhow!("device was attached too many times")), | ||
_ => { | ||
// When ref_count is greater than 0, it indicates that how many times the device has been attempted to | ||
// be inserted into the Guest. | ||
// In this case, we know the device has been attached into the Guest. | ||
*ref_count += 1; | ||
Ok(true) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using checked_add()
? FYI, https://doc.rust-lang.org/std/primitive.u32.html#method.checked_add.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx @justxuewei Although I know that the checked_add()
method is a good choice in some cases, I don't think it's appropriate here. And I don't want to introduce too much judgment logic because of this method, so I choose to stick to the original approach.
Another reason is, the code are just moved here from its original places, and the key point is, simple and it works well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@justxuewei Maybe saturating_add(1)/saturating_sub(1) is a good choice !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, It's indeed better to refactor it with check_add as @justxuewei said:
checked_add(1).ok_or(anyhow!("error msg")?;
return Ok(*ref_count != 1))
} | ||
|
||
// do_decrease_count | ||
pub fn do_decrease_count(ref_count: &mut u64) -> Result<bool> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
@@ -69,9 +69,55 @@ pub(crate) fn get_virt_drive_name(mut index: i32) -> Result<String> { | |||
Ok(String::from(PREFIX) + std::str::from_utf8(&disk_letters)?) | |||
} | |||
|
|||
// do_increase_count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove it or expand on this, as this comment doesn't provide any valuable information.
} | ||
} | ||
|
||
// do_decrease_count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
c38c8ac
to
be20973
Compare
dc81a77
to
7b7153a
Compare
When there's a pod with multiple containers, there may be case that attach point more than 2, we should not return Err in that case when we are doing attach ops, but just return Ok. Fixes: kata-containers#8738 Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
37b4796
to
25ed1c2
Compare
/test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm, thanks! @Apokleos
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, a few comments.
25ed1c2
to
9b8b5be
Compare
Since there are many implementations of reference counting in the drivers, all of which have the same implementation, we should try to reduce such duplicated code as much as possible. Therefore, a new function is introduced to solve the problem of duplicated code. Fixes: kata-containers#8738 Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
Introduce a dedicated public function do_decrease_count to reduce duplicated code in drivers' decrease_attach_count. Fixes: kata-containers#8738 Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
Try to reduce duplicated code in increase_attach_count with public new function do_increase_count. Fixes: kata-containers#8738 Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
Try to reduce duplicated code in decrease_attach_count with public new function do_decrease_count. Fixes: kata-containers#8738 Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
9b8b5be
to
935a1a3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/test |