A comprehensive numerical study is performed to reveal influence of the upstream wall slope on aerodynamic properties as rarefied hypersonic flows pass through the two- and three-dimensional cavity configurations using the direct simulation Monte Carlo (DSMC) method. In this work, slope effect is obtained by the cavity upstream wall sloping upward and downward as well as bulging partially. Besides, cases of freestream with different velocities and altitudes are taken into account to examine the slope effect. It is found that cavity flow is transformed from the open type to the transitional and finally to the closed as the cavity upstream wall gradually slopes upward, promoting the heat energy exchange between gas molecules with the cavity floor, and meanwhile achieving a considerable rate of decrease of the peak heat flux on the reattachment corner. However, the upstream wall sloping downward or bulging partially seems to only have a slight effect on cavity flow and wall heat flux. It's worth noting that slope effect is closely related to velocity and altitude of the freestream, such as the greater the velocity and altitude, the stronger the slope effect. For the three-dimensional cavity configuration, influence of the upstream wall slope on cavity flow and wall heat flux is weaken considerably due to the three-dimensional effect. From the perspective of optimal design for the hypersonic thermal protection system, the cavity with an upward upstream wall is proposed because it not only reduces peak heat flux on the reattachment corner, but also decreases gas temperature inside the cavity.