We provide a microfounded framework for the welfare analysis of macroprudential policy by means of an overlapping generation model where productivity and credit supply are subject to random shocks in order to analyze rational bubbles that can be fueled by banking credit. We find that credit financed bubbles may be welfare improving because of their role as a buffer in channeling excessive credit supply and inefficient investment at the firms' level, but can cause systemic risk. Therefore macroprudential policy plays a key role in improving efficiency while preserving financial stability. Our approach allows us to compare the efficiency of alternative macroprudential policies. Contrarily to conventional wisdom, we show that macroprudential policy may be efficient even in the absence of systemic risk, that it has to be contingent on productivity shocks, to take into account real interest rates.