As KMP is scanning the text, the state of the KMP shows the length of the longest prefix of the pattern that matches the text up to the current point, so you could record the maximum length seen and the point in the pattern at which it was seen, and it does look like you could use this to find a longest matching prefix of P.
Another way of doing this would be to put all prefixes of P into Aho-Corasick. The run-time behaviour would be very similar, although it would consume a little more store. It would allow you to use an existing library - if you had one for Aho-Corasick, instead of modifying a KMP implementation.