GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-116017: Put JIT code and data on the same page by brandtbucher · Pull Request #116845 · python/cpython

Conversation

@brandtbucher

@brandtbucher brandtbucher commented Mar 14, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Instead of marking code as read-exec and data as read-only, put both on the same read-exec pages. This halves the amount of memory used for short traces, and also results in half as many expensive mprotect calls per trace.

3% faster (10% faster startup), 9% less memory (12% less at startup).

The next step after this will be sharing pages between traces, to further reduce the amount of wasted memory. At the same time, we can expose a way to compile multiple traces at once with a single mprotect call (useful for the cold exit array).

  • Issue: Compiling tiny traces wastes lots of memory #116017

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner, erlend-aasland, and scastlara reacted with rocket emoji
All reactions
  • 🚀 3 reactions
brandtbucher added 6 commits February 29, 2024 15:31
@brandtbucher
Use one mprotect call instead of two
84bbac4
@brandtbucher
Don't use separate pages for code and data
4f21fe2
@brandtbucher
Fix alignment issues
9217066
@brandtbucher
Add missing alignment check
41455df
@brandtbucher
Catch up with main
6efff81
@brandtbucher
Clean up alignement calculation
36c6c78
@brandtbucher brandtbucher added performance Performance or resource usage skip news interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 14, 2024
@brandtbucher
brandtbucher requested a review from markshannon March 14, 2024 22:56
@brandtbucher brandtbucher self-assigned this Mar 14, 2024
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 14, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Mar 14, 2024
Compiling tiny traces wastes lots of memory #116017
Open
@brandtbucher

brandtbucher commented Mar 14, 2024

Copy link
Copy Markdown
Member Author

This also fixes an issue where we weren't using the correct alignment during parts of the build process and things just accidentally worked (due to the data starting on a page boundary).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher
Be extra paranoid
26c31d9
mdboom
mdboom reviewed Mar 15, 2024
View reviewed changes
Comment thread Python/jit.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher
total_size
e6d8e6d
@brandtbucher brandtbucher mentioned this pull request Mar 15, 2024
GH-116422: Tier2 hot/cold splitting #116813
Merged
@mdboom

mdboom commented Mar 15, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I realise it probably makes sense to benchmark this one on macOS (since that's where we saw the greatest memory increase in #116017). I'll fire off a run right now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Mar 15, 2024

Copy link
Copy Markdown
Member Author

Oh, I already did. :) Results were less good: 1% faster, no memory impact (which is a bit odd, but I haven't really dug into it yet).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hartwork

hartwork commented Mar 16, 2024

Copy link
Copy Markdown
Contributor

Instead of marking code as read-exec and data as read-only, put both on the same read-exec pages.

@brandtbucher I may be missing something here: is this a reduction in security? Has this been discussed with the security team?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Mar 18, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Instead of marking code as read-exec and data as read-only, put both on the same read-exec pages.

@brandtbucher I may be missing something here: is this a reduction in security? Has this been discussed with the security team?

I should clarify: this change doesn't mean that we're now executing arbitrary user data. In this case, "data" is stuff like C string literals for error messages, pointers to cached objects, static C function addresses, version numbers, opargs, etc.

Since this data is now executable (as it lives on the same pages as the machine code), in theory an attacker capable of forcing the compilation of an instruction operand that happens to have the same encoding as a machine instruction could take control of the program. However, that would require that we jump into the data at some point, something which never happens during normal operation. As such, this would require a separate, highly specific bug in machine code generation in order to actually exploit.

The most likely way this would happen is not by jumping from the machine code into the data, but rather accidentally running off the end of the machine code into the data portion of the buffer (again, not something that happens with the sort of well-formed traces that tier two creates). This situation currently crashes on main, but would indeed begin executing the "read-only" data with the proposed change. However, this patch mitigates this risk by always adding a _FATAL_ERROR block to the end of the code portion of the trace, assuring that even if we did have a malformed trace, the program would abort before it actually overran the buffer.

I appreciate your comment, though. Please let me know if I've missed anything, or if you still have any concerns.

stonebig reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher
brandtbucher merged commit 2c82592 into python:main Mar 19, 2024
@bedevere-app bedevere-app Bot removed the awaiting core review label Mar 19, 2024
vstinner pushed a commit to vstinner/cpython that referenced this pull request Mar 20, 2024
@brandtbucher @vstinner
pythonGH-116017: Put JIT code and data on the same page (pythonGH-116845
318e6f1
)
@mdboom

mdboom commented Mar 20, 2024

Copy link
Copy Markdown
Contributor

@brandtbucher: Just FYI: a 20% reduction in memory usage on macOS on the benchmarking suite: https://github.com/faster-cpython/benchmarking-public/blob/main/results/bm-20240315-3.13.0a5%2B-e6d8e6d-JIT/bm-20240315-darwin-arm64-brandtbucher-justin_mprotect-3.13.0a5%2B-e6d8e6d-vs-base-mem.png

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Mar 20, 2024

Copy link
Copy Markdown
Member Author

Awesome! Wonder why it didn't come through in my first run...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdboom

mdboom commented Mar 20, 2024

Copy link
Copy Markdown
Contributor

The compiler on that machine was still broken when you kicked it off yesterday -- I didn't truly fix it until this morning.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdboom

mdboom commented Mar 20, 2024

Copy link
Copy Markdown
Contributor

It's also a solid 1-3% faster on macOS.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdboom

mdboom commented Mar 20, 2024

Copy link
Copy Markdown
Contributor

The compiler on that machine was still broken when you kicked it off yesterday -- I didn't truly fix it until this morning.

Oh, I see you mean the run from 5 days ago. Yeah, it's not clear why it shows "no change". Something to keep an eye on for the future in case there is a bug somewhere in the benchmarking infra (but not clear what it would be).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
@brandtbucher @adorilson
pythonGH-116017: Put JIT code and data on the same page (pythonGH-116845
62ef665
)
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@brandtbucher @diegorusso
pythonGH-116017: Put JIT code and data on the same page (pythonGH-116845
6b82138
)
@brandtbucher brandtbucher added the topic-JIT label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@mdboom mdboom mdboom left review comments
@markshannon markshannon Awaiting requested review from markshannon

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@brandtbucher brandtbucher

Labels

interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage skip news topic-JIT

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@brandtbucher @mdboom @hartwork
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.