We used a toddler bed rail for a few years on the top bunk. Something like This Munchkin. Find one the size that fits your opening and you'll be set.
A & A parents of DD 2005, DS1 2007, DS2 2009 2011 Suburban 2500 6.0L 3.73 pulling 2011 Heartland North Trail 28BRS 2017 Subaru Outback 3.6R 2x 2023 Chevrolet Bolt EUV (Gray and Black Twins)
Our lower bunk is a futon mattress setup that can be easily removed. We set up a pack-n-play play pen in its place which worked well. You can also use it outside other times.
'17 Ram 2500 Crewcab Laramie CTD '13 Keystone Bullet Premier 310BHPR Hitched by Hensley
I know, I know, it's a pet gate, But the same company that manufactures pet gates also manufactures the same gates for toddlers and charge more money for them.
I bought a similar pet gate to pen the family Chihuahua in the bathroom when we were not at home. I didn't use the wall attachments because it friction fit tight enough between the door jambs. I purchased mine as a pet gate and found the same exact one in a 2 pack at BJ's Wholesale Club sold as child gates in two different colors.