2014-06-02 15:25:05 +02:00
|
|
|
/*
|
|
|
|
* Dimm device for Memory Hotplug
|
|
|
|
*
|
|
|
|
* Copyright ProfitBricks GmbH 2012
|
|
|
|
* Copyright (C) 2014 Red Hat Inc
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-23 14:44:24 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2014-06-02 15:25:05 +02:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:30 +01:00
|
|
|
#include "qemu/osdep.h"
|
2019-08-12 07:23:53 +02:00
|
|
|
#include "hw/boards.h"
|
2014-06-02 15:25:05 +02:00
|
|
|
#include "hw/mem/pc-dimm.h"
|
2019-08-12 07:23:51 +02:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2018-03-11 04:02:12 +01:00
|
|
|
#include "hw/mem/nvdimm.h"
|
2018-04-23 18:51:16 +02:00
|
|
|
#include "hw/mem/memory-device.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2014-06-02 15:25:05 +02:00
|
|
|
#include "qapi/visitor.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2019-08-12 07:23:55 +02:00
|
|
|
#include "sysemu/hostmem.h"
|
2015-02-08 19:51:16 +01:00
|
|
|
#include "sysemu/numa.h"
|
2015-06-29 10:20:23 +02:00
|
|
|
#include "trace.h"
|
2015-01-27 05:05:01 +01:00
|
|
|
|
2018-06-19 15:41:33 +02:00
|
|
|
static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
|
|
|
|
|
2021-04-25 14:11:36 +02:00
|
|
|
static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp)
|
|
|
|
{
|
|
|
|
if (!dimm->hostmem) {
|
|
|
|
error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return host_memory_backend_get_memory(dimm->hostmem);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:20:12 +02:00
|
|
|
void pc_dimm_pre_plug(PCDIMMDevice *dimm, MachineState *machine,
|
2018-08-01 15:34:44 +02:00
|
|
|
const uint64_t *legacy_align, Error **errp)
|
2018-08-01 15:34:41 +02:00
|
|
|
{
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int slot;
|
|
|
|
|
2018-10-05 11:20:12 +02:00
|
|
|
slot = object_property_get_int(OBJECT(dimm), PC_DIMM_SLOT_PROP,
|
2018-08-01 15:34:41 +02:00
|
|
|
&error_abort);
|
pc-dimm: fix crash when invalid slot number is used
QEMU will crash with:
Segmentation fault (core dumped)
when negative slot number is used, ex:
qemu-system-x86_64 -m 1G,maxmem=20G,slots=256 \
-object memory-backend-ram,id=mem1,size=1G \
-device pc-dimm,id=dimm1,memdev=mem1,slot=-2
fix it by checking that slot number is within valid range.
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Message-Id: <20190723160859.27250-1-imammedo@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Li Qiang <liq3ea@gmail.com>
Signed-off-by: Igor Mammedov <<a href="mailto:imammedo@redhat.com" target="_blank">imammedo@redhat.com</a>><br></blockquote><div><br></div><div>Reviewed-by: Li Qiang <<a href="mailto:liq3ea@gmail.com">liq3ea@gmail.com</a>><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Reviewed-by: Pankaj Gupta <pagupta@redhat.com>
2019-07-23 18:08:59 +02:00
|
|
|
if ((slot < 0 || slot >= machine->ram_slots) &&
|
|
|
|
slot != PC_DIMM_UNASSIGNED_SLOT) {
|
error: Avoid unnecessary error_propagate() after error_setg()
Replace
error_setg(&err, ...);
error_propagate(errp, err);
by
error_setg(errp, ...);
Related pattern:
if (...) {
error_setg(&err, ...);
goto out;
}
...
out:
error_propagate(errp, err);
return;
When all paths to label out are that way, replace by
if (...) {
error_setg(errp, ...);
return;
}
and delete the label along with the error_propagate().
When we have at most one other path that actually needs to propagate,
and maybe one at the end that where propagation is unnecessary, e.g.
foo(..., &err);
if (err) {
goto out;
}
...
bar(..., &err);
out:
error_propagate(errp, err);
return;
move the error_propagate() to where it's needed, like
if (...) {
foo(..., &err);
error_propagate(errp, err);
return;
}
...
bar(..., errp);
return;
and transform the error_setg() as above.
In some places, the transformation results in obviously unnecessary
error_propagate(). The next few commits will eliminate them.
Bonus: the elimination of gotos will make later patches in this series
easier to review.
Candidates for conversion tracked down with this Coccinelle script:
@@
identifier err, errp;
expression list args;
@@
- error_setg(&err, args);
+ error_setg(errp, args);
... when != err
error_propagate(errp, err);
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-34-armbru@redhat.com>
2020-07-07 18:06:01 +02:00
|
|
|
error_setg(errp,
|
|
|
|
"invalid slot number %d, valid range is [0-%" PRIu64 "]",
|
|
|
|
slot, machine->ram_slots - 1);
|
|
|
|
return;
|
pc-dimm: fix crash when invalid slot number is used
QEMU will crash with:
Segmentation fault (core dumped)
when negative slot number is used, ex:
qemu-system-x86_64 -m 1G,maxmem=20G,slots=256 \
-object memory-backend-ram,id=mem1,size=1G \
-device pc-dimm,id=dimm1,memdev=mem1,slot=-2
fix it by checking that slot number is within valid range.
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Message-Id: <20190723160859.27250-1-imammedo@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Li Qiang <liq3ea@gmail.com>
Signed-off-by: Igor Mammedov <<a href="mailto:imammedo@redhat.com" target="_blank">imammedo@redhat.com</a>><br></blockquote><div><br></div><div>Reviewed-by: Li Qiang <<a href="mailto:liq3ea@gmail.com">liq3ea@gmail.com</a>><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Reviewed-by: Pankaj Gupta <pagupta@redhat.com>
2019-07-23 18:08:59 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:34:41 +02:00
|
|
|
slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot,
|
|
|
|
machine->ram_slots, &local_err);
|
|
|
|
if (local_err) {
|
error: Avoid unnecessary error_propagate() after error_setg()
Replace
error_setg(&err, ...);
error_propagate(errp, err);
by
error_setg(errp, ...);
Related pattern:
if (...) {
error_setg(&err, ...);
goto out;
}
...
out:
error_propagate(errp, err);
return;
When all paths to label out are that way, replace by
if (...) {
error_setg(errp, ...);
return;
}
and delete the label along with the error_propagate().
When we have at most one other path that actually needs to propagate,
and maybe one at the end that where propagation is unnecessary, e.g.
foo(..., &err);
if (err) {
goto out;
}
...
bar(..., &err);
out:
error_propagate(errp, err);
return;
move the error_propagate() to where it's needed, like
if (...) {
foo(..., &err);
error_propagate(errp, err);
return;
}
...
bar(..., errp);
return;
and transform the error_setg() as above.
In some places, the transformation results in obviously unnecessary
error_propagate(). The next few commits will eliminate them.
Bonus: the elimination of gotos will make later patches in this series
easier to review.
Candidates for conversion tracked down with this Coccinelle script:
@@
identifier err, errp;
expression list args;
@@
- error_setg(&err, args);
+ error_setg(errp, args);
... when != err
error_propagate(errp, err);
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-34-armbru@redhat.com>
2020-07-07 18:06:01 +02:00
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
2018-08-01 15:34:41 +02:00
|
|
|
}
|
qom: Put name parameter before value / visitor parameter
The object_property_set_FOO() setters take property name and value in
an unusual order:
void object_property_set_FOO(Object *obj, FOO_TYPE value,
const char *name, Error **errp)
Having to pass value before name feels grating. Swap them.
Same for object_property_set(), object_property_get(), and
object_property_parse().
Convert callers with this Coccinelle script:
@@
identifier fun = {
object_property_get, object_property_parse, object_property_set_str,
object_property_set_link, object_property_set_bool,
object_property_set_int, object_property_set_uint, object_property_set,
object_property_set_qobject
};
expression obj, v, name, errp;
@@
- fun(obj, v, name, errp)
+ fun(obj, name, v, errp)
Chokes on hw/arm/musicpal.c's lcd_refresh() with the unhelpful error
message "no position information". Convert that one manually.
Fails to convert hw/arm/armsse.c, because Coccinelle gets confused by
ARMSSE being used both as typedef and function-like macro there.
Convert manually.
Fails to convert hw/rx/rx-gdbsim.c, because Coccinelle gets confused
by RXCPU being used both as typedef and function-like macro there.
Convert manually. The other files using RXCPU that way don't need
conversion.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-27-armbru@redhat.com>
[Straightforwad conflict with commit 2336172d9b "audio: set default
value for pcspk.iobase property" resolved]
2020-07-07 18:05:54 +02:00
|
|
|
object_property_set_int(OBJECT(dimm), PC_DIMM_SLOT_PROP, slot,
|
2018-10-05 11:20:12 +02:00
|
|
|
&error_abort);
|
2018-08-01 15:34:41 +02:00
|
|
|
trace_mhp_pc_dimm_assigned_slot(slot);
|
2018-08-01 15:34:44 +02:00
|
|
|
|
2018-10-05 11:20:21 +02:00
|
|
|
memory_device_pre_plug(MEMORY_DEVICE(dimm), machine, legacy_align,
|
error: Avoid unnecessary error_propagate() after error_setg()
Replace
error_setg(&err, ...);
error_propagate(errp, err);
by
error_setg(errp, ...);
Related pattern:
if (...) {
error_setg(&err, ...);
goto out;
}
...
out:
error_propagate(errp, err);
return;
When all paths to label out are that way, replace by
if (...) {
error_setg(errp, ...);
return;
}
and delete the label along with the error_propagate().
When we have at most one other path that actually needs to propagate,
and maybe one at the end that where propagation is unnecessary, e.g.
foo(..., &err);
if (err) {
goto out;
}
...
bar(..., &err);
out:
error_propagate(errp, err);
return;
move the error_propagate() to where it's needed, like
if (...) {
foo(..., &err);
error_propagate(errp, err);
return;
}
...
bar(..., errp);
return;
and transform the error_setg() as above.
In some places, the transformation results in obviously unnecessary
error_propagate(). The next few commits will eliminate them.
Bonus: the elimination of gotos will make later patches in this series
easier to review.
Candidates for conversion tracked down with this Coccinelle script:
@@
identifier err, errp;
expression list args;
@@
- error_setg(&err, args);
+ error_setg(errp, args);
... when != err
error_propagate(errp, err);
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-34-armbru@redhat.com>
2020-07-07 18:06:01 +02:00
|
|
|
errp);
|
2018-08-01 15:34:41 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 10:48:04 +02:00
|
|
|
void pc_dimm_plug(PCDIMMDevice *dimm, MachineState *machine)
|
2015-06-29 10:20:23 +02:00
|
|
|
{
|
2021-04-25 14:11:36 +02:00
|
|
|
MemoryRegion *vmstate_mr = pc_dimm_get_memory_region(dimm,
|
|
|
|
&error_abort);
|
2015-06-29 10:20:23 +02:00
|
|
|
|
2018-10-05 11:20:22 +02:00
|
|
|
memory_device_plug(MEMORY_DEVICE(dimm), machine);
|
2018-10-05 11:20:12 +02:00
|
|
|
vmstate_register_ram(vmstate_mr, DEVICE(dimm));
|
2023-03-09 08:52:57 +01:00
|
|
|
/* count only "real" DIMMs, not NVDIMMs */
|
|
|
|
if (!object_dynamic_cast(OBJECT(dimm), TYPE_NVDIMM)) {
|
|
|
|
machine->device_memory->dimm_size += memory_region_size(vmstate_mr);
|
|
|
|
}
|
2015-06-29 10:20:23 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 11:20:12 +02:00
|
|
|
void pc_dimm_unplug(PCDIMMDevice *dimm, MachineState *machine)
|
2015-06-29 10:20:23 +02:00
|
|
|
{
|
2021-04-25 14:11:36 +02:00
|
|
|
MemoryRegion *vmstate_mr = pc_dimm_get_memory_region(dimm,
|
|
|
|
&error_abort);
|
2015-06-29 10:20:25 +02:00
|
|
|
|
2018-10-05 11:20:23 +02:00
|
|
|
memory_device_unplug(MEMORY_DEVICE(dimm), machine);
|
2018-10-05 11:20:12 +02:00
|
|
|
vmstate_unregister_ram(vmstate_mr, DEVICE(dimm));
|
2023-03-09 08:52:57 +01:00
|
|
|
if (!object_dynamic_cast(OBJECT(dimm), TYPE_NVDIMM)) {
|
|
|
|
machine->device_memory->dimm_size -= memory_region_size(vmstate_mr);
|
|
|
|
}
|
2015-06-29 10:20:23 +02:00
|
|
|
}
|
|
|
|
|
2014-06-02 15:25:14 +02:00
|
|
|
static int pc_dimm_slot2bitmap(Object *obj, void *opaque)
|
|
|
|
{
|
|
|
|
unsigned long *bitmap = opaque;
|
|
|
|
|
|
|
|
if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
if (dev->realized) { /* count only realized DIMMs */
|
|
|
|
PCDIMMDevice *d = PC_DIMM(obj);
|
|
|
|
set_bit(d->slot, bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object_child_foreach(obj, pc_dimm_slot2bitmap, opaque);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-19 15:41:33 +02:00
|
|
|
static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
|
2014-06-02 15:25:14 +02:00
|
|
|
{
|
2018-04-27 14:05:15 +02:00
|
|
|
unsigned long *bitmap;
|
2014-06-02 15:25:14 +02:00
|
|
|
int slot = 0;
|
|
|
|
|
2018-04-27 14:05:15 +02:00
|
|
|
if (max_slots <= 0) {
|
|
|
|
error_setg(errp, "no slots where allocated, please specify "
|
|
|
|
"the 'slots' option");
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
bitmap = bitmap_new(max_slots);
|
2014-06-02 15:25:14 +02:00
|
|
|
object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap);
|
|
|
|
|
|
|
|
/* check if requested slot is not occupied */
|
|
|
|
if (hint) {
|
|
|
|
if (*hint >= max_slots) {
|
|
|
|
error_setg(errp, "invalid slot# %d, should be less than %d",
|
|
|
|
*hint, max_slots);
|
|
|
|
} else if (!test_bit(*hint, bitmap)) {
|
|
|
|
slot = *hint;
|
|
|
|
} else {
|
|
|
|
error_setg(errp, "slot %d is busy", *hint);
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* search for free slot */
|
|
|
|
slot = find_first_zero_bit(bitmap, max_slots);
|
|
|
|
if (slot == max_slots) {
|
|
|
|
error_setg(errp, "no free slots available");
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
g_free(bitmap);
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2014-06-02 15:25:05 +02:00
|
|
|
static Property pc_dimm_properties[] = {
|
|
|
|
DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
|
|
|
|
DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
|
|
|
|
DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
|
|
|
|
PC_DIMM_UNASSIGNED_SLOT),
|
2017-07-14 04:14:59 +02:00
|
|
|
DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem,
|
|
|
|
TYPE_MEMORY_BACKEND, HostMemoryBackend *),
|
2014-06-02 15:25:05 +02:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
qom: Swap 'name' next to visitor in ObjectPropertyAccessor
Similar to the previous patch, it's nice to have all functions
in the tree that involve a visitor and a name for conversion to
or from QAPI to consistently stick the 'name' parameter next
to the Visitor parameter.
Done by manually changing include/qom/object.h and qom/object.c,
then running this Coccinelle script and touching up the fallout
(Coccinelle insisted on adding some trailing whitespace).
@ rule1 @
identifier fn;
typedef Object, Visitor, Error;
identifier obj, v, opaque, name, errp;
@@
void fn
- (Object *obj, Visitor *v, void *opaque, const char *name,
+ (Object *obj, Visitor *v, const char *name, void *opaque,
Error **errp) { ... }
@@
identifier rule1.fn;
expression obj, v, opaque, name, errp;
@@
fn(obj, v,
- opaque, name,
+ name, opaque,
errp)
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1454075341-13658-20-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-01-29 14:48:55 +01:00
|
|
|
static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
2014-06-02 15:25:05 +02:00
|
|
|
{
|
2018-10-05 11:20:17 +02:00
|
|
|
Error *local_err = NULL;
|
2017-06-07 18:36:14 +02:00
|
|
|
uint64_t value;
|
2014-06-02 15:25:05 +02:00
|
|
|
|
2018-10-05 11:20:17 +02:00
|
|
|
value = memory_device_get_region_size(MEMORY_DEVICE(obj), &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2017-08-21 08:30:29 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-06-02 15:25:05 +02:00
|
|
|
|
2017-06-07 18:36:14 +02:00
|
|
|
visit_type_uint64(v, name, &value, errp);
|
2014-06-02 15:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pc_dimm_init(Object *obj)
|
|
|
|
{
|
2017-06-07 18:36:14 +02:00
|
|
|
object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size,
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 17:29:22 +02:00
|
|
|
NULL, NULL, NULL);
|
2014-06-02 15:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pc_dimm_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
PCDIMMDevice *dimm = PC_DIMM(dev);
|
2016-05-20 10:19:59 +02:00
|
|
|
PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
|
2019-08-09 08:57:22 +02:00
|
|
|
MachineState *ms = MACHINE(qdev_get_machine());
|
hw/mem/pc-dimm: Restrict NUMA-specific code to NUMA machines
When trying to use the pc-dimm device on a non-NUMA machine, we get:
$ qemu-system-arm -M none -cpu max -S \
-object memory-backend-file,id=mem1,size=1M,mem-path=/tmp/1m \
-device pc-dimm,id=dimm1,memdev=mem1
Segmentation fault (core dumped)
(gdb) bt
#0 pc_dimm_realize (dev=0x555556da3e90, errp=0x7fffffffcd10) at hw/mem/pc-dimm.c:184
#1 0x0000555555fe1f8f in device_set_realized (obj=0x555556da3e90, value=true, errp=0x7fffffffce18) at hw/core/qdev.c:531
#2 0x0000555555feb4a9 in property_set_bool (obj=0x555556da3e90, v=0x555556e54420, name=0x5555563c3c41 "realized", opaque=0x555556a704f0, errp=0x7fffffffce18) at qom/object.c:2257
To avoid that crash, restrict the pc-dimm NUMA check to machines
supporting NUMA, and do not allow the use of 'node' property on
non-NUMA machines.
Suggested-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-Id: <20211106145016.611332-1-f4bug@amsat.org>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
2021-11-06 15:50:16 +01:00
|
|
|
|
|
|
|
if (ms->numa_state) {
|
|
|
|
int nb_numa_nodes = ms->numa_state->num_nodes;
|
|
|
|
|
|
|
|
if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) ||
|
|
|
|
(!nb_numa_nodes && dimm->node)) {
|
|
|
|
error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
|
|
|
|
PRIu32 "' which exceeds the number of numa nodes: %d",
|
|
|
|
dimm->node, nb_numa_nodes ? nb_numa_nodes : 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (dimm->node > 0) {
|
|
|
|
error_setg(errp, "machine doesn't support NUMA");
|
|
|
|
return;
|
|
|
|
}
|
2014-06-02 15:25:05 +02:00
|
|
|
|
|
|
|
if (!dimm->hostmem) {
|
|
|
|
error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
|
|
|
|
return;
|
2017-07-14 04:14:59 +02:00
|
|
|
} else if (host_memory_backend_is_mapped(dimm->hostmem)) {
|
2020-07-14 18:02:00 +02:00
|
|
|
error_setg(errp, "can't use already busy memdev: %s",
|
|
|
|
object_get_canonical_path_component(OBJECT(dimm->hostmem)));
|
2017-07-14 04:14:59 +02:00
|
|
|
return;
|
2014-06-02 15:25:05 +02:00
|
|
|
}
|
2016-05-20 10:19:59 +02:00
|
|
|
|
|
|
|
if (ddc->realize) {
|
|
|
|
ddc->realize(dimm, errp);
|
|
|
|
}
|
2016-07-13 06:18:06 +02:00
|
|
|
|
|
|
|
host_memory_backend_set_mapped(dimm->hostmem, true);
|
|
|
|
}
|
|
|
|
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 17:29:24 +02:00
|
|
|
static void pc_dimm_unrealize(DeviceState *dev)
|
2016-07-13 06:18:06 +02:00
|
|
|
{
|
|
|
|
PCDIMMDevice *dimm = PC_DIMM(dev);
|
2022-02-18 08:34:13 +01:00
|
|
|
PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
|
|
|
|
|
|
|
|
if (ddc->unrealize) {
|
|
|
|
ddc->unrealize(dimm);
|
|
|
|
}
|
2016-07-13 06:18:06 +02:00
|
|
|
|
|
|
|
host_memory_backend_set_mapped(dimm->hostmem, false);
|
2014-06-02 15:25:05 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 18:51:16 +02:00
|
|
|
static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState *md)
|
|
|
|
{
|
2020-03-10 19:05:10 +01:00
|
|
|
return object_property_get_uint(OBJECT(md), PC_DIMM_ADDR_PROP,
|
|
|
|
&error_abort);
|
2018-04-23 18:51:16 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 11:20:20 +02:00
|
|
|
static void pc_dimm_md_set_addr(MemoryDeviceState *md, uint64_t addr,
|
|
|
|
Error **errp)
|
|
|
|
{
|
qom: Put name parameter before value / visitor parameter
The object_property_set_FOO() setters take property name and value in
an unusual order:
void object_property_set_FOO(Object *obj, FOO_TYPE value,
const char *name, Error **errp)
Having to pass value before name feels grating. Swap them.
Same for object_property_set(), object_property_get(), and
object_property_parse().
Convert callers with this Coccinelle script:
@@
identifier fun = {
object_property_get, object_property_parse, object_property_set_str,
object_property_set_link, object_property_set_bool,
object_property_set_int, object_property_set_uint, object_property_set,
object_property_set_qobject
};
expression obj, v, name, errp;
@@
- fun(obj, v, name, errp)
+ fun(obj, name, v, errp)
Chokes on hw/arm/musicpal.c's lcd_refresh() with the unhelpful error
message "no position information". Convert that one manually.
Fails to convert hw/arm/armsse.c, because Coccinelle gets confused by
ARMSSE being used both as typedef and function-like macro there.
Convert manually.
Fails to convert hw/rx/rx-gdbsim.c, because Coccinelle gets confused
by RXCPU being used both as typedef and function-like macro there.
Convert manually. The other files using RXCPU that way don't need
conversion.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-27-armbru@redhat.com>
[Straightforwad conflict with commit 2336172d9b "audio: set default
value for pcspk.iobase property" resolved]
2020-07-07 18:05:54 +02:00
|
|
|
object_property_set_uint(OBJECT(md), PC_DIMM_ADDR_PROP, addr, errp);
|
2018-10-05 11:20:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 11:20:18 +02:00
|
|
|
static MemoryRegion *pc_dimm_md_get_memory_region(MemoryDeviceState *md,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
return pc_dimm_get_memory_region(PC_DIMM(md), errp);
|
|
|
|
}
|
|
|
|
|
2018-04-23 18:51:16 +02:00
|
|
|
static void pc_dimm_md_fill_device_info(const MemoryDeviceState *md,
|
|
|
|
MemoryDeviceInfo *info)
|
|
|
|
{
|
|
|
|
PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1);
|
|
|
|
const DeviceClass *dc = DEVICE_GET_CLASS(md);
|
|
|
|
const PCDIMMDevice *dimm = PC_DIMM(md);
|
|
|
|
const DeviceState *dev = DEVICE(md);
|
|
|
|
|
|
|
|
if (dev->id) {
|
|
|
|
di->id = g_strdup(dev->id);
|
|
|
|
}
|
|
|
|
di->hotplugged = dev->hotplugged;
|
|
|
|
di->hotpluggable = dc->hotpluggable;
|
|
|
|
di->addr = dimm->addr;
|
|
|
|
di->slot = dimm->slot;
|
|
|
|
di->node = dimm->node;
|
|
|
|
di->size = object_property_get_uint(OBJECT(dimm), PC_DIMM_SIZE_PROP,
|
|
|
|
NULL);
|
|
|
|
di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem));
|
|
|
|
|
|
|
|
if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
|
|
|
|
info->u.nvdimm.data = di;
|
|
|
|
info->type = MEMORY_DEVICE_INFO_KIND_NVDIMM;
|
|
|
|
} else {
|
|
|
|
info->u.dimm.data = di;
|
|
|
|
info->type = MEMORY_DEVICE_INFO_KIND_DIMM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 15:25:05 +02:00
|
|
|
static void pc_dimm_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
2018-04-23 18:51:16 +02:00
|
|
|
MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
|
2014-06-02 15:25:05 +02:00
|
|
|
|
|
|
|
dc->realize = pc_dimm_realize;
|
2016-07-13 06:18:06 +02:00
|
|
|
dc->unrealize = pc_dimm_unrealize;
|
2020-01-10 16:30:32 +01:00
|
|
|
device_class_set_props(dc, pc_dimm_properties);
|
2015-03-10 12:25:51 +01:00
|
|
|
dc->desc = "DIMM memory module";
|
2014-06-02 15:25:05 +02:00
|
|
|
|
2018-04-23 18:51:16 +02:00
|
|
|
mdc->get_addr = pc_dimm_md_get_addr;
|
2018-10-05 11:20:20 +02:00
|
|
|
mdc->set_addr = pc_dimm_md_set_addr;
|
2018-04-23 18:51:16 +02:00
|
|
|
/* for a dimm plugged_size == region_size */
|
2018-10-05 11:20:19 +02:00
|
|
|
mdc->get_plugged_size = memory_device_get_region_size;
|
2018-10-05 11:20:18 +02:00
|
|
|
mdc->get_memory_region = pc_dimm_md_get_memory_region;
|
2018-04-23 18:51:16 +02:00
|
|
|
mdc->fill_device_info = pc_dimm_md_fill_device_info;
|
2014-06-02 15:25:05 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:58:04 +01:00
|
|
|
static const TypeInfo pc_dimm_info = {
|
2014-06-02 15:25:05 +02:00
|
|
|
.name = TYPE_PC_DIMM,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(PCDIMMDevice),
|
|
|
|
.instance_init = pc_dimm_init,
|
|
|
|
.class_init = pc_dimm_class_init,
|
|
|
|
.class_size = sizeof(PCDIMMDeviceClass),
|
2018-04-23 18:51:16 +02:00
|
|
|
.interfaces = (InterfaceInfo[]) {
|
|
|
|
{ TYPE_MEMORY_DEVICE },
|
|
|
|
{ }
|
|
|
|
},
|
2014-06-02 15:25:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void pc_dimm_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&pc_dimm_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(pc_dimm_register_types)
|