Skip to content

performEvictions: mem_freed may be negative#7908

Merged
oranagra merged 1 commit intoredis:unstablefrom
guybe7:fix_mem_freed
Oct 13, 2020
Merged

performEvictions: mem_freed may be negative#7908
oranagra merged 1 commit intoredis:unstablefrom
guybe7:fix_mem_freed

Conversation

@guybe7
Copy link
Collaborator

@guybe7 guybe7 commented Oct 13, 2020

If 'delta' is negative 'mem_freed' may underflow and cause
the while loop to exit prematurely (And not evicting enough
memory)

mem_freed can be negative when:

  1. We use lazy free (consuming memory by appending to a list)
  2. Thread doing an allocation between the two calls to zmalloc_used_memory.

@oranagra
Copy link
Member

@guybe7 can you describe how it can become negative (should at least be stated for the record)?
i'm assuming it either involves lazy free (consuming memory by appending to a list).
or another thread doing an allocation between the two calls to zmalloc_used_memory.
anything else?

@guybe7
Copy link
Collaborator Author

guybe7 commented Oct 13, 2020

@oranagra i can't think of any other cases

src/evict.c Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to consider a case where on 32bit system, redis is eating more than 2GB of ram, and decided it needs to free most of it (i.e mem_tofree > 2gb)?
in which case, maybe mem_freed needs to be long long? or not count negative deltas?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the right thing to do is to change it to long long instead of ssize_t
should i do it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hold on..
if there's a small sporadic negative delta, that's not that bad.. pretty soon other positive deltas will overcome it and mem_freed will become positive again, right?
so it's only a problem if it consistently gets negative deltas, right?

but another look at the code it actually seems that the initial report is wrong.
in case of negative delta it'll just exit immediately (not keep running the loop for a lot more than it should). making my claim above (about positive deltas eventually overcoming the rare negative delta) wrong.

is that right? if it is, then yes. let's make it long long and update the comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems you're right... we're not talking about over-eviction we're talking about reaching OOM
i'll change to long long and update the comment

If 'delta' is negative 'mem_freed' may underflow and cause
the while loop to exit prematurely (And not evicting enough
memory)

mem_freed can be negative when:
1. We use lazy free (consuming memory by appending to a list)
2. Thread doing an allocation between the two calls to zmalloc_used_memory.
@guybe7
Copy link
Collaborator Author

guybe7 commented Oct 13, 2020

@oranagra done

@oranagra oranagra merged commit 37fd3d4 into redis:unstable Oct 13, 2020
JackieXie168 pushed a commit to JackieXie168/redis that referenced this pull request Nov 4, 2020
If 'delta' is negative 'mem_freed' may underflow and cause
the while loop to exit prematurely (And not evicting enough
memory)

mem_freed can be negative when:
1. We use lazy free (consuming memory by appending to a list)
2. Thread doing an allocation between the two calls to zmalloc_used_memory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants