There still are secure units in psychiatric hospitals. There are medium secure units in many hospitals around the UK, and these are units with very tight security where the patients can't come and go as they like (although there are cases every so often of patients absconding - the mental health system is at breaking point, so that's not surprising). Many of the patients in MSUs have been convicted of serious offences and either transferred from prison because they are obviously mentally ill, or sent to hospital instead of prison in the first instance. They are monitored by the Ministry of Justice. Others have a history of violence, arson, sex offences etc as well as a diagnosed mental illness, and this should feed into the risk assessment done before deciding whether to move them into lower security in-patient units, or back to the community, if they seem to be improving. If they get worse there is the possibility of a transfer to a high-security hospital. In England there are three such hospitals: Broadmoor, Ashworth and Rampton. In Scotland there's Carstairs.
I am not a mental health professional but I used to work with many and visited a medium secure unit for work purposes several times. It's not a picnic being an in-patient in a place like that. Most of my colleagues had worked at Broadmoor and that sounded like a very grim place indeed. The security is exceptionally tight and it's not a remotely normal life for anyone there.
Decades ago when there a lot more in-patient beds in psychiatric hospitals most wards were not locked. Care in the community has failed because there has never been enough money put into mental health. The money made from selling off the old asylums should have been ringfenced for care in the community. I don't imagine it was.
I believe it is an established fact that most people with mental illnesses are not violent and are actually at greater risk of experiencing violence than the population as a whole.