5V ups circuit oscillating?
TL431 is working out of the specifications, the datasheet states a minimum 0.7mA to 1mA cathode current required for the reference to work properly, see the tables listed from page 5 to 13 &quot;minimum cathode current for regulation&quot; parameter.
At a first sight R1 is way to high even before the voltage will be cut by U3. Also the cathode voltage must be at least close to the reference voltage, see the comparator example in page 21 and the table in page 22 and also your common sense on how a reference should work.
Maybe lowering the value of R1 and feeding it from the highest voltage source through two diodes might do the job.



simulate this circuit &ndash; Schematic created using CircuitLab
If your circuit works well on battery and you're worried about the higher current consumption then you could make a compromise and slightly modify the schematic to supply TL431 in parameters only when V1 is higher enough.



simulate this circuit
Update
I couldn't make your circuit work as is or with slight changes.
The voltage drop detector doesn't work as intended since M1 transistor is always open when U1 gets in the working range with cathode voltage higher than 2V.