Reliably predicting and estimating roof collapse in underground cavities such as mine goafs and power caverns is a serious concern in mining and geotechnical engineering fields. An analytical expression for a three-dimensional (3D) failure mode of a generic-shaped cavity crown is derived according to the upper bound theorem via limit analysis, where the Hoek-Brown yield criterion represents rock mass strength. The proposed method is validated by comparison against the extant research; the comparison also suggests that the roof profile plays a crucial role in cavity stability. Specific example is given to ellipsoidal cavity and the stability graph with respect to roof profile and cavity span is obtained and discussed. The effects of numerous factors on potential collapse are investigated based on the numerical calculation results. These results may provide workable guidelines for the optimization of roof shapes and support designs in minimizing rock detachment above cavity roofs in practice.