On Wed, Apr 04, 2018 at 12:10:03AM +0800, Wei Wang wrote:
+static int add_one_sg(struct virtqueue *vq, unsigned long pfn, uint32_t len)why not stop scanning of following pages though?
+{
+ struct scatterlist sg;
+ unsigned int unused;
+
+ sg_init_table(&sg, 1);
+ sg_set_page(&sg, pfn_to_page(pfn), len, 0);
+
+ /* Detach all the used buffers from the vq */
+ while (virtqueue_get_buf(vq, &unused))
+ ;
+
+ /*
+ * Since this is an optimization feature, losing a couple of free
+ * pages to report isn't important. We simply return without adding
+ * the page hint if the vq is full.
+ * We are adding one entry each time, which essentially results in nothis access to cmd_id_use and cmd_id_received without locks
+ * memory allocation, so the GFP_KERNEL flag below can be ignored.
+ * Host works by polling the free page vq for hints after sending the
+ * starting cmd id, so the driver doesn't need to kick after filling
+ * the vq.
+ * Lastly, there is always one entry reserved for the cmd id to use.
+ */
+ if (vq->num_free > 1)
+ return virtqueue_add_inbuf(vq, &sg, 1, vq, GFP_KERNEL);
+
+ return 0;
+}
+
+static int virtio_balloon_send_free_pages(void *opaque, unsigned long pfn,
+ unsigned long nr_pages)
+{
+ struct virtio_balloon *vb = (struct virtio_balloon *)opaque;
+ uint32_t len = nr_pages << PAGE_SHIFT;
+
+ /*
+ * If a stop id or a new cmd id was just received from host, stop
+ * the reporting, and return 1 to indicate an active stop.
+ */
+ if (virtio32_to_cpu(vb->vdev, vb->cmd_id_use) != vb->cmd_id_received)
+ return 1;
+
bothers me. Pls document why it's safe.
+ return add_one_sg(vb->free_page_vq, pfn, len);why the inconsistency?
+}
+
+static int send_start_cmd_id(struct virtio_balloon *vb, uint32_t cmd_id)
+{
+ struct scatterlist sg;
+ struct virtqueue *vq = vb->free_page_vq;
+
+ vb->cmd_id_use = cpu_to_virtio32(vb->vdev, cmd_id);
+ sg_init_one(&sg, &vb->cmd_id_use, sizeof(vb->cmd_id_use));
+ return virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
+}
+
+static int send_stop_cmd_id(struct virtio_balloon *vb)
+{
+ struct scatterlist sg;
+ struct virtqueue *vq = vb->free_page_vq;
+
+ sg_init_one(&sg, &vb->stop_cmd_id, sizeof(vb->cmd_id_use));