The paper studies the device-to-device (D2D) underlaying communication in the case of sufficient spectrum resources available in cellular networks. A hierarchical game is used to solve the problem of mode selection, spectrum allocation and power control for D2D underlaying communication. The mode selection and spectrum allocation are solved by a hedonic coalition game. A utility function based on the number of participants in the coalition is proposed to maximize the system throughput while ensuring that users have fair access to resources. After the initial formation of the coalition, a power control algorithm based on the non-cooperative game is presented to further improve the performance. The simulation results show that the algorithm can not only solve the fairness of user resource allocation, but also improve the system throughput.